ZLayout EDA Library v1.0.0
Advanced Electronic Design Automation Layout Library with Bilingual Documentation
Loading...
Searching...
No Matches
zlayout::geometry::Polygon Class Reference

Polygon class supporting both convex and concave polygons. More...

#include <polygon.hpp>

Collaboration diagram for zlayout::geometry::Polygon:

Public Member Functions

 Polygon ()=default
 Default constructor - creates empty polygon.
 Polygon (const std::vector< Point > &vertices)
 Constructor from vertex list.
 Polygon (std::initializer_list< Point > vertices)
 Constructor from initializer list.
 Polygon (const Polygon &other)=default
 Copy constructor.
 Polygon (Polygon &&other) noexcept=default
 Move constructor.
Polygonoperator= (const Polygon &other)=default
 Assignment operator.
Polygonoperator= (Polygon &&other) noexcept=default
 Move assignment operator.
bool operator== (const Polygon &other) const
 Equality operator.
bool operator!= (const Polygon &other) const
 Inequality operator.
size_t vertex_count () const
 Get number of vertices.
bool is_empty () const
 Check if polygon is empty.
bool is_valid () const
 Check if polygon is valid (at least 3 vertices)
std::vector< std::pair< Point, Point > > edges () const
 Get polygon edges as pairs of consecutive vertices.
double area () const
 Calculate polygon area using shoelace formula.
double signed_area () const
 Calculate signed area (preserves orientation)
double perimeter () const
 Calculate polygon perimeter.
Point centroid () const
 Calculate polygon centroid.
Rectangle bounding_box () const
 Calculate axis-aligned bounding box.
bool is_convex () const
 Check if polygon is convex.
bool is_clockwise () const
 Check if polygon is clockwise oriented.
bool is_counterclockwise () const
 Check if polygon is counterclockwise oriented.
bool is_simple () const
 Check if polygon is simple (no self-intersections)
bool contains_point (const Point &point) const
 Check if point is inside polygon (ray casting algorithm)
bool point_on_boundary (const Point &point, double tolerance=Point::TOLERANCE) const
 Check if point is on polygon boundary.
std::vector< size_t > get_sharp_angles (double threshold_degrees=30.0) const
 Find vertices with sharp angles.
double vertex_angle (size_t vertex_index) const
 Calculate angle at a specific vertex.
std::vector< double > all_vertex_angles () const
 Get all vertex angles.
double distance_to (const Polygon &other) const
 Calculate minimum distance to another polygon.
double distance_to (const Point &point) const
 Calculate minimum distance to a point.
double distance_to_line (const Point &line_start, const Point &line_end) const
 Calculate minimum distance to a line segment.
Point closest_point_to (const Point &point) const
 Find closest point on polygon boundary to given point.
double min_edge_distance_to (const Polygon &other) const
 Calculate minimum distance between this polygon's edges and another polygon's edges.
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.
bool intersects (const Polygon &other) const
 Check if this polygon intersects with another polygon.
std::vector< Pointintersection_points (const Polygon &other) const
 Find intersection points between polygon edges.
bool has_self_intersections () const
 Check if polygon edges intersect (for self-intersection detection)
Polygon translate (const Point &offset) const
 Translate polygon by given offset.
Polygon rotate (double angle) const
 Rotate polygon around origin by given angle.
Polygon rotate_around (const Point &center, double angle) const
 Rotate polygon around a specific center point.
Polygon scale (double factor) const
 Scale polygon by given factor around its centroid.
Polygon scale (double x_factor, double y_factor) const
 Scale polygon by different factors in X and Y.
Polygon reverse () const
 Reverse vertex order (change orientation)
Polygon simplify (double tolerance=Point::TOLERANCE) const
 Simplify polygon by removing collinear vertices.
Polygon ensure_counterclockwise () const
 Ensure polygon has counterclockwise orientation.
Polygon ensure_clockwise () const
 Ensure polygon has clockwise orientation.
void add_vertex (const Point &vertex)
 Add vertex to polygon.
void insert_vertex (size_t index, const Point &vertex)
 Insert vertex at specific index.
void remove_vertex (size_t index)
 Remove vertex at specific index.
void clear ()
 Clear all vertices.
std::string to_string () const
 Get string representation.
 __init__ (self, List[Point] vertices)
