POJ 3304 Segments
判断直线与线段相交
#include<cstdio> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<list> #include<algorithm> using namespace std; int T,n; int tot; struct point { double x; double y; } p[200+10]; struct Line { point a; point b; } line[200+10]; const double eps=1e-8; int intersect_in(point a,point b,point c,point d) { double X1=a.x; double Y1=a.y; double X2=b.x; double Y2=b.y; double A=Y2-Y1; double B=-(X2-X1); double C=Y2*(X2-X1)-X2*(Y2-Y1); if((A*c.x+B*c.y+C)*(A*d.x+B*d.y+C)<=eps) return 1; return 0; } int main() { scanf("%d",&T); while(T--) { scanf("%d",&n); tot=0; for(int i=1; i<=n; i++) { double a,b,c,d; scanf("%lf%lf%lf%lf",&a,&b,&c,&d); p[tot+0].x=a; p[tot+0].y=b; p[tot+1].x=c; p[tot+1].y=d; line[i].a=p[tot+0]; line[i].b=p[tot+1]; tot=tot+2; } if(n==1||n==2) { printf("Yes!\n"); continue; } int ans=0; for(int i=0; i<tot; i++) { for(int j=i+1; j<tot; j++) { if(sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y))<eps) continue; bool flag=0; for(int k=1; k<=n; k++) { if(intersect_in(p[i],p[j],line[k].a,line[k].b)==0) { flag=1;//不相交 break; } } if(flag==0) { ans=1; break; } } if(ans) break; } if(ans) printf("Yes!\n"); else printf("No!\n"); } return 0; }