forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10867.cpp
134 lines (122 loc) · 2.19 KB
/
10867.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <bits/stdc++.h>
using namespace std;
#define REP(i, b, n) for (int i = b; i < n; i++)
#define rep(i, n) REP(i, 0, n)
typedef complex<double> P;
typedef vector<P> Polygon;
const double eps = 1e-10;
double cross(P a, P b)
{
return a.real() * b.imag() - a.imag() * b.real();
}
double dot(P a, P b, P c)
{
return a.real() * b.real() + a.imag() * b.imag();
}
bool is_point_online_seg(P a, P b, P c)
{
return abs(a - c) + abs(b - c) < abs(a - b) + eps;
}
P intersect_ll(P a1, P a2, P b1, P b2)
{
P a = a2 - a1, b = b2 - b1;
return a1 + a * cross(b, b1 - a1) / cross(b, a);
}
double distance_l_p(P &a, P &b, P &c)
{
return abs(cross(b - a, c - a)) / abs(b - a);
}
bool is_in(Polygon &in, P &a)
{
int cnt = 0;
rep(i, in.size())
{
P cur = in[i] - a, next = in[(i + 1) % in.size()] - a;
if (cur.imag() > next.imag())
{
swap(cur, next);
}
if (cur.imag() < 0 && 0 <= next.imag() &&
cross(next, cur) >= 0)
{
cnt++;
}
if (is_point_online_seg(in[i], in[(i + 1) % in.size()], a))
{
return true;
}
}
if (cnt % 2 == 1)
{
return true;
}
else
{
return false;
}
}
bool cmp(const P &a, const P &b)
{
if (abs(a.real() - b.real()) > eps)
{
return a.real() < b.real();
}
return a.imag() < b.imag();
}
double solve(Polygon &in, P l1, P l2)
{
vector<P> CP;
rep(i, in.size())
{
double leng = distance_l_p(l1, l2, in[i]);
if (leng < eps)
{
CP.push_back(in[i]);
}
else if (cross(l2 - l1, in[i] - l1) * cross(l2 - l1, in[(i + 1) % in.size()] - l1) < 0)
{
CP.push_back(intersect_ll(l1, l2, in[i], in[(i + 1) % in.size()]));
}
}
sort(CP.begin(), CP.end(), cmp);
if (CP.size() == 0)
{
return 0;
}
double ret = 0;
rep(i, CP.size() - 1)
{
P mid = (CP[i] + CP[(i + 1) % CP.size()]);
mid /= 2;
if (is_in(in, mid))
{
ret += abs(CP[i] - CP[(i + 1) % CP.size()]);
}
}
return ret;
}
int main()
{
int n, q;
while (scanf("%d%d", &n, &q) && n)
{
Polygon a;
rep(i, n)
{
double re, im;
scanf("%lf%lf", &re, &im);
P tmp(re, im);
a.push_back(tmp);
}
rep(i, q)
{
double re, im;
scanf("%lf%lf", &re, &im);
P l1(re, im);
scanf("%lf%lf", &re, &im);
P l2(re, im);
printf("%.3lf\n", solve(a, l1, l2));
}
}
return 0;
}