Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

计算几何全家桶

计算几何模板。点与向量共用 Vector,^ 为点积,& 为叉积,接口统一使用首字母大写命名。多边形不重复存储首点,凸包统一按逆时针排列。

  • Pick 定理:给定顶点均为整点的简单多边形,其面积 $S$ 和内部格点数目 $a$ 、边上格点数目 $b$ 的关系:$S = a + \frac b 2 - 1$ 。
  • 欧拉公式:多面体的点数 $V$ 与边数 $E$ 与面数 $F$ 的关系:$V - E + F = 2$ 。
  • 线段接口:OnSegment(q, a, b) 判断点在线段上;IntersectSegment(a, b, c, d, strict) 判断相交;Dis(q, a, b)、Dis(a, b, c, d) 分别为点到线段、两线段距离。Segment 本身仍表示直线。
  • 包含判定统一为内部 1、边界 0、外部 -1。InConvex 要求不保留共线点的逆时针凸包;Cut 保留有向直线左侧。交点接口按参数类型重载 intersection,返回 vector<Point> 时,相切只返回一个交点。
  • Delaunay 三角剖分:采用分治与 Quad-edge,参考 OI Wiki。Delaunay dt; dt.Init(p);,GetEdge() 返回无向边,GetTriangle() 返回逆时针三角形,编号均为输入点集的下标(从 0 开始)。