str __repr__ (self)
List[Tuple[Point, Point]] edges (self)
Rectangle bounding_box (self)
float area (self)
bool is_convex (self)
bool contains_point (self, Point point)
List[int] get_sharp_angles (self, float threshold_degrees=30.0)

Static Public Member Functions

static Polygon regular_polygon (const Point &center, double radius, size_t vertex_count)
 Create regular polygon with given center, radius, and vertex count.
static Polygon from_rectangle (const Rectangle &rect)
 Create polygon from rectangle.
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.
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.
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.

Public Attributes

std::vector< Pointvertices
 Polygon vertices in order.
 vertices = vertices.copy()

Protected Member Functions

bool _point_on_edge (self, Point point, Point edge_start, Point edge_end)

Friends

std::ostream & operator<< (std::ostream &os, const Polygon &polygon)
 Stream output operator.

Detailed Description

Polygon class supporting both convex and concave polygons.

Polygon class supporting both convex and concave polygons.

Represents a polygon in 2D space with arbitrary vertex count. Supports complex geometric operations, sharp angle detection, and various polygon analysis functions essential for EDA applications.

Definition at line 25 of file polygon.hpp.

Constructor & Destructor Documentation

◆ Polygon() [1/5]

zlayout::geometry::Polygon::Polygon ( )
default

Default constructor - creates empty polygon.

Here is the caller graph for this function:

◆ Polygon() [2/5]

zlayout::geometry::Polygon::Polygon ( const std::vector< Point > & vertices)
explicit

Constructor from vertex list.

Parameters
verticesVector of polygon vertices

Definition at line 22 of file polygon.cpp.

◆ Polygon() [3/5]

zlayout::geometry::Polygon::Polygon ( std::initializer_list< Point > vertices)

Constructor from initializer list.

Parameters
verticesInitializer list of vertices

Definition at line 24 of file polygon.cpp.

◆ Polygon() [4/5]

zlayout::geometry::Polygon::Polygon ( const Polygon & other)
default

Copy constructor.

Here is the call graph for this function:

◆ Polygon() [5/5]

zlayout::geometry::Polygon::Polygon ( Polygon && other)
defaultnoexcept

Move constructor.

Here is the call graph for this function:

◆ __init__()

zlayout.geometry.Polygon.__init__ ( self,
List[Point] vertices )

Definition at line 125 of file geometry.py.

Member Function Documentation

◆ __repr__()

str zlayout.geometry.Polygon.__repr__ ( self)

Definition at line 130 of file geometry.py.

◆ _point_on_edge()

bool zlayout.geometry.Polygon._point_on_edge ( self,
Point point,
Point edge_start,
Point edge_end )
protected
Check if a point lies on an edge.

Definition at line 223 of file geometry.py.

Here is the caller graph for this function:

◆ add_vertex()

void zlayout::geometry::Polygon::add_vertex ( const Point & vertex)

Add vertex to polygon.

Parameters
vertexVertex to add

Definition at line 379 of file polygon.cpp.

◆ all_vertex_angles()

std::vector< double > zlayout::geometry::Polygon::all_vertex_angles ( ) const

Get all vertex angles.

Returns
Vector of angles in degrees for each vertex

Definition at line 210 of file polygon.cpp.

Here is the call graph for this function:

◆ area() [1/2]

float zlayout.geometry.Polygon.area ( self)
Calculate polygon area using shoelace formula.

Definition at line 154 of file geometry.py.

◆ area() [2/2]

double zlayout::geometry::Polygon::area ( ) const

Calculate polygon area using shoelace formula.

Returns
Polygon area (positive for counterclockwise, negative for clockwise)

Definition at line 47 of file polygon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bounding_box() [1/2]

Rectangle zlayout.geometry.Polygon.bounding_box ( self)
Calculate axis-aligned bounding box.

Definition at line 142 of file geometry.py.

◆ bounding_box() [2/2]

Rectangle zlayout::geometry::Polygon::bounding_box ( ) const

Calculate axis-aligned bounding box.

Definition at line 84 of file polygon.cpp.

◆ centroid()

Point zlayout::geometry::Polygon::centroid ( ) const

Calculate polygon centroid.

Definition at line 73 of file polygon.cpp.

◆ clear()

void zlayout::geometry::Polygon::clear ( )
inline

Clear all vertices.

Definition at line 350 of file polygon.hpp.

◆ closest_point_to()

