-
Notifications
You must be signed in to change notification settings - Fork 3
/
Geometry.h
351 lines (301 loc) · 6.67 KB
/
Geometry.h
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
#ifdef SUPER_PRECISE
using ld = long double;
#define sqrt sqrtl
#define fabs fabsl
#define cos cosl
#define sin sinl
#define acos acosl
#define asin asinl
#else
using ld = double;
#endif
const ld eps = 1e-9;
const ld PI = acosl(-1);
#ifdef INT_GEOM
using ptype = long long;
#define ptype_zero(x) ((x) == 0)
#define abs llabs
#else
using ptype = ld;
#define ptype_zero(x) (fabs((x)) < eps)
#define abs fabs
#endif
int sign(ptype x) {
if (ptype_zero(x)) {
return 0;
} else if (x > 0) {
return 1;
} else {
return -1;
}
}
class Point {
public:
Point() {}
Point(ptype _x, ptype _y): x(_x), y(_y) {}
ptype getX() const {
return x;
}
ptype getY() const {
return y;
}
Point operator +(const Point& ot) const {
return Point(x + ot.x, y + ot.y);
}
Point operator -(const Point& ot) const {
return Point(x - ot.x, y - ot.y);
}
Point operator *(ptype k) const {
return Point(x * k, y * k);
}
void operator +=(const Point& ot) {
x += ot.x;
y += ot.y;
}
void operator -=(const Point& ot) {
x -= ot.x;
y -= ot.y;
}
void operator *=(ptype k) {
x *= k;
y *= k;
}
#ifndef INT_GEOM
Point operator /(ptype k) const {
return Point(x / k, y / k);
}
void operator /=(ptype k) {
x /= k;
y /= k;
}
#endif
ptype operator %(const Point& ot) const {
return x * ot.x + y * ot.y;
}
ptype operator *(const Point& ot) const {
return x * ot.y - y * ot.x;
}
ptype getSqrDist() const {
return x * x + y * y;
}
ptype getSqrDist(const Point& ot) const {
return (ot - *this) % (ot - *this);
}
ld getDist() const {
return sqrt(getSqrDist());
}
ld getDist(const Point& pt) const {
return sqrt(getSqrDist(pt));
}
void rot90() {
swap(x, y);
x = -x;
}
#ifndef INT_GEOM
void rot(ld angle) {
ptype nx = x * cos(angle) + y * sin(angle);
ptype ny = x * sin(angle) - y * cos(angle);
x = nx, y = ny;
}
void inverse(ptype radius = 1) {
ptype dist2 = getSqrDist();
x = x * radius * radius / dist2;
y = y * radius * radius / dist2;
}
Point getInversed(ptype radius = 1) const {
ptype dist2 = getSqrDist();
return Point(x * radius * radius / dist2, y * radius * radius / dist2);
}
#endif
bool operator <(const Point& ot) const {
int sx = sign(x - ot.x);
int sy = sign(y - ot.y);
if (sx) {
return sx == -1;
} else {
return sy == -1;
}
}
void scan() {
cin >> x >> y;
}
// private:
ptype x, y;
};
istream& operator >>(istream& in, Point& pt) {
ptype x, y;
in >> x >> y;
pt = Point(x, y);
return in;
}
ostream& operator <<(ostream& out, const Point& pt) {
out << "(" << pt.getX() << ", " << pt.getY() << ")";
return out;
}
class Line {
public:
Line() {}
Line(const Point& fst, const Point& snd) {
p = fst;
n = snd - fst;
n.rot90();
}
bool intersects(const Line& ot) const {
return !ptype_zero(n * ot.n);
}
bool contains(const Point& pt) const {
return ptype_zero((pt - p) % n);
}
bool is_parallel(const Line& ot) const {
return ptype_zero(n * ot.n);
}
ptype apply(const Point& pt) const {
return (pt - p) % n;
}
#ifndef INT_GEOM
Point intersect(const Line& ot) const {
if (!intersects(ot)) {
return Point();
}
ptype c1 = p % n, c2 = ot.p % ot.n;
return (n * (c1 * ot.n.getSqrDist() - c2 * (n % ot.n)) +
ot.n * (c2 * n.getSqrDist() - c1 * (n % ot.n))) /
(n.getSqrDist() * ot.n.getSqrDist() - (n % ot.n) * (n % ot.n));
}
#endif
// private:
Point p, n;
};
class Segment {
public:
Segment() {}
Segment(const Point& _p, const Point& _q): p(_p), q(_q) {
l = Line(_p, _q);
}
bool intersectsInside(const Segment& ot) const {
if (ptype_zero(l.n * ot.l.n)) {
return false;
}
return (sign(l.apply(ot.p)) * sign(l.apply(ot.q)) == -1) &&
(sign(ot.l.apply(p)) * sign(ot.l.apply(q)) == -1);
}
#ifndef INT_GEOM
Point intersectInside(const Segment& ot) const {
if (!intersectsInside(ot)) {
return Point();
} else {
return l.intersect(ot.l);
}
}
#endif
bool contains(const Point& pt) const {
return (sign((pt - p) % (pt - q)) == -1) && ptype_zero((pt - p) * (pt - q));
}
// private:
Point p, q;
Line l;
};
class Polygon {
public:
Polygon() {}
Polygon(const vector<Point>& _pts): pts(_pts) {}
ptype getDoubleSquare() const {
ptype result = 0;
int n = pts.size();
for (int i = 1; i < n - 1; ++i) {
result += (pts[i] - pts[0]) * (pts[i + 1] - pts[0]);
}
return abs(result);
}
virtual bool contains(const Point& pt) const {
int n = pts.size();
for (int i = 0; i < n; ++i) {
int j = i + 1;
if (j == n) {
j = 0;
}
if (Segment(pts[i], pts[j]).contains(pt)) {
return true;
}
}
Point v(rand(), rand());
ptype maxx = 0, maxy = 0;
for (const Point& p : pts) {
maxx = max(maxx, abs(p.x - pt.x));
maxy = max(maxy, abs(p.y - pt.y));
}
while (ptype_zero(v.x) || ptype_zero(v.y)) {
v = Point(rand(), rand());
}
ptype k = min(maxx / abs(v.x), maxy / abs(v.y)) + 1;
v *= k;
Segment ray(pt, pt + v);
bool result = false;
for (int i = 0; i < n; ++i) {
int j = i + 1;
if (j == n) {
j = 0;
}
if (ray.intersectsInside(Segment(pts[i], pts[j]))) {
result ^= 1;
}
}
return result;
}
// private:
vector<Point> pts;
};
/***************************************
Convex polygon should have its
vertices in the CCW order.
Moreover, the 0th vertex should
be the lowest among the left ones.
However, it may be not necessary
***************************************/
class ConvexPolygon : public Polygon {
public:
ConvexPolygon(const vector<Point>& _pts): pts(_pts) {}
bool contains(const Point& pt) const {
int n = pts.size();
int l = 1, r = n - 1;
while (r > l + 1) {
int mid = (l + r) / 2;
int sgn = sign((pt - pts[0]) * (pts[mid] - pts[0]));
if (sgn == 0) {
return Segment(pts[0], pts[mid]).contains(pt);
} else if (sgn == 1) {
r = mid;
} else {
l = mid;
}
}
Segment side(pts[l], pts[r]);
if (Segment(pts[0], pts[l]).contains(pt) || Segment(pts[0], pts[r]).contains(pt)) {
return true;
}
return !side.intersectsInside(Segment(pts[0], pt));
}
// private:
vector<Point> pts;
};
ConvexPolygon getConvexHull(vector<Point> pts) {
int index = min_element(pts.begin(), pts.end()) - pts.begin();
swap(pts[index], pts[0]);
Point origin = pts[0];
sort(pts.begin() + 1, pts.end(), [&origin](const Point& fi, const Point& se) {
int sgn = sign((fi - origin) * (se - origin));
if (sgn) {
return sgn == 1;
} else {
return (fi - origin).getSqrDist() < (se - origin).getSqrDist();
}
});
vector<Point> result = {pts[0]};
for (int i = 1; i < (int)pts.size(); ++i) {
while (result.size() > 1 && sign((result.back() - result[result.size() - 2]) * (pts[i] - result.back())) != 1) {
result.pop_back();
}
result.push_back(pts[i]);
}
return ConvexPolygon(result);
}