-
Notifications
You must be signed in to change notification settings - Fork 1
/
geometry.cpp
983 lines (879 loc) · 27.8 KB
/
geometry.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
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
#include <bits/stdc++.h>
using namespace std;
using ld = long double;
const ld eps = 1e-9, inf = numeric_limits<ld>::max(), pi = acos(-1);
// For use with integers, just set eps=0 and everything remains the same
bool geq(ld a, ld b){return a-b >= -eps;} //a >= b
bool leq(ld a, ld b){return b-a >= -eps;} //a <= b
bool ge(ld a, ld b){return a-b > eps;} //a > b
bool le(ld a, ld b){return b-a > eps;} //a < b
bool eq(ld a, ld b){return abs(a-b) <= eps;} //a == b
bool neq(ld a, ld b){return abs(a-b) > eps;} //a != b
struct point{
ld x, y;
point(): x(0), y(0){}
point(ld x, ld y): x(x), y(y){}
point operator+(const point & p) const{return point(x + p.x, y + p.y);}
point operator-(const point & p) const{return point(x - p.x, y - p.y);}
point operator*(const ld & k) const{return point(x * k, y * k);}
point operator/(const ld & k) const{return point(x / k, y / k);}
point operator+=(const point & p){*this = *this + p; return *this;}
point operator-=(const point & p){*this = *this - p; return *this;}
point operator*=(const ld & p){*this = *this * p; return *this;}
point operator/=(const ld & p){*this = *this / p; return *this;}
point rotate(const ld & a) const{return point(x*cos(a) - y*sin(a), x*sin(a) + y*cos(a));}
point perp() const{return point(-y, x);}
ld ang() const{
ld a = atan2l(y, x); a += le(a, 0) ? 2*pi : 0; return a;
}
ld dot(const point & p) const{return x * p.x + y * p.y;}
ld cross(const point & p) const{return x * p.y - y * p.x;}
ld norm() const{return x * x + y * y;}
ld length() const{return sqrtl(x * x + y * y);}
point unit() const{return (*this) / length();}
bool operator==(const point & p) const{return eq(x, p.x) && eq(y, p.y);}
bool operator!=(const point & p) const{return !(*this == p);}
bool operator<(const point & p) const{return le(x, p.x) || (eq(x, p.x) && le(y, p.y));}
bool operator>(const point & p) const{return ge(x, p.x) || (eq(x, p.x) && ge(y, p.y));}
bool half(const point & p) const{return le(p.cross(*this), 0) || (eq(p.cross(*this), 0) && le(p.dot(*this), 0));}
};
istream &operator>>(istream &is, point & p){return is >> p.x >> p.y;}
ostream &operator<<(ostream &os, const point & p){return os << "(" << p.x << ", " << p.y << ")";}
int sgn(ld x){
if(ge(x, 0)) return 1;
if(le(x, 0)) return -1;
return 0;
}
void polarSort(vector<point> & P, const point & o, const point & v){
//sort points in P around o, taking the direction of v as first angle
sort(P.begin(), P.end(), [&](const point & a, const point & b){
return point((a - o).half(v), 0) < point((b - o).half(v), (a - o).cross(b - o));
});
}
bool pointInLine(const point & a, const point & v, const point & p){
//line a+tv, point p
return eq((p - a).cross(v), 0);
}
bool pointInSegment(const point & a, const point & b, const point & p){
//segment ab, point p
return pointInLine(a, b - a, p) && leq((a - p).dot(b - p), 0);
}
int intersectLinesInfo(const point & a1, const point & v1, const point & a2, const point & v2){
//lines a1+tv1 and a2+tv2
ld det = v1.cross(v2);
if(eq(det, 0)){
if(eq((a2 - a1).cross(v1), 0)){
return -1; //infinity points
}else{
return 0; //no points
}
}else{
return 1; //single point
}
}
point intersectLines(const point & a1, const point & v1, const point & a2, const point & v2){
//lines a1+tv1, a2+tv2
//assuming that they intersect
ld det = v1.cross(v2);
return a1 + v1 * ((a2 - a1).cross(v2) / det);
}
int intersectLineSegmentInfo(const point & a, const point & v, const point & c, const point & d){
//line a+tv, segment cd
point v2 = d - c;
ld det = v.cross(v2);
if(eq(det, 0)){
if(eq((c - a).cross(v), 0)){
return -1; //infinity points
}else{
return 0; //no point
}
}else{
return sgn(v.cross(c - a)) != sgn(v.cross(d - a)); //1: single point, 0: no point
}
}
int intersectSegmentsInfo(const point & a, const point & b, const point & c, const point & d){
//segment ab, segment cd
point v1 = b - a, v2 = d - c;
int t = sgn(v1.cross(c - a)), u = sgn(v1.cross(d - a));
if(t == u){
if(t == 0){
if(pointInSegment(a, b, c) || pointInSegment(a, b, d) || pointInSegment(c, d, a) || pointInSegment(c, d, b)){
return -1; //infinity points
}else{
return 0; //no point
}
}else{
return 0; //no point
}
}else{
return sgn(v2.cross(a - c)) != sgn(v2.cross(b - c)); //1: single point, 0: no point
}
}
ld distancePointLine(const point & a, const point & v, const point & p){
//line: a + tv, point p
return abs(v.cross(p - a)) / v.length();
}
ld perimeter(vector<point> & P){
int n = P.size();
ld ans = 0;
for(int i = 0; i < n; i++){
ans += (P[i] - P[(i + 1) % n]).length();
}
return ans;
}
ld area(vector<point> & P){
int n = P.size();
ld ans = 0;
for(int i = 0; i < n; i++){
ans += P[i].cross(P[(i + 1) % n]);
}
return abs(ans / 2);
}
vector<point> convexHull(vector<point> P){
sort(P.begin(), P.end());
vector<point> L, U;
for(int i = 0; i < P.size(); i++){
while(L.size() >= 2 && leq((L[L.size() - 2] - P[i]).cross(L[L.size() - 1] - P[i]), 0)){
L.pop_back();
}
L.push_back(P[i]);
}
for(int i = P.size() - 1; i >= 0; i--){
while(U.size() >= 2 && leq((U[U.size() - 2] - P[i]).cross(U[U.size() - 1] - P[i]), 0)){
U.pop_back();
}
U.push_back(P[i]);
}
L.pop_back();
U.pop_back();
L.insert(L.end(), U.begin(), U.end());
return L;
}
bool pointInPerimeter(const vector<point> & P, const point & p){
int n = P.size();
for(int i = 0; i < n; i++){
if(pointInSegment(P[i], P[(i + 1) % n], p)){
return true;
}
}
return false;
}
bool crossesRay(const point & a, const point & b, const point & p){
return (geq(b.y, p.y) - geq(a.y, p.y)) * sgn((a - p).cross(b - p)) > 0;
}
int pointInPolygon(const vector<point> & P, const point & p){
if(pointInPerimeter(P, p)){
return -1; //point in the perimeter
}
int n = P.size();
int rays = 0;
for(int i = 0; i < n; i++){
rays += crossesRay(P[i], P[(i + 1) % n], p);
}
return rays & 1; //0: point outside, 1: point inside
}
//point in convex polygon in O(log n)
//make sure that P is convex and in ccw
//before the queries, do the preprocess on P:
// rotate(P.begin(), min_element(P.begin(), P.end()), P.end());
// int right = max_element(P.begin(), P.end()) - P.begin();
//returns 0 if p is outside, 1 if p is inside, -1 if p is in the perimeter
int pointInConvexPolygon(const vector<point> & P, const point & p, int right){
if(p < P[0] || P[right] < p) return 0;
int orientation = sgn((P[right] - P[0]).cross(p - P[0]));
if(orientation == 0){
if(p == P[0] || p == P[right]) return -1;
return (right == 1 || right + 1 == P.size()) ? -1 : 1;
}else if(orientation < 0){
auto r = lower_bound(P.begin() + 1, P.begin() + right, p);
int det = sgn((p - r[-1]).cross(r[0] - r[-1])) - 1;
if(det == -2) det = 1;
return det;
}else{
auto l = upper_bound(P.rbegin(), P.rend() - right - 1, p);
int det = sgn((p - l[0]).cross((l == P.rbegin() ? P[0] : l[-1]) - l[0])) - 1;
if(det == -2) det = 1;
return det;
}
}
vector<point> cutPolygon(const vector<point> & P, const point & a, const point & v){
//returns the part of the convex polygon P on the left side of line a+tv
int n = P.size();
vector<point> lhs;
for(int i = 0; i < n; ++i){
if(geq(v.cross(P[i] - a), 0)){
lhs.push_back(P[i]);
}
if(intersectLineSegmentInfo(a, v, P[i], P[(i+1)%n]) == 1){
point p = intersectLines(a, v, P[i], P[(i+1)%n] - P[i]);
if(p != P[i] && p != P[(i+1)%n]){
lhs.push_back(p);
}
}
}
return lhs;
}
point centroid(vector<point> & P){
point num;
ld den = 0;
int n = P.size();
for(int i = 0; i < n; ++i){
ld cross = P[i].cross(P[(i + 1) % n]);
num += (P[i] + P[(i + 1) % n]) * cross;
den += cross;
}
return num / (3 * den);
}
vector<pair<int, int>> antipodalPairs(vector<point> & P){
vector<pair<int, int>> ans;
int n = P.size(), k = 1;
auto f = [&](int u, int v, int w){return abs((P[v%n]-P[u%n]).cross(P[w%n]-P[u%n]));};
while(ge(f(n-1, 0, k+1), f(n-1, 0, k))) ++k;
for(int i = 0, j = k; i <= k && j < n; ++i){
ans.emplace_back(i, j);
while(j < n-1 && ge(f(i, i+1, j+1), f(i, i+1, j)))
ans.emplace_back(i, ++j);
}
return ans;
}
pair<ld, ld> diameterAndWidth(vector<point> & P){
int n = P.size(), k = 0;
auto dot = [&](int a, int b){return (P[(a+1)%n]-P[a]).dot(P[(b+1)%n]-P[b]);};
auto cross = [&](int a, int b){return (P[(a+1)%n]-P[a]).cross(P[(b+1)%n]-P[b]);};
ld diameter = 0;
ld width = inf;
while(ge(dot(0, k), 0)) k = (k+1) % n;
for(int i = 0; i < n; ++i){
while(ge(cross(i, k), 0)) k = (k+1) % n;
//pair: (i, k)
diameter = max(diameter, (P[k] - P[i]).length());
width = min(width, distancePointLine(P[i], P[(i+1)%n] - P[i], P[k]));
}
return {diameter, width};
}
pair<ld, ld> smallestEnclosingRectangle(vector<point> & P){
int n = P.size();
auto dot = [&](int a, int b){return (P[(a+1)%n]-P[a]).dot(P[(b+1)%n]-P[b]);};
auto cross = [&](int a, int b){return (P[(a+1)%n]-P[a]).cross(P[(b+1)%n]-P[b]);};
ld perimeter = inf, area = inf;
for(int i = 0, j = 0, k = 0, m = 0; i < n; ++i){
while(ge(dot(i, j), 0)) j = (j+1) % n;
if(!i) k = j;
while(ge(cross(i, k), 0)) k = (k+1) % n;
if(!i) m = k;
while(le(dot(i, m), 0)) m = (m+1) % n;
//pairs: (i, k) , (j, m)
point v = P[(i+1)%n] - P[i];
ld h = distancePointLine(P[i], v, P[k]);
ld w = distancePointLine(P[j], v.perp(), P[m]);
perimeter = min(perimeter, 2 * (h + w));
area = min(area, h * w);
}
return {area, perimeter};
}
ld distancePointCircle(const point & c, ld r, const point & p){
//point p, circle with center c and radius r
return max((ld)0, (p - c).length() - r);
}
point projectionPointCircle(const point & c, ld r, const point & p){
//point p (outside the circle), circle with center c and radius r
return c + (p - c).unit() * r;
}
pair<point, point> pointsOfTangency(const point & c, ld r, const point & p){
//point p (outside the circle), circle with center c and radius r
point v = (p - c).unit() * r;
ld d2 = (p - c).norm(), d = sqrt(d2);
point v1 = v * (r / d), v2 = v.perp() * (sqrt(d2 - r*r) / d);
return {c + v1 - v2, c + v1 + v2};
}
vector<point> intersectLineCircle(const point & a, const point & v, const point & c, ld r){
//line a+tv, circle with center c and radius r
ld h2 = r*r - v.cross(c - a) * v.cross(c - a) / v.norm();
point p = a + v * v.dot(c - a) / v.norm();
if(eq(h2, 0)) return {p}; //line tangent to circle
else if(le(h2, 0)) return {}; //no intersection
else{
point u = v.unit() * sqrt(h2);
return {p - u, p + u}; //two points of intersection (chord)
}
}
vector<point> intersectSegmentCircle(const point & a, const point & b, const point & c, ld r){
//segment ab, circle with center c and radius r
vector<point> P = intersectLineCircle(a, b - a, c, r), ans;
for(const point & p : P){
if(pointInSegment(a, b, p)) ans.push_back(p);
}
return ans;
}
pair<point, ld> getCircle(const point & m, const point & n, const point & p){
//find circle that passes through points p, q, r
point c = intersectLines((n + m) / 2, (n - m).perp(), (p + n) / 2, (p - n).perp());
ld r = (c - m).length();
return {c, r};
}
vector<point> intersectionCircles(const point & c1, ld r1, const point & c2, ld r2){
//circle 1 with center c1 and radius r1
//circle 2 with center c2 and radius r2
point d = c2 - c1;
ld d2 = d.norm();
if(eq(d2, 0)) return {}; //concentric circles
ld pd = (d2 + r1*r1 - r2*r2) / 2;
ld h2 = r1*r1 - pd*pd/d2;
point p = c1 + d*pd/d2;
if(eq(h2, 0)) return {p}; //circles touch at one point
else if(le(h2, 0)) return {}; //circles don't intersect
else{
point u = d.perp() * sqrt(h2/d2);
return {p - u, p + u};
}
}
int circleInsideCircle(const point & c1, ld r1, const point & c2, ld r2){
//test if circle 2 is inside circle 1
//returns "-1" if 2 touches internally 1, "1" if 2 is inside 1, "0" if they overlap
ld l = r1 - r2 - (c1 - c2).length();
return (ge(l, 0) ? 1 : (eq(l, 0) ? -1 : 0));
}
int circleOutsideCircle(const point & c1, ld r1, const point & c2, ld r2){
//test if circle 2 is outside circle 1
//returns "-1" if they touch externally, "1" if 2 is outside 1, "0" if they overlap
ld l = (c1 - c2).length() - (r1 + r2);
return (ge(l, 0) ? 1 : (eq(l, 0) ? -1 : 0));
}
int pointInCircle(const point & c, ld r, const point & p){
//test if point p is inside the circle with center c and radius r
//returns "0" if it's outside, "-1" if it's in the perimeter, "1" if it's inside
ld l = (p - c).length() - r;
return (le(l, 0) ? 1 : (eq(l, 0) ? -1 : 0));
}
vector<vector<point>> tangents(const point & c1, ld r1, const point & c2, ld r2, bool inner){
//returns a vector of segments or a single point
if(inner) r2 = -r2;
point d = c2 - c1;
ld dr = r1 - r2, d2 = d.norm(), h2 = d2 - dr*dr;
if(eq(d2, 0) || le(h2, 0)) return {};
point v = d*dr/d2;
if(eq(h2, 0)) return {{c1 + v*r1}};
else{
point u = d.perp()*sqrt(h2)/d2;
return {{c1 + (v - u)*r1, c2 + (v - u)*r2}, {c1 + (v + u)*r1, c2 + (v + u)*r2}};
}
}
ld signed_angle(const point & a, const point & b){
return sgn(a.cross(b)) * acosl(a.dot(b) / (a.length() * b.length()));
}
ld intersectPolygonCircle(const vector<point> & P, const point & c, ld r){
//Gets the area of the intersection of the polygon with the circle
int n = P.size();
ld ans = 0;
for(int i = 0; i < n; ++i){
point p = P[i], q = P[(i+1)%n];
bool p_inside = (pointInCircle(c, r, p) != 0);
bool q_inside = (pointInCircle(c, r, q) != 0);
if(p_inside && q_inside){
ans += (p - c).cross(q - c);
}else if(p_inside && !q_inside){
point s1 = intersectSegmentCircle(p, q, c, r)[0];
point s2 = intersectSegmentCircle(c, q, c, r)[0];
ans += (p - c).cross(s1 - c) + r*r * signed_angle(s1 - c, s2 - c);
}else if(!p_inside && q_inside){
point s1 = intersectSegmentCircle(c, p, c, r)[0];
point s2 = intersectSegmentCircle(p, q, c, r)[0];
ans += (s2 - c).cross(q - c) + r*r * signed_angle(s1 - c, s2 - c);
}else{
auto info = intersectSegmentCircle(p, q, c, r);
if(info.size() <= 1){
ans += r*r * signed_angle(p - c, q - c);
}else{
point s2 = info[0], s3 = info[1];
point s1 = intersectSegmentCircle(c, p, c, r)[0];
point s4 = intersectSegmentCircle(c, q, c, r)[0];
ans += (s2 - c).cross(s3 - c) + r*r * (signed_angle(s1 - c, s2 - c) + signed_angle(s3 - c, s4 - c));
}
}
}
return abs(ans)/2;
}
pair<point, ld> mec2(vector<point> & S, const point & a, const point & b, int n){
ld hi = inf, lo = -hi;
for(int i = 0; i < n; ++i){
ld si = (b - a).cross(S[i] - a);
if(eq(si, 0)) continue;
point m = getCircle(a, b, S[i]).first;
ld cr = (b - a).cross(m - a);
if(le(si, 0)) hi = min(hi, cr);
else lo = max(lo, cr);
}
ld v = (ge(lo, 0) ? lo : le(hi, 0) ? hi : 0);
point c = (a + b) / 2 + (b - a).perp() * v / (b - a).norm();
return {c, (a - c).norm()};
}
pair<point, ld> mec(vector<point> & S, const point & a, int n){
random_shuffle(S.begin(), S.begin() + n);
point b = S[0], c = (a + b) / 2;
ld r = (a - c).norm();
for(int i = 1; i < n; ++i){
if(ge((S[i] - c).norm(), r)){
tie(c, r) = (n == S.size() ? mec(S, S[i], i) : mec2(S, a, S[i], i));
}
}
return {c, r};
}
pair<point, ld> smallestEnclosingCircle(vector<point> S){
assert(!S.empty());
auto r = mec(S, S[0], S.size());
return {r.first, sqrt(r.second)};
}
bool comp1(const point & a, const point & b){
return le(a.y, b.y);
}
pair<point, point> closestPairOfPoints(vector<point> P){
sort(P.begin(), P.end(), comp1);
set<point> S;
ld ans = inf;
point p, q;
int pos = 0;
for(int i = 0; i < P.size(); ++i){
while(pos < i && geq(P[i].y - P[pos].y, ans)){
S.erase(P[pos++]);
}
auto lower = S.lower_bound({P[i].x - ans - eps, -inf});
auto upper = S.upper_bound({P[i].x + ans + eps, -inf});
for(auto it = lower; it != upper; ++it){
ld d = (P[i] - *it).length();
if(le(d, ans)){
ans = d;
p = P[i];
q = *it;
}
}
S.insert(P[i]);
}
return {p, q};
}
struct vantage_point_tree{
struct node
{
point p;
ld th;
node *l, *r;
}*root;
vector<pair<ld, point>> aux;
vantage_point_tree(vector<point> &ps){
for(int i = 0; i < ps.size(); ++i)
aux.push_back({ 0, ps[i] });
root = build(0, ps.size());
}
node *build(int l, int r){
if(l == r)
return 0;
swap(aux[l], aux[l + rand() % (r - l)]);
point p = aux[l++].second;
if(l == r)
return new node({ p });
for(int i = l; i < r; ++i)
aux[i].first = (p - aux[i].second).dot(p - aux[i].second);
int m = (l + r) / 2;
nth_element(aux.begin() + l, aux.begin() + m, aux.begin() + r);
return new node({ p, sqrt(aux[m].first), build(l, m), build(m, r) });
}
priority_queue<pair<ld, node*>> que;
void k_nn(node *t, point p, int k){
if(!t)
return;
ld d = (p - t->p).length();
if(que.size() < k)
que.push({ d, t });
else if(ge(que.top().first, d)){
que.pop();
que.push({ d, t });
}
if(!t->l && !t->r)
return;
if(le(d, t->th)){
k_nn(t->l, p, k);
if(leq(t->th - d, que.top().first))
k_nn(t->r, p, k);
}else{
k_nn(t->r, p, k);
if(leq(d - t->th, que.top().first))
k_nn(t->l, p, k);
}
}
vector<point> k_nn(point p, int k){
k_nn(root, p, k);
vector<point> ans;
for(; !que.empty(); que.pop())
ans.push_back(que.top().second->p);
reverse(ans.begin(), ans.end());
return ans;
}
};
vector<point> minkowskiSum(vector<point> A, vector<point> B){
int na = (int)A.size(), nb = (int)B.size();
if(A.empty() || B.empty()) return {};
rotate(A.begin(), min_element(A.begin(), A.end()), A.end());
rotate(B.begin(), min_element(B.begin(), B.end()), B.end());
int pa = 0, pb = 0;
vector<point> M;
while(pa < na && pb < nb){
M.push_back(A[pa] + B[pb]);
ld x = (A[(pa + 1) % na] - A[pa]).cross(B[(pb + 1) % nb] - B[pb]);
if(leq(x, 0)) pb++;
if(geq(x, 0)) pa++;
}
while(pa < na) M.push_back(A[pa++] + B[0]);
while(pb < nb) M.push_back(B[pb++] + A[0]);
return M;
}
//Delaunay triangulation in O(n log n)
const point inf_pt(inf, inf);
struct QuadEdge{
point origin;
QuadEdge* rot = nullptr;
QuadEdge* onext = nullptr;
bool used = false;
QuadEdge* rev() const{return rot->rot;}
QuadEdge* lnext() const{return rot->rev()->onext->rot;}
QuadEdge* oprev() const{return rot->onext->rot;}
point dest() const{return rev()->origin;}
};
QuadEdge* make_edge(const point & from, const point & to){
QuadEdge* e1 = new QuadEdge;
QuadEdge* e2 = new QuadEdge;
QuadEdge* e3 = new QuadEdge;
QuadEdge* e4 = new QuadEdge;
e1->origin = from;
e2->origin = to;
e3->origin = e4->origin = inf_pt;
e1->rot = e3;
e2->rot = e4;
e3->rot = e2;
e4->rot = e1;
e1->onext = e1;
e2->onext = e2;
e3->onext = e4;
e4->onext = e3;
return e1;
}
void splice(QuadEdge* a, QuadEdge* b){
swap(a->onext->rot->onext, b->onext->rot->onext);
swap(a->onext, b->onext);
}
void delete_edge(QuadEdge* e){
splice(e, e->oprev());
splice(e->rev(), e->rev()->oprev());
delete e->rot;
delete e->rev()->rot;
delete e;
delete e->rev();
}
QuadEdge* connect(QuadEdge* a, QuadEdge* b){
QuadEdge* e = make_edge(a->dest(), b->origin);
splice(e, a->lnext());
splice(e->rev(), b);
return e;
}
bool left_of(const point & p, QuadEdge* e){
return ge((e->origin - p).cross(e->dest() - p), 0);
}
bool right_of(const point & p, QuadEdge* e){
return le((e->origin - p).cross(e->dest() - p), 0);
}
ld det3(ld a1, ld a2, ld a3, ld b1, ld b2, ld b3, ld c1, ld c2, ld c3) {
return a1 * (b2 * c3 - c2 * b3) - a2 * (b1 * c3 - c1 * b3) + a3 * (b1 * c2 - c1 * b2);
}
bool in_circle(const point & a, const point & b, const point & c, const point & d) {
ld det = -det3(b.x, b.y, b.norm(), c.x, c.y, c.norm(), d.x, d.y, d.norm());
det += det3(a.x, a.y, a.norm(), c.x, c.y, c.norm(), d.x, d.y, d.norm());
det -= det3(a.x, a.y, a.norm(), b.x, b.y, b.norm(), d.x, d.y, d.norm());
det += det3(a.x, a.y, a.norm(), b.x, b.y, b.norm(), c.x, c.y, c.norm());
return ge(det, 0);
}
pair<QuadEdge*, QuadEdge*> build_tr(int l, int r, vector<point> & P){
if(r - l + 1 == 2){
QuadEdge* res = make_edge(P[l], P[r]);
return {res, res->rev()};
}
if(r - l + 1 == 3){
QuadEdge *a = make_edge(P[l], P[l + 1]), *b = make_edge(P[l + 1], P[r]);
splice(a->rev(), b);
int sg = sgn((P[l + 1] - P[l]).cross(P[r] - P[l]));
if(sg == 0)
return {a, b->rev()};
QuadEdge* c = connect(b, a);
if(sg == 1)
return {a, b->rev()};
else
return {c->rev(), c};
}
int mid = (l + r) / 2;
QuadEdge *ldo, *ldi, *rdo, *rdi;
tie(ldo, ldi) = build_tr(l, mid, P);
tie(rdi, rdo) = build_tr(mid + 1, r, P);
while(true){
if(left_of(rdi->origin, ldi)){
ldi = ldi->lnext();
continue;
}
if(right_of(ldi->origin, rdi)){
rdi = rdi->rev()->onext;
continue;
}
break;
}
QuadEdge* basel = connect(rdi->rev(), ldi);
auto valid = [&basel](QuadEdge* e){return right_of(e->dest(), basel);};
if(ldi->origin == ldo->origin)
ldo = basel->rev();
if(rdi->origin == rdo->origin)
rdo = basel;
while(true){
QuadEdge* lcand = basel->rev()->onext;
if(valid(lcand)){
while(in_circle(basel->dest(), basel->origin, lcand->dest(), lcand->onext->dest())){
QuadEdge* t = lcand->onext;
delete_edge(lcand);
lcand = t;
}
}
QuadEdge* rcand = basel->oprev();
if(valid(rcand)){
while(in_circle(basel->dest(), basel->origin, rcand->dest(), rcand->oprev()->dest())){
QuadEdge* t = rcand->oprev();
delete_edge(rcand);
rcand = t;
}
}
if(!valid(lcand) && !valid(rcand))
break;
if(!valid(lcand) || (valid(rcand) && in_circle(lcand->dest(), lcand->origin, rcand->origin, rcand->dest())))
basel = connect(rcand, basel->rev());
else
basel = connect(basel->rev(), lcand->rev());
}
return {ldo, rdo};
}
vector<tuple<point, point, point>> delaunay(vector<point> & P){
sort(P.begin(), P.end());
auto res = build_tr(0, (int)P.size() - 1, P);
QuadEdge* e = res.first;
vector<QuadEdge*> edges = {e};
while(le((e->dest() - e->onext->dest()).cross(e->origin - e->onext->dest()), 0))
e = e->onext;
auto add = [&P, &e, &edges](){
QuadEdge* curr = e;
do{
curr->used = true;
P.push_back(curr->origin);
edges.push_back(curr->rev());
curr = curr->lnext();
}while(curr != e);
};
add();
P.clear();
int kek = 0;
while(kek < (int)edges.size())
if(!(e = edges[kek++])->used)
add();
vector<tuple<point, point, point>> ans;
for(int i = 0; i < (int)P.size(); i += 3){
ans.emplace_back(P[i], P[i + 1], P[i + 2]);
}
return ans;
}
struct circ{
point c;
ld r;
circ() {}
circ(const point & c, ld r): c(c), r(r) {}
set<pair<ld, ld>> ranges;
void disable(ld l, ld r){
ranges.emplace(l, r);
}
auto getActive() const{
vector<pair<ld, ld>> ans;
ld maxi = 0;
for(const auto & dis : ranges){
ld l, r;
tie(l, r) = dis;
if(l > maxi){
ans.emplace_back(maxi, l);
}
maxi = max(maxi, r);
}
if(!eq(maxi, 2*pi)){
ans.emplace_back(maxi, 2*pi);
}
return ans;
}
};
ld areaUnionCircles(const vector<circ> & circs){
vector<circ> valid;
for(const circ & curr : circs){
if(eq(curr.r, 0)) continue;
circ nuevo = curr;
for(circ & prev : valid){
if(circleInsideCircle(prev.c, prev.r, nuevo.c, nuevo.r)){
nuevo.disable(0, 2*pi);
}else if(circleInsideCircle(nuevo.c, nuevo.r, prev.c, prev.r)){
prev.disable(0, 2*pi);
}else{
auto cruce = intersectionCircles(prev.c, prev.r, nuevo.c, nuevo.r);
if(cruce.size() == 2){
ld a1 = (cruce[0] - prev.c).ang();
ld a2 = (cruce[1] - prev.c).ang();
ld b1 = (cruce[1] - nuevo.c).ang();
ld b2 = (cruce[0] - nuevo.c).ang();
if(a1 < a2){
prev.disable(a1, a2);
}else{
prev.disable(a1, 2*pi);
prev.disable(0, a2);
}
if(b1 < b2){
nuevo.disable(b1, b2);
}else{
nuevo.disable(b1, 2*pi);
nuevo.disable(0, b2);
}
}
}
}
valid.push_back(nuevo);
}
ld ans = 0;
for(const circ & curr : valid){
for(const auto & range : curr.getActive()){
ld l, r;
tie(l, r) = range;
ans += curr.r*(curr.c.x * (sin(r) - sin(l)) - curr.c.y * (cos(r) - cos(l))) + curr.r*curr.r*(r-l);
}
}
return ans/2;
};
struct plane{
point a, v;
plane(): a(), v(){}
plane(const point& a, const point& v): a(a), v(v){}
point intersect(const plane& p) const{
ld t = (p.a - a).cross(p.v) / v.cross(p.v);
return a + v*t;
}
bool outside(const point& p) const{ // test if point p is strictly outside
return le(v.cross(p - a), 0);
}
bool inside(const point& p) const{ // test if point p is inside or in the boundary
return geq(v.cross(p - a), 0);
}
bool operator<(const plane& p) const{ // sort by angle
auto lhs = make_tuple(v.half({1, 0}), ld(0), v.cross(p.a - a));
auto rhs = make_tuple(p.v.half({1, 0}), v.cross(p.v), ld(0));
return lhs < rhs;
}
bool operator==(const plane& p) const{ // paralell and same directions, not really equal
return eq(v.cross(p.v), 0) && ge(v.dot(p.v), 0);
}
};
vector<point> halfPlaneIntersection(vector<plane> planes){
planes.push_back({{0, -inf}, {1, 0}});
planes.push_back({{inf, 0}, {0, 1}});
planes.push_back({{0, inf}, {-1, 0}});
planes.push_back({{-inf, 0}, {0, -1}});
sort(planes.begin(), planes.end());
planes.erase(unique(planes.begin(), planes.end()), planes.end());
deque<plane> ch;
deque<point> poly;
for(const plane& p : planes){
while(ch.size() >= 2 && p.outside(poly.back())) ch.pop_back(), poly.pop_back();
while(ch.size() >= 2 && p.outside(poly.front())) ch.pop_front(), poly.pop_front();
if(p.v.half({1, 0}) && poly.empty()) return {};
ch.push_back(p);
if(ch.size() >= 2) poly.push_back(ch[ch.size()-2].intersect(ch[ch.size()-1]));
}
while(ch.size() >= 3 && ch.front().outside(poly.back())) ch.pop_back(), poly.pop_back();
while(ch.size() >= 3 && ch.back().outside(poly.front())) ch.pop_front(), poly.pop_front();
poly.push_back(ch.back().intersect(ch.front()));
return vector<point>(poly.begin(), poly.end());
}
vector<point> halfPlaneIntersectionRandomized(vector<plane> planes){
point p = planes[0].a;
int n = planes.size();
random_shuffle(planes.begin(), planes.end());
for(int i = 0; i < n; ++i){
if(planes[i].inside(p)) continue;
ld lo = -inf, hi = inf;
for(int j = 0; j < i; ++j){
ld A = planes[j].v.cross(planes[i].v);
ld B = planes[j].v.cross(planes[j].a - planes[i].a);
if(ge(A, 0)){
lo = max(lo, B/A);
}else if(le(A, 0)){
hi = min(hi, B/A);
}else{
if(ge(B, 0)) return {};
}
if(ge(lo, hi)) return {};
}
p = planes[i].a + planes[i].v*lo;
}
return {p};
}
int main(){
/*vector<pair<point, point>> centers = {{point(-2, 5), point(-8, -7)}, {point(14, 4), point(18, 6)}, {point(9, 20), point(9, 28)},
{point(21, 20), point(21, 29)}, {point(8, -10), point(14, -10)}, {point(24, -6), point(34, -6)},
{point(34, 8), point(36, 9)}, {point(50, 20), point(56, 24.5)}};
vector<pair<ld, ld>> radii = {{7, 4}, {3, 5}, {4, 4}, {4, 5}, {3, 3}, {4, 6}, {5, 1}, {10, 2.5}};
int n = centers.size();
for(int i = 0; i < n; ++i){
cout << "\n" << centers[i].first << " " << radii[i].first << " " << centers[i].second << " " << radii[i].second << "\n";
auto extLines = tangents(centers[i].first, radii[i].first, centers[i].second, radii[i].second, false);
cout << "Exterior tangents:\n";
for(auto par : extLines){
for(auto p : par){
cout << p << " ";
}
cout << "\n";
}
auto intLines = tangents(centers[i].first, radii[i].first, centers[i].second, radii[i].second, true);
cout << "Interior tangents:\n";
for(auto par : intLines){
for(auto p : par){
cout << p << " ";
}
cout << "\n";
}
}*/
/*int n;
cin >> n;
vector<point> P(n);
for(auto & p : P) cin >> p;
auto triangulation = delaunay(P);
for(auto triangle : triangulation){
cout << get<0>(triangle) << " " << get<1>(triangle) << " " << get<2>(triangle) << "\n";
}*/
/*int n;
cin >> n;
vector<point> P(n);
for(auto & p : P) cin >> p;
auto ans = smallestEnclosingCircle(P);
cout << ans.first << " " << ans.second << "\n";*/
/*vector<point> P;
srand(time(0));
for(int i = 0; i < 1000; ++i){
P.emplace_back(rand() % 1000000000, rand() % 1000000000);
}
point o(rand() % 1000000000, rand() % 1000000000), v(rand() % 1000000000, rand() % 1000000000);
polarSort(P, o, v);
auto ang = [&](point p){
ld th = atan2(p.y, p.x);
if(th < 0) th += acosl(-1)*2;
ld t = atan2(v.y, v.x);
if(t < 0) t += acosl(-1)*2;
if(th < t) th += acosl(-1)*2;
return th;
};
for(int i = 0; i < P.size()-1; ++i){
assert(leq(ang(P[i] - o), ang(P[i+1] - o)));
}*/
return 0;
}