A - You can Solve a Geometry Problem too
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is very easy, after all we are now attending an exam, not a contest :)
Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point.
Note:
You can assume that two segments would not intersect at more than one point.
Give you N (1<=N<=100) segments(线段), please output the number of all intersections(交点). You should count repeatedly if M (M>2) segments intersect at the same point.
Note:
You can assume that two segments would not intersect at more than one point.
Input
Input contains multiple test cases. Each test case contains a integer N (1=N<=100) in a line first, and then N lines follow. Each line describes one segment with four float values x1, y1, x2, y2 which are coordinates of the segment’s ending.
A test case starting with 0 terminates the input and this test case is not to be processed.
A test case starting with 0 terminates the input and this test case is not to be processed.
Output
For each case, print the number of intersections, and one line one case.
Sample Input
2
0.00 0.00 1.00 1.00
0.00 1.00 1.00 0.00
3
0.00 0.00 1.00 1.00
0.00 1.00 1.00 0.000
0.00 0.00 1.00 0.00
0
Sample Output
1
3
给N个线段。问交点总数。
多条线段相交于同一个点算多次。
规范相交和非规范相交都算。
规范相交就是,交点不能是线段的端点。
而非规范相交可以。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 /************************************************************************* 2 > File Name: code/hdu/1086.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年11月06日 星期五 13时41分46秒 6 ************************************************************************/ 7 8 #include<iostream> 9 #include<iomanip> 10 #include<cstdio> 11 #include<algorithm> 12 #include<cmath> 13 #include<cstring> 14 #include<string> 15 #include<map> 16 #include<set> 17 #include<queue> 18 #include<vector> 19 #include<stack> 20 #include<cctype> 21 #define fst first 22 #define lson l,m,rt<<1 23 #define rson m+1,r,rt<<1|1 24 #define ms(a,x) memset(a,x,sizeof(a)) 25 using namespace std; 26 const int dx4[4]={1,0,0,-1}; 27 const int dy4[4]={0,-1,1,0}; 28 const double eps = 1E-8; 29 typedef long long LL; 30 #define sec second 31 const int inf = 0x3f3f3f3f; 32 const int N=105; 33 int n; 34 int dblcmp(double d) 35 { 36 return d<-eps?-1:d>eps; 37 } 38 39 struct point 40 { 41 double x,y; 42 point(){} 43 point (double _x,double _y): 44 x(_x),y(_y){}; 45 void input() 46 { 47 scanf("%lf%lf",&x,&y); 48 } 49 point sub(point p) 50 { 51 return point(x-p.x,y-p.y); 52 } 53 double det(point p) 54 { 55 return x*p.y-y*p.x; 56 } 57 double dot(point p) 58 { 59 return x*p.x+y*p.y; 60 } 61 }; 62 63 struct line 64 { 65 point a,b; 66 void input() 67 { 68 a.input(); 69 b.input(); 70 } 71 int segcrossseg(line v) 72 { 73 int d1=dblcmp(b.sub(a).det(v.a.sub(a))); 74 int d2=dblcmp(b.sub(a).det(v.b.sub(a))); 75 int d3=dblcmp(v.b.sub(v.a).det(a.sub(v.a))); 76 int d4=dblcmp(v.b.sub(v.a).det(b.sub(v.a))); 77 if ((d1^d2)==-2&&(d3^d4)==-2) return 2; 78 return (d1==0&&dblcmp(v.a.sub(a).dot(v.a.sub(b)))<=0|| 79 d2==0&&dblcmp(v.b.sub(a).dot(v.b.sub(b)))<=0|| 80 d3==0&&dblcmp(a.sub(v.a).dot(a.sub(v.b)))<=0|| 81 d4==0&&dblcmp(b.sub(v.a).dot(b.sub(v.b)))<=0); 82 } 83 }li[N]; 84 int main() 85 { 86 #ifndef ONLINE_JUDGE 87 freopen("in.txt","r",stdin); 88 #endif 89 90 while (~scanf("%d",&n)!=EOF&&n) 91 { 92 for ( int i = 1 ; i <= n ; i++) li[i].input(); 93 int cnt = 0; 94 for ( int i = 1 ; i < n ; i++) 95 for ( int j = i+1 ; j <= n ; j++) 96 { 97 98 if (li[i].segcrossseg(li[j])) 99 cnt++; 100 } 101 printf("%d ",cnt); 102 } 103 104 105 106 #ifndef ONLINE_JUDGE 107 fclose(stdin); 108 #endif 109 return 0; 110 }