-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvexHull.cpp
1827 lines (1651 loc) · 55.5 KB
/
ConvexHull.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
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <cmath>
#include <iomanip>
#include <optional>
#include <deque>
#include <vector>
#include <memory>
#include <cstddef>
#include <set>
#include <algorithm>
#include <list>
template <typename T> int sgn(T val) {
return (T(0) < val) - (val < T(0));
}
template <typename NumberType>
NumberType Round(NumberType x) {
const long double kAccuracy = 0.0'0'1;
if (std::abs(x) < kAccuracy) {
return 0;
}
return x;
}
template <typename NumberType>
struct Point;
template <typename NumberType>
struct Vector;
template <typename NumberType>
class Line;
template <typename NumberType>
class Ray;
template <typename NumberType>
class Segment;
template <typename NumberType,
bool IsClockwise,
template <typename, typename> typename ContainerType = std::deque,
typename Alloc = std::allocator<Point<NumberType>>>
class Polygon;
template <typename NumberType>
struct Point {
Point& operator+=(const Point& other) {
x += other.x;
y += other.y;
return *this;
}
Point& operator-=(const Point& other) {
x -= other.x;
y -= other.y;
return *this;
}
Point& operator*=(NumberType scalar) {
x *= scalar;
y *= scalar;
return *this;
}
Point& operator/=(NumberType scalar) {
x /= scalar;
y /= scalar;
return *this;
}
std::optional<Point> Intersection(const Point<NumberType>& point) const;
std::optional<Point> Intersection(const Line<NumberType>& line) const;
std::optional<Point> Intersection(const Ray<NumberType>& ray) const;
std::optional<Point> Intersection(const Segment<NumberType>& segment) const;
template <bool IsClockwise,
template <typename, typename> typename ContainerType,
typename Alloc>
std::optional<Point> Intersection(const Polygon<NumberType,
IsClockwise,
ContainerType,
Alloc>& polygon) const;
template <typename OtherNumberType>
friend std::ostream& operator<<(std::ostream& os,
const Point<OtherNumberType>& point);
template <typename OtherNumberType>
friend std::istream& operator>>(std::istream& is,
Point<OtherNumberType>& point);
NumberType x = 0;
NumberType y = 0;
};
template <typename NumberType>
struct Vector {
Vector(NumberType x, NumberType y) : x(x), y(y) {}
Vector(const Point<NumberType>& begin, const Point<NumberType>& end) : x(
end.x - begin.x),
y(end.y
- begin.y) {}
Vector() = default;
Vector(const Vector& vector) = default;
Vector(Vector&& vector) noexcept = default;
Vector& operator=(const Vector& vector) = default;
Vector& operator=(Vector&& vector) noexcept = default;
Vector& operator+=(const Vector& other) {
x += other.x;
y += other.y;
return *this;
}
Vector& operator-=(const Vector& other) {
x -= other.x;
y -= other.y;
return *this;
}
Vector& operator*=(NumberType X) {
x *= X;
y *= X;
return *this;
}
Vector& operator/=(NumberType X) {
x /= X;
y /= X;
return *this;
}
template <typename OtherNumberType>
friend std::ostream& operator<<(std::ostream& os,
const Vector<OtherNumberType>& vector);
template <typename OtherNumberType>
friend std::istream& operator>>(std::istream& is,
Vector<OtherNumberType>& vector);
NumberType x = 0;
NumberType y = 0;
};
template <typename NumberType>
std::ostream& operator<<(std::ostream& os, const Point<NumberType>& point) {
os << point.x << ' ' << point.y;
return os;
}
template <typename NumberType>
std::istream& operator>>(std::istream& is, Point<NumberType>& point) {
NumberType x = 0;
NumberType y = 0;
is >> x >> y;
point = Point<NumberType>({x, y});
return is;
}
template <typename NumberType>
Point<NumberType> operator+(const Point<NumberType>& first,
const Point<NumberType>& second) {
auto copy = first;
copy += second;
return copy;
}
template <typename NumberType>
Point<NumberType> operator-(const Point<NumberType>& first,
const Point<NumberType>& second) {
auto copy = first;
copy -= second;
return copy;
}
template <typename NumberType>
Point<NumberType> operator*(const Point<NumberType>& point, long double x) {
auto copy = point;
copy *= x;
return copy;
}
template <typename NumberType>
Point<NumberType> operator/(const Point<NumberType>& point, long double x) {
auto copy = point;
copy /= x;
return copy;
}
template <typename NumberType>
bool operator==(const Point<NumberType>& first,
const Point<NumberType>& second) {
return first.x == second.x && first.y == second.y;
}
template <typename NumberType>
bool operator!=(const Point<NumberType>& first,
const Point<NumberType>& second) {
return !(first == second);
}
template <typename NumberType>
std::ostream& operator<<(std::ostream& os, const Vector<NumberType>& vector) {
os << vector.x << ' ' << vector.y;
return os;
}
template <typename NumberType>
std::istream& operator>>(std::istream& is, Vector<NumberType>& vector) {
NumberType x = 0;
NumberType y = 0;
is >> x >> y;
vector = Vector<NumberType>({x, y});
return is;
}
template <typename NumberType>
Vector<NumberType> operator+(const Vector<NumberType>& first,
const Vector<NumberType>& second) {
auto copy = first;
copy += second;
return copy;
}
template <typename NumberType>
Vector<NumberType> operator-(const Vector<NumberType>& first,
const Vector<NumberType>& second) {
auto copy = first;
copy -= second;
return copy;
}
template <typename NumberType>
Vector<NumberType> operator*(const Vector<NumberType>& vector, NumberType x) {
auto copy = vector;
copy *= x;
return copy;
}
template <typename NumberType>
Vector<NumberType> operator/(const Vector<NumberType>& vector, NumberType x) {
auto copy = vector;
copy /= x;
return copy;
}
bool operator==(const Vector<long double>& first,
const Vector<long double>& second) {
return Round(first.x - second.x) == 0 && Round(first.y - second.y) == 0;
}
bool operator==(const Vector<int>& first, const Vector<int>& second) {
return first.x == second.x && first.y == second.y;
}
template <typename NumberType>
Point<NumberType> operator+(const Point<NumberType>& point,
const Vector<NumberType>& vector) {
auto copy = point;
copy.x += vector.x;
copy.y += vector.y;
return copy;
}
template <typename NumberType>
Point<NumberType> operator-(const Point<NumberType>& point,
const Vector<NumberType>& vector) {
auto copy = point;
copy.x -= vector.x;
copy.y -= vector.y;
return copy;
}
template <typename NumberType>
NumberType ABS(const Vector<NumberType>& vector) {
auto x = vector.x;
x *= x;
auto y = vector.y;
y *= y;
auto abs = std::sqrt(x + y);
return abs;
}
template <typename NumberType>
NumberType ScalarMul(const Vector<NumberType>& first,
const Vector<NumberType>& second) {
auto scalar = first.x * second.x + first.y * second.y;
return scalar;
}
template <typename NumberType>
NumberType VectorMul(const Vector<NumberType>& first,
const Vector<NumberType>& second) {
auto scalar = first.x * second.y - first.y * second.x;
return scalar;
}
template <typename NumberType>
NumberType VectorMul(const Point<NumberType>& first,
const Point<NumberType>& second) {
auto scalar = first.x * second.y - first.y * second.x;
return scalar;
}
template <typename NumberType>
class Line {
public:
Line(NumberType a, NumberType b, NumberType c)
: a_(a), b_(b), c_(c), normal_(a_, b_), guiding_(-b_, a_) {}
Line(const Point<NumberType>& first, const Point<NumberType>& second)
: a_(first.y - second.y),
b_(second.x - first.x),
c_(VectorMul(first, second)),
normal_(a_, b_),
guiding_(-b_, a_) {}
Line(const Point<NumberType>& point, const Vector<NumberType>& vector) : Line(
point,
point + vector) {}
Line() = default;
Line(const Line& line) = default;
Line(Line&& line) noexcept = default;
Line& operator=(const Line& line) = default;
Line& operator=(Line&& line) noexcept = default;
std::optional<Point<NumberType>> Intersection(const Point<NumberType>& point) const;
std::optional<Point<NumberType>> Intersection(const Line<NumberType>& line) const;
std::optional<Point<NumberType>> Intersection(const Ray<NumberType>& ray) const;
std::optional<Point<NumberType>> Intersection(const Segment<NumberType>& segment) const;
template <bool IsClockwise,
template <typename, typename> typename ContainerType,
typename Alloc>
std::optional<Point<NumberType>> Intersection(const Polygon<NumberType,
IsClockwise,
ContainerType,
Alloc>& polygon) const;
Vector<NumberType> GetNormal() const {
return normal_;
}
Vector<NumberType> GetGuiding() const {
return guiding_;
}
bool OnLine(const Point<NumberType>& point) const {
return (Round(a_ * point.x + b_ * point.y + c_) == 0);
}
NumberType OrientedDistance(const Point<NumberType>& point) const {
auto tmp = a_ * point.x + b_ * point.y + c_;
tmp /= std::sqrt(a_ * a_ + b_ * b_);
return tmp;
}
NumberType Distance(const Point<NumberType>& point) const {
auto tmp = a_ * point.x + b_ * point.y + c_;
tmp /= std::sqrt(a_ * a_ + b_ * b_);
return std::abs(tmp);
}
NumberType Distance(const Line& other) const {
auto det = VectorMul(normal_, other.normal_);
if (Round(det) != 0) {
return 0;
}
Point<NumberType> point;
if (b_ == 0) {
point.x = -c_ / a_;
} else {
point.y = -c_ / b_;
}
auto distance = other.Distance(point);
return distance;
}
NumberType Substitute(const Point<NumberType>& point) const {
auto tmp = a_ * point.x + b_ * point.y + c_;
return tmp;
}
private:
NumberType a_ = 0;
NumberType b_ = 1;
NumberType c_ = 0;
Vector<NumberType> normal_;
Vector<NumberType> guiding_;
};
template <typename NumberType>
class Ray {
public:
Ray(const Point<NumberType>& begin, const Vector<NumberType>& guiding)
: begin_(begin),
guiding_(guiding) {}
Ray() = default;
Ray(const Ray& ray) = default;
Ray(Ray&& ray) noexcept = default;
Ray& operator=(const Ray& ray) = default;
Ray& operator=(Ray&& ray) noexcept = default;
std::optional<Point<NumberType>> Intersection(const Point<NumberType>& point) {
auto tmp_vector = Vector(point.x - begin_.x, point.y - begin_.y);
if (Round(VectorMul(guiding_, tmp_vector)) == 0
&& ScalarMul(guiding_, tmp_vector) >= 0) {
return point;
}
return std::nullopt;
}
std::optional<Point<NumberType>> Intersection(const Point<NumberType>& point) const;
std::optional<Point<NumberType>> Intersection(const Line<NumberType>& line) const;
std::optional<Point<NumberType>> Intersection(const Ray<NumberType>& ray) const;
std::optional<Point<NumberType>> Intersection(const Segment<NumberType>& segment) const;
template <bool IsClockwise,
template <typename, typename> typename ContainerType,
typename Alloc>
std::optional<Point<NumberType>> Intersection(const Polygon<NumberType,
IsClockwise,
ContainerType,
Alloc>& polygon) const;
std::optional<Point<NumberType>> Intersection(const Line<NumberType>& line) {
auto intersection = line.Intersection(Line(begin_,
Point({begin_.x + guiding_.x,
begin_.y + guiding_.y})));
if (intersection) {
return Intersection(intersection.value());
}
return std::nullopt;
}
Point<NumberType> GetBegin() const {
return begin_;
}
Vector<NumberType> GetGuiding() const {
return guiding_;
}
private:
Point<NumberType> begin_;
Vector<NumberType> guiding_;
};
template <typename NumberType>
class Segment {
public:
Segment(const Point<NumberType>& begin, const Point<NumberType>& end)
: begin(begin),
end(end) {}
Segment() = default;
Segment(const Segment& line) = default;
Segment(Segment&& line) noexcept = default;
Segment& operator=(const Segment& line) = default;
Segment& operator=(Segment&& line) noexcept = default;
std::optional<Point<NumberType>> Intersection(const Point<NumberType>& point) const;
std::optional<Point<NumberType>> Intersection(const Line<NumberType>& line) const;
std::optional<Point<NumberType>> Intersection(const Ray<NumberType>& ray) const;
std::optional<Point<NumberType>> Intersection(const Segment<NumberType>& segment) const;
template <bool IsClockwise,
template <typename, typename> typename ContainerType,
typename Alloc>
std::optional<Point<NumberType>> Intersection(const Polygon<NumberType,
IsClockwise,
ContainerType,
Alloc>& polygon) const;
Vector<NumberType> GetNormal() const {
return {begin.x - end.x, begin.y - end.y};
}
enum SegmentIntersection {
Infinite,
Single,
Empty,
};
SegmentIntersection CheckSegmentIntersection(const Segment& other) const {
auto guiding1 = GetGuiding();
auto guiding2 = GetGuiding();
auto mul = VectorMul(guiding1, guiding2);
auto intersection = Intersection(other);
if (!intersection) {
return Empty;
}
if (Round(mul) != 0) {
return Infinite;
}
auto vector1 = Vector<NumberType>(begin, other.begin);
auto vector2 = Vector<NumberType>(begin, other.end);
auto scalar1 = ScalarMul(vector1, vector2);
if (scalar1 < 0) {
return Infinite;
}
auto scalar2 = ScalarMul(vector1, guiding1);
auto scalar3 = ScalarMul(vector2, guiding1);
if (scalar2 >= 0 && scalar3 >= 0) {
return Single;
}
return Infinite;
}
Vector<NumberType> GetGuiding() const {
return {end.x - begin.x, end.y - begin.y};
}
Point<NumberType> begin;
Point<NumberType> end;
};
template <typename NumberType, bool IsClockwise,
template <typename, typename> typename ContainerType,
typename Alloc>
class Polygon {
public:
template <bool is_const>
class Iterator;
using iterator = Iterator<false>; // NOLINT
using const_iterator = Iterator<true>; // NOLINT
using reverse_iterator = std::reverse_iterator<iterator>; // NOLINT
using Container = ContainerType<Point<NumberType>,
Alloc>; // NOLINT
using container_allocator = typename Container::allocator_type; // NOLINT
using container_iterator = typename Container::iterator; // NOLINT
using container_const_iterator = typename Container::const_iterator; // NOLINT
using value_type = typename Container::value_type; // NOLINT
Polygon() = default;
Polygon(const Polygon& polygon) = default;
Polygon(Polygon&& polygon) noexcept = default;
Polygon& operator=(const Polygon& polygon) = default;
Polygon& operator=(Polygon&& polygon) noexcept = default;
template <typename Iterator>
Polygon(Iterator begin, Iterator end) : points_(begin, end) {}
explicit Polygon(Container container) : points_(container) {}
explicit Polygon(uint64_t points_number)
: points_(points_number) {}
uint64_t Size() const {
return points_.size();
}
std::optional<Point<NumberType>> Intersection(const Point<NumberType>& point) const;
std::optional<Point<NumberType>> Intersection(const Line<NumberType>& line) const;
std::optional<Point<NumberType>> Intersection(const Ray<NumberType>& ray) const;
std::optional<Point<NumberType>> Intersection(const Segment<NumberType>& segment) const;
template <bool OtherIsClockwise,
template <typename, typename> typename OtherContainerType,
typename OtherAlloc>
std::optional<Point<NumberType>> Intersection(const Polygon<NumberType,
IsClockwise,
ContainerType,
Alloc>& polygon) const;
bool IsConvex() const {
if (precalculated_) {
return is_convex_;
}
if (points_.size() < 4) {
return is_convex_ = true;
}
bool rotation = true;
uint64_t start = 0;
for (; start < points_.size(); ++start) {
auto point1 = points_[start % points_.size()];
auto point2 = points_[(start + 1) % points_.size()];
auto point3 = points_[(start + 2) % points_.size()];
auto vector1 = Vector(point2.x - point1.x, point2.y - point1.y);
auto vector2 = Vector(point3.x - point2.x, point3.y - point2.y);
auto mul = VectorMul(vector1, vector2);
if (mul != 0) {
rotation = mul > 0;
break;
}
}
for (uint64_t pos = 0; pos < points_.size(); ++pos) {
auto point1 = points_[(start + pos) % points_.size()];
auto point2 = points_[(start + pos + 1) % points_.size()];
auto point3 = points_[(start + pos + 2) % points_.size()];
auto vector1 = Vector(point2.x - point1.x, point2.y - point1.y);
auto vector2 = Vector(point3.x - point2.x, point3.y - point2.y);
auto mul = VectorMul(vector1, vector2);
if (rotation && mul >= 0 || !rotation && mul <= 0) {
continue;
}
return is_convex_ = false;
}
return is_convex_ = true;
}
void Homothety(NumberType scalar) {
for (auto& point: points_) {
point /= scalar;
}
}
void Compress(NumberType scalar) {
for (auto& point: points_) {
point /= scalar;
}
}
uint64_t NumberOfTriangulations(const uint64_t mod) const {
std::vector<std::vector<uint64_t>>
dp(points_.size(), std::vector<uint64_t>(points_.size(), 0));
auto are_visible = Diagonals();
for (uint64_t left = 0; left + 1 < dp.size(); ++left) {
dp[left][left + 1] = 1;
}
for (uint64_t len = 2; len < points_.size(); ++len) {
for (uint64_t left = 0; left + len < points_.size(); ++left) {
uint64_t right = left + len;
if (are_visible[left][right]) {
for (uint64_t j = left + 1; j < right; ++j) {
auto tmp = dp[left][j] * dp[j][right];
tmp %= mod;
dp[left][right] += tmp;
dp[left][right] %= mod;
}
} else {
dp[left][right] = 0;
}
}
}
return dp[0][points_.size() - 1];
}
std::vector<std::vector<bool>> Diagonals() const {
if (!precalculated_) {
IsConvex();
IsClockWise();
precalculated_ = true;
}
std::vector<std::vector<bool>>
answer(points_.size(), std::vector<bool>(points_.size(), false));
for (uint64_t begin = 0; begin < points_.size(); ++begin) {
answer[begin][((begin + 1 + points_.size()) % points_.size()) % points_.size()] = true;
answer[((begin + 1) % points_.size() + points_.size()) % points_.size()][begin] = true;
answer[(begin + points_.size() - 1) % points_.size()][begin] = true;
answer[begin][(begin + points_.size() - 1) % points_.size()] = true;
for (uint64_t end = begin + 2; end < points_.size(); ++end) {
auto diagonal = Segment((*this)[begin], (*this)[end]);
auto diagonal_guiding = diagonal.GetGuiding();
bool has_intersection = false;
for (uint64_t pos = 0; pos < points_.size(); ++pos) {
auto point1 = (*this)[pos];
auto point2 = (*this)[pos + 1];
auto side = Segment<NumberType>(point1, point2);
auto side_guiding = side.GetGuiding();
if (pos != begin && pos != end && diagonal.Intersection(point1) ||
(pos + points_.size() + 1) % points_.size() != begin && (pos + points_.size() + 1) % points_.size() != end && diagonal.Intersection(point2)) {
has_intersection = true;
break;
}
if (pos == begin || pos == end || (pos + 1) % points_.size() == begin || (pos + 1) % points_.size() == end) {
continue;
}
auto intersection = side.Intersection(diagonal);
if (intersection) {
has_intersection = true;
break;
}
}
if (has_intersection) {
continue;
}
answer[begin][end] = CheckDiagonal(begin, end);
answer[end][begin] = answer[begin][end];
}
}
return std::move(answer);
}
bool CheckDiagonal(size_t begin, size_t end) const {
auto cur = (*this)[begin];
auto next = (*this)[begin + 1];
auto prev = (*this)[begin - 1];
auto point_end = (*this)[end];
auto vector1 = Vector<NumberType>(cur, prev);
auto vector2 = Vector<NumberType>(cur, next);
auto diagonal_guiding = Vector<NumberType>(cur, point_end);
auto mul1 = VectorMul(vector1, vector2);
auto mul3 = VectorMul(vector2, diagonal_guiding);
bool tmp = PointBetweenTwoRelative(cur, prev, next, point_end);
if (Round(mul1) == 0) {
return is_clockwise_ && mul3 <= 0 || !is_clockwise_ && mul3 > 0;
}
bool is_valid = false;
if (Round(mul1) > 0 && tmp || mul1 < 0 && !tmp) {
is_valid = true;
}
if (!is_clockwise_) {
is_valid = !is_valid;
}
return is_valid;
}
bool IsClockWise() const {
auto left_most = LeftMost();
auto prev = (*this)[left_most - 1];
auto cur = (*this)[left_most];
auto next = (*this)[left_most + 1];
return is_clockwise_ = ClockwiseRotation(cur, prev, next);
// NumberType sum = 0;
// for (uint64_t pos = 0; pos < points_.size(); ++pos) {
// auto point1 = (*this)[pos];
// auto point2 = (*this)[pos + 1];
// sum += (point2.x - point1.x) * (point2.y + point1.y);
// }
// return is_clockwise_ = sum >= 0;
}
size_t LeftMost() const {
auto answer_num = 0;
auto answer = points_[answer_num];
for (size_t pos = 1; pos < points_.size(); ++pos) {
auto point = points_[pos];
if (point.x < answer.x) {
answer_num = pos;
answer = point;
continue;
}
if (point.x == answer.x && point.y < answer.y) {
answer_num = pos;
answer = point;
}
}
return answer_num;
}
enum PointIntersection {
Inside,
Outside,
Boundary
};
PointIntersection CheckPoint(const Point<NumberType>& point) const {
if (!precalculated_) {
IsConvex();
precalculated_ = true;
}
if (is_convex_) {
return CheckPointInConvex(point);
}
Ray<NumberType> ray = Ray(point, {0, 1});
uint64_t intersection_counter = 0;
for (uint64_t pos = 0; pos < points_.size(); ++pos) {
Point<NumberType> begin = (*this)[pos];
Point<NumberType> end = (*this)[pos + 1];
Segment side = Segment(begin, end);
auto on_side = side.Intersection(point);
if (on_side) {
return PointIntersection::Boundary;
}
if (begin.y > end.y) {
std::swap(begin, end);
}
if (begin.y > point.y || end.y <= point.y) {
continue;
}
auto mul = Round(VectorMul(point - begin, end - begin));
if (mul <= 0) {
++intersection_counter;
}
}
if (intersection_counter % 2 == 1) {
return PointIntersection::Inside;
}
return PointIntersection::Outside;
}
void Insert(const_iterator iterator, const Point<NumberType>& point) {
precalculated_ = false;
points_.insert(iterator.iterator_, point);
}
void Erase(const_iterator iterator) {
precalculated_ = false;
points_.erase(iterator.iterator_);
}
void PopBack() {
Erase(--points_.end());
}
void PopFront() {
Erase(points_.begin());
}
void PushBack(const Point<NumberType>& point) {
Insert(points_.end(), point);
}
void PushFront(const Point<NumberType>& point) {
Insert(points_.begin(), point);
}
void AddPoint(const Point<NumberType>& point) {
points_.push_back(point);
}
Point<NumberType>& operator[](int pos) {
precalculated_ = false;
pos %= (int) points_.size();
if (pos >= 0) {
return points_[pos];
}
return points_[points_.size() + pos];
}
const Point<NumberType>& operator[](int pos) const {
pos %= (int) points_.size();
if (pos >= 0) {
return points_[pos];
}
return points_[points_.size() + pos];
}
iterator begin() { return iterator(points_.begin()); } // NOLINT
const_iterator begin() const { // NOLINT
return (const_cast<Polygon<NumberType,
IsClockwise,
ContainerType,
Alloc>*>(this))->begin();
}
reverse_iterator rbegin() { return reverse_iterator(begin()); } // NOLINT
iterator end() { return iterator(points_.end()); } // NOLINT
const_iterator end() const { // NOLINT
return (const_cast<Polygon<NumberType,
IsClockwise,
ContainerType,
Alloc>*>(this))->end();
}
reverse_iterator rend() { return reverse_iterator(end()); } // NOLINT
private:
PointIntersection CheckPointInConvex(const Point<NumberType>& point) const {
auto origin = points_[0];
int64_t left = 1;
int64_t right = points_.size() - 1;
if (Segment<NumberType>(origin, points_[right]).Intersection(point) ||
Segment<NumberType>(origin, points_[left]).Intersection(point)) {
return PointIntersection::Boundary;
}
if (!PointBetweenTwoRelative(points_[0],
points_[left],
points_[right],
point)) {
return PointIntersection::Outside;
}
while (right > left + 1) {
int64_t mid = (right + left) / 2;
auto orientation = ClockwiseRotation(origin, points_[mid], point);
if (!is_clockwise_ && orientation || is_clockwise_ && !orientation) {
right = mid;
} else {
left = mid;
}
}
right = left + 1;
if (Segment<NumberType>(points_[left],
points_[right]).Intersection(point)) {
return PointIntersection::Boundary;
}
if (PointInTriangle(points_[0], points_[left], points_[right], point)) {
return PointIntersection::Inside;
}
return PointIntersection::Outside;
}
bool PointInTriangle(const Point<NumberType>& point1,
const Point<NumberType>& point2,
const Point<NumberType>& point3,
const Point<NumberType>& point) const {
auto vector1 = Vector<NumberType>(point1, point2);
auto vector2 = Vector<NumberType>(point1, point3);
auto vector3 = Vector<NumberType>(point1, point);
auto s1 = std::abs(VectorMul(vector1, vector2));
vector1 = Vector<NumberType>(point, point1);
vector2 = Vector<NumberType>(point, point2);
vector3 = Vector<NumberType>(point, point3);
s1 -= std::abs(VectorMul(vector1, vector2));
s1 -= std::abs(VectorMul(vector2, vector3));
s1 -= std::abs(VectorMul(vector3, vector1));
return Round(s1) == 0;
}
bool PointBetweenTwoRelative(const Point<NumberType>& origin,
const Point<NumberType>& from,
const Point<NumberType>& to,
const Point<NumberType>& between) const {
auto vector_from = Vector<NumberType>(origin, from);
auto vector_to = Vector<NumberType>(origin, to);
auto vector_between = Vector<NumberType>(origin, between);
bool left = VectorMul(vector_from, vector_between)
* VectorMul(vector_from, vector_to) >= 0;
bool right =
VectorMul(vector_to, vector_between) * VectorMul(vector_to, vector_from)
>= 0;
return (left && right);
}
bool ClockwiseRotation(const Point<NumberType>& origin,
const Point<NumberType>& from,
const Point<NumberType>& to) const {
auto vector1 = Vector<NumberType>(origin, from);
auto vector2 = Vector<NumberType>(origin, to);
auto mul = Round(VectorMul(vector1, vector2));
return mul >= 0;
}
bool mutable precalculated_ = false;
bool mutable is_clockwise_ = false;
bool mutable is_convex_ = false;
Container points_;
};
template <typename NumberType, bool IsClockwise,
template <typename, typename> typename ContainerType,
typename Alloc>
Polygon<NumberType, IsClockwise, ContainerType, Alloc> operator+(
const Polygon<NumberType, IsClockwise, ContainerType, Alloc>& polygon1,
const Polygon<NumberType, IsClockwise, ContainerType, Alloc>& polygon2) {
if (!polygon1.IsConvex() || !polygon2.IsConvex()) {
throw (std::invalid_argument("Non convex polygon"));
}
auto start1 = polygon1.LeftMost();
auto start2 = polygon2.LeftMost();
auto end1 = start1 + polygon1.Size();
auto end2 = start2 + polygon2.Size();
Polygon<NumberType, IsClockwise, ContainerType, Alloc> sum_polygon;
auto last_added_point = polygon1[start1] + polygon2[start2];
sum_polygon.PushBack(last_added_point);
while (start1 < end1 || start2 < end2) {
auto vector1 = Vector<NumberType>(polygon1[start1], polygon1[start1 + 1]);
auto vector2 = Vector<NumberType>(polygon2[start2], polygon2[start2 + 1]);
auto vector_mul = VectorMul(vector1, vector2);
if (vector_mul > 0) {
++start1;
last_added_point = last_added_point + vector1;
} else {
++start2;
last_added_point = last_added_point + vector2;
}
uint64_t sum_polygon_size = sum_polygon.Size();
vector1 = Vector<NumberType>(sum_polygon[sum_polygon_size - 2],
sum_polygon[sum_polygon_size - 1]);
vector2 =
Vector<NumberType>(sum_polygon[sum_polygon_size - 1], last_added_point);
if (sum_polygon_size > 1) {
if (Round(VectorMul(vector1, vector2)) == 0) {
sum_polygon.PopBack();
}
}
sum_polygon.PushBack(last_added_point);
}
sum_polygon.PopBack();
return std::move(sum_polygon);
}
template <typename NumberType>
std::optional<Point<NumberType>> Point<NumberType>::Intersection(const Point<
NumberType>& point) const {
if ((*this) == point) {
return *this;
}
return std::nullopt;
}
template <typename NumberType>
std::optional<Point<NumberType>> Point<NumberType>::Intersection(const Line<
NumberType>& line) const {
return line.Intersection(*this);
}
template <typename NumberType>
std::optional<Point<NumberType>> Point<NumberType>::Intersection(const Ray<
NumberType>& ray) const {
return ray.Intersection(*this);
}
template <typename NumberType>
std::optional<Point<NumberType>> Point<NumberType>::Intersection(const Segment<
NumberType>& segment) const {
return segment.Intersection(*this);
}
template <typename NumberType>
template <bool IsClockwise, template <typename, typename> typename ContainerType, typename Alloc>
std::optional<Point<NumberType>> Point<NumberType>::Intersection(const Polygon<
NumberType,
IsClockwise,
ContainerType,
Alloc>& polygon) const {
return polygon.Intersection(*this);
}
template <typename NumberType>
std::optional<Point<NumberType>> Line<NumberType>::Intersection(const Point<
NumberType>& point) const {