码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-07-29 13:59:18      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   for   art   

链接

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

bubuko.com,布布扣
  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

 

poj3304Segments(直线与多条线段相交),布布扣,bubuko.com

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

标签:style   blog   http   color   os   io   for   art   

原文地址:http://www.cnblogs.com/shangyu/p/3875027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!