Point zlayout::geometry::Polygon::closest_point_to ( const Point & point) const

Find closest point on polygon boundary to given point.

Parameters
pointTarget point
Returns
Closest point on polygon boundary

Definition at line 266 of file polygon.cpp.

◆ contains_point() [1/2]

bool zlayout.geometry.Polygon.contains_point ( self,
Point point )
Check if point is inside polygon using ray casting.

Definition at line 193 of file geometry.py.

Here is the call graph for this function:

◆ contains_point() [2/2]

bool zlayout::geometry::Polygon::contains_point ( const Point & point) const

Check if point is inside polygon (ray casting algorithm)

Parameters
pointPoint to test
Returns
true if point is inside polygon

Definition at line 146 of file polygon.cpp.

Here is the caller graph for this function:

◆ distance_to() [1/2]

double zlayout::geometry::Polygon::distance_to ( const Point & point) const

Calculate minimum distance to a point.

Parameters
pointTarget point
Returns
Minimum distance to point

Definition at line 239 of file polygon.cpp.

Here is the call graph for this function:

◆ distance_to() [2/2]

double zlayout::geometry::Polygon::distance_to ( const Polygon & other) const

Calculate minimum distance to another polygon.

Parameters
otherTarget polygon
Returns
Minimum distance between polygons

Definition at line 219 of file polygon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ distance_to_line()

double zlayout::geometry::Polygon::distance_to_line ( const Point & line_start,
const Point & line_end ) const

Calculate minimum distance to a line segment.

Parameters
line_startStart point of line segment
line_endEnd point of line segment
Returns
Minimum distance to line segment

Definition at line 255 of file polygon.cpp.

◆ edges() [1/2]

List[Tuple[Point, Point]] zlayout.geometry.Polygon.edges ( self)
Get all edges as (start, end) point pairs.

Definition at line 134 of file geometry.py.

◆ edges() [2/2]

std::vector< std::pair< Point, Point > > zlayout::geometry::Polygon::edges ( ) const

Get polygon edges as pairs of consecutive vertices.

Returns
Vector of edge pairs (start_point, end_point)

Definition at line 36 of file polygon.cpp.

Here is the caller graph for this function:

◆ ensure_clockwise()

Polygon zlayout::geometry::Polygon::ensure_clockwise ( ) const

Ensure polygon has clockwise orientation.

Returns
Polygon with clockwise orientation
Here is the call graph for this function:

◆ ensure_counterclockwise()

Polygon zlayout::geometry::Polygon::ensure_counterclockwise ( ) const

Ensure polygon has counterclockwise orientation.

Returns
Polygon with counterclockwise orientation
Here is the call graph for this function:

◆ find_narrow_regions()

std::vector< std::tuple< Point, Point, double > > zlayout::geometry::Polygon::find_narrow_regions ( const Polygon & other,
double threshold_distance ) const

Find narrow regions where edges are too close.

Parameters
otherTarget polygon
threshold_distanceMaximum allowed distance
Returns
Vector of (point1, point2, distance) for narrow regions

Definition at line 301 of file polygon.cpp.

Here is the call graph for this function:

◆ from_rectangle()

Polygon zlayout::geometry::Polygon::from_rectangle ( const Rectangle & rect)
static

Create polygon from rectangle.

Parameters
rectRectangle to convert
Returns
Polygon with rectangle vertices
Here is the call graph for this function:

◆ get_sharp_angles() [1/2]

List[int] zlayout.geometry.Polygon.get_sharp_angles ( self,
float threshold_degrees = 30.0 )
Find vertices with sharp angles or boundary angles.

For traditional sharp angle detection (small thresholds < 60°): 
finds acute angles smaller than threshold.
For boundary angle detection (large thresholds ≥ 60°): 
finds angles larger than threshold.

Definition at line 229 of file geometry.py.

◆ get_sharp_angles() [2/2]

std::vector< size_t > zlayout::geometry::Polygon::get_sharp_angles ( double threshold_degrees = 30.0) const

Find vertices with sharp angles.

Parameters
threshold_degreesAngle threshold in degrees (angles less than this are considered sharp)
Returns
Vector of vertex indices with sharp angles

Definition at line 175 of file polygon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ has_self_intersections()

bool zlayout::geometry::Polygon::has_self_intersections ( ) const

Check if polygon edges intersect (for self-intersection detection)

