Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
polygon.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
84# ifndef POLYGON_H
85# define POLYGON_H
86
87# include <dlink.H>
88# include <point.H>
89# include <ah-errors.H>
90# include <ah-dry.H>
91# include <ahDry.H>
92# include <ah-iterator.H>
93# include <cmath>
94# include <string>
95# include <utility>
96
97namespace Aleph
98{
99 using Aleph::Dlink;
100
119 class Vertex : public Point, public Dlink
120 {
121 public:
123 Vertex() = default;
124
127 Vertex(const Point & point) : Point(point)
128 { /* empty */
129 }
130
134 Vertex(const Vertex & vertex) : Point(vertex), Dlink()
135 { /* empty */
136 }
137
142 Vertex &operator=(const Vertex & vertex)
143 {
144 if (this != &vertex)
145 Point::operator=(vertex);
146 return *this;
147 }
148
151 [[nodiscard]] Point to_point() const
152 {
153 return static_cast<const Point>(*this);
154 }
155
160 [[nodiscard]] static Vertex * dlink_to_vertex(Dlink *link)
161 {
162 return static_cast<Vertex *>(link);
163 }
164
168 [[nodiscard]] static const Vertex * dlink_to_vertex(const Dlink *link)
169 {
170 return static_cast<const Vertex *>(link);
171 }
172
177 [[nodiscard]] const Vertex &prev_vertex() const
178 {
179 assert(not this->is_empty());
180
181 ah_domain_error_if(this->is_unitarian()) << "There is an only vertex";
182
183 return *dlink_to_vertex(this->get_prev());
184 }
185
190 [[nodiscard]] const Vertex &next_vertex() const
191 {
192 assert(not this->is_empty());
193
194 ah_domain_error_if(this->is_unitarian()) << "There is an only vertex";
195
196 return *dlink_to_vertex(this->get_next());
197 }
198
199 // TODO: Implement next_segment() and prev_segment() methods
200 };
201
202
203 class Regular_Polygon;
204
242 class Polygon : public Geom_Object,
243 public GenericTraverse<Polygon>,
244 public FunctionalMethods<Polygon, Point>,
245 public LocateFunctions<Polygon, Point>,
246 public StlAlephIterator<Polygon>
247 {
249 size_t num_vertex_;
251
256
259 void update_extreme_points(const Point & point)
260 {
261 if (num_vertex_ == 0)
262 {
263 leftmost_ = rightmost_ = lowest_ = highest_ = point;
264 return;
265 }
266
267 if (point.get_x() < leftmost_.get_x())
268 leftmost_ = point;
269
270 if (point.get_x() > rightmost_.get_x())
271 rightmost_ = point;
272
273 if (point.get_y() < lowest_.get_y())
274 lowest_ = point;
275
276 if (point.get_y() > highest_.get_y())
277 highest_ = point;
278 }
279
282 {
283 while (not vertex_list_.is_empty())
285
286 num_vertex_ = 0;
287 is_closed_ = false;
288 }
289
292 void copy_points(const Polygon & poly)
293 {
294 auto *list = const_cast<Dlink *>(&poly.vertex_list_);
295
296 for (Dlink::Iterator it(list); it.has_curr(); it.next_ne())
298 }
299
302 void copy_regular_polygon(const Regular_Polygon & poly);
303
304 public:
307
310
318 {
320
321 public:
322 Iterator() noexcept = default;
323
324 Iterator(const Polygon & poly) noexcept
325 : dit_(poly.vertex_list_) {}
326
327 [[nodiscard]] bool has_curr() const noexcept { return dit_.has_curr(); }
328
329 void next() { dit_.next(); }
330
331 void next_ne() noexcept { dit_.next_ne(); }
332
333 void prev() { dit_.prev(); }
334
335 void prev_ne() noexcept { dit_.prev_ne(); }
336
337 void reset_first() noexcept { dit_.reset_first(); }
338
339 void end() noexcept { dit_.end(); }
340
341 [[nodiscard]] const Point &get_curr() const
342 {
344 }
345
349 [[nodiscard]] const Point &get_curr_ne() const noexcept
350 {
352 }
353
354 [[nodiscard]] Dlink * get_pos() const noexcept
355 {
356 return dit_.get_curr_ne();
357 }
358
359 [[nodiscard]] bool operator==(const Iterator & o) const noexcept
360 {
361 return dit_ == o.dit_;
362 }
363
364 [[nodiscard]] bool operator!=(const Iterator & o) const noexcept
365 {
366 return dit_ != o.dit_;
367 }
368 };
369
372 { /* empty */
373 }
374
377 {
379 }
380
383 Polygon(const Polygon & poly)
384 : Geom_Object(poly),
386 lowest_(poly.lowest_), highest_(poly.highest_),
388 {
389 copy_points(poly);
390 }
391
394 Polygon(Polygon && poly) noexcept
395 : Polygon()
396 {
397 vertex_list_.swap(poly.vertex_list_);
398 std::swap(num_vertex_, poly.num_vertex_);
399 std::swap(is_closed_, poly.is_closed_);
400 std::swap(lowest_, poly.lowest_);
401 std::swap(highest_, poly.highest_);
402 std::swap(leftmost_, poly.leftmost_);
403 std::swap(rightmost_, poly.rightmost_);
404 }
405
410 : num_vertex_(0), is_closed_(false)
411 {
413 }
414
419 {
420 if (this == &poly)
421 return *this;
422
424
426 is_closed_ = poly.is_closed_;
427 lowest_ = poly.lowest_;
428 highest_ = poly.highest_;
429 leftmost_ = poly.leftmost_;
430 rightmost_ = poly.rightmost_;
431
432 copy_points(poly);
433
434 return *this;
435 }
436
440 Polygon &operator =(Polygon && poly) noexcept
441 {
442 vertex_list_.swap(poly.vertex_list_);
443 std::swap(num_vertex_, poly.num_vertex_);
444 std::swap(is_closed_, poly.is_closed_);
445 std::swap(lowest_, poly.lowest_);
446 std::swap(highest_, poly.highest_);
447 std::swap(leftmost_, poly.leftmost_);
448 std::swap(rightmost_, poly.rightmost_);
449
450 return *this;
451 }
452
457 {
460
461 return *this;
462 }
463
466 [[nodiscard]] const Point &lowest_point() const { return lowest_; }
467
470 [[nodiscard]] const Point &highest_point() const { return highest_; }
471
474 [[nodiscard]] const Point &leftmost_point() const { return leftmost_; }
475
478 [[nodiscard]] const Point &rightmost_point() const { return rightmost_; }
479
482 [[nodiscard]] const bool &is_closed() const { return is_closed_; }
483
486 [[nodiscard]] const size_t &size() const { return num_vertex_; }
487
498 {
503 {
504 ah_domain_error_if(poly.vertex_list_.is_empty()) << "Polygon has not any vertex";
505 }
506
510 {
512 }
513 };
514
529 {
531
532 public:
537 : Dlink::Iterator(poly.vertex_list_), poly_(const_cast<Polygon &>(poly))
538 {
540 << "Polygon has less than two vertex";
541 }
542
546 [[nodiscard]] bool has_curr() const
547 {
548 if (this->is_in_last())
549 return poly_.is_closed() ? true : false;
550
552 }
553
557 [[nodiscard]] Segment get_current_segment() const
558 {
559 ah_domain_error_if(not poly_.is_closed() and this->is_in_last())
560 << "Segment iterator is in the last point and it is not closed";
561
562 const Vertex *src = Vertex::dlink_to_vertex(this->get_curr());
563
564 // Determine target: if at last vertex, wrap to first; otherwise, next
565 const Vertex & tgt =
566 this->is_in_last() ? poly_.get_first_vertex() : src->next_vertex();
567
568 return {src->to_point(), tgt.to_point()};
569 }
570 };
571
575 [[nodiscard]] bool vertex_belong_polygon(const Vertex & v) const
576 {
577 for (Vertex_Iterator it(*this); it.has_curr(); it.next_ne())
578 if (&it.get_current_vertex() == &v)
579 return true;
580
581 return false;
582 }
583
587 [[nodiscard]] const Vertex &get_first_vertex() const
588 {
589 ah_domain_error_if(vertex_list_.is_empty()) << "Polygon has not any vertex";
590
592 }
593
597 [[nodiscard]] const Vertex &get_last_vertex() const
598 {
599 ah_domain_error_if(vertex_list_.is_empty()) << "Polygon has not any vertex";
600
602 }
603
610 [[nodiscard]] const Vertex &get_next_vertex(const Vertex & v) const
611 {
612 // If v is the last vertex (next points to sentinel), wrap to the first vertex
613 return v.get_next() == &vertex_list_ ?
615 v.next_vertex();
616 }
617
624 [[nodiscard]] const Vertex &get_prev_vertex(const Vertex & v) const
625 {
626 // If v is the first vertex (prev points to sentinel), wrap to last vertex
627 return v.get_prev() == &vertex_list_ ?
629 v.prev_vertex();
630 }
631
635 [[nodiscard]] Segment get_first_segment() const
636 {
638 << "polygon has less than two vertex";
639
640 const Vertex & first_vertex = get_first_vertex();
641
642 return {first_vertex.to_point(), first_vertex.next_vertex().to_point()};
643 }
644
648 [[nodiscard]] Segment get_last_segment() const
649 {
651 << "polygon has less than two vertex";
652
653 const Vertex & last_vertex = get_last_vertex();
654
655 return {last_vertex.prev_vertex().to_point(), last_vertex.to_point()};
656 }
657
661 [[nodiscard]] bool intersects_with(const Segment & sg) const
662 {
663 // Traverse all edges and check for intersection
664 for (Segment_Iterator it(*this); it.has_curr(); it.next_ne())
665 if (const Segment & curr_side = it.get_current_segment(); curr_side.intersects_with(sg))
666 return true;
667
668 return false;
669 }
670
686 void add_vertex(const Point & point)
687 {
688 ah_domain_error_if(is_closed_) << "Polygon is already closed";
689
690 // Check if the new point is colinear with the last segment
691 if (num_vertex_ > 1)
692 if (const Segment last_sg = get_last_segment(); point.is_colinear_with(last_sg))
693 {
694 ah_domain_error_if(point.is_inside(last_sg))
695 << "new vertex is inside of last polygon's segment";
696
697 // Colinear point: replace the last vertex with this new one
698 auto & last_vertex = const_cast<Vertex &>(get_last_vertex());
699 Point old_point = last_vertex.to_point(); // Store old point before replacing
700 Point & last_point = last_vertex;
701 last_point = point;
702
703 // Recalculate extremes if the old point was an extreme point
704 bool was_extreme = (old_point == lowest_ || old_point == highest_ ||
705 old_point == leftmost_ || old_point == rightmost_);
706
707 if (was_extreme)
708 {
709 // We need a full recalculation of extremes
711
712 for (Vertex_Iterator it(*this); it.has_curr(); it.next_ne())
713 {
714 const Point p = it.get_current_vertex().to_point();
715
716 if (p.get_x() < leftmost_.get_x())
717 leftmost_ = p;
718 if (p.get_x() > rightmost_.get_x())
719 rightmost_ = p;
720 if (p.get_y() < lowest_.get_y())
721 lowest_ = p;
722 if (p.get_y() > highest_.get_y())
723 highest_ = p;
724 }
725 }
726 else
727 {
728 // Just check if the new point is an extreme
730 }
731
732 return;
733 }
734
735 // If we have at least 3 vertices, verify no self-intersection
736 if (num_vertex_ >= 3)
737 {
738 // New edge that would be formed by adding this point
739 const Segment new_side(get_last_vertex().to_point(), point);
740
741 // Check for intersection with all edges except the last one
742 // (which shares a vertex with the new edge)
743 for (Segment_Iterator it(*this); true; it.next_ne())
744 {
745 const Segment curr_side = it.get_current_segment();
746
747 if (curr_side == get_last_segment())
748 break;
749
750 ah_domain_error_if(curr_side.intersects_with(new_side)) << "new side intersects";
751 }
752 }
753
754 // Insert new vertex
755 vertex_list_.append(new Vertex(point));
757 ++num_vertex_;
758 }
759
765 void add_vertex(const Geom_Number & x, const Geom_Number & y)
766 {
767 add_vertex(Point(x, y));
768 }
769
773 void append(const Point & point) { add_vertex(point); }
774
776
786 void remove_vertex(const Vertex & v)
787 {
788 Vertex *victim = nullptr;
789 for (Vertex_Iterator it(*this); it.has_curr(); it.next_ne())
790 if (&it.get_current_vertex() == &v)
791 {
792 victim = &const_cast<Vertex &>(it.get_current_vertex());
793 break;
794 }
795
796 ah_domain_error_if(victim == nullptr) << "Vertex does not belong to polygon";
797
798 // Store point for later comparison with extremes
799 // cppcheck-suppress nullPointer
800 const Point victim_point = victim->to_point();
801
802 // Delete the vertex
803 // cppcheck-suppress nullPointer
804 victim->del();
805 --num_vertex_;
806 delete victim;
807
808 // Recalculate extremes if needed
809 if (victim_point == lowest_ || victim_point == highest_ ||
810 victim_point == leftmost_ || victim_point == rightmost_)
811 {
812 if (num_vertex_ == 0)
813 // No vertices left, reset extremes
815 else
816 {
817 // Recalculate extremes from remaining vertices
818 const Point first_point = get_first_vertex().to_point();
819 lowest_ = highest_ = leftmost_ = rightmost_ = first_point;
820
821 for (Vertex_Iterator it(*this); it.has_curr(); it.next_ne())
822 {
823 const Point point = it.get_current_vertex().to_point();
824 if (point.get_x() < leftmost_.get_x())
825 leftmost_ = point;
826 if (point.get_x() > rightmost_.get_x())
827 rightmost_ = point;
828 if (point.get_y() < lowest_.get_y())
829 lowest_ = point;
830 if (point.get_y() > highest_.get_y())
831 highest_ = point;
832 }
833 }
834 }
835
836 // Reset the closed state if the polygon now has fewer than 3 vertices
837 if (is_closed_ && num_vertex_ < 3)
838 is_closed_ = false;
839 }
840
851 void close()
852 {
853 ah_domain_error_if(is_closed_) << "Polygon is already closed";
855 << "Polygon needs at least 3 vertices to close (has " << num_vertex_ << ")";
856
857 if (num_vertex_ >= 4)
858 {
859 // Check for intersection with all edges except the first
860 // and last (which share endpoints with the closing edge)
861 const Segment last_side(get_first_vertex().to_point(), get_last_vertex().to_point());
862
863 Segment_Iterator it(*this);
864
865 for (it.next(); true; it.next_ne())
866 {
867 const Segment curr_side = it.get_current_segment();
868
869 if (curr_side == get_last_segment())
870 break;
871
872 ah_domain_error_if(curr_side.intersects_with(last_side))
873 << "closing causes an intersection";
874 }
875 }
876
877 is_closed_ = true;
878 }
879
880 enum class PointLocation
881 {
882 Outside,
883 Boundary,
884 Inside
885 };
886
895 [[nodiscard]] PointLocation locate_point(const Point & p) const
896 {
897 ah_domain_error_if(not is_closed_) << "Polygon is not closed";
898
899 int winding = 0;
900
901 for (Segment_Iterator it(*this); it.has_curr(); it.next_ne())
902 {
903 const Segment edge = it.get_current_segment();
904 const Point & a = edge.get_src_point();
905 const Point & b = edge.get_tgt_point();
906
907 if (on_segment(edge, p))
909
910 if (a.get_y() <= p.get_y())
911 {
912 if (b.get_y() > p.get_y() and
913 orientation(a, b, p) == Orientation::CCW)
914 ++winding;
915 }
916 else
917 {
918 if (b.get_y() <= p.get_y() and
919 orientation(a, b, p) == Orientation::CW)
920 --winding;
921 }
922 }
923
924 return winding == 0 ? PointLocation::Outside : PointLocation::Inside;
925 }
926
937 [[nodiscard]] bool contains(const Point & p) const
938 {
940 }
941
943 [[deprecated("Use contains() instead")]]
944 [[nodiscard]] bool contains_to(const Point & p) const { return contains(p); }
945
954 [[nodiscard]] Geom_Number signed_area() const
955 {
956 ah_domain_error_if(num_vertex_ < 3) << "Polygon must have at least 3 vertices";
957
958 Geom_Number sum = 0;
959 for (Segment_Iterator it(*this); it.has_curr(); it.next_ne())
960 {
961 const Segment seg = it.get_current_segment();
962 const Point & a = seg.get_src_point();
963 const Point & b = seg.get_tgt_point();
964 sum += a.get_x() * b.get_y() - a.get_y() * b.get_x();
965 }
966 return sum / 2;
967 }
968
974 [[nodiscard]] Geom_Number area() const
975 {
977 return a < 0 ? Geom_Number(-a) : a;
978 }
979
987 [[nodiscard]] Geom_Number perimeter() const
988 {
989 ah_domain_error_if(num_vertex_ < 2) << "Polygon must have at least 2 vertices";
990
991 Geom_Number sum = 0;
992 for (Segment_Iterator it(*this); it.has_curr(); it.next_ne())
993 sum += it.get_current_segment().length();
994 return sum;
995 }
996
1004 [[nodiscard]] Point centroid() const
1005 {
1006 ah_domain_error_if(num_vertex_ < 3) << "Polygon must have at least 3 vertices";
1007
1008 Geom_Number cx = 0, cy = 0, a6 = 0;
1009 for (Segment_Iterator it(*this); it.has_curr(); it.next_ne())
1010 {
1011 const Segment seg = it.get_current_segment();
1012 const Point & p = seg.get_src_point();
1013 const Point & q = seg.get_tgt_point();
1014 const Geom_Number cross = p.get_x() * q.get_y() - q.get_x() * p.get_y();
1015 cx += (p.get_x() + q.get_x()) * cross;
1016 cy += (p.get_y() + q.get_y()) * cross;
1017 a6 += cross;
1018 }
1019
1020 ah_domain_error_if(a6 == 0) << "Polygon has zero area";
1021 return {cx / (3 * a6), cy / (3 * a6)};
1022 }
1023
1032 [[nodiscard]] bool is_convex() const
1033 {
1034 ah_domain_error_if(num_vertex_ < 3) << "Polygon must have at least 3 vertices";
1035
1036 // Check turn direction at each vertex using segment iterator
1037 int sign = 0;
1038 Segment_Iterator it(*this);
1039 Segment prev_seg = it.get_current_segment();
1040 it.next_ne();
1041
1042 while (it.has_curr())
1043 {
1044 const Segment curr_seg = it.get_current_segment();
1045 // Turn at the shared vertex (prev_seg.tgt == curr_seg.src)
1047 prev_seg.get_src_point(), prev_seg.get_tgt_point(),
1048 curr_seg.get_tgt_point());
1049
1050 if (turn != 0)
1051 {
1052 const int curr_sign = turn > 0 ? 1 : -1;
1053 if (sign == 0)
1054 sign = curr_sign;
1055 else if (sign != curr_sign)
1056 return false;
1057 }
1058
1059 prev_seg = curr_seg;
1060 it.next_ne();
1061 }
1062
1063 // Check the wrap-around turn (last vertex to first)
1064 const Segment first_seg = Segment_Iterator(*this).get_current_segment();
1066 prev_seg.get_src_point(), prev_seg.get_tgt_point(),
1067 first_seg.get_tgt_point());
1068
1069 if (turn != 0)
1070 if (const int curr_sign = turn > 0 ? 1 : -1; sign != 0 and sign != curr_sign)
1071 return false;
1072
1073 return true;
1074 }
1075
1082 Polygon(const Triangle & tr)
1083 : num_vertex_(0), is_closed_(false)
1084 {
1085 add_vertex(tr.get_p1());
1086 add_vertex(tr.get_p2());
1087 add_vertex(tr.get_p3());
1088
1089 close();
1090 }
1091 };
1092
1093
1143 {
1145 double side_size_;
1147 double angle_;
1148 double r_;
1149 double beta_;
1150
1151 public:
1157 : center_(0, 0), side_size_(0), num_vertex_(0), angle_(0), r_(0), beta_(0)
1158 { /* empty */
1159 }
1160
1172 const double & side_sz,
1173 const size_t & n,
1174 const double & ang = 0)
1175 : center_(std::move(c)), side_size_(side_sz), num_vertex_(n), angle_(ang),
1176 r_(0), beta_(0)
1177 {
1178 ah_domain_error_if(n < 3) << "Regular polygon must have at least 3 sides";
1179 ah_domain_error_if(not std::isfinite(side_sz) or side_sz <= 0)
1180 << "Regular polygon side length must be positive and finite";
1181 ah_domain_error_if(not std::isfinite(ang))
1182 << "Regular polygon angle must be finite";
1183
1184 beta_ = 2 * PI / static_cast<double>(n);
1185
1186 // Calculate radius using law of sines with angles between vertices
1187 // and the angle between radius and side
1188 const double alpha = (PI - beta_) / 2; // angle between radius and side
1189 r_ = side_size_ * sin(alpha) / sin(beta_);
1190 ah_domain_error_if(not std::isfinite(r_) or r_ <= 0)
1191 << "Regular polygon circumradius is not representable";
1192 }
1193
1196 [[nodiscard]] const double &get_side_size() const { return side_size_; }
1197
1200 [[nodiscard]] const Point &get_center() const { return center_; }
1201
1204 [[nodiscard]] const size_t &size() const { return num_vertex_; }
1205
1208 [[nodiscard]] const double &radius() const { return r_; }
1209
1215 [[nodiscard]] bool is_closed() const noexcept { return num_vertex_ >= 3; }
1216
1226 [[nodiscard]] Point get_vertex(const size_t & i) const
1227 {
1229 << "vertex " << i << " is greater than " << num_vertex_;
1230
1231 // The first segment is the negative vertical from center
1232 // Initial point is the target of this segment with origin at center
1233 Segment sg(center_, center_ - Point(0, r_));
1234
1235 sg.rotate(i * beta_ + angle_);
1236
1237 return sg.get_tgt_point();
1238 }
1239
1242 [[nodiscard]] Point get_first_vertex() const { return get_vertex(0); }
1243
1246 [[nodiscard]] Point get_last_vertex() const { return get_vertex(num_vertex_ - 1); }
1247
1250 [[nodiscard]] Segment get_first_segment() const
1251 {
1252 return {get_vertex(0), get_vertex(1)};
1253 }
1254
1257 [[nodiscard]] Segment get_last_segment() const
1258 {
1259 return {get_vertex(num_vertex_ - 1), get_vertex(0)};
1260 }
1261
1274 {
1276 size_t curr_;
1278
1279 public:
1283 : poly_(poly), curr_(0)
1284 {
1285 // empty
1286 }
1287
1290 [[nodiscard]] bool has_curr() const { return curr_ < poly_.size(); }
1291
1296 {
1297 ah_overflow_error_if(not has_curr()) << "Iterator has not current";
1298
1300
1301 return vertex_;
1302 }
1303
1305 void next_ne() noexcept { ++curr_; }
1306
1309 void next()
1310 {
1311 ah_overflow_error_if(not has_curr()) << "Iterator has not current";
1312 next_ne();
1313 }
1314
1317 void prev()
1318 {
1319 --curr_;
1320
1321 ah_underflow_error_if(not has_curr()) << "Iterator has not current";
1322 }
1323 };
1324
1337 {
1339 size_t curr_;
1340
1341 public:
1345 : poly_(poly), curr_(0)
1346 {
1347 // empty
1348 }
1349
1352 [[nodiscard]] bool has_curr() const { return curr_ < poly_.size(); }
1353
1357 [[nodiscard]] Segment get_current_segment() const
1358 {
1359 ah_overflow_error_if(not has_curr()) << "Iterator has not current";
1360
1361 return {poly_.get_vertex(curr_), poly_.get_vertex((curr_ + 1) % poly_.size())};
1362 }
1363
1365 void next_ne() noexcept { ++curr_; }
1366
1369 void next()
1370 {
1371 ah_overflow_error_if(not has_curr()) << "Iterator has not current";
1372 next_ne();
1373 }
1374
1377 void prev()
1378 {
1379 --curr_;
1380
1381 ah_underflow_error_if(not has_curr()) << "Iterator has not current";
1382 }
1383 };
1384
1389 [[nodiscard]] Point lowest_point() const { return center_ + Point(0, -r_); }
1390
1393 [[nodiscard]] Point highest_point() const { return center_ + Point(0, r_); }
1394
1397 [[nodiscard]] Point leftmost_point() const { return center_ + Point(-r_, 0); }
1398
1401 [[nodiscard]] Point rightmost_point() const { return center_ + Point(r_, 0); }
1402 };
1403
1409 {
1410 assert(num_vertex_ == 0 and not is_closed_);
1411
1412 for (size_t i = 0; i < poly.size(); ++i)
1413 add_vertex(poly.get_vertex(i));
1414
1415 close();
1416 }
1417
1418 // ============================================================================
1419 // Stream output operators for polygon classes
1420 // ============================================================================
1421
1428 inline std::ostream &operator<<(std::ostream & o, const Polygon & p)
1429 {
1430 o << "Polygon(n=" << p.size() << ", "
1431 << (p.is_closed() ? "closed" : "open") << ", [";
1432 size_t count = 0;
1433 for (Polygon::Vertex_Iterator it(p); it.has_curr(); it.next_ne())
1434 {
1435 if (count > 0) o << ", ";
1436 o << it.get_current_vertex();
1437 ++count;
1438 }
1439 o << "])";
1440 return o;
1441 }
1442
1449 inline std::ostream &operator<<(std::ostream & o, const Regular_Polygon & p)
1450 {
1451 o << "Regular_Polygon(center=" << p.get_center()
1452 << ", n=" << p.size()
1453 << ", radius=" << p.radius() << ")";
1454 return o;
1455 }
1456} // namespace Aleph
1457
1458#endif // POLYGON_H
Container traversal and functional operation mixins.
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:727
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:517
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
STL-compatible iterator adapters for Aleph containers.
DRY (Don't Repeat Yourself) utilities and macros.
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Definition ahDry.H:123
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:221
bool is_inside(const Segment &s) const
Checks if this point is contained within a segment.
Definition point.H:1460
const Geom_Number & get_x() const noexcept
Gets the x-coordinate value.
Definition point.H:448
const Geom_Number & get_y() const noexcept
Gets the y-coordinate value.
Definition point.H:457
bool is_colinear_with(const Point &p1, const Point &p2) const
Checks if this point is collinear with two other points.
Definition point.H:468
STL/Aleph-compatible iterator over polygon vertices as Points.
Definition polygon.H:318
void prev_ne() noexcept
Definition polygon.H:335
const Point & get_curr() const
Definition polygon.H:341
Dlink::Iterator dit_
Definition polygon.H:319
bool operator==(const Iterator &o) const noexcept
Definition polygon.H:359
const Point & get_curr_ne() const noexcept
Current vertex, without checking that there is one.
Definition polygon.H:349
Iterator() noexcept=default
void next_ne() noexcept
Definition polygon.H:331
Dlink * get_pos() const noexcept
Definition polygon.H:354
bool operator!=(const Iterator &o) const noexcept
Definition polygon.H:364
void end() noexcept
Definition polygon.H:339
bool has_curr() const noexcept
Definition polygon.H:327
void reset_first() noexcept
Definition polygon.H:337
Iterator over the edges (segments) of a polygon.
Definition polygon.H:529
Polygon & poly_
Reference to the polygon being iterated.
Definition polygon.H:530
Segment get_current_segment() const
Get the current segment (edge).
Definition polygon.H:557
bool has_curr() const
Check if there is a current segment.
Definition polygon.H:546
Segment_Iterator(const Polygon &poly)
Construct a segment iterator from a polygon.
Definition polygon.H:536
A general (irregular) 2D polygon defined by a sequence of vertices.
Definition polygon.H:247
const Point & rightmost_point() const
Get the vertex with the maximum x-coordinate.
Definition polygon.H:478
void copy_points(const Polygon &poly)
Copy all vertices from another polygon.
Definition polygon.H:292
void delete_points()
Delete all vertices and reset the polygon state.
Definition polygon.H:281
Polygon(Polygon &&poly) noexcept
Move constructor.
Definition polygon.H:394
bool contains_to(const Point &p) const
Definition polygon.H:944
Point lowest_
Vertex with minimum y-coordinate.
Definition polygon.H:252
void copy_regular_polygon(const Regular_Polygon &poly)
Copy vertices from a regular polygon.
Definition polygon.H:1408
Geom_Number signed_area() const
Compute the signed area of the polygon using the shoelace formula.
Definition polygon.H:954
Point centroid() const
Compute the centroid (center of mass) of the polygon.
Definition polygon.H:1004
bool is_closed_
True if the polygon has been closed.
Definition polygon.H:250
Geom_Number perimeter() const
Compute the perimeter of the polygon.
Definition polygon.H:987
void update_extreme_points(const Point &point)
Update extreme points after adding a new vertex.
Definition polygon.H:259
const Point & highest_point() const
Get the vertex with the maximum y-coordinate.
Definition polygon.H:470
const Vertex & get_next_vertex(const Vertex &v) const
Get the vertex following v in the polygon.
Definition polygon.H:610
Polygon & operator=(const Polygon &poly)
Copy assignment operator.
Definition polygon.H:418
const Vertex & get_prev_vertex(const Vertex &v) const
Get the vertex preceding v in the polygon.
Definition polygon.H:624
Geom_Number area() const
Compute the absolute area of the polygon.
Definition polygon.H:974
void remove_vertex(const Vertex &v)
Remove a vertex from the polygon.
Definition polygon.H:786
void append(const Point &point)
Append a vertex (Aleph container protocol).
Definition polygon.H:773
size_t num_vertex_
Number of vertices in the polygon.
Definition polygon.H:249
PointLocation locate_point(const Point &p) const
Classify a point against this closed polygon.
Definition polygon.H:895
Point rightmost_
Vertex with maximum x-coordinate.
Definition polygon.H:255
const Vertex & get_first_vertex() const
Get the first vertex of the polygon.
Definition polygon.H:587
Polygon(const Regular_Polygon &poly)
Construct from a Regular_Polygon.
Definition polygon.H:409
const Vertex & get_last_vertex() const
Get the last vertex of the polygon.
Definition polygon.H:597
Segment get_last_segment() const
Get the last edge (segment) of the polygon.
Definition polygon.H:648
bool is_convex() const
Check if the polygon is convex.
Definition polygon.H:1032
~Polygon()
Destructor. Frees all vertices.
Definition polygon.H:376
void add_vertex(const Geom_Number &x, const Geom_Number &y)
Add a vertex using coordinates.
Definition polygon.H:765
void add_vertex(const Point &point)
Add a vertex to the polygon.
Definition polygon.H:686
Point leftmost_
Vertex with minimum x-coordinate.
Definition polygon.H:254
Segment get_first_segment() const
Get the first edge (segment) of the polygon.
Definition polygon.H:635
Polygon(const Polygon &poly)
Copy constructor.
Definition polygon.H:383
Point highest_
Vertex with maximum y-coordinate.
Definition polygon.H:253
void close()
Close the polygon.
Definition polygon.H:851
const Point & leftmost_point() const
Get the vertex with the minimum x-coordinate.
Definition polygon.H:474
const bool & is_closed() const
Check if the polygon is closed.
Definition polygon.H:482
bool vertex_belong_polygon(const Vertex &v) const
Check if a vertex belongs to this polygon.
Definition polygon.H:575
Dlink vertex_list_
Doubly linked list of vertices.
Definition polygon.H:248
const size_t & size() const
Get the number of vertices.
Definition polygon.H:486
bool intersects_with(const Segment &sg) const
Check if a segment intersects with any edge of the polygon.
Definition polygon.H:661
Polygon(const Triangle &tr)
Construct a polygon from a Triangle.
Definition polygon.H:1082
const Point & lowest_point() const
Get the vertex with the minimum y-coordinate.
Definition polygon.H:466
Polygon()
Default constructor. Creates an empty, open polygon.
Definition polygon.H:371
bool contains(const Point &p) const
Check if a point is inside the polygon (or on its boundary).
Definition polygon.H:937
Iterator over the edges (segments) of a regular polygon.
Definition polygon.H:1337
void prev()
Move to the previous segment.
Definition polygon.H:1377
void next()
Advance to the next segment.
Definition polygon.H:1369
Segment get_current_segment() const
Get the current segment (edge).
Definition polygon.H:1357
bool has_curr() const
Check if there is a current segment.
Definition polygon.H:1352
const Regular_Polygon & poly_
Reference to the polygon.
Definition polygon.H:1338
Segment_Iterator(const Regular_Polygon &poly)
Construct segment iterator from a regular polygon.
Definition polygon.H:1344
size_t curr_
Current segment index.
Definition polygon.H:1339
void next_ne() noexcept
Advance to the next segment (no exception on overflow).
Definition polygon.H:1365
Iterator over the vertices of a regular polygon.
Definition polygon.H:1274
const Regular_Polygon & poly_
Reference to the polygon.
Definition polygon.H:1275
Vertex & get_current_vertex()
Get the current vertex.
Definition polygon.H:1295
size_t curr_
Current vertex index.
Definition polygon.H:1276
bool has_curr() const
Check if there is a current vertex.
Definition polygon.H:1290
void next()
Advance to the next vertex.
Definition polygon.H:1309
void prev()
Move to previous vertex.
Definition polygon.H:1317
void next_ne() noexcept
Advance to the next vertex (no exception on overflow).
Definition polygon.H:1305
Vertex vertex_
Cache for compatibility with Polygon::Vertex_Iterator.
Definition polygon.H:1277
Vertex_Iterator(const Regular_Polygon &poly)
Construct iterator from a regular polygon.
Definition polygon.H:1282
A regular polygon defined by center, side length, and vertex count.
Definition polygon.H:1143
Point get_last_vertex() const
Get the last vertex (index size()-1).
Definition polygon.H:1246
Point get_vertex(const size_t &i) const
Get the i-th vertex of the polygon.
Definition polygon.H:1226
Point get_first_vertex() const
Get the first vertex (index 0).
Definition polygon.H:1242
double r_
Circumradius (distance from center to vertices)
Definition polygon.H:1148
const size_t & size() const
Get the number of vertices.
Definition polygon.H:1204
const double & get_side_size() const
Get the side length.
Definition polygon.H:1196
double angle_
Rotation angle in radians.
Definition polygon.H:1147
Point lowest_point() const
Get the lowest point of the bounding circle.
Definition polygon.H:1389
Regular_Polygon(Point c, const double &side_sz, const size_t &n, const double &ang=0)
Construct a regular polygon.
Definition polygon.H:1171
Point highest_point() const
Get the highest point of the bounding circle.
Definition polygon.H:1393
const Point & get_center() const
Get the center point.
Definition polygon.H:1200
double side_size_
Length of each side (double due to trig limitations)
Definition polygon.H:1145
Regular_Polygon()
Default constructor.
Definition polygon.H:1156
Segment get_first_segment() const
Get the first segment (edge).
Definition polygon.H:1250
Segment get_last_segment() const
Get the last segment (closing edge).
Definition polygon.H:1257
Point center_
Center point of the polygon.
Definition polygon.H:1144
size_t num_vertex_
Number of vertices (sides)
Definition polygon.H:1146
double beta_
Central angle between adjacent vertices (2π/n)
Definition polygon.H:1149
Point leftmost_point() const
Get the leftmost point of the bounding circle.
Definition polygon.H:1397
bool is_closed() const noexcept
Check if the polygon is a valid closed regular polygon.
Definition polygon.H:1215
const double & radius() const
Get the circumradius.
Definition polygon.H:1208
Point rightmost_point() const
Get the rightmost point of the bounding circle.
Definition polygon.H:1401
Represents a line segment between two points.
Definition point.H:837
void rotate(const Geom_Number &angle)
Rotates the segment by a given angle around its source point.
Definition point.H:1431
bool intersects_with(const Segment &s) const
Checks if this segment intersects another one (including endpoints and collinear overlap).
Definition point.H:1291
const Point & get_tgt_point() const noexcept
Gets the target point of the segment.
Definition point.H:933
const Point & get_src_point() const noexcept
Gets the source point of the segment.
Definition point.H:924
A non-degenerate triangle defined by three points.
Definition point.H:1512
const Point & get_p3() const
Gets the third vertex.
Definition point.H:1629
const Point & get_p2() const
Gets the second vertex.
Definition point.H:1624
const Point & get_p1() const
Gets the first vertex.
Definition point.H:1619
A vertex in a polygon's doubly linked vertex list.
Definition polygon.H:120
Vertex(const Vertex &vertex)
Copy constructor.
Definition polygon.H:134
Vertex(const Point &point)
Construct a vertex from a Point.
Definition polygon.H:127
const Vertex & prev_vertex() const
Get the previous vertex in the polygon.
Definition polygon.H:177
Vertex()=default
Default constructor. Creates a vertex at origin (0, 0).
static const Vertex * dlink_to_vertex(const Dlink *link)
Convert a const Dlink pointer to a const Vertex pointer.
Definition polygon.H:168
static Vertex * dlink_to_vertex(Dlink *link)
Convert a Dlink pointer to a Vertex pointer.
Definition polygon.H:160
Vertex & operator=(const Vertex &vertex)
Copy assignment operator.
Definition polygon.H:142
const Vertex & next_vertex() const
Get the next vertex in the polygon.
Definition polygon.H:190
Point to_point() const
Return this vertex as a plain Point value.
Definition polygon.H:151
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:1235
and
Insert a new element into the container by constructing it from the given args.
Definition ah-dry.H:1273
Common sequential searching methods on containers.
Definition ah-dry.H:555
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sin_function > > sin(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4148
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
Geom_Number area_of_parallelogram(const Point &a, const Point &b, const Point &c)
Compute the signed area of the parallelogram defined by vectors a->b and a->c.
Definition point.H:2886
bool on_segment(const Segment &s, const Point &p)
Return true if p lies on segment s (exact).
Definition point.H:2961
Orientation orientation(const Point &a, const Point &b, const Point &c)
Return the orientation of the triple (a, b, c).
Definition point.H:2902
mpq_class Geom_Number
Numeric type used by the geometry module.
Definition point.H:113
std::ostream & operator<<(std::ostream &osObject, const Field< T > &rightOp)
Definition ahField.H:121
constexpr double PI
Definition point.H:129
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
STL namespace.
2D point and geometric utilities.
Base class for all geometric objects.
Definition point.H:206
Iterator over the vertices of a polygon.
Definition polygon.H:498
Vertex_Iterator(const Polygon &poly)
Construct iterator from a polygon.
Definition polygon.H:502
Vertex & get_current_vertex() const
Get the current vertex.
Definition polygon.H:509
Generic traversal of the container through its iterator.
Definition ah-dry.H:363