-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpolygon.cpp
More file actions
469 lines (414 loc) · 17.2 KB
/
polygon.cpp
File metadata and controls
469 lines (414 loc) · 17.2 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
template <class F>
struct Point {
F x, y;
Point() : x(0), y(0) {}
Point(const F& x, const F& y) : x(x), y(y) {}
void swap(Point& other) { using std::swap; swap(x, other.x); swap(y, other.y); }
template <class F1> explicit operator Point<F1> () const {
return Point<F1>(static_cast<F1>(x), static_cast<F1>(y)); }
template <class F1> Point& operator = (const Point<F1>& other) {
x = other.x; y = other.y; return *this; }
template <class F1> Point& operator += (const Point<F1>& other) {
x += other.x; y += other.y; return *this; }
template <class F1> Point& operator -= (const Point<F1>& other) {
x -= other.x; y -= other.y; return *this; }
template <class F1> Point& operator *= (const F1& factor) {
x *= factor; y *= factor; return *this; }
template <class F1> Point& operator /= (const F1& factor) {
x /= factor; y /= factor; return *this; }
};
template <class F> int read(Point<F>& point) { return read(point.x, point.y) / 2; }
template <class F> int write(const Point<F>& point) { return write(point.x, point.y); }
template <class F> istream& operator >> (istream& is, Point<F>& point) {
return is >> point.x >> point.y; }
template <class F> ostream& operator << (ostream& os, const Point<F>& point) {
return os << point.x << ' ' << point.y; }
template <class F> inline Point<F> makePoint(const F& x, const F& y) { return Point<F>(x, y); }
template <class F> void swap(Point<F>& lhs, Point<F>& rhs) { lhs.swap(rhs); }
#define FUNC1(name, arg, expr) \
template <class F> inline auto name(const arg) -> decltype(expr) { return expr; }
#define FUNC2(name, arg1, arg2, expr) \
template <class F1, class F2> \
inline auto name(const arg1, const arg2) -> decltype(expr) { return expr; }
#define FUNC3(name, arg1, arg2, arg3, expr) \
template <class F1, class F2, class F3> \
inline auto name(const arg1, const arg2, const arg3) -> decltype(expr) { return expr; }
FUNC1(operator -, Point<F>& point, makePoint(-point.x, -point.y))
FUNC2(operator +, Point<F1>& lhs, Point<F2>& rhs, makePoint(lhs.x + rhs.x, lhs.y + rhs.y))
FUNC2(operator -, Point<F1>& lhs, Point<F2>& rhs, makePoint(lhs.x - rhs.x, lhs.y - rhs.y))
FUNC2(operator *, F1& factor, Point<F2>& rhs, makePoint(factor * rhs.x, factor * rhs.y))
FUNC2(operator *, Point<F1>& lhs, F2& factor, makePoint(lhs.x * factor, lhs.y * factor))
FUNC2(operator /, Point<F1>& lhs, F2& factor, makePoint(lhs.x / factor, lhs.y / factor))
FUNC2(operator *, Point<F1>& lhs, Point<F2>& rhs, lhs.x * rhs.x + lhs.y * rhs.y)
FUNC2(operator ^, Point<F1>& lhs, Point<F2>& rhs, lhs.x * rhs.y - lhs.y * rhs.x)
// < 0 if rhs <- lhs counter-clockwise, 0 if collinear, > 0 if clockwise.
FUNC2(ccw, Point<F1>& lhs, Point<F2>& rhs, rhs ^ lhs)
FUNC3(ccw, Point<F1>& lhs, Point<F2>& rhs, Point<F3>& origin, ccw(lhs - origin, rhs - origin))
FUNC2(operator ==, Point<F1>& lhs, Point<F2>& rhs, lhs.x == rhs.x && lhs.y == rhs.y)
FUNC2(operator !=, Point<F1>& lhs, Point<F2>& rhs, !(lhs == rhs))
FUNC2(operator <, Point<F1>& lhs, Point<F2>& rhs,
lhs.y < rhs.y || (lhs.y == rhs.y && lhs.x < rhs.x))
FUNC2(operator >, Point<F1>& lhs, Point<F2>& rhs, rhs < lhs)
FUNC2(operator <=, Point<F1>& lhs, Point<F2>& rhs, !(lhs > rhs))
FUNC2(operator >=, Point<F1>& lhs, Point<F2>& rhs, !(lhs < rhs))
// Angles and rotations (counter-clockwise).
FUNC1(angle, Point<F>& point, atan2(point.y, point.x))
FUNC2(angle, Point<F1>& lhs, Point<F2>& rhs, atan2(lhs ^ rhs, lhs * rhs))
FUNC3(angle, Point<F1>& lhs, Point<F2>& rhs, Point<F3>& origin,
angle(lhs - origin, rhs - origin))
FUNC3(rotate, Point<F1>& point, F2& angleSin, F3& angleCos,
makePoint(angleCos * point.x - angleSin * point.y,
angleSin * point.x + angleCos * point.y))
FUNC2(rotate, Point<F1>& point, F2& angle, rotate(point, sin(angle), cos(angle)))
FUNC3(rotate, Point<F1>& point, F2& angle, Point<F3>& origin,
origin + rotate(point - origin, angle))
FUNC1(perp, Point<F>& point, makePoint(-point.y, point.x))
// Distances.
FUNC1(abs, Point<F>& point, point * point)
FUNC1(norm, Point<F>& point, sqrt(abs(point)))
FUNC2(dist, Point<F1>& lhs, Point<F2>& rhs, norm(lhs - rhs))
FUNC2(dist2, Point<F1>& lhs, Point<F2>& rhs, abs(lhs - rhs))
FUNC2(bisector, Point<F1>& lhs, Point<F2>& rhs, lhs * norm(rhs) + rhs * norm(lhs))
#undef FUNC1
#undef FUNC2
#undef FUNC3
template <class F>
struct Line {
Point<F> a, ab;
Line() : a(), ab() {}
Line(const Point<F>& a, const Point<F>& b, bool twoPoints = true)
: a(a), ab(twoPoints ? b - a : b) {}
Line(const F& xa, const F& ya, const F& xb, const F& yb)
: a(xa, ya), ab(xb - xa, yb - ya) {}
void swap(Line& other) { using std::swap; swap(a, other.a); swap(ab, other.ab); }
template <class F1> explicit operator Line<F1> () const {
return Line<F1>(Point<F1>(a), Point<F1>(ab), false); }
template <class F1> Line& operator = (const Line<F1>& other) {
a = other.a; ab = other.ab; return *this; }
Point<F> b() const { return a + ab; }
operator bool () const { return ab != Point<F>(); }
};
template <class F> int read(Line<F>& line) {
int res = read(line.a, line.ab) / 2;
return line.ab -= line.a, res;
}
template <class F>
inline Line<F> makeLine(const Point<F>& a, const Point<F>& b, bool twoPoints = true) {
return Line<F>(a, b, twoPoints);
}
template <class F> void swap(Line<F>& lhs, Line<F>& rhs) { lhs.swap(rhs); }
template <class F1, class F2>
bool onLine(const Point<F1>& point, const Line<F2>& line) {
if (!line) return point == line.a;
return ((point - line.a) ^ line.ab) == 0;
}
template <class F1, class F2>
bool onSegment(const Point<F1>& point, const Line<F2>& seg) {
if (!seg) return point == seg.a;
auto vecta = seg.a - point, vectb = seg.b() - point;
return (vecta ^ vectb) == 0 && (vecta * vectb) <= 0;
}
template <class F1, class F2> using distF = decltype(sqrt(F1() + F2()));
template <class F1, class F2>
distF<F1, F2> distLine(const Point<F1>& point, const Line<F2>& line) {
if (!line) return dist(point, line.a);
return abs((point - line.a) ^ line.ab) / norm(line.ab);
}
template <class F1, class F2, class F3>
void projection(const Point<F1>& point, const Line<F2>& line, Point<F3>& res) {
res = line.a;
if (line) res += line.ab * static_cast<F3>((point - line.a) * line.ab) / abs(line.ab);
}
template <class F1, class F2, class F3>
void reflection(const Point<F1>& point, const Line<F2>& line, Point<F3>& res) {
projection(point, line, res);
res = 2 * res - point;
}
template <class F1, class F2, class F3>
void closest(const Point<F1>& point, const Line<F2>& seg, Point<F3>& res) {
if (((point - seg.a) * seg.ab) <= 0) res = seg.a;
else if (((point - seg.b()) * seg.ab) >= 0) res = seg.b();
else projection(point, seg, res);
}
template <int TYPE> struct EndpointChecker {};
template <> struct EndpointChecker<0> { // no endpoint (ray)
template <class F> bool operator ()(const F& a, const F& b) const { return true; }};
template <> struct EndpointChecker<1> { // closed endpoint
template <class F> bool operator ()(const F& a, const F& b) const { return a <= b; }};
template <> struct EndpointChecker<2> { // open endpoint
template <class F> bool operator ()(const F& a, const F& b) const { return a < b; }};
template <int LA, int LB, int RA, int RB, class F1, class F2, class F3>
bool intersectLines(const Line<F1>& lhs, const Line<F2>& rhs, Point<F3>& res) {
assert(lhs && rhs);
auto s = lhs.ab ^ rhs.ab;
if (s == 0) return false;
auto ls = (rhs.a - lhs.a) ^ rhs.ab;
auto rs = (rhs.a - lhs.a) ^ lhs.ab;
if (s < 0) s = -s, ls = -ls, rs = -rs;
bool intersect =
EndpointChecker<LA>()(decltype(ls)(0), ls) && EndpointChecker<LB>()(ls, s) &&
EndpointChecker<RA>()(decltype(rs)(0), rs) && EndpointChecker<RB>()(rs, s);
if (intersect) res = lhs.a + lhs.ab * static_cast<F3>(ls) / s;
return intersect;
}
template <class F1, class F2, class F3>
bool intersectClosedSegments(const Line<F1>& lhs, const Line<F2>& rhs, Point<F3>& res) {
return intersectLines<1, 1, 1, 1>(lhs, rhs, res);
}
template <class F1, class F2, class F3>
bool intersectSegments(const Line<F1>& lhs, const Line<F2>& rhs, Line<F3>& res) {
auto s = lhs.ab ^ rhs.ab;
auto ls = (rhs.a - lhs.a) ^ rhs.ab;
if (s == 0) {
if (ls != 0) return false;
auto lhsa = lhs.a, lhsb = lhs.b();
auto rhsa = rhs.a, rhsb = rhs.b();
if (lhsa > lhsb) swap(lhsa, lhsb);
if (rhsa > rhsb) swap(rhsa, rhsb);
res = Line<F3>(max(lhsa, rhsa), min(lhsb, rhsb));
return res.ab >= Point<F3>();
}
auto rs = (rhs.a - lhs.a) ^ lhs.ab;
if (s < 0) s = -s, ls = -ls, rs = -rs;
bool intersect = 0 <= ls && ls <= s && 0 <= rs && rs <= s;
if (intersect)
res = Line<F3>(lhs.a + lhs.ab * static_cast<F3>(ls) / s, Point<F3>());
return intersect;
}
template <class F>
struct AngleCompare {
const Point<F> origin;
const bool zero;
AngleCompare(const Point<F>& origin = Point<F>())
: origin(origin), zero(origin == Point<F>()) {}
template <class F1, class F2>
bool operator () (const Point<F1>& lhs, const Point<F2>& rhs) const {
return zero ? ccw(lhs, rhs) < 0 : ccw(lhs, rhs, origin) < 0;
}
};
template <class Iterator, class F>
void sortByAngle(Iterator first, Iterator last, const Point<F>& origin) {
first = partition(first, last, [&origin](const decltype(*first)& point) {
return point == origin; });
auto pivot = partition(first, last, [&origin](const decltype(*first)& point) {
return point > origin; });
AngleCompare<F> acmp(origin);
sort(first, pivot, acmp);
sort(pivot, last, acmp);
}
/* <======================================= POLYGON =================================> */
template <class F> using Polygon = vector<Point<F>>;
inline int prev(int i, int n) { return i == 0 ? n-1 : i-1; }
inline int next(int i, int n) { return i == n-1 ? 0 : i+1; }
template <class T> inline int sgn(const T& x) { return (T(0) < x) - (x < T(0)); }
template <class F>
F area(const Polygon<F>& poly) {
int n = static_cast<int>(poly.size());
F area = F(0);
for (int i = 0; i < n; ++i)
area += poly[i].x * (poly[next(i, n)].y - poly[prev(i, n)].y);
return area;
}
// True if orientation of a simple polygon is counter-clockwise.
template <class F>
bool orientation(const Polygon<F>& poly) {
int n = static_cast<int>(poly.size());
int i = static_cast<int>(min_element(begin(poly), end(poly)) - begin(poly));
return ccw(poly[prev(i, n)], poly[next(i, n)], poly[i]) > 0;
}
template <class F>
Polygon<F> convexHull(Polygon<F> points) {
sort(begin(points), end(points));
Polygon<F> hull;
hull.reserve(points.size() + 1);
for (int phase = 0; phase < 2; ++phase) {
auto start = hull.size();
for (auto& point : points) {
while (hull.size() >= start+2 &&
ccw(point, hull.back(), hull[hull.size()-2]) <= 0)
hull.pop_back();
hull.push_back(point);
}
hull.pop_back();
reverse(begin(points), end(points));
}
if (hull.size() == 2 && hull[0] == hull[1]) hull.pop_back();
return hull;
}
template <class F1, class F2>
int pointVsTriangle(const Point<F1>& point, const Polygon<F2>& triangle) {
assert(triangle.size() == 3);
int signs[3];
for (int i = 0; i < 3; ++i)
signs[i] = sgn(ccw(point, triangle[next(i, 3)], triangle[i]));
if (signs[0] == signs[1] && signs[1] == signs[2]) return -1;
for (int i = 0; i < 3; ++i) if (signs[i] * signs[next(i, 3)] == -1) return 1;
return 0;
}
template <class F1, class F2>
int pointVsConvexPolygon(const Point<F1>& point, const Polygon<F2>& poly, int top) {
if (point < poly[0] || point > poly[top]) return 1;
auto orientation = ccw(point, poly[top], poly[0]);
if (orientation == 0) {
if (point == poly[0] || point == poly[top]) return 0;
return top == 1 || top + 1 == poly.size() ? 0 : -1;
} else if (orientation < 0) {
auto itRight = lower_bound(begin(poly) + 1, begin(poly) + top, point);
return sgn(ccw(itRight[0], point, itRight[-1]));
} else {
auto itLeft = upper_bound(poly.rbegin(), poly.rend() - top-1, point);
return sgn(ccw(itLeft == poly.rbegin() ? poly[0] : itLeft[-1], point, itLeft[0]));
}
}
template <class F1, class F2>
int pointVsPolygon(const Point<F1>& point, const Polygon<F2>& poly) {
int n = static_cast<int>(poly.size()), windingNumber = 0;
for (int i = 0; i < n; ++i) {
if (point == poly[i]) return 0;
int j = next(i, n);
if (poly[i].y == point.y && poly[j].y == point.y) {
if (min(poly[i].x, poly[j].x) <= point.x &&
point.x <= max(poly[i].x, poly[j].x)) return 0;
} else {
bool below = poly[i].y < point.y;
if (below != (poly[j].y < point.y)) {
auto orientation = ccw(point, poly[j], poly[i]);
if (orientation == 0) return 0;
if (below == (orientation > 0)) windingNumber += below ? 1 : -1;
}
}
}
return windingNumber == 0 ? 1 : -1;
}
template <class F, class Function>
int extremeVertex(const Polygon<F>& poly, Function direction) {
int n = static_cast<int>(poly.size()), left = 0, leftSgn;
auto vertexCmp = [&poly, direction](int i, int j) {
return sgn(ccw(direction(poly[j]), poly[j] - poly[i])); };
auto isExtreme = [n, vertexCmp](int i, int& iSgn) {
return (iSgn = vertexCmp(next(i, n), i)) >= 0 && vertexCmp(i, prev(i, n)) < 0; };
for (int right = isExtreme(0, leftSgn) ? 1 : n; left + 1 < right;) {
int middle = (left + right) / 2, middleSgn;
if (isExtreme(middle, middleSgn)) return middle;
if (leftSgn != middleSgn ? leftSgn < middleSgn
: leftSgn == vertexCmp(left, middle)) right = middle;
else left = middle, leftSgn = middleSgn;
}
return left;
}
template <class F1, class F2>
pair<int, int> tangentsConvex(const Point<F1>& point, const Polygon<F2>& poly) {
return {
extremeVertex(poly, [&point](const Point<F2>& q) { return q - point; }),
extremeVertex(poly, [&point](const Point<F2>& q) { return point - q; })};
}
template <class F1, class F2, class F3>
bool stabConvexPolygon(const Line<F1>& line, const Polygon<F2>& poly, Line<F3>& res) {
assert(line);
int right = extremeVertex(poly, [&line](const Point<F2>&) { return line.ab; });
int left = extremeVertex(poly, [&line](const Point<F2>&) { return -line.ab; });
auto vertexCmp = [&line](const Point<F2>& vertex) {
return sgn(ccw(line.ab, vertex - line.a)); };
int rightSgn = vertexCmp(poly[right]), leftSgn = vertexCmp(poly[left]);
if (rightSgn < 0 || leftSgn > 0) return false;
auto intersectChain = [&line, &poly, vertexCmp](int first, int last,
int firstSgn, Point<F3>& res) {
int n = static_cast<int>(poly.size());
while (next(first, n) != last) {
int middle = (first + last + (first < last ? 0 : n)) / 2;
if (middle >= n) middle -= n;
if (vertexCmp(poly[middle]) == firstSgn) first = middle;
else last = middle;
}
intersectLines<0, 0, 0, 0>(line, makeLine(poly[first], poly[last]), res);
};
intersectChain(left, right, leftSgn, res.a);
intersectChain(right, left, rightSgn, res.ab);
res.ab -= res.a;
return true;
}
template <class F1, class F2, class F = distF<F1, F2>>
F stabPolygonLength(const Line<F1>& line, const Polygon<F2>& poly) {
assert(line);
F tSum = F(0);
int n = static_cast<int>(poly.size());
auto vertexSgn = [&line, &poly](int i) { return sgn(line.ab ^ (poly[i] - line.a)); };
int prevSgn = vertexSgn(n - 1), iSgn = vertexSgn(0), nextSgn;
for (int i = 0; i < n; ++i, prevSgn = iSgn, iSgn = nextSgn) {
nextSgn = vertexSgn(next(i, n));
if (iSgn == 0) {
if (prevSgn == 0) {
if (nextSgn != 0 && nextSgn == sgn((poly[i] - poly[prev(i, n)]) * line.ab))
tSum += nextSgn * static_cast<F>((poly[i] - line.a) * line.ab) / abs(line.ab);
} else {
if ((nextSgn != 0 && nextSgn != prevSgn) ||
(nextSgn == 0 && prevSgn == sgn((poly[next(i, n)] - poly[i]) * line.ab)))
tSum -= prevSgn * static_cast<F>((poly[i] - line.a) * line.ab) / abs(line.ab);
}
} else if (nextSgn == -iSgn) {
auto vect = poly[next(i, n)] - poly[i];
tSum += nextSgn * static_cast<F>((poly[i] - line.a) ^ vect) / (line.ab ^ vect);
}
}
return tSum * norm(line.ab);
}
template <class F>
F maxDist2(const Polygon<F>& poly) {
int n = static_cast<int>(poly.size());
F res = F(0);
for (int i = 0, j = n < 2 ? 0 : 1; i < j; ++i)
for (;; j = next(j, n)) {
res = max(res, dist2(poly[i], poly[j]));
if (ccw(poly[i+1] - poly[i], poly[next(j, n)] - poly[j]) >= 0) break;
}
return res;
}
int xc, yc ;
bool comp(Point <int> p1, Point <int> p2) {
return (p1.x - xc) * (p1.x - xc) + (p1.y - yc) * (p1.y - yc) <= (p2.x - xc) * (p2.x - xc) + (p2.y - yc) * (p2.y - yc);
}
void solve() {
int n, q;
cin >> n >> q;
set <Point <int>> s;
for(int i = 0; i < n; i++) {
Point <int> p;
cin >> p
s.insert(p);
}
Polygon points;
for(auto &it : s)
points.push_back(it);
Polygon poly;
int lo = 0, hi = points.size - 1;
while(q--) {
cin >> xc >> yc;
sort(points.begin(), points.end(), comp);
while(lo <= hi) {
int mi = (lo + hi) >> 1;
int i = lo;
while(i <= mi)
new_points.push_back(points[i++]);
Polygon <int> H(new_points);
for(auto &it : poly)
if(pointVsPolygon(it, new_points) <= 0) {
ok = false;
break;
}
if(ok)
hi = mi - 1;
else
lo = mi + 1;
}
while()
}
}
int32_t main() {
int t;
cin >> t;
while(t--)
solve();
}