Polygon and ConvexPolygon
Main author: Simon Rohou
The following classes provide a reliable representation of polygons and convex polygons.
Polygon class
-
class Polygon : public std::vector<Segment>
Represents a polygon (convex or non-convex) defined by its vertices enclosed in
IntervalVectors.A polygon can be constructed from a list of vertices (either
VectororIntervalVector) or from a list of edges (Segments). Internally, it stores a list of edges enclosed inSegments.Subclassed by codac2::ConvexPolygon
Public Functions
-
Polygon(std::initializer_list<Vector> vertices)
Constructs a
Polygonfrom an initializer list ofVectorvertices.- Parameters:
vertices – A list of vertices defining the polygon.
-
Polygon(const std::vector<Vector> &vertices)
Constructs a
Polygonfrom a vector ofVectorvertices.- Parameters:
vertices – A vector of vertices defining the polygon.
-
explicit Polygon(const std::vector<IntervalVector> &vertices)
Constructs a
Polygonfrom a vector ofIntervalVectorvertices.- Parameters:
vertices – A vector of
IntervalVectors enclosing the polygon vertices.
-
Polygon(std::initializer_list<Segment> edges)
Constructs a
Polygonfrom an initializer list ofSegmentedges.- Parameters:
edges – A list of
Segments forming the polygon.
-
Polygon(const std::vector<Segment> &edges)
Constructs a
Polygonfrom a vector ofSegmentedges.- Parameters:
edges – A vector of
Segments forming the polygon.
-
explicit Polygon(const IntervalVector &x)
Constructs a box as a
Polygon.Typically used to create a rectangular polygon.
- Parameters:
x – An
IntervalVectorrepresenting the bounds of the polygon.
-
const std::vector<Segment> &edges() const
Returns the list of edges of the polygon.
- Returns:
A constant reference to the vector of
Segments.
-
std::list<IntervalVector> unsorted_vertices() const
Returns the list of unique vertices in no particular order.
If a vertex is involved several times in the polygon definition, then it will be returned only once in the output list.
- Returns:
A list of
IntervalVectors enclosing the unique vertices.
-
std::vector<IntervalVector> sorted_vertices() const
Returns the list of vertices sorted in polygonal order.
- Returns:
A vector of
IntervalVectors enclosing the ordered vertices.
-
IntervalVector box() const
Computes the bounding box of the polygon.
- Returns:
The
IntervalVectorhull box.
-
bool is_empty() const
Checks whether the polygon is empty (has no vertex).
- Returns:
True if the polygon is empty.
-
BoolInterval contains(const IntervalVector &p) const
Checks whether the polygon contains a given point.
- Parameters:
p – The point to check, enclosed in an
IntervalVector.- Returns:
A
BoolIntervalindicating possible containment.
-
Polygon(std::initializer_list<Vector> vertices)
ConvexPolygon class
-
class ConvexPolygon : public codac2::Polygon
Represents a convex polygon defined by vertices enclosed in
IntervalVectors.A convex polygon is a special case of polygon where all internal angles are less than 180°, and every line segment between any two points in the polygon lies entirely within it. It inherits all functionality from
Polygonand ensures convexity of the structure.Public Functions
-
ConvexPolygon(std::initializer_list<Vector> vertices)
Constructs a
ConvexPolygonfrom an initializer list ofVectorvertices.- Parameters:
vertices – A list of vertices defining the convex polygon.
-
ConvexPolygon(const std::vector<Vector> &vertices, bool compute_convex_hull = true)
Constructs a
ConvexPolygonfrom a vector ofVectorvertices.- Parameters:
vertices – A vector of vertices defining the convex polygon.
compute_convex_hull – if true, will create the shape as the convex hull of the provided points. This option may be set to false when one can ensure that the points are already convex and provided in counterclockwise order. This would speed up computations, avoiding a Graham scan procedure.
-
explicit ConvexPolygon(const std::vector<IntervalVector> &vertices, bool compute_convex_hull = true)
Constructs a
ConvexPolygonfrom a vector ofIntervalVectorvertices.- Parameters:
vertices – A vector of
IntervalVectors enclosing the convex polygon vertices.compute_convex_hull – if true, will create the shape as the convex hull of the provided points. This option may be set to false when one can ensure that the points are already convex and provided in counterclockwise order. This would speed up computations, avoiding a Graham scan procedure.
-
ConvexPolygon(std::initializer_list<Segment> edges)
Constructs a
ConvexPolygonfrom an initializer list ofSegmentedges.- Parameters:
edges – A list of
Segments forming the convex polygon.
-
ConvexPolygon(const std::vector<Segment> &edges)
Constructs a
ConvexPolygonfrom a vector ofSegmentedges.- Parameters:
edges – A vector of
Segments forming the convex polygon.
-
explicit ConvexPolygon(const IntervalVector &x)
Constructs a box as a
ConvexPolygon.Typically used to create a rectangular convex polygon.
- Parameters:
x – An
IntervalVectorrepresenting the bounds of the convex polygon.
Public Static Functions
-
static ConvexPolygon empty()
Provides an empty convex polygon.
- Returns:
An empty convex polygon without vertices.
-
ConvexPolygon(std::initializer_list<Vector> vertices)
-
ConvexPolygon codac2::operator&(const ConvexPolygon &p1, const ConvexPolygon &p2)
Computes the intersection of two convex polygons.
The result is a new
ConvexPolygonenclosing the intersection area. If the polygons do not intersect, the result is an empty convex polygon.- Parameters:
p1 – The first convex polygon.
p2 – The second convex polygon.
- Returns:
A
ConvexPolygonenclosing the intersection area.
# ConvexPolygons are defined from sets of vertices in any order
p1 = ConvexPolygon([ # drawn in red
[4,2],[1,8],[2,10],[2,3],
[1,6],[8,6],[7,3],[5,11],[7,10]
])
p2 = ConvexPolygon([ # drawn in blue
[5,1],[7,1],[9,3],[4,3],[4,5],
[9,6],[5,6],[8,8],[6,7]
])
p3 = p1 & p2
// ConvexPolygons are defined from sets of vertices in any order
ConvexPolygon p1({ // drawn in red
{4,2},{1,8},{2,10},{2,3},
{1,6},{8,6},{7,3},{5,11},{7,10}
});
ConvexPolygon p2({ // drawn in blue
{5,1},{7,1},{9,3},{4,3},{4,5},
{9,6},{5,6},{8,8},{6,7}
});
ConvexPolygon p3 = p1 & p2;
Intersection of two convex polygons.