标签:this ems end geo 线段相交 out sam 几何 技术
Problem 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.
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.
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
Means:
问你有多少对线段相交
Solve:
直接枚举线段对,然后进行快速排斥和跨立实验
Code:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define CLR(x , v) memset(x , v , sizeof(x)) 5 6 struct Point 7 { 8 double x , y; 9 Point(double _x = 0.0 , double _y = 0.0):x(_x) , y(_y) {} 10 }; 11 12 struct Seg 13 { 14 Point a , b; 15 }; 16 17 Point operator - (Point a , Point b) 18 { 19 return Point(a.x - b.x , a.y - b.y); 20 } 21 22 double operator ^ (Point a , Point b) 23 { 24 return (a.x * b.y) - (a.y * b.x); 25 } 26 27 static const int MAXN = 110; 28 Seg data[MAXN]; 29 int n; 30 31 bool Judge(Seg s1 , Seg s2) 32 { 33 Point a1 = s1.a; 34 Point a2 = s1.b; 35 Point b1 = s2.a; 36 Point b2 = s2.b; 37 if(min(a1.x , a2.x) > max(b1.x , b2.x) || min(a1.y , a2.y) > max(b1.y , b2.y) || min(b1.x , b2.x) > max(a1.x , a2.x) || min(b1.y , b2.y) > max(a1.y , a2.y)) 38 return 0; 39 double u1 = ((a2 - a1) ^ (b1 - a1)) * ((a2 - a1) ^ (b2 - a1)); 40 double u2 = ((b2 - b1) ^ (a1 - b1)) * ((b2 - b1) ^ (a2 - b1)); 41 return u1 <= 0 && u2 <= 0; 42 } 43 44 int main() 45 { 46 while(~scanf("%d" , &n) && n) 47 { 48 int ans = 0; 49 CLR(data , 0); 50 for(int i = 1 ; i <= n ; ++i) 51 { 52 scanf("%lf%lf%lf%lf" , &data[i].a.x , &data[i].a.y , &data[i].b.x , &data[i].b.y); 53 } 54 55 for(int i = 1 ; i <= n ; ++i) 56 { 57 for(int j = i + 1 ; j <= n ; ++j) 58 { 59 if(Judge(data[i] , data[j])) 60 ++ans; 61 } 62 } 63 64 printf("%d\n" , ans); 65 } 66 }
HDOJ1086-You can Solve a Geometry Problem too(线段相交)
标签:this ems end geo 线段相交 out sam 几何 技术
原文地址:http://www.cnblogs.com/jianglingxin/p/6938659.html