[HDU]1086——You can Solve a Geometry Problem too

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
思路{若两线段相交,另外两线段端点在该直线两侧,由此想到叉乘!
 1 #include<map>
 2 #include<set>
 3 #include<list>
 4 #include<deque>
 5 #include<cmath>
 6 #include<queue>
 7 #include<stack>
 8 #include<vector>
 9 #include<cstdio>
10 #include<complex>
11 #include<cstring>
12 #include<cstdlib>
13 #include<iostream>
14 #include<algorithm>
15 #define db double
16 #define LL long long
17 #define maxx 200
18 #define RG register
19 using namespace std;
20 struct point{
21   db x,y;
22   point() {}
23   point(db _x,db _y):x(_x),y(_y) {}
24   point operator -(const point a) const {
25     return point(x-a.x,y-a.y);
26   }
27   point operator +(const point a) const {
28     return point(x+a.x,y+a.y);
29   }
30   point operator * (const db k) const {
31     return point(k*x,k*y);
32   }
33   db operator ^(const point a) const {
34     return x*a.y-y*a.x;
35   }
36   db operator *(const point a) const {
37     return x*a.x+y*a.y;
38   }
39   bool operator ==(const point a) const {
40     return x==a.x&&y==a.y;
41   }
42 }a[maxx+maxx+maxx];
43 int n;/*
44 bool check(point A,point B,point C,point D){
45   if(((B.y-A.y)/(B.x-A.x))==((D.y-C.y)/(D.x-C.x)){
46       if(A.x==C.x&&A.y==C.y&&B.x==D.x&&B.y==D.y)return 1;
47       point AC,AB;AC=(C-A);AB=(B-A);
48       if((AC.x*AB.y-AC.y*AB.x)==0&&)return 1;
49   }
50   db k=-((A-C)^(D-C))/((B-A)^(D-C));
51   point P,AB;P=(B-A)*k+A;AB=(B-A);
52   if(k<0||k>1)return 0; 
53   return 1;
54   }*/
55 bool check(point A,point B,point C,point D){
56   point AB,AC,AD;AB=B-A,AC=C-A,AD=D-A;
57   db k1=AB^AC,k2=AB^AD;
58   point CB,CD,CA;CB=B-C,CD=D-C,CA=A-C;
59   db k3=CD^CA,k4=CD^CB;
60   if(k1*k2<=0&&k3*k4<=0)return 1;
61   return 0;
62 }
63 int main(){
64   while(scanf("%d",&n)&&n){LL ans=0;db x,y;
65     for(RG int i=1;i<=n;++i)
66       cin>>x>>y,a[(i*2)-1]=point(x,y),
67     cin>>x>>y,a[(i*2)]=point(x,y);
68     for(RG int i=1;i<=2*n;i+=2)
69       for(RG int j=1;j<=2*n;j+=2){
70     if(i==j)continue;
71     if(check(a[i],a[i+1],a[j],a[j+1]))ans++;
72       }
73     printf("%lld
",ans/2);
74   }
75   return 0;
76 }
原文地址:https://www.cnblogs.com/zzmmm/p/6950538.html