HDUOJ1086You can Solve a Geometry Problem too

时间:2022-05-05
本文章向大家介绍HDUOJ1086You 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): 6340    Accepted Submission(s): 3064

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<stdio.h>
 2 #include<math.h>
 3 const double eps=1e-10 ;
 4 typedef struct
 5 {
 6     double x,y;
 7 }point;
 8 
 9 double min(double a, double b)
10 {
11     return a<b?a:b;
12 }
13 double max(double a,double b)
14 {
15     return a>b?a:b;
16 }
17 //判断线段是否有焦点
18 bool inter(point a ,point b, point c ,point d)
19 {
20     if(min(a.x,b.x)>max(c.x,d.x)||min(a.y,b.y)>max(c.y,d.y)||
21       min(c.x,d.x)>max(a.x,b.x)||min(c.y,d.y)>max(a.y,b.y))
22       return 0;
23     double h,i,j,k;
24     h=(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
25     i=(b.x-a.x)*(d.y-a.y)-(b.y-a.y)*(d.x-a.x);
26     j=(d.x-c.x)*(a.y-c.y)-(d.y-c.y)*(a.x-c.x);
27     k=(d.x-c.x)*(b.y-c.y)-(d.y-c.y)*(b.x-c.x);
28     return h*i<=eps&&j*k<=eps;
29 };
30 point st[102],en[102];
31 int main()
32 {
33     int n,j,i,cnt=0;
34     while(scanf("%d",&n),n)
35     {
36         cnt=0;
37         for( i=0 ; i<n ; i++ )
38          scanf("%lf%lf%lf%lf",&st[i].x,&st[i].y,&en[i].x,&en[i].y);
39 
40         for( i=0 ; i<n ; i++ )
41         {
42             for(j=i+1 ; j<n ;j++ )
43             {
44                 if(inter(st[i],en[i],st[j],en[j]))
45                     cnt++;
46             }
47         }
48         printf("%dn",cnt);
49     }
50     return 0;
51 }