A - You can Solve a Geometry Problem too
**Time Limit:**1000MS **Memory Limit:**32768KB 64bit IO Format:%I64d & %I64u
Submit Status
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
给N个线段。问交点总数。
多条线段相交于同一个点算多次。
规范相交和非规范相交都算。
规范相交就是,交点不能是线段的端点。
而非规范相交可以。
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))
25using namespace std;
26const int dx4[4]={1,0,0,-1};
27const int dy4[4]={0,-1,1,0};
28const double eps = 1E-8;
29typedef long long LL;
30#define sec second
31const int inf = 0x3f3f3f3f;
32const int N=105;
33int n;
34int dblcmp(double d)
35{
36return d<-eps?-1:d>eps;
37}
38 38
39struct point
40{
41double x,y;
42point(){}
43point (double _x,double _y):
44x(_x),y(_y){};
45void input()
46{
47scanf("%lf%lf",&x;,&y);
48}
49point sub(point p)
50{
51return point(x-p.x,y-p.y);
52}
53double det(point p)
54{
55return x*p.y-y*p.x;
56}
57double dot(point p)
58{
59return x*p.x+y*p.y;
60}
61};
62 62
63struct line
64{
65point a,b;
66void input()
67{
68a.input();
69b.input();
70}
71int segcrossseg(line v)
72{
73int d1=dblcmp(b.sub(a).det(v.a.sub(a)));
74int d2=dblcmp(b.sub(a).det(v.b.sub(a)));
75int d3=dblcmp(v.b.sub(v.a).det(a.sub(v.a)));
76int d4=dblcmp(v.b.sub(v.a).det(b.sub(v.a)));
77if ((d1^d2)==-2&&(d3^d4)==-2) return 2;
78return (d1==0&&dblcmp;(v.a.sub(a).dot(v.a.sub(b)))<=0||
79d2==0&&dblcmp;(v.b.sub(a).dot(v.b.sub(b)))<=0||
80d3==0&&dblcmp;(a.sub(v.a).dot(a.sub(v.b)))<=0||
81d4==0&&dblcmp;(b.sub(v.a).dot(b.sub(v.b)))<=0);
82}
83}li[N];
84int main()
85{
86#ifndef ONLINE_JUDGE
87freopen("in.txt","r",stdin);
88#endif
89
90while (~scanf("%d",&n;)!=EOF&&n)
91{
92for ( int i = 1 ; i <= n ; i++) li[i].input();
93int cnt = 0;
94for ( int i = 1 ; i < n ; i++)
95for ( int j = i+1 ; j <= n ; j++)
96{
97 97
98if (li[i].segcrossseg(li[j]))
99cnt++;
100}
101printf("%dn",cnt);
102}
103
104
105
106#ifndef ONLINE_JUDGE
107fclose(stdin);
108#endif
109return 0;
110}