-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2d.cpp
More file actions
1169 lines (1169 loc) · 42.8 KB
/
2d.cpp
File metadata and controls
1169 lines (1169 loc) · 42.8 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
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
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const double inf = 1e100;
const double eps = 1e-9;
const double PI = acos((double)-1.0);
int sign(double x) { return (x > eps) - (x < -eps); }
struct PT {
double x, y;
PT() { x = 0, y = 0; }
PT(double x, double y) : x(x), y(y) {}
PT(const PT &p) : x(p.x), y(p.y) {}
PT operator + (const PT &a) const { return PT(x + a.x, y + a.y); }
PT operator - (const PT &a) const { return PT(x - a.x, y - a.y); }
PT operator * (const double a) const { return PT(x * a, y * a); }
friend PT operator * (const double &a, const PT &b) { return PT(a * b.x, a * b.y); }
PT operator / (const double a) const { return PT(x / a, y / a); }
bool operator == (PT a) const { return sign(a.x - x) == 0 && sign(a.y - y) == 0; }
bool operator != (PT a) const { return !(*this == a); }
bool operator < (PT a) const { return sign(a.x - x) == 0 ? y < a.y : x < a.x; }
bool operator > (PT a) const { return sign(a.x - x) == 0 ? y > a.y : x > a.x; }
double norm() { return sqrt(x * x + y * y); }
double norm2() { return x * x + y * y; }
PT perp() { return PT(-y, x); }
double arg() { return atan2(y, x); }
PT truncate(double r) { // returns a vector with norm r and having same direction
double k = norm();
if (!sign(k)) return *this;
r /= k;
return PT(x * r, y * r);
}
};
inline double dot(PT a, PT b) { return a.x * b.x + a.y * b.y; }
inline double dist2(PT a, PT b) { return dot(a - b, a - b); }
inline double dist(PT a, PT b) { return sqrt(dot(a - b, a - b)); }
inline double cross(PT a, PT b) { return a.x * b.y - a.y * b.x; }
inline double cross2(PT a, PT b, PT c) { return cross(b - a, c - a); }
inline int orientation(PT a, PT b, PT c) { return sign(cross(b - a, c - a)); }
PT perp(PT a) { return PT(-a.y, a.x); }
PT rotateccw90(PT a) { return PT(-a.y, a.x); }
PT rotatecw90(PT a) { return PT(a.y, -a.x); }
PT rotateccw(PT a, double t) { return PT(a.x * cos(t) - a.y * sin(t), a.x * sin(t) + a.y * cos(t)); }
PT rotatecw(PT a, double t) { return PT(a.x * cos(t) + a.y * sin(t), -a.x * sin(t) + a.y * cos(t)); }
double SQ(double x) { return x * x; }
double rad_to_deg(double r) { return (r * 180.0 / PI); }
double deg_to_rad(double d) { return (d * PI / 180.0); }
double get_angle(PT a, PT b) {
double costheta = dot(a, b) / a.norm() / b.norm();
return acos(max((double)-1.0, min((double)1.0, costheta)));
}
bool is_point_in_angle(PT b, PT a, PT c, PT p) { // does point p lie in angle <bac
assert(orientation(a, b, c) != 0);
if (orientation(a, c, b) < 0) swap(b, c);
return orientation(a, c, p) >= 0 && orientation(a, b, p) <= 0;
}
bool half(PT p) {
return p.y > 0.0 || (p.y == 0.0 && p.x < 0.0);
}
void polar_sort(vector<PT> &v) { // sort points in counterclockwise
sort(v.begin(), v.end(), [](PT a,PT b) {
return make_tuple(half(a), 0.0, a.norm2()) < make_tuple(half(b), cross(a, b), b.norm2());
});
}
void polar_sort(vector<PT> &v, PT o) { // sort points in counterclockwise with respect to point o
sort(v.begin(), v.end(), [&](PT a,PT b) {
return make_tuple(half(a - o), 0.0, (a - o).norm2()) < make_tuple(half(b - o), cross(a - o, b - o), (b - o).norm2());
});
}
struct line {
PT a, b; // goes through points a and b
PT v; double c; //line form: direction vec [cross] (x, y) = c
line() {}
//direction vector v and offset c
line(PT v, double c) : v(v), c(c) {
auto p = get_points();
a = p.first; b = p.second;
}
// equation ax + by + c = 0
line(double _a, double _b, double _c) : v({_b, -_a}), c(-_c) {
auto p = get_points();
a = p.first; b = p.second;
}
// goes through points p and q
line(PT p, PT q) : v(q - p), c(cross(v, p)), a(p), b(q) {}
pair<PT, PT> get_points() { //extract any two points from this line
PT p, q; double a = -v.y, b = v.x; // ax + by = c
if (sign(a) == 0) {
p = PT(0, c / b);
q = PT(1, c / b);
}
else if (sign(b) == 0) {
p = PT(c / a, 0);
q = PT(c / a, 1);
}
else {
p = PT(0, c / b);
q = PT(1, (c - a) / b);
}
return {p, q};
}
//ax + by + c = 0
array<double, 3> get_abc() {
double a = -v.y, b = v.x;
return {a, b, c};
}
// 1 if on the left, -1 if on the right, 0 if on the line
int side(PT p) { return sign(cross(v, p) - c); }
// line that is perpendicular to this and goes through point p
line perpendicular_through(PT p) { return {p, p + perp(v)}; }
// translate the line by vector t i.e. shifting it by vector t
line translate(PT t) { return {v, c + cross(v, t)}; }
// compare two points by their orthogonal projection on this line
// a projection point comes before another if it comes first according to vector v
bool cmp_by_projection(PT p, PT q) { return dot(v, p) < dot(v, q); }
line shift_left(double d) {
PT z = v.perp().truncate(d);
return line(a + z, b + z);
}
};
// find a point from a through b with distance d
PT point_along_line(PT a, PT b, double d) {
assert(a != b);
return a + (((b - a) / (b - a).norm()) * d);
}
// projection point c onto line through a and b assuming a != b
PT project_from_point_to_line(PT a, PT b, PT c) {
return a + (b - a) * dot(c - a, b - a) / (b - a).norm2();
}
// reflection point c onto line through a and b assuming a != b
PT reflection_from_point_to_line(PT a, PT b, PT c) {
PT p = project_from_point_to_line(a,b,c);
return p + p - c;
}
// minimum distance from point c to line through a and b
double dist_from_point_to_line(PT a, PT b, PT c) {
return fabs(cross(b - a, c - a) / (b - a).norm());
}
// returns true if point p is on line segment ab
bool is_point_on_seg(PT a, PT b, PT p) {
if (fabs(cross(p - b, a - b)) < eps) {
if (p.x < min(a.x, b.x) || p.x > max(a.x, b.x)) return false;
if (p.y < min(a.y, b.y) || p.y > max(a.y, b.y)) return false;
return true;
}
return false;
}
// minimum distance point from point c to segment ab that lies on segment ab
PT project_from_point_to_seg(PT a, PT b, PT c) {
double r = dist2(a, b);
if (sign(r) == 0) return a;
r = dot(c - a, b - a) / r;
if (r < 0) return a;
if (r > 1) return b;
return a + (b - a) * r;
}
// minimum distance from point c to segment ab
double dist_from_point_to_seg(PT a, PT b, PT c) {
return dist(c, project_from_point_to_seg(a, b, c));
}
// 0 if not parallel, 1 if parallel, 2 if collinear
int is_parallel(PT a, PT b, PT c, PT d) {
double k = fabs(cross(b - a, d - c));
if (k < eps){
if (fabs(cross(a - b, a - c)) < eps && fabs(cross(c - d, c - a)) < eps) return 2;
else return 1;
}
else return 0;
}
// check if two lines are same
bool are_lines_same(PT a, PT b, PT c, PT d) {
if (fabs(cross(a - c, c - d)) < eps && fabs(cross(b - c, c - d)) < eps) return true;
return false;
}
// bisector vector of <abc
PT angle_bisector(PT &a, PT &b, PT &c){
PT p = a - b, q = c - b;
return p + q * sqrt(dot(p, p) / dot(q, q));
}
// 1 if point is ccw to the line, 2 if point is cw to the line, 3 if point is on the line
int point_line_relation(PT a, PT b, PT p) {
int c = sign(cross(p - a, b - a));
if (c < 0) return 1;
if (c > 0) return 2;
return 3;
}
// intersection point between ab and cd assuming unique intersection exists
bool line_line_intersection(PT a, PT b, PT c, PT d, PT &ans) {
double a1 = a.y - b.y, b1 = b.x - a.x, c1 = cross(a, b);
double a2 = c.y - d.y, b2 = d.x - c.x, c2 = cross(c, d);
double det = a1 * b2 - a2 * b1;
if (det == 0) return 0;
ans = PT((b1 * c2 - b2 * c1) / det, (c1 * a2 - a1 * c2) / det);
return 1;
}
// intersection point between segment ab and segment cd assuming unique intersection exists
bool seg_seg_intersection(PT a, PT b, PT c, PT d, PT &ans) {
double oa = cross2(c, d, a), ob = cross2(c, d, b);
double oc = cross2(a, b, c), od = cross2(a, b, d);
if (oa * ob < 0 && oc * od < 0){
ans = (a * ob - b * oa) / (ob - oa);
return 1;
}
else return 0;
}
// intersection point between segment ab and segment cd assuming unique intersection may not exists
// se.size()==0 means no intersection
// se.size()==1 means one intersection
// se.size()==2 means range intersection
set<PT> seg_seg_intersection_inside(PT a, PT b, PT c, PT d) {
PT ans;
if (seg_seg_intersection(a, b, c, d, ans)) return {ans};
set<PT> se;
if (is_point_on_seg(c, d, a)) se.insert(a);
if (is_point_on_seg(c, d, b)) se.insert(b);
if (is_point_on_seg(a, b, c)) se.insert(c);
if (is_point_on_seg(a, b, d)) se.insert(d);
return se;
}
// intersection between segment ab and line cd
// 0 if do not intersect, 1 if proper intersect, 2 if segment intersect
int seg_line_relation(PT a, PT b, PT c, PT d) {
double p = cross2(c, d, a);
double q = cross2(c, d, b);
if (sign(p) == 0 && sign(q) == 0) return 2;
else if (p * q < 0) return 1;
else return 0;
}
// intersection between segament ab and line cd assuming unique intersection exists
bool seg_line_intersection(PT a, PT b, PT c, PT d, PT &ans) {
bool k = seg_line_relation(a, b, c, d);
assert(k != 2);
if (k) line_line_intersection(a, b, c, d, ans);
return k;
}
// minimum distance from segment ab to segment cd
double dist_from_seg_to_seg(PT a, PT b, PT c, PT d) {
PT dummy;
if (seg_seg_intersection(a, b, c, d, dummy)) return 0.0;
else return min({dist_from_point_to_seg(a, b, c), dist_from_point_to_seg(a, b, d),
dist_from_point_to_seg(c, d, a), dist_from_point_to_seg(c, d, b)});
}
// minimum distance from point c to ray (starting point a and direction vector b)
double dist_from_point_to_ray(PT a, PT b, PT c) {
b = a + b;
double r = dot(c - a, b - a);
if (r < 0.0) return dist(c, a);
return dist_from_point_to_line(a, b, c);
}
// starting point as and direction vector ad
bool ray_ray_intersection(PT as, PT ad, PT bs, PT bd) {
double dx = bs.x - as.x, dy = bs.y - as.y;
double det = bd.x * ad.y - bd.y * ad.x;
if (fabs(det) < eps) return 0;
double u = (dy * bd.x - dx * bd.y) / det;
double v = (dy * ad.x - dx * ad.y) / det;
if (sign(u) >= 0 && sign(v) >= 0) return 1;
else return 0;
}
double ray_ray_distance(PT as, PT ad, PT bs, PT bd) {
if (ray_ray_intersection(as, ad, bs, bd)) return 0.0;
double ans = dist_from_point_to_ray(as, ad, bs);
ans = min(ans, dist_from_point_to_ray(bs, bd, as));
return ans;
}
struct circle {
PT p; double r;
circle() {}
circle(PT _p, double _r): p(_p), r(_r) {};
// center (x, y) and radius r
circle(double x, double y, double _r): p(PT(x, y)), r(_r) {};
// circumcircle of a triangle
// the three points must be unique
circle(PT a, PT b, PT c) {
b = (a + b) * 0.5;
c = (a + c) * 0.5;
line_line_intersection(b, b + rotatecw90(a - b), c, c + rotatecw90(a - c), p);
r = dist(a, p);
}
// inscribed circle of a triangle
circle(PT a, PT b, PT c, bool t) {
line u, v;
double m = atan2(b.y - a.y, b.x - a.x), n = atan2(c.y - a.y, c.x - a.x);
u.a = a;
u.b = u.a + (PT(cos((n + m)/2.0), sin((n + m)/2.0)));
v.a = b;
m = atan2(a.y - b.y, a.x - b.x), n = atan2(c.y - b.y, c.x - b.x);
v.b = v.a + (PT(cos((n + m)/2.0), sin((n + m)/2.0)));
line_line_intersection(u.a, u.b, v.a, v.b, p);
r = dist_from_point_to_seg(a, b, p);
}
bool operator == (circle v) { return p == v.p && sign(r - v.r) == 0; }
double area() { return PI * r * r; }
double circumference() { return 2.0 * PI * r; }
};
//0 if outside, 1 if on circumference, 2 if inside circle
int circle_point_relation(PT p, double r, PT b) {
double d = dist(p, b);
if (sign(d - r) < 0) return 2;
if (sign(d - r) == 0) return 1;
return 0;
}
// 0 if outside, 1 if on circumference, 2 if inside circle
int circle_line_relation(PT p, double r, PT a, PT b) {
double d = dist_from_point_to_line(a, b, p);
if (sign(d - r) < 0) return 2;
if (sign(d - r) == 0) return 1;
return 0;
}
//compute intersection of line through points a and b with
//circle centered at c with radius r > 0
vector<PT> circle_line_intersection(PT c, double r, PT a, PT b) {
vector<PT> ret;
b = b - a; a = a - c;
double A = dot(b, b), B = dot(a, b);
double C = dot(a, a) - r * r, D = B * B - A * C;
if (D < -eps) return ret;
ret.push_back(c + a + b * (-B + sqrt(D + eps)) / A);
if (D > eps) ret.push_back(c + a + b * (-B - sqrt(D)) / A);
return ret;
}
//5 - outside and do not intersect
//4 - intersect outside in one point
//3 - intersect in 2 points
//2 - intersect inside in one point
//1 - inside and do not intersect
int circle_circle_relation(PT a, double r, PT b, double R) {
double d = dist(a, b);
if (sign(d - r - R) > 0) return 5;
if (sign(d - r - R) == 0) return 4;
double l = fabs(r - R);
if (sign(d - r - R) < 0 && sign(d - l) > 0) return 3;
if (sign(d - l) == 0) return 2;
if (sign(d - l) < 0) return 1;
assert(0); return -1;
}
vector<PT> circle_circle_intersection(PT a, double r, PT b, double R) {
if (a == b && sign(r - R) == 0) return {PT(1e18, 1e18)};
vector<PT> ret;
double d = sqrt(dist2(a, b));
if (d > r + R || d + min(r, R) < max(r, R)) return ret;
double x = (d * d - R * R + r * r) / (2 * d);
double y = sqrt(r * r - x * x);
PT v = (b - a) / d;
ret.push_back(a + v * x + rotateccw90(v) * y);
if (y > 0) ret.push_back(a + v * x - rotateccw90(v) * y);
return ret;
}
// returns two circle c1, c2 through points a, b and of radius r
// 0 if there is no such circle, 1 if one circle, 2 if two circle
int get_circle(PT a, PT b, double r, circle &c1, circle &c2) {
vector<PT> v = circle_circle_intersection(a, r, b, r);
int t = v.size();
if (!t) return 0;
c1.p = v[0], c1.r = r;
if (t == 2) c2.p = v[1], c2.r = r;
return t;
}
// returns two circle c1, c2 which is tangent to line u, goes through
// point q and has radius r1; 0 for no circle, 1 if c1 = c2 , 2 if c1 != c2
int get_circle(line u, PT q, double r1, circle &c1, circle &c2) {
double d = dist_from_point_to_line(u.a, u.b, q);
if (sign(d - r1 * 2.0) > 0) return 0;
if (sign(d) == 0) {
cout << u.v.x << ' ' << u.v.y << '\n';
c1.p = q + rotateccw90(u.v).truncate(r1);
c2.p = q + rotatecw90(u.v).truncate(r1);
c1.r = c2.r = r1;
return 2;
}
line u1 = line(u.a + rotateccw90(u.v).truncate(r1), u.b + rotateccw90(u.v).truncate(r1));
line u2 = line(u.a + rotatecw90(u.v).truncate(r1), u.b + rotatecw90(u.v).truncate(r1));
circle cc = circle(q, r1);
PT p1, p2; vector<PT> v;
v = circle_line_intersection(q, r1, u1.a, u1.b);
if (!v.size()) v = circle_line_intersection(q, r1, u2.a, u2.b);
v.push_back(v[0]);
p1 = v[0], p2 = v[1];
c1 = circle(p1, r1);
if (p1 == p2) {
c2 = c1;
return 1;
}
c2 = circle(p2, r1);
return 2;
}
// returns area of intersection between two circles
double circle_circle_area(PT a, double r1, PT b, double r2) {
double d = (a - b).norm();
if(r1 + r2 < d + eps) return 0;
if(r1 + d < r2 + eps) return PI * r1 * r1;
if(r2 + d < r1 + eps) return PI * r2 * r2;
double theta_1 = acos((r1 * r1 + d * d - r2 * r2) / (2 * r1 * d)),
theta_2 = acos((r2 * r2 + d * d - r1 * r1)/(2 * r2 * d));
return r1 * r1 * (theta_1 - sin(2 * theta_1)/2.) + r2 * r2 * (theta_2 - sin(2 * theta_2)/2.);
}
// tangent lines from point q to the circle
int tangent_lines_from_point(PT p, double r, PT q, line &u, line &v) {
int x = sign(dist2(p, q) - r * r);
if (x < 0) return 0; // point in cricle
if (x == 0) { // point on circle
u = line(q, q + rotateccw90(q - p));
v = u;
return 1;
}
double d = dist(p, q);
double l = r * r / d;
double h = sqrt(r * r - l * l);
u = line(q, p + ((q - p).truncate(l) + (rotateccw90(q - p).truncate(h))));
v = line(q, p + ((q - p).truncate(l) + (rotatecw90(q - p).truncate(h))));
return 2;
}
// returns outer tangents line of two circles
// if inner == 1 it returns inner tangent lines
int tangents_lines_from_circle(PT c1, double r1, PT c2, double r2, bool inner, line &u, line &v) {
if (inner) r2 = -r2;
PT d = c2 - c1;
double dr = r1 - r2, d2 = d.norm2(), h2 = d2 - dr * dr;
if (d2 == 0 || h2 < 0) {
assert(h2 != 0);
return 0;
}
vector<pair<PT, PT>>out;
for (int tmp: {- 1, 1}) {
PT v = (d * dr + rotateccw90(d) * sqrt(h2) * tmp) / d2;
out.push_back({c1 + v * r1, c2 + v * r2});
}
u = line(out[0].first, out[0].second);
if (out.size() == 2) v = line(out[1].first, out[1].second);
return 1 + (h2 > 0);
}
//O(n^2 log n)
struct CircleUnion {
int n;
double x[2020], y[2020], r[2020];
int covered[2020];
vector<pair<double, double> > seg, cover;
double arc, pol;
inline int sign(double x) {return x < -eps ? -1 : x > eps;}
inline int sign(double x, double y) {return sign(x - y);}
inline double SQ(const double x) {return x * x;}
inline double dist(double x1, double y1, double x2, double y2) {return sqrt(SQ(x1 - x2) + SQ(y1 - y2));}
inline double angle(double A, double B, double C) {
double val = (SQ(A) + SQ(B) - SQ(C)) / (2 * A * B);
if (val < -1) val = -1;
if (val > +1) val = +1;
return acos(val);
}
CircleUnion() {
n = 0;
seg.clear(), cover.clear();
arc = pol = 0;
}
void init() {
n = 0;
seg.clear(), cover.clear();
arc = pol = 0;
}
void add(double xx, double yy, double rr) {
x[n] = xx, y[n] = yy, r[n] = rr, covered[n] = 0, n++;
}
void getarea(int i, double lef, double rig) {
arc += 0.5 * r[i] * r[i] * (rig - lef - sin(rig - lef));
double x1 = x[i] + r[i] * cos(lef), y1 = y[i] + r[i] * sin(lef);
double x2 = x[i] + r[i] * cos(rig), y2 = y[i] + r[i] * sin(rig);
pol += x1 * y2 - x2 * y1;
}
double solve() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (!sign(x[i] - x[j]) && !sign(y[i] - y[j]) && !sign(r[i] - r[j])) {
r[i] = 0.0;
break;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j && sign(r[j] - r[i]) >= 0 && sign(dist(x[i], y[i], x[j], y[j]) - (r[j] - r[i])) <= 0) {
covered[i] = 1;
break;
}
}
}
for (int i = 0; i < n; i++) {
if (sign(r[i]) && !covered[i]) {
seg.clear();
for (int j = 0; j < n; j++) {
if (i != j) {
double d = dist(x[i], y[i], x[j], y[j]);
if (sign(d - (r[j] + r[i])) >= 0 || sign(d - abs(r[j] - r[i])) <= 0) {
continue;
}
double alpha = atan2(y[j] - y[i], x[j] - x[i]);
double beta = angle(r[i], d, r[j]);
pair<double, double> tmp(alpha - beta, alpha + beta);
if (sign(tmp.first) <= 0 && sign(tmp.second) <= 0) {
seg.push_back(pair<double, double>(2 * PI + tmp.first, 2 * PI + tmp.second));
}
else if (sign(tmp.first) < 0) {
seg.push_back(pair<double, double>(2 * PI + tmp.first, 2 * PI));
seg.push_back(pair<double, double>(0, tmp.second));
}
else {
seg.push_back(tmp);
}
}
}
sort(seg.begin(), seg.end());
double rig = 0;
for (vector<pair<double, double> >::iterator iter = seg.begin(); iter != seg.end(); iter++) {
if (sign(rig - iter->first) >= 0) {
rig = max(rig, iter->second);
}
else {
getarea(i, rig, iter->first);
rig = iter->second;
}
}
if (!sign(rig)) {
arc += r[i] * r[i] * PI;
}
else {
getarea(i, rig, 2 * PI);
}
}
}
return pol / 2.0 + arc;
}
} CU;
double area_of_triangle(PT a, PT b, PT c) {
return fabs(cross(b - a, c - a) * 0.5);
}
// -1 if strictly inside, 0 if on the polygon, 1 if strictly outside
int is_point_in_triangle(PT a, PT b, PT c, PT p) {
if (sign(cross(b - a,c - a)) < 0) swap(b, c);
int c1 = sign(cross(b - a,p - a));
int c2 = sign(cross(c - b,p - b));
int c3 = sign(cross(a - c,p - c));
if (c1<0 || c2<0 || c3 < 0) return 1;
if (c1 + c2 + c3 != 3) return 0;
return -1;
}
double perimeter(vector<PT> &p) {
double ans=0; int n = p.size();
for (int i = 0; i < n; i++) ans += dist(p[i], p[(i + 1) % n]);
return ans;
}
double area(vector<PT> &p) {
double ans = 0; int n = p.size();
for (int i = 0; i < n; i++) ans += cross(p[i], p[(i + 1) % n]);
return fabs(ans) * 0.5;
}
// centroid of a (possibly non-convex) polygon,
// assuming that the coordinates are listed in a clockwise or
// counterclockwise fashion. Note that the centroid is often known as
// the "center of gravity" or "center of mass".
PT centroid(vector<PT> &p) {
int n = p.size(); PT c(0, 0);
double sum = 0;
for (int i = 0; i < n; i++) sum += cross(p[i], p[(i + 1) % n]);
double scale = 3.0 * sum;
for (int i = 0; i < n; i++) {
int j = (i + 1) % n;
c = c + (p[i] + p[j]) * cross(p[i], p[j]);
}
return c / scale;
}
// 0 if cw, 1 if ccw
bool get_direction(vector<PT> &p) {
double ans = 0; int n = p.size();
for (int i = 0; i < n; i++) ans += cross(p[i], p[(i + 1) % n]);
if (sign(ans) > 0) return 1;
return 0;
}
// it returns a point such that the sum of distances
// from that point to all points in p is minimum
// O(n log^2 MX)
PT geometric_median(vector<PT> p) {
auto tot_dist = [&](PT z) {
double res = 0;
for (int i = 0; i < p.size(); i++) res += dist(p[i], z);
return res;
};
auto findY = [&](double x) {
double yl = -1e5, yr = 1e5;
for (int i = 0; i < 60; i++) {
double ym1 = yl + (yr - yl) / 3;
double ym2 = yr - (yr - yl) / 3;
double d1 = tot_dist(PT(x, ym1));
double d2 = tot_dist(PT(x, ym2));
if (d1 < d2) yr = ym2;
else yl = ym1;
}
return pair<double, double> (yl, tot_dist(PT(x, yl)));
};
double xl = -1e5, xr = 1e5;
for (int i = 0; i < 60; i++) {
double xm1 = xl + (xr - xl) / 3;
double xm2 = xr - (xr - xl) / 3;
double y1, d1, y2, d2;
auto z = findY(xm1); y1 = z.first; d1 = z.second;
z = findY(xm2); y2 = z.first; d2 = z.second;
if (d1 < d2) xr = xm2;
else xl = xm1;
}
return {xl, findY(xl).first };
}
vector<PT> convex_hull(vector<PT> &p) {
if (p.size() <= 1) return p;
vector<PT> v = p;
sort(v.begin(), v.end());
vector<PT> up, dn;
for (auto& p : v) {
while (up.size() > 1 && orientation(up[up.size() - 2], up.back(), p) >= 0) {
up.pop_back();
}
while (dn.size() > 1 && orientation(dn[dn.size() - 2], dn.back(), p) <= 0) {
dn.pop_back();
}
up.push_back(p);
dn.push_back(p);
}
v = dn;
if (v.size() > 1) v.pop_back();
reverse(up.begin(), up.end());
up.pop_back();
for (auto& p : up) {
v.push_back(p);
}
if (v.size() == 2 && v[0] == v[1]) v.pop_back();
return v;
}
//checks if convex or not
bool is_convex(vector<PT> &p) {
bool s[3]; s[0] = s[1] = s[2] = 0;
int n = p.size();
for (int i = 0; i < n; i++) {
int j = (i + 1) % n;
int k = (j + 1) % n;
s[sign(cross(p[j] - p[i], p[k] - p[i])) + 1] = 1;
if (s[0] && s[2]) return 0;
}
return 1;
}
// -1 if strictly inside, 0 if on the polygon, 1 if strictly outside
// it must be strictly convex, otherwise make it strictly convex first
int is_point_in_convex(vector<PT> &p, const PT& x) { // O(log n)
int n = p.size(); assert(n >= 3);
int a = orientation(p[0], p[1], x), b = orientation(p[0], p[n - 1], x);
if (a < 0 || b > 0) return 1;
int l = 1, r = n - 1;
while (l + 1 < r) {
int mid = l + r >> 1;
if (orientation(p[0], p[mid], x) >= 0) l = mid;
else r = mid;
}
int k = orientation(p[l], p[r], x);
if (k <= 0) return -k;
if (l == 1 && a == 0) return 0;
if (r == n - 1 && b == 0) return 0;
return -1;
}
bool is_point_on_polygon(vector<PT> &p, const PT& z) {
int n = p.size();
for (int i = 0; i < n; i++) {
if (is_point_on_seg(p[i], p[(i + 1) % n], z)) return 1;
}
return 0;
}
// returns 1e9 if the point is on the polygon
int winding_number(vector<PT> &p, const PT& z) { // O(n)
if (is_point_on_polygon(p, z)) return 1e9;
int n = p.size(), ans = 0;
for (int i = 0; i < n; ++i) {
int j = (i + 1) % n;
bool below = p[i].y < z.y;
if (below != (p[j].y < z.y)) {
auto orient = orientation(z, p[j], p[i]);
if (orient == 0) return 0;
if (below == (orient > 0)) ans += below ? 1 : -1;
}
}
return ans;
}
// -1 if strictly inside, 0 if on the polygon, 1 if strictly outside
int is_point_in_polygon(vector<PT> &p, const PT& z) { // O(n)
int k = winding_number(p, z);
return k == 1e9 ? 0 : k == 0 ? 1 : -1;
}
// id of the vertex having maximum dot product with z
// polygon must need to be convex
// top - upper right vertex
// for minimum dot prouct negate z and return -dot(z, p[id])
int extreme_vertex(vector<PT> &p, const PT &z, const int top) { // O(log n)
int n = p.size();
if (n == 1) return 0;
double ans = dot(p[0], z); int id = 0;
if (dot(p[top], z) > ans) ans = dot(p[top], z), id = top;
int l = 1, r = top - 1;
while (l < r) {
int mid = l + r >> 1;
if (dot(p[mid + 1], z) >= dot(p[mid], z)) l = mid + 1;
else r = mid;
}
if (dot(p[l], z) > ans) ans = dot(p[l], z), id = l;
l = top + 1, r = n - 1;
while (l < r) {
int mid = l + r >> 1;
if (dot(p[(mid + 1) % n], z) >= dot(p[mid], z)) l = mid + 1;
else r = mid;
}
l %= n;
if (dot(p[l], z) > ans) ans = dot(p[l], z), id = l;
return id;
}
double diameter(vector<PT> &p) {
int n = (int)p.size();
if (n == 1) return 0;
if (n == 2) return dist(p[0], p[1]);
double ans = 0;
int i = 0, j = 1;
while (i < n) {
while (cross(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j]) >= 0) {
ans = max(ans, dist2(p[i], p[j]));
j = (j + 1) % n;
}
ans = max(ans, dist2(p[i], p[j]));
i++;
}
return sqrt(ans);
}
double width(vector<PT> &p) {
int n = (int)p.size();
if (n <= 2) return 0;
double ans = inf;
int i = 0, j = 1;
while (i < n) {
while (cross(p[(i + 1) % n] - p[i], p[(j + 1) % n] - p[j]) >= 0) j = (j + 1) % n;
ans = min(ans, dist_from_point_to_line(p[i], p[(i + 1) % n], p[j]));
i++;
}
return ans;
}
// minimum perimeter
double minimum_enclosing_rectangle(vector<PT> &p) {
int n = p.size();
if (n <= 2) return perimeter(p);
int mndot = 0; double tmp = dot(p[1] - p[0], p[0]);
for (int i = 1; i < n; i++) {
if (dot(p[1] - p[0], p[i]) <= tmp) {
tmp = dot(p[1] - p[0], p[i]);
mndot = i;
}
}
double ans = inf;
int i = 0, j = 1, mxdot = 1;
while (i < n) {
PT cur = p[(i + 1) % n] - p[i];
while (cross(cur, p[(j + 1) % n] - p[j]) >= 0) j = (j + 1) % n;
while (dot(p[(mxdot + 1) % n], cur) >= dot(p[mxdot], cur)) mxdot = (mxdot + 1) % n;
while (dot(p[(mndot + 1) % n], cur) <= dot(p[mndot], cur)) mndot = (mndot + 1) % n;
ans = min(ans, 2.0 * ((dot(p[mxdot], cur) / cur.norm() - dot(p[mndot], cur) / cur.norm()) + dist_from_point_to_line(p[i], p[(i + 1) % n], p[j])));
i++;
}
return ans;
}
// given n points, find the minimum enclosing circle of the points
// call convex_hull() before this for faster solution
// expected O(n)
circle minimum_enclosing_circle(vector<PT> &p) {
random_shuffle(p.begin(), p.end());
int n = p.size();
circle c(p[0], 0);
for (int i = 1; i < n; i++) {
if (sign(dist(c.p, p[i]) - c.r) > 0) {
c = circle(p[i], 0);
for (int j = 0; j < i; j++) {
if (sign(dist(c.p, p[j]) - c.r) > 0) {
c = circle((p[i] + p[j]) / 2, dist(p[i], p[j]) / 2);
for (int k = 0; k < j; k++) {
if (sign(dist(c.p, p[k]) - c.r) > 0) {
c = circle(p[i], p[j], p[k]);
}
}
}
}
}
}
return c;
}
// returns a vector with the vertices of a polygon with everything
// to the left of the line going from a to b cut away.
vector<PT> cut(vector<PT> &p, PT a, PT b) {
vector<PT> ans;
int n = (int)p.size();
for (int i = 0; i < n; i++) {
double c1 = cross(b - a, p[i] - a);
double c2 = cross(b - a, p[(i + 1) % n] - a);
if (sign(c1) >= 0) ans.push_back(p[i]);
if (sign(c1 * c2) < 0) {
if (!is_parallel(p[i], p[(i + 1) % n], a, b)) {
PT tmp; line_line_intersection(p[i], p[(i + 1) % n], a, b, tmp);
ans.push_back(tmp);
}
}
}
return ans;
}
// not necessarily convex, boundary is included in the intersection
// returns total intersected length
double polygon_line_intersection(vector<PT> p, PT a, PT b) {
int n = p.size();
p.push_back(p[0]);
line l = line(a, b);
double ans = 0.0;
vector< pair<double, int> > vec;
for (int i = 0; i < n; i++) {
int s1 = sign(cross(b - a, p[i] - a));
int s2 = sign(cross(b - a, p[i+1] - a));
if (s1 == s2) continue;
line t = line(p[i], p[i + 1]);
PT inter = (t.v * l.c - l.v * t.c) / cross(l.v, t.v);
double tmp = dot(inter, l.v);
int f;
if (s1 > s2) f = s1 && s2 ? 2 : 1;
else f = s1 && s2 ? -2 : -1;
vec.push_back(make_pair(tmp, f));
}
sort(vec.begin(), vec.end());
for (int i = 0, j = 0; i + 1 < (int)vec.size(); i++){
j += vec[i].second;
if (j) ans += vec[i + 1].first - vec[i].first;
}
ans = ans / sqrt(dot(l.v, l.v));
p.pop_back();
return ans;
}
pair<PT, PT> convex_line_intersection(vector<PT> &p, PT a, PT b) {
return {{0, 0}, {0, 0}};
}
// minimum distance from a point to a convex polygon
// it assumes point does not lie strictly inside the polygon
double dist_from_point_to_polygon(vector<PT> &v, PT p) { // O(log n)
int n = (int)v.size();
if (n <= 3) {
double ans = inf;
for(int i = 0; i < n; i++) ans = min(ans, dist_from_point_to_seg(v[i], v[(i + 1) % n], p));
return ans;
}
PT bscur, bs = angle_bisector(v[n - 1], v[0], v[1]);
int ok, i, pw = 1, ans = 0, sgncur, sgn = sign(cross(bs, p - v[0]));
while (pw <= n) pw <<= 1;
while ((pw >>= 1)) {
if ((i = ans + pw) < n) {
bscur = angle_bisector(v[i - 1], v[i], v[(i + 1) % n]);
sgncur = sign(cross(bscur, p - v[i]));
ok = sign(cross(bs, bscur)) >= 0 ? (sgn >= 0 || sgncur <= 0) : (sgn >= 0 && sgncur <= 0);
if (ok) ans = i, bs = bscur, sgn = sgncur;
}
}
return dist_from_point_to_seg(v[ans], v[(ans + 1) % n], p);
}
// minimum distance from convex polygon p to line ab
// returns 0 is it intersects with the polygon
// top - upper right vertex
double dist_from_polygon_to_line(vector<PT> &p, PT a, PT b, int top) { //O(log n)
PT orth = (b - a).perp();
if (orientation(a, b, p[0]) > 0) orth = (a - b).perp();
int id = extreme_vertex(p, orth, top);
if (dot(p[id] - a, orth) > 0) return 0.0; //if orth and a are in the same half of the line, then poly and line intersects
return dist_from_point_to_line(a, b, p[id]); //does not intersect
}
// minimum distance from a convex polygon to another convex polygon
double dist_from_polygon_to_polygon(vector<PT> &p1, vector<PT> &p2) { // O(n log n)
double ans = inf;
for (int i = 0; i < p1.size(); i++) {
ans = min(ans, dist_from_point_to_polygon(p2, p1[i]));
}
for (int i = 0; i < p2.size(); i++) {
ans = min(ans, dist_from_point_to_polygon(p1, p2[i]));
}
return ans;
}
// maximum distance from a convex polygon to another convex polygon
double maximum_dist_from_polygon_to_polygon(vector<PT> &u, vector<PT> &v){ //O(n)
int n = (int)u.size(), m = (int)v.size();
double ans = 0;
if (n < 3 || m < 3) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) ans = max(ans, dist2(u[i], v[j]));
}
return sqrt(ans);
}
if (u[0].x > v[0].x) swap(n, m), swap(u, v);
int i = 0, j = 0, step = n + m + 10;
while (j + 1 < m && v[j].x < v[j + 1].x) j++ ;
while (step--) {
if (cross(u[(i + 1)%n] - u[i], v[(j + 1)%m] - v[j]) >= 0) j = (j + 1) % m;
else i = (i + 1) % n;
ans = max(ans, dist2(u[i], v[j]));
}
return sqrt(ans);
}
pair<PT, int> point_poly_tangent(vector<PT> &p, PT Q, int dir, int l, int r) {
while (r - l > 1) {
int mid = (l + r) >> 1;
bool pvs = orientation(Q, p[mid], p[mid - 1]) != -dir;
bool nxt = orientation(Q, p[mid], p[mid + 1]) != -dir;
if (pvs && nxt) return {p[mid], mid};
if (!(pvs || nxt)) {
auto p1 = point_poly_tangent(p, Q, dir, mid + 1, r);
auto p2 = point_poly_tangent(p, Q, dir, l, mid - 1);
return orientation(Q, p1.first, p2.first) == dir ? p1 : p2;
}
if (!pvs) {
if (orientation(Q, p[mid], p[l]) == dir) r = mid - 1;
else if (orientation(Q, p[l], p[r]) == dir) r = mid - 1;
else l = mid + 1;
}
if (!nxt) {
if (orientation(Q, p[mid], p[l]) == dir) l = mid + 1;
else if (orientation(Q, p[l], p[r]) == dir) r = mid - 1;
else l = mid + 1;
}
}
pair<PT, int> ret = {p[l], l};
for (int i = l + 1; i <= r; i++) ret = orientation(Q, ret.first, p[i]) != dir ? make_pair(p[i], i) : ret;
return ret;
}
// (cw, ccw) tangents from a point that is outside this convex polygon
// returns indexes of the points
pair<int, int> tangents_from_point_to_polygon(vector<PT> &p, PT Q){
int cw = point_poly_tangent(p, Q, 1, 0, (int)p.size() - 1).second;
int ccw = point_poly_tangent(p, Q, -1, 0, (int)p.size() - 1).second;
return make_pair(cw, ccw);
}
// calculates the area of the union of n polygons (not necessarily convex).
// the points within each polygon must be given in CCW order.
// complexity: O(N^2), where N is the total number of points
double rat(PT a, PT b, PT p) {
return !sign(a.x - b.x) ? (p.y - a.y) / (b.y - a.y) : (p.x - a.x) / (b.x - a.x);
};
double polygon_union(vector<vector<PT>> &p) {
int n = p.size();
double ans=0;
for(int i = 0; i < n; ++i) {
for (int v = 0; v < (int)p[i].size(); ++v) {
PT a = p[i][v], b = p[i][(v + 1) % p[i].size()];
vector<pair<double, int>> segs;
segs.emplace_back(0, 0), segs.emplace_back(1, 0);
for(int j = 0; j < n; ++j) {
if(i != j) {
for(size_t u = 0; u < p[j].size(); ++u) {
PT c = p[j][u], d = p[j][(u + 1) % p[j].size()];
int sc = sign(cross(b - a, c - a)), sd = sign(cross(b - a, d - a));
if(!sc && !sd) {
if(sign(dot(b - a, d - c)) > 0 && i > j) {
segs.emplace_back(rat(a, b, c), 1), segs.emplace_back(rat(a, b, d), -1);
}
}
else {
double sa = cross(d - c, a - c), sb = cross(d - c, b - c);
if(sc >= 0 && sd < 0) segs.emplace_back(sa / (sa - sb), 1);
else if(sc < 0 && sd >= 0) segs.emplace_back(sa / (sa - sb), -1);
}
}
}
}
sort(segs.begin(), segs.end());
double pre = min(max(segs[0].first, 0.0), 1.0), now, sum = 0;
int cnt = segs[0].second;
for(int j = 1; j < segs.size(); ++j) {
now = min(max(segs[j].first, 0.0), 1.0);
if (!cnt) sum += now - pre;
cnt += segs[j].second;
pre = now;
}
ans += cross(a, b) * sum;
}
}
return ans * 0.5;
}
// contains all points p such that: cross(b - a, p - a) >= 0
struct HP {
PT a, b;
HP() {}
HP(PT a, PT b) : a(a), b(b) {}
HP(const HP& rhs) : a(rhs.a), b(rhs.b) {}
int operator < (const HP& rhs) const {
PT p = b - a;
PT q = rhs.b - rhs.a;
int fp = (p.y < 0 || (p.y == 0 && p.x < 0));
int fq = (q.y < 0 || (q.y == 0 && q.x < 0));
if (fp != fq) return fp == 0;
if (cross(p, q)) return cross(p, q) > 0;
return cross(p, rhs.b - a) < 0;
}
PT line_line_intersection(PT a, PT b, PT c, PT d) {
b = b - a; d = c - d; c = c - a;
return a + b * cross(c, d) / cross(b, d);
}
PT intersection(const HP &v) {
return line_line_intersection(a, b, v.a, v.b);