首页 > 代码库 > poj3304Segments(直线与多条线段相交)

poj3304Segments(直线与多条线段相交)

链接

枚举两点(端点),循环遍历与直线相交的线段。

  1 #include <iostream>  2 #include<cstdio>  3 #include<cstring>  4 #include<algorithm>  5 #include<stdlib.h>  6 #include<vector>  7 #include<cmath>  8 #include<queue>  9 #include<set> 10 using namespace std; 11 #define N 105 12 #define LL long long 13 #define INF 0xfffffff 14 const double eps = 1e-10; 15 const double pi = acos(-1.0); 16 const double inf = ~0u>>2; 17 struct point 18 { 19     double x,y; 20     point(double x=0,double y = 0):x(x),y(y){} 21 }p[N<<1]; 22 struct line 23 { 24     point a,b; 25 }; 26 typedef point pointt ; 27 pointt operator -(point a,point b) 28 { 29     return point(a.x-b.x,a.y-b.y); 30 } 31 int dcmp(double x) 32 { 33     if(fabs(x)<eps) return 0; 34     return x<0?-1:1; 35 } 36 double cross(point a,point b) 37 { 38     return a.x*b.y-a.y*b.x; 39 } 40 double xmult(point a,point b,point c) 41 { 42     return cross(a-c,b-c); 43 } 44 int dotline(point p,line l) 45 { 46     return (!dcmp(xmult(p,l.a,l.b)))&&(l.a.x-p.x)*(l.b.x-p.x)<eps 47                 &&(l.a.y-p.y)*(l.b.y-p.y)<eps; 48 } 49 double dis(point a) 50 { 51     return sqrt(a.x*a.x+a.y*a.y); 52 } 53 int Intersection( point a,point b,point c,point d ) 54 { 55     int d1,d2; 56     d1 = dcmp(xmult( a,c,b )); 57     d2 = dcmp(xmult( a,d,b )); 58     if( d1*d2==-1 ) return 1;//规范相交 59      if( d1==0||d2==0 ) return 2;//非规范相交 60     return 0;//不相交 61 } 62 int main() 63 { 64     int t,i,j,n,g; 65     cin>>t; 66     while(t--) 67     { 68         cin>>n; 69         for(i=  1; i <= n; i++) 70         { 71             scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i+n].x,&p[i+n].y); 72         } 73         if(n==1||n==2) 74         { 75             puts("Yes!"); 76             continue; 77         } 78         int flag = 0; 79         for(i = 1; i <= 2*n;  i++) 80         { 81             for(j = i+1; j<= 2*n; j++) 82             { 83                 line l1; 84                 l1.a = p[i],l1.b = p[j]; 85                 if(dcmp(dis(p[i]-p[j]))==0) continue; 86                 int num = 0; 87                 for(g = 1; g <= n ;g++) 88                 { 89                    if(Intersection(p[i],p[j],p[g],p[g+n])) num++; 90                    else break; 91                 } 92                 if(num==n) 93                 { 94                     flag = 1; 95                     break; 96                 } 97             } 98             if(flag) break; 99         }100         if(flag) puts("Yes!");101         else puts("No!");102     }103     return 0;104 }
View Code