HDU 1086 线段相交(不规范相交模板)
题意:
很多线段,求总交点个数(不规范相交),没有三线共点
题解:
模板,拿几道计算几何水题开头,准备计算几何专题了~
这个题的细节其实很多,只是数据太水了!
不规范相交模板:
View Code
1 #include <iostream> 2 #include <cstdlib> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <cmath> 7 8 #define N 110 9 #define EPS 1e-7 10 #define dc doublecmp 11 //不规范相交 12 using namespace std; 13 14 struct PO 15 { 16 double x,y; 17 }; 18 19 struct LI 20 { 21 PO a,b; 22 }li[N]; 23 24 int n; 25 26 inline int doublecmp(double x) 27 { 28 if(x>EPS) return 1; 29 else if(x<-EPS) return -1; 30 return 0; 31 } 32 33 inline double cross(PO &a,PO &b,PO &c) 34 { 35 return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x); 36 } 37 38 inline double dot(PO &a,PO &b,PO &c) 39 { 40 return (b.x-a.x)*(c.x-a.x)+(b.y-a.y)*(c.y-a.y); 41 } 42 43 inline bool onseg(PO &a,PO &b,PO &c)//c在ab线段上 44 { 45 double maxx=max(a.x,b.x); 46 double maxy=max(a.y,b.y); 47 double minx=min(a.x,b.x); 48 double miny=min(a.y,b.y); 49 if(dc(cross(a,b,c))==0&&dc(c.x-minx)>=0&&dc(c.x-maxx)<=0&&dc(c.y-miny)>=0&&dc(c.y-maxy)<=0) 50 return true; 51 return false; 52 } 53 54 inline bool segcross(LI &a,LI &b)//判断线段相交(不规范) 55 { 56 int d1,d2,d3,d4; 57 d1=dc(cross(b.a,b.b,a.a)); 58 d2=dc(cross(b.a,b.b,a.b)); 59 d3=dc(cross(a.a,a.b,b.a)); 60 d4=dc(cross(a.a,a.b,b.b)); 61 if(d1*d2<0&&d3*d4<0) return true; 62 if(d1==0&&onseg(b.a,b.b,a.a)) return true; 63 if(d2==0&&onseg(b.a,b.b,a.b)) return true; 64 if(d3==0&&onseg(a.a,a.b,b.a)) return true; 65 if(d4==0&&onseg(a.a,a.b,b.b)) return true; 66 return false; 67 } 68 69 inline void read() 70 { 71 for(int i=1;i<=n;i++) 72 scanf("%lf%lf%lf%lf",&li[i].a.x,&li[i].a.y,&li[i].b.x,&li[i].b.y); 73 } 74 75 inline void go() 76 { 77 int ans=0; 78 for(int i=1;i<=n;i++) 79 for(int j=i+1;j<=n;j++) 80 if(segcross(li[i],li[j])) ans++; 81 printf("%d\n",ans); 82 } 83 84 int main() 85 { 86 while(scanf("%d",&n),n) read(),go(); 87 return 0; 88 }
没有人能阻止我前进的步伐,除了我自己!