查看代码
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <utility>
#include <array>
#define pb push_back
using namespace std;
const double pi = acos(-1), eps = 1e-12; //eps 按题目精度和坐标范围调整
int Cmp (double a, double b)
{
if (a - b > eps) return 1;
if (b - a > eps) return -1;
return 0;
}
int Sign (double a) { return Cmp(a, 0); }
struct Vector
{
double x, y;
Vector () { } //默认构造不初始化坐标
Vector (double a, double b)
{ x = a, y = b; }
void Input () { scanf("%lf%lf", &x, &y); }
void Output () { printf("%.10lf %.10lf\n", x + eps, y + eps); }
bool operator < (const Vector &_) const //排序不用 eps
{ return x != _.x ? x < _.x : y < _.y; }
Vector operator + (const Vector &_) const
{ return Vector(x + _.x, y + _.y); }
Vector operator - (const Vector &_) const
{ return Vector(x - _.x, y - _.y); }
Vector operator * (double _) const
{ return Vector(x * _, y * _); }
Vector operator / (double _) const //除数非零
{ return Vector(x / _, y / _); }
double operator ^ (const Vector &_) const
{ return x * _.x + y * _.y; }
double operator & (const Vector &_) const
{ return x * _.y - y * _.x; }
double Len () const { return hypot(x, y); }
Vector Unit () const { return *this / Len(); } //非零向量
Vector Perp () const { return Vector(-y, x); } //逆时针转 90 度
Vector Rotate (double th) const
{ return Vector(x * cos(th) - y * sin(th), x * sin(th) + y * cos(th)); }
};
using Point = Vector;
Point Mid (Point a, Point b)
{ return (a + b) / 2; }
double Area (Point a, Point b, Point c) //有向面积的两倍
{ return (b - a) & (c - a); }
double Angle (Vector a, Vector b) //非零向量的夹角,范围 [0, pi]
{ return atan2(abs(a & b), a ^ b); }
double Dis (Point a, Point b)
{ return (a - b).Len(); }
bool PolarLess (Vector a, Vector b) //极角排序可直接 sort(..., PolarLess),不用 eps
{
if ((!a.x && !a.y) || (!b.x && !b.y)) return (a ^ a) < (b ^ b); //零向量在前
bool x = a.y < 0 || (a.y == 0 && a.x < 0);
bool y = b.y < 0 || (b.y == 0 && b.x < 0);
return x != y ? x < y : (a & b) > 0; //同向时不比较长度
}
bool OnSegment (Point q, Point a, Point b) //含端点,允许退化为点
{
if (a.x == b.x && a.y == b.y) return !Sign(Dis(q, a));
return !Sign(Area(a, b, q)) && Sign((q - a) ^ (q - b)) <= 0;
}
bool IntersectSegment (Point a, Point b, Point c, Point d, bool strict = false)
{
//默认含端点及重合;strict 为 true 时仅算两线段内部的非平行交叉
int x = Sign(Area(a, b, c)), y = Sign(Area(a, b, d));
int z = Sign(Area(c, d, a)), w = Sign(Area(c, d, b));
if (x * y < 0 && z * w < 0) return true;
return !strict && (OnSegment(a, c, d) || OnSegment(b, c, d) ||
OnSegment(c, a, b) || OnSegment(d, a, b));
}
struct Segment //表示有向直线,不限制在线段内;方向向量非零
{
Point p; Vector t;
Segment (Point _p, Vector _t)
{ p = _p, t = _t; }
double Angle () const { return atan2(t.y, t.x); }
int On (Point _) const //左侧为 1,线上为 0,右侧为 -1;按垂距判断,与方向长度无关
{ return Sign(t.Unit() & (_ - p)); }
bool operator < (const Segment &_) const //按极角排序,同向时更靠左的直线在前,不用 eps
{
if (PolarLess(t, _.t)) return true;
if (PolarLess(_.t, t)) return false;
return (t.Unit() & p) > (_.t.Unit() & _.p);
}
Point Proj (Point _) const
{
Vector v = t.Unit();
return p + v * (v ^ (_ - p));
}
Point Reflect (Point _) const { return Proj(_) * 2 - _; }
double Dis (Point _) const
{ return abs(t.Unit() & (_ - p)); }
};
Segment Vertical (Point a, Point b)
{ return Segment(Mid(a, b), (b - a).Perp()); }
Point Intersection (Segment a, Segment b) //两直线不平行
{
Vector u = a.t.Unit(), v = b.t.Unit();
return a.p + u * ((v & (a.p - b.p)) / (u & v));
}
double Dis (Point q, Point a, Point b) //点到线段,允许退化为点
{
Vector v = b - a;
double z = v ^ v;
if (!z) return Dis(q, a);
double t = max(0.0, min(1.0, (v ^ (q - a)) / z));
return Dis(q, a + v * t);
}
double Dis (Point a, Point b, Point c, Point d) //两线段距离
{
if (IntersectSegment(a, b, c, d)) return 0;
return min(min(Dis(a, c, d), Dis(b, c, d)), min(Dis(c, a, b), Dis(d, a, b)));
}
struct Circle
{
Point p; double r;
Circle () { }
Circle (Point _p, double _r)
{ p = _p, r = _r; }
Circle (Point a, Point b, Point c) //外接圆;共线时取最远两点为直径
{
if (!Sign(Area(a, b, c))) //近共线也按共线处理,精度敏感题需留意
{
p = Mid(a, b), r = Dis(p, a);
if (Cmp(r, Dis(p, c)) < 0)
p = Mid(a, c), r = Dis(p, a);
if (Cmp(r, Dis(p, b)) < 0)
p = Mid(b, c), r = Dis(p, b);
return;
}
p = Intersection(Vertical(a, b), Vertical(a, c));
r = Dis(p, a);
}
int On (Point q) { return Cmp(r, Dis(p, q)); } //内 1,圆上 0,外 -1
vector <Point> Tangent (Point q) //点到圆的切点,要求 r > 0
{
Vector v = q - p;
double d = v.Len();
int s = Cmp(d, r); //长度与长度比较,不对平方量使用 eps
if (s < 0 || !d) return {};
if (!s) return {p + v * (r / d)};
double t = r / d;
Point h = p + v * (t * t);
Vector w = v.Perp() * (t * sqrt(max(0.0, 1 - t * t)));
return {h - w, h + w};
}
};
vector <Point> Intersection (Circle c, Segment l) //直线方向非零;相切仅返回一个点
{
Point h = l.Proj(c.p);
double d = Dis(h, c.p);
int s = Cmp(d, c.r);
if (s > 0) return {};
if (!s) return {h};
Vector v = l.t.Unit() * sqrt(max(0.0, (c.r - d) * (c.r + d)));
return {h - v, h + v};
}
vector <Point> Intersection (Circle a, Circle b) //要求半径 > 0;重合圆返回空,需另判无限交点
{
Vector v = b.p - a.p;
double d = v.Len();
int s = Cmp(d, a.r + b.r), t = Cmp(d, abs(a.r - b.r));
if (!d || s > 0 || t < 0) return {};
double x = (d + (a.r - b.r) * (a.r + b.r) / d) / 2;
Point h = a.p + v * (x / d);
if (!s || !t) return {h}; //相切附近按一个交点处理
v = v.Perp() * (sqrt(max(0.0, (a.r - x) * (a.r + x))) / d);
return {h - v, h + v};
}
struct Polygon
{
vector <Point> p;
void Input ()
{
int n; scanf("%d", &n);
p.resize(n);
for (int i = 0; i < n; ++i)
p[i].Input();
}
double PolygonArea ()
{
double res = 0;
for (int i = 1; i + 1 < p.size(); ++i)
res += Area(p[0], p[i], p[i + 1]); //以首点为原点,减少大坐标平移后的抵消
return abs(res) / 2;
}
double PolygonCircumference ()
{
double res = 0;
for (int i = 0; i < p.size(); ++i)
res += Dis(p[i], p[(i + 1) % p.size()]);
return res;
}
Point Centroid () //面积重心,要求简单多边形且面积非零;顺逆时针均可
{
Point o = p[0], res(0, 0);
double s = 0;
for (int i = 0; i < p.size(); ++i)
{
Vector a = p[i] - o, b = p[(i + 1) % p.size()] - o;
double t = a & b;
res = res + (a + b) * t, s += t;
}
return o + res / (3 * s);
}
int InPolygon (Point q) //简单多边形,O(n);内 1,边界 0,外 -1;空集返回 -1
{
int w = 0;
for (int i = 0; i < p.size(); ++i)
{
Point a = p[i], b = p[(i + 1) % p.size()];
if (OnSegment(q, a, b)) return 0;
int x = Cmp(a.y, q.y), y = Cmp(b.y, q.y), z = Sign(Area(a, b, q));
if (x <= 0 && y > 0 && z > 0) ++w;
if (y <= 0 && x > 0 && z < 0) --w;
}
return w ? 1 : -1;
}
int InConvex (Point q) //逆时针凸包,不保留共线点,O(log n);返回值同 InPolygon
{
int n = p.size();
if (!n) return -1;
if (n <= 2) return OnSegment(q, p[0], p[n - 1]) ? 0 : -1;
int a = Sign(Area(p[0], p[1], q)), b = Sign(Area(p[0], p[n - 1], q));
if (a < 0 || b > 0) return -1;
if (!a) return OnSegment(q, p[0], p[1]) ? 0 : -1;
if (!b) return OnSegment(q, p[0], p[n - 1]) ? 0 : -1;
int l = 1, r = n - 1;
while (r - l > 1)
{
int m = (l + r) / 2;
if (Sign(Area(p[0], p[m], q)) >= 0) l = m;
else r = m;
}
return Sign(Area(p[l], p[r], q));
}
Polygon Cut (Segment l) //凸多边形裁剪,保留直线左侧及边界,O(n),不修改原多边形
{
Polygon res;
auto add = [&] (Point q)
{
if (res.p.empty() || Sign(Dis(res.p.back(), q))) res.p.pb(q);
};
for (int i = 0; i < p.size(); ++i)
{
Point a = p[i], b = p[(i + 1) % p.size()];
int x = l.On(a), y = l.On(b);
if (x >= 0) add(a);
if (x * y < 0) add(Intersection(Segment(a, b - a), l));
}
if (res.p.size() > 1 && !Sign(Dis(res.p.front(), res.p.back()))) res.p.pop_back();
return res; //可能为空、单点或线段;凹多边形的多块结果不适用
}
Polygon ConvexHull () //逆时针,不保留边上的共线点;会排序、去重原点集
{
sort(p.begin(), p.end());
p.erase(unique(p.begin(), p.end(), [] (Point a, Point b)
{ return a.x == b.x && a.y == b.y; }), p.end());
if (p.size() < 3) return *this;
vector <int> stk(p.size() * 2);
int top = 0;
for (int i = 0; i < p.size(); ++i)
{
while (top >= 2 && Sign(Area(p[stk[top - 1]], p[stk[top]], p[i])) <= 0)
--top;
stk[++top] = i;
}
int t = top;
for (int i = (int)p.size() - 2; ~i; --i)
{
while (top > t && Sign(Area(p[stk[top - 1]], p[stk[top]], p[i])) <= 0)
--top;
stk[++top] = i;
}
Polygon res;
for (int i = 1; i < top; ++i)
res.p.pb(p[stk[i]]);
return res;
}
void RotateConvex () //将最低、其次最左的点旋转到开头,不改变方向
{
if (p.size() < 2) return;
int s = 0;
for (int i = 1; i < p.size(); ++i)
if (p[i].y < p[s].y || (p[i].y == p[s].y && p[i].x < p[s].x))
s = i;
rotate(p.begin(), p.begin() + s, p.end());
}
double Diameter () //非空逆时针凸包
{
if (p.size() <= 2) return Dis(p.front(), p.back());
double res = 0;
for (int i = 0, j = 1; i < p.size(); ++i)
{
Point d = p[i], e = p[(i + 1) % p.size()];
while (Cmp(Area(d, e, p[j]), Area(d, e, p[(j + 1) % p.size()])) == -1)
j = (j + 1) % p.size();
res = max(res, max(Dis(d, p[j]), Dis(e, p[j])));
}
return res;
}
Circle CircleCover () //非空点集;会打乱原顺序,之后需重新求凸包才能用旋转卡壳
{
random_shuffle(p.begin(), p.end()); //C++14;C++17 起用 shuffle 并传入随机数引擎
Circle c;
c.p = p[0], c.r = 0;
for (int i = 1; i < p.size(); ++i)
{
if (Cmp(c.r, Dis(c.p, p[i])) >= 0) continue;
c.p = p[i], c.r = 0;
for (int j = 0; j < i; ++j)
{
if (Cmp(c.r, Dis(c.p, p[j])) >= 0) continue;
c.p = Mid(p[i], p[j]), c.r = Dis(p[i], p[j]) / 2;
for (int k = 0; k < j; ++k)
if (Cmp(c.r, Dis(c.p, p[k])) < 0)
c = Circle(p[i], p[j], p[k]);
}
}
return c;
}
Polygon RectangleCover () //至少三个点的非退化逆时针凸包,不保留共线点
{
double mn = 0;
Polygon res; res.p.resize(4);
for (int i = 0, a = 2, b = 1, c = 2; i < p.size(); ++i)
{
Segment l(p[i], p[(i + 1) % p.size()] - p[i]);
while (Cmp(l.Dis(p[a]), l.Dis(p[(a + 1) % p.size()])) < 0)
a = (a + 1) % p.size();
while (Cmp(l.t ^ (p[b] - l.p), l.t ^ (p[(b + 1) % p.size()] - l.p)) < 0)
b = (b + 1) % p.size();
if (!i) c = a;
while (Cmp(l.t ^ (p[c] - l.p), l.t ^ (p[(c + 1) % p.size()] - l.p)) > 0)
c = (c + 1) % p.size();
double h = l.Dis(p[a]), w = Dis(l.Proj(p[b]), l.Proj(p[c]));
if (!i || h * w < mn)
{
mn = h * w;
res.p[0] = l.Proj(p[c]), res.p[1] = l.Proj(p[b]);
Vector u = l.t.Unit().Perp() * h;
res.p[2] = res.p[1] + u, res.p[3] = res.p[0] + u;
}
}
return res;
}
};
Polygon Intersection (Polygon a, Polygon b) //两个非退化逆时针凸多边形求交,O(nm)
{
for (int i = 0; i < b.p.size() && !a.p.empty(); ++i)
a = a.Cut(Segment(b.p[i], b.p[(i + 1) % b.p.size()] - b.p[i]));
return a;
}
Polygon Minkowski (Polygon a, Polygon b) //输入逆时针凸包,无重复点;结果可能保留共线点
{
//接 InConvex / RectangleCover 前,先对结果调用 ConvexHull 去掉共线点
Polygon res;
a.RotateConvex(), b.RotateConvex();
if (a.p.empty() || b.p.empty()) return res;
int n = a.p.size(), m = b.p.size();
res.p.pb(a.p[0] + b.p[0]);
a.p.pb(a.p[0]), b.p.pb(b.p[0]);
for (int i = 0; i < n; ++i) a.p[i] = a.p[i + 1] - a.p[i];
for (int i = 0; i < m; ++i) b.p[i] = b.p[i + 1] - b.p[i];
a.p.resize(n > 1 ? n : 0), b.p.resize(m > 1 ? m : 0); //单点没有边
res.p.resize(a.p.size() + b.p.size() + 1);
merge(a.p.begin(), a.p.end(), b.p.begin(), b.p.end(), res.p.begin() + 1, PolarLess);
for (int i = 1; i < res.p.size(); ++i)
res.p[i] = res.p[i - 1] + res.p[i];
if (res.p.size() > 1) res.p.pop_back(); //去掉重复首点
return res;
}
Polygon HalfPlaneIntersection (vector <Segment> &l) //取直线左侧
{
//简版:按有界且有正面积的交集使用;无界、退化、空交及反向平行线需另行处理
//需要有界交时可加入足够大的包围盒;本函数会排序原直线集
sort(l.begin(), l.end());
vector <int> q(l.size() + 1);
int hd = 0, tl = -1;
for (int i = 0; i < l.size(); ++i)
{
if (i && !PolarLess(l[i - 1].t, l[i].t)) continue; //仅合并同向直线,不用 eps 合并近似方向
while (hd < tl && l[i].On(Intersection(l[q[tl - 1]], l[q[tl]])) <= 0) --tl;
while (hd < tl && l[i].On(Intersection(l[q[hd + 1]], l[q[hd]])) <= 0) ++hd;
q[++tl] = i;
}
while (hd + 1 < tl && l[q[hd]].On(Intersection(l[q[tl - 1]], l[q[tl]])) <= 0) --tl;
while (hd + 1 < tl && l[q[tl]].On(Intersection(l[q[hd + 1]], l[q[hd]])) <= 0) ++hd;
q[++tl] = q[hd];
Polygon res;
for (int i = hd; i < tl; ++i)
res.p.pb(Intersection(l[q[i]], l[q[i + 1]]));
return res;
}
bool InCircle (Point a, Point b, Point c, Point d) //abc 逆时针,d 是否严格在外接圆内
{
a = a - d, b = b - d, c = c - d;
return Sign((a ^ a) * (b & c) + (b ^ b) * (c & a) + (c ^ c) * (a & b)) > 0;
}
struct Delaunay //点集剖分,非多边形剖分;时间 O(n log n),空间 O(n)
{
//要求点互不重合;共线时连成链,共圆时取一种剖分
//简版浮点判断:近共线、近共圆或坐标较大时,Area / InCircle 可能有精度问题
struct Edge { int u, next; }; //每条无向边占四格,偶数为原图,奇数为对偶图
vector <Point> p;
vector <int> id, pool;
vector <Edge> e;
int Rot (int x) { return (x & ~3) | ((x + 1) & 3); }
int Rev (int x) { return x ^ 2; }
int Org (int x) { return e[x].u; }
int Dest (int x) { return Org(Rev(x)); }
int Next (int x) { return e[x].next; } //同起点的下一条逆时针边
int Prev (int x) { return Rot(Next(Rot(x))); }
int LNext (int x) { return Rot(Next(Rev(Rot(x)))); } //沿左侧面前进
int Side (int v, int x) { return Sign(Area(p[Org(x)], p[Dest(x)], p[v])); }
int New (int u, int v)
{
int x;
if (pool.empty()) x = e.size(), e.resize(x + 4);
else x = pool.back(), pool.pop_back();
e[x] = {u, x}, e[x + 1] = {-1, x + 3};
e[x + 2] = {v, x + 2}, e[x + 3] = {-1, x + 1};
return x;
}
void Splice (int a, int b) //交换后继,同时维护对偶图
{
int u = Rot(Next(a)), v = Rot(Next(b));
swap(e[a].next, e[b].next), swap(e[u].next, e[v].next);
}
void Del (int x)
{
Splice(x, Prev(x)), Splice(Rev(x), Prev(Rev(x)));
x &= ~3;
e[x].u = e[x + 2].u = -1;
pool.pb(x);
}
int Link (int a, int b) //连接 a 的终点与 b 的起点
{
int x = New(Dest(a), Org(b));
Splice(x, LNext(a)), Splice(Rev(x), b);
return x;
}
pair <int, int> Divide (int l, int r) //[l, r),返回左右端点的凸包边
{
if (r - l == 2)
{
int a = New(l, l + 1);
return {a, Rev(a)};
}
if (r - l == 3)
{
int a = New(l, l + 1), b = New(l + 1, l + 2);
Splice(Rev(a), b);
int t = Sign(Area(p[l], p[l + 1], p[l + 2]));
if (!t) return {a, Rev(b)};
int c = Link(b, a);
return t > 0 ? make_pair(a, Rev(b)) : make_pair(Rev(c), c);
}
int m = (l + r) / 2;
auto a = Divide(l, m), b = Divide(m, r);
int u = a.second, v = b.first;
while (true) //寻找下公切线
{
if (Side(Org(v), u) > 0) u = LNext(u);
else if (Side(Org(u), v) < 0) v = Next(Rev(v));
else break;
}
int base = Link(Rev(v), u); //从右向左
if (Org(u) == Org(a.first)) a.first = Rev(base);
if (Org(v) == Org(b.second)) b.second = base;
while (true)
{
int x = Next(Rev(base));
if (Side(Dest(x), base) < 0)
while (InCircle(p[Dest(base)], p[Org(base)], p[Dest(x)], p[Dest(Next(x))]))
{
int t = Next(x);
Del(x), x = t;
}
int y = Prev(base);
if (Side(Dest(y), base) < 0)
while (InCircle(p[Dest(base)], p[Org(base)], p[Dest(y)], p[Dest(Prev(y))]))
{
int t = Prev(y);
Del(y), y = t;
}
bool vx = Side(Dest(x), base) < 0, vy = Side(Dest(y), base) < 0;
if (!vx && !vy) break;
if (!vx || (vy && InCircle(p[Dest(x)], p[Org(x)], p[Org(y)], p[Dest(y)])))
base = Link(y, Rev(base));
else base = Link(Rev(base), Rev(x));
}
return {a.first, b.second};
}
void Init (const vector <Point> &a) //不修改输入;同一对象可重复使用
{
int n = a.size();
id.resize(n), p.resize(n), e.clear(), pool.clear();
for (int i = 0; i < n; ++i) id[i] = i;
sort(id.begin(), id.end(), [&] (int x, int y) { return a[x] < a[y]; });
for (int i = 0; i < n; ++i) p[i] = a[id[i]];
if (n >= 2) Divide(0, n);
}
vector <pair <int, int>> GetEdge () //每条无向边仅返回一次
{
vector <pair <int, int>> res;
for (int i = 0; i < e.size(); i += 4)
if (Org(i) != -1) res.pb({id[Org(i)], id[Dest(i)]});
return res;
}
vector <array <int, 3>> GetTriangle () //仅枚举有界面,每个逆时针三角形返回一次
{
vector <array <int, 3>> res;
for (int i = 0; i < e.size(); i += 2)
{
if (Org(i) == -1) continue;
int j = LNext(i), k = LNext(j);
if (LNext(k) == i && i < j && i < k && Side(Org(k), i) > 0)
res.pb({id[Org(i)], id[Org(j)], id[Org(k)]});
}
return res;
}
};
int main ()
{

return 0;
}