HDU 1086 You can Solve a Geometry Problem too

You can Solve a Geometry Problem too

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2734 Accepted Submission(s): 1284


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

Author

lcy
 
 1 #include<iostream>
2 using namespace std;
3 double p[100][2],q[100][2];
4 double direction( double p[] , double q[] , double r[])
5 {
6 return ( (r[0] - p[0]) * (q[1] - p[1]) - (r[1] - p[1]) * (q[0] - p[0]));
7 }
8 bool onsegment(double p[],double q[], double r[])
9 {
10 if(((r[0] - p[0])*(r[0] - q[0]) <= 0) && ((r[1] - p[1]) * (r[1] - q[1]) <= 0))
11 return true;
12 else return false;
13 }
14 bool judge(int i, int j)
15 {
16 double d1 , d2 , d3 , d4 ;
17 d1 = direction(p[i],q[i],p[j]);
18 d2 = direction(p[i],q[i],q[j]);
19 d3 = direction(p[j],q[j],p[i]);
20 d4 = direction(p[j],q[j],q[i]);
21 if( (d1 * d2 < 0) && (d3 * d4 < 0))
22 return true;
23 else if( d1 == 0 && onsegment(p[i],q[i],p[j]) == 1)
24 return true;
25 else if( d2 == 0 && onsegment(p[i],q[i],q[j]) == 1)
26 return true;
27 else if( d3 == 0 && onsegment(p[j],q[j],p[i]) == 1)
28 return true;
29 else if( d4 == 0 && onsegment(p[j],q[j],q[i]) == 1)
30 return true;
31 else return false;
32 }
33 int main(){
34 int n, i , j , count;
35 while( scanf( "%d" , &n ) , n)
36 {
37 for( i = 0 ; i < n ; ++ i )
38 {
39 scanf( "%lf %lf %lf %lf", &p[i][0] , &p[i][1] , &q[i][0] , &q[i][1] ) ;
40 }
41 for( i = 0 , count = 0 ; i < n ; i ++ )
42 for( j = i + 1 ; j < n ; j ++ )
43 if( judge( i , j ) == 1 )
44 ++count;
45 printf("%d\n",count);
46 }
47 return 0;
48 }

  

posted @ 2011-08-04 18:34  贺佐安  阅读(507)  评论(0编辑  收藏  举报