Returns
true if polygon has self-intersecting edges

Definition at line 361 of file polygon.cpp.

Here is the call graph for this function:

◆ insert_vertex()

void zlayout::geometry::Polygon::insert_vertex ( size_t index,
const Point & vertex )

Insert vertex at specific index.

Parameters
indexIndex to insert at
vertexVertex to insert

Definition at line 383 of file polygon.cpp.

◆ intersection_points()

std::vector< Point > zlayout::geometry::Polygon::intersection_points ( const Polygon & other) const

Find intersection points between polygon edges.

Parameters
otherTarget polygon
Returns
Vector of intersection points

Definition at line 341 of file polygon.cpp.

Here is the call graph for this function:

◆ intersects()

bool zlayout::geometry::Polygon::intersects ( const Polygon & other) const

Check if this polygon intersects with another polygon.

Parameters
otherTarget polygon
Returns
true if polygons intersect

Definition at line 325 of file polygon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_clockwise()

bool zlayout::geometry::Polygon::is_clockwise ( ) const

Check if polygon is clockwise oriented.

Returns
true if polygon vertices are in clockwise order

Definition at line 124 of file polygon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_convex() [1/2]

bool zlayout.geometry.Polygon.is_convex ( self)
Check if polygon is convex.

Definition at line 167 of file geometry.py.

◆ is_convex() [2/2]

bool zlayout::geometry::Polygon::is_convex ( ) const

Check if polygon is convex.

Returns
true if polygon is convex

Definition at line 101 of file polygon.cpp.

◆ is_counterclockwise()

bool zlayout::geometry::Polygon::is_counterclockwise ( ) const
inline

Check if polygon is counterclockwise oriented.

Definition at line 142 of file polygon.hpp.

Here is the call graph for this function:

◆ is_empty()

bool zlayout::geometry::Polygon::is_empty ( ) const
inline

Check if polygon is empty.

Definition at line 86 of file polygon.hpp.

◆ is_simple()

bool zlayout::geometry::Polygon::is_simple ( ) const

Check if polygon is simple (no self-intersections)

Returns
true if polygon has no self-intersections

Definition at line 128 of file polygon.cpp.

Here is the call graph for this function:

◆ is_valid()

bool zlayout::geometry::Polygon::is_valid ( ) const
inline

Check if polygon is valid (at least 3 vertices)

Definition at line 91 of file polygon.hpp.

◆ line_segment_intersection()

Point zlayout::geometry::Polygon::line_segment_intersection ( const Point & seg1_start,
const Point & seg1_end,
const Point & seg2_start,
const Point & seg2_end,
bool & intersects )
static

Helper function to find intersection point of two line segments.

Definition at line 421 of file polygon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ min_edge_distance_to()

double zlayout::geometry::Polygon::min_edge_distance_to ( const Polygon & other) const

Calculate minimum distance between this polygon's edges and another polygon's edges.

Parameters
otherTarget polygon
Returns
Minimum edge-to-edge distance

Definition at line 284 of file polygon.cpp.

Here is the call graph for this function:

◆ operator!=()

bool zlayout::geometry::Polygon::operator!= ( const Polygon & other) const

Inequality operator.

Definition at line 31 of file polygon.cpp.

Here is the call graph for this function:

◆ operator=() [1/2]

Polygon & zlayout::geometry::Polygon::operator= ( const Polygon & other)
default

Assignment operator.

Here is the call graph for this function:

◆ operator=() [2/2]

Polygon & zlayout::geometry::Polygon::operator= ( Polygon && other)
defaultnoexcept

Move assignment operator.

Here is the call graph for this function:

◆ operator==()

bool zlayout::geometry::Polygon::operator== ( const Polygon & other) const

Equality operator.

Definition at line 27 of file polygon.cpp.

Here is the call graph for this function:

◆ perimeter()

double zlayout::geometry::Polygon::perimeter ( ) const

Calculate polygon perimeter.

Definition at line 62 of file polygon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ point_on_boundary()

bool zlayout::geometry::Polygon::point_on_boundary ( const Point & point,
double tolerance = Point::TOLERANCE ) const

Check if point is on polygon boundary.

Parameters
pointPoint to test
toleranceDistance tolerance for boundary check
Returns
true if point is on polygon boundary

Definition at line 164 of file polygon.cpp.

Here is the call graph for this function:

◆ regular_polygon()

Polygon zlayout::geometry::Polygon::regular_polygon ( const Point & center,
double radius,
size_t vertex_count )
static

Create regular polygon with given center, radius, and vertex count.

Parameters
centerCenter point
radiusRadius from center to vertices
vertex_countNumber of vertices
Returns
Regular polygon
Here is the call graph for this function:

◆ remove_vertex()

void zlayout::geometry::Polygon::remove_vertex ( size_t index)

Remove vertex at specific index.

Parameters
indexIndex of vertex to remove

Definition at line 389 of file polygon.cpp.

◆ reverse()

Polygon zlayout::geometry::Polygon::reverse ( ) const

Reverse vertex order (change orientation)

Returns
Polygon with reversed vertex order
Here is the call graph for this function:

◆ rotate()

Polygon zlayout::geometry::Polygon::rotate ( double angle) const

Rotate polygon around origin by given angle.

Parameters
angleRotation angle in radians
Returns
Rotated polygon
Here is the call graph for this function:

◆ rotate_around()

Polygon zlayout::geometry::Polygon::rotate_around ( const Point & center,
double angle ) const

Rotate polygon around a specific center point.

Parameters
centerCenter of rotation
angleRotation angle in radians
Returns
Rotated polygon
Here is the call graph for this function:

◆ scale() [1/2]

Polygon zlayout::geometry::Polygon::scale ( double factor) const

Scale polygon by given factor around its centroid.

Parameters
factorScale factor
Returns
Scaled polygon
Here is the call graph for this function:

◆ scale() [2/2]

Polygon zlayout::geometry::Polygon::scale ( double x_factor,
double y_factor ) const

Scale polygon by different factors in X and Y.

Parameters
x_factorX scale factor
y_factorY scale factor
Returns
Scaled polygon
Here is the call graph for this function:

◆ segment_to_segment_distance()

double zlayout::geometry::Polygon::segment_to_segment_distance ( const Point & seg1_start,
const Point & seg1_end,
const Point & seg2_start,
const Point & seg2_end )
static

Helper function to calculate distance between two line segments.

Definition at line 411 of file polygon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ segments_intersect()

bool zlayout::geometry::Polygon::segments_intersect ( const Point & seg1_start,
const Point & seg1_end,
const Point & seg2_start,
const Point & seg2_end )
static

Helper function to check if two line segments intersect.

Definition at line 449 of file polygon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ signed_area()

double zlayout::geometry::Polygon::signed_area ( ) const

Calculate signed area (preserves orientation)

Definition at line 51 of file polygon.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ simplify()

Polygon zlayout::geometry::Polygon::simplify ( double tolerance = Point::TOLERANCE) const

Simplify polygon by removing collinear vertices.

Parameters
toleranceTolerance for collinearity check
Returns
Simplified polygon
Here is the call graph for this function:

◆ to_string()

std::string zlayout::geometry::Polygon::to_string ( ) const

Get string representation.

Definition at line 395 of file polygon.cpp.

Here is the caller graph for this function:

◆ translate()

Polygon zlayout::geometry::Polygon::translate ( const Point & offset) const

Translate polygon by given offset.

Parameters
offsetTranslation vector
Returns
Translated polygon
Here is the call graph for this function:

◆ vertex_angle()

double zlayout::geometry::Polygon::vertex_angle ( size_t vertex_index) const

Calculate angle at a specific vertex.

Parameters
vertex_indexIndex of vertex to calculate angle for
Returns
Angle in degrees (0-180)

Definition at line 189 of file polygon.cpp.

Here is the caller graph for this function:

◆ vertex_count()

size_t zlayout::geometry::Polygon::vertex_count ( ) const
inline

Get number of vertices.

Definition at line 81 of file polygon.hpp.

Here is the caller graph for this function:

◆ operator<<

std::ostream & operator<< ( std::ostream & os,
const Polygon & polygon )
friend

Stream output operator.

Definition at line 406 of file polygon.cpp.

Member Data Documentation

◆ vertices [1/2]

zlayout.geometry.Polygon.vertices = vertices.copy()

Definition at line 128 of file geometry.py.

◆ vertices [2/2]

std::vector<Point> zlayout::geometry::Polygon::vertices

Polygon vertices in order.

Definition at line 27 of file polygon.hpp.


The documentation for this class was generated from the following files: