ZLayout EDA Library v1.0.0
Advanced Electronic Design Automation Layout Library with Bilingual Documentation
Loading...
Searching...
No Matches
polygon.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
10#include <vector>
11#include <string>
12#include <utility>
13
14namespace zlayout {
15namespace geometry {
16
25class Polygon {
26public:
27 std::vector<Point> vertices;
28
32 Polygon() = default;
33
38 explicit Polygon(const std::vector<Point>& vertices);
39
44 Polygon(std::initializer_list<Point> vertices);
45
49 Polygon(const Polygon& other) = default;
50
54 Polygon(Polygon&& other) noexcept = default;
55
59 Polygon& operator=(const Polygon& other) = default;
60
64 Polygon& operator=(Polygon&& other) noexcept = default;
65
69 bool operator==(const Polygon& other) const;
70
74 bool operator!=(const Polygon& other) const;
75
76 // Basic properties
77
81 size_t vertex_count() const { return vertices.size(); }
82
86 bool is_empty() const { return vertices.empty(); }
87
91 bool is_valid() const { return vertices.size() >= 3; }
92
97 std::vector<std::pair<Point, Point>> edges() const;
98
103 double area() const;
104
108 double signed_area() const;
109
113 double perimeter() const;
114
118 Point centroid() const;
119
123 Rectangle bounding_box() const;
124
125 // Geometric properties
126
131 bool is_convex() const;
132
137 bool is_clockwise() const;
138
142 bool is_counterclockwise() const { return !is_clockwise(); }
143
148 bool is_simple() const;
149
150 // Point containment
151
157 bool contains_point(const Point& point) const;
158
165 bool point_on_boundary(const Point& point, double tolerance = Point::TOLERANCE) const;
166
167 // Sharp angle detection (Interview Problem 1)
168
174 std::vector<size_t> get_sharp_angles(double threshold_degrees = 30.0) const;
175
181 double vertex_angle(size_t vertex_index) const;
182
187 std::vector<double> all_vertex_angles() const;
188
189 // Distance calculations (Interview Problem 2)
190
196 double distance_to(const Polygon& other) const;
197
203 double distance_to(const Point& point) const;
204
211 double distance_to_line(const Point& line_start, const Point& line_end) const;
212
218 Point closest_point_to(const Point& point) const;
219
220 // Edge operations
221
227 double min_edge_distance_to(const Polygon& other) const;
228
235 std::vector<std::tuple<Point, Point, double>> find_narrow_regions(
236 const Polygon& other, double threshold_distance) const;
237
238 // Intersection detection (Interview Problem 3)
239
245 bool intersects(const Polygon& other) const;
246
252 std::vector<Point> intersection_points(const Polygon& other) const;
253
258 bool has_self_intersections() const;
259
260 // Transformations
261
267 Polygon translate(const Point& offset) const;
268
274 Polygon rotate(double angle) const;
275
282 Polygon rotate_around(const Point& center, double angle) const;
283
289 Polygon scale(double factor) const;
290
297 Polygon scale(double x_factor, double y_factor) const;
298
304
305 // Polygon operations
306
312 Polygon simplify(double tolerance = Point::TOLERANCE) const;
313
319
325
326 // Utility functions
327
332 void add_vertex(const Point& vertex);
333
339 void insert_vertex(size_t index, const Point& vertex);
340
345 void remove_vertex(size_t index);
346
350 void clear() { vertices.clear(); }
351
355 std::string to_string() const;
356
360 friend std::ostream& operator<<(std::ostream& os, const Polygon& polygon);
361
362 // Static utility functions
363
371 static Polygon regular_polygon(const Point& center, double radius, size_t vertex_count);
372
378 static Polygon from_rectangle(const Rectangle& rect);
379
380 // Static utility functions for geometry calculations
381
385 static double segment_to_segment_distance(
386 const Point& seg1_start, const Point& seg1_end,
387 const Point& seg2_start, const Point& seg2_end);
388
393 const Point& seg1_start, const Point& seg1_end,
394 const Point& seg2_start, const Point& seg2_end,
395 bool& intersects);
396
400 static bool segments_intersect(
401 const Point& seg1_start, const Point& seg1_end,
402 const Point& seg2_start, const Point& seg2_end);
403
404private:
405};
406
411 std::size_t operator()(const Polygon& polygon) const;
412};
413
414// Utility functions for line segment operations
415
425bool line_intersection(const Point& line1_start, const Point& line1_end,
426 const Point& line2_start, const Point& line2_end,
427 Point& intersection);
428
437bool segments_intersect(const Point& seg1_start, const Point& seg1_end,
438 const Point& seg2_start, const Point& seg2_end);
439
446double angle_between_vectors(const Point& v1, const Point& v2);
447
448} // namespace geometry
449} // namespace zlayout
2D point with high-precision coordinates and utility methods
Definition point.hpp:23
static constexpr double TOLERANCE
Default precision tolerance for floating point comparisons.
Definition point.hpp:29
Polygon class supporting both convex and concave polygons.
Definition polygon.hpp:25
bool operator==(const Polygon &other) const
Equality operator.
Definition polygon.cpp:27
bool is_empty() const
Check if polygon is empty.
Definition polygon.hpp:86
std::vector< Point > intersection_points(const Polygon &other) const
Find intersection points between polygon edges.
Definition polygon.cpp:341
void remove_vertex(size_t index)
Remove vertex at specific index.
Definition polygon.cpp:389
std::vector< std::pair< Point, Point > > edges() const
Get polygon edges as pairs of consecutive vertices.
Definition polygon.cpp:36
static Point line_segment_intersection(const Point &seg1_start, const Point &seg1_end, const Point &seg2_start, const Point &seg2_end, bool &intersects)
Helper function to find intersection point of two line segments.
Definition polygon.cpp:421
bool point_on_boundary(const Point &point, double tolerance=Point::TOLERANCE) const
Check if point is on polygon boundary.
Definition polygon.cpp:164
double distance_to(const Polygon &other) const
Calculate minimum distance to another polygon.
Definition polygon.cpp:219
static Polygon regular_polygon(const Point &center, double radius, size_t vertex_count)
Create regular polygon with given center, radius, and vertex count.
Rectangle bounding_box() const
Calculate axis-aligned bounding box.
Definition polygon.cpp:84
Polygon(Polygon &&other) noexcept=default
Move constructor.
Polygon scale(double factor) const
Scale polygon by given factor around its centroid.
static Polygon from_rectangle(const Rectangle &rect)
Create polygon from rectangle.
std::vector< double > all_vertex_angles() const
Get all vertex angles.
Definition polygon.cpp:210
bool operator!=(const Polygon &other) const
Inequality operator.
Definition polygon.cpp:31
std::vector< size_t > get_sharp_angles(double threshold_degrees=30.0) const
Find vertices with sharp angles.
Definition polygon.cpp:175
double vertex_angle(size_t vertex_index) const
Calculate angle at a specific vertex.
Definition polygon.cpp:189
static bool segments_intersect(const Point &seg1_start, const Point &seg1_end, const Point &seg2_start, const Point &seg2_end)
Helper function to check if two line segments intersect.
Definition polygon.cpp:449
Polygon simplify(double tolerance=Point::TOLERANCE) const
Simplify polygon by removing collinear vertices.
Polygon & operator=(const Polygon &other)=default
Assignment operator.
Polygon reverse() const
Reverse vertex order (change orientation)
Polygon rotate_around(const Point &center, double angle) const
Rotate polygon around a specific center point.
Point centroid() const
Calculate polygon centroid.
Definition polygon.cpp:73
Point closest_point_to(const Point &point) const
Find closest point on polygon boundary to given point.
Definition polygon.cpp:266
bool intersects(const Polygon &other) const
Check if this polygon intersects with another polygon.
Definition polygon.cpp:325
std::string to_string() const
Get string representation.
Definition polygon.cpp:395
bool is_simple() const
Check if polygon is simple (no self-intersections)
Definition polygon.cpp:128
Polygon & operator=(Polygon &&other) noexcept=default
Move assignment operator.
void insert_vertex(size_t index, const Point &vertex)
Insert vertex at specific index.
Definition polygon.cpp:383
double perimeter() const
Calculate polygon perimeter.
Definition polygon.cpp:62
std::vector< Point > vertices
Polygon vertices in order.
Definition polygon.hpp:27
friend std::ostream & operator<<(std::ostream &os, const Polygon &polygon)
Stream output operator.
Definition polygon.cpp:406
std::vector< std::tuple< Point, Point, double > > find_narrow_regions(const Polygon &other, double threshold_distance) const
Find narrow regions where edges are too close.
Definition polygon.cpp:301
Polygon(const Polygon &other)=default
Copy constructor.
bool is_convex() const
Check if polygon is convex.
Definition polygon.cpp:101
bool is_clockwise() const
Check if polygon is clockwise oriented.
Definition polygon.cpp:124
bool is_valid() const
Check if polygon is valid (at least 3 vertices)
Definition polygon.hpp:91
double min_edge_distance_to(const Polygon &other) const
Calculate minimum distance between this polygon's edges and another polygon's edges.
Definition polygon.cpp:284
Polygon rotate(double angle) const
Rotate polygon around origin by given angle.
bool is_counterclockwise() const
Check if polygon is counterclockwise oriented.
Definition polygon.hpp:142
bool has_self_intersections() const
Check if polygon edges intersect (for self-intersection detection)
Definition polygon.cpp:361
Polygon ensure_counterclockwise() const
Ensure polygon has counterclockwise orientation.
Polygon translate(const Point &offset) const
Translate polygon by given offset.
double signed_area() const
Calculate signed area (preserves orientation)
Definition polygon.cpp:51
Polygon()=default
Default constructor - creates empty polygon.
void clear()
Clear all vertices.
Definition polygon.hpp:350
void add_vertex(const Point &vertex)
Add vertex to polygon.
Definition polygon.cpp:379
Polygon ensure_clockwise() const
Ensure polygon has clockwise orientation.
double distance_to_line(const Point &line_start, const Point &line_end) const
Calculate minimum distance to a line segment.
Definition polygon.cpp:255
bool contains_point(const Point &point) const
Check if point is inside polygon (ray casting algorithm)
Definition polygon.cpp:146
size_t vertex_count() const
Get number of vertices.
Definition polygon.hpp:81
Polygon scale(double x_factor, double y_factor) const
Scale polygon by different factors in X and Y.
double area() const
Calculate polygon area using shoelace formula.
Definition polygon.cpp:47
static double segment_to_segment_distance(const Point &seg1_start, const Point &seg1_end, const Point &seg2_start, const Point &seg2_end)
Helper function to calculate distance between two line segments.
Definition polygon.cpp:411
Axis-aligned rectangle for bounding boxes and simple EDA components.
Definition rectangle.hpp:26
bool line_intersection(const Point &line1_start, const Point &line1_end, const Point &line2_start, const Point &line2_end, Point &intersection)
Calculate intersection point of two infinite lines.
double angle_between_vectors(const Point &v1, const Point &v2)
Calculate angle between two vectors in degrees.
Definition polygon.cpp:473
bool segments_intersect(const Point &seg1_start, const Point &seg1_end, const Point &seg2_start, const Point &seg2_end)
Check if two line segments intersect.
Definition polygon.cpp:468
Main namespace for ZLayout library.
Definition component.hpp:20
2D Point class for geometric calculations
Axis-aligned rectangle class for bounding boxes and simple components.
Hash function for Polygon (for use in std::unordered_map, etc.)
Definition polygon.hpp:410
std::size_t operator()(const Polygon &polygon) const
Definition polygon.cpp:459