版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sxh759151483/article/details/82902406
用到了跨立实验和快速排斥实验
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double eps = 1e-8;
const int maxn = 100005;
struct point{
double xx,yy;
point(double x=0,double y=0):xx(x),yy(y){};
};
struct segment{//线段
point a,b;
};
double min(double A,double B){return A<B?A:B;}
double max(double A,double B){return A>B?A:B;}
double Cross(point A,point B){return A.xx*B.yy-A.yy*B.xx;}//叉乘
int dcmp(double x){
if(fabs(x)<eps) return 0;
return x<0?-1:1;
}
point operator + (point A,point B){return point(A.xx+B.xx,A.yy+B.yy);}
point operator - (point A,point B){return point(A.xx-B.xx,A.yy-B.yy);}
point operator * (point A,double p){return point(A.xx*p,A.yy*p);}
point operator / (point A,double p){return point(A.xx/p,A.yy/p);}
bool operator == (point A,point B){return dcmp(A.xx - B.xx)==0&&dcmp(A.yy-B.yy)==0;}
bool Exclusion(segment p,segment q){//快速排斥试验
if(min(p.a.xx,p.b.xx)-max(q.a.xx,q.b.xx)>eps||min(p.a.yy,p.b.yy)-max(q.a.yy,q.b.yy)>eps||min(q.a.xx,q.b.xx)-max(p.a.xx,p.b.xx)>eps||min(q.a.yy,q.b.yy)-max(p.a.yy,p.b.yy)>eps)
return 0;
return 1;
}
bool Straddling(segment p,segment q){//跨立实验
if(dcmp(Cross(p.b-p.a,q.a-p.a))*dcmp(Cross(p.b-p.a,q.b-p.a))<=0&&dcmp(Cross(q.b-q.a,p.a-q.a))*dcmp(Cross(q.b-q.a,p.b-q.a))<=0)
return 1;
return 0;
}
bool check(segment p,segment q){//判断两线段是否相交
if(Exclusion(p, q) && Straddling(p,q))
return 1;
return 0;
}
/*------------------------------------------------------------------------------------*/
int main(){
int T;
scanf("%d", &T);
while(T--){
segment x, y;
scanf("%lf%lf%lf%lf%lf%lf%lf%lf", &x.a.xx, &x.a.yy, &x.b.xx, &x.b.yy, &y.a.xx, &y.a.yy, &y.b.xx, &y.b.yy);
if(check(x, y)) printf("Yes\n");
else printf("No\n");
}
return 0;
}