-
Notifications
You must be signed in to change notification settings - Fork 438
/
Segment-Intersection.cpp
57 lines (51 loc) · 961 Bytes
/
Segment-Intersection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <cstdio>
using namespace std;
struct point
{
double x, y;
point() { }
point(double _x, double _y) : x(_x), y(_y) { }
};
struct segment
{
point s, t;
segment() { }
segment(point _s, point _t) : s(_s), t(_t) { }
}s[110];
double cross(segment base, segment x)
{
return (base.s.x - base.t.x) * (x.s.y - x.t.y) - (base.s.y - base.t.y) * (x.s.x - x.t.x);
}
bool segment_intersect(segment x, segment y)
{
return cross(x, segment(x.s, y.s)) * cross(x, segment(x.s, y.t)) <= 0 && cross(y, segment(y.s, x.s)) * cross(y, segment(y.s, x.t)) <= 0;
}
int main()
{
int m;
while (true)
{
scanf("%d", &m);
if (m == 0)
{
break;
}
for (int i = 0; i < m; i++)
{
scanf("%lf%lf%lf%lf", &s[i].s.x, &s[i].s.y, &s[i].t.x, &s[i].t.y);
}
int ans = 0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < i; j++)
{
if (segment_intersect(s[i], s[j]) == true)
{
ans++;
}
}
}
printf("%d\n", ans);
}
return 0;
}