Polygon
Polygon
A polygon defined by a list of 2D vertices. More…
#include <polygon.hpp>
Public Functions
| Name | |
|---|---|
| Polygon(std::vector< Point2D< float > > const & vertices) Construct a polygon from a list of vertices. |
|
| Polygon(BoundingBox const & bbox) Construct a rectangular polygon from a bounding box. |
|
| bool | containsPoint(Point2D< float > const & point) const Check if a point is inside this polygon. |
| BoundingBox | getBoundingBox() const Get the bounding box that encloses this polygon. |
| std::vector< Point2D< float > > const & | getVertices() const Get the polygon vertices. |
| bool | isValid() const Check if this polygon is valid for geometric operations. |
| size_t | vertexCount() const Get the number of vertices in the polygon. |
| Polygon | unionWith(Polygon const & other) const Compute the union of this polygon with another polygon. |
| Polygon | intersectionWith(Polygon const & other) const Compute the intersection of this polygon with another polygon. |
| bool | intersects(Polygon const & other) const Check if this polygon intersects with another polygon. |
| Polygon | differenceWith(Polygon const & other) const Compute the difference of this polygon with another polygon (this - other). |
Detailed Description
class Polygon;A polygon defined by a list of 2D vertices.
Provides functionality for:
- Point-in-polygon testing using ray casting algorithm
- Bounding box calculation
- Vertex access
Public Functions Documentation
function Polygon
explicit Polygon(
std::vector< Point2D< float > > const & vertices
)Construct a polygon from a list of vertices.
Parameters:
- vertices The vertices defining the polygon boundary
function Polygon
explicit Polygon(
BoundingBox const & bbox
)Construct a rectangular polygon from a bounding box.
Parameters:
- bbox The bounding box to convert to a polygon
Creates a rectangular polygon with vertices at the four corners of the bounding box. Vertices are ordered counter-clockwise starting from bottom-left.
function containsPoint
bool containsPoint(
Point2D< float > const & point
) constCheck if a point is inside this polygon.
Parameters:
- point The point to test
Return: True if the point is inside the polygon, false otherwise
Uses ray casting algorithm for point-in-polygon test. Returns false for polygons with fewer than 3 vertices.
function getBoundingBox
BoundingBox getBoundingBox() constGet the bounding box that encloses this polygon.
Return: BoundingBox that tightly bounds all vertices
function getVertices
inline std::vector< Point2D< float > > const & getVertices() constGet the polygon vertices.
Return: Const reference to the vector of vertices
function isValid
inline bool isValid() constCheck if this polygon is valid for geometric operations.
Return: True if polygon has at least 3 vertices
function vertexCount
inline size_t vertexCount() constGet the number of vertices in the polygon.
Return: Number of vertices
function unionWith
Polygon unionWith(
Polygon const & other
) constCompute the union of this polygon with another polygon.
Parameters:
- other The other polygon to union with
Return: A new polygon representing the union, or empty polygon if operation fails
Note: This is a simplified implementation that works by computing the bounding box union for most cases. For complex polygon unions, consider using a dedicated computational geometry library like CGAL or Clipper.
function intersectionWith
Polygon intersectionWith(
Polygon const & other
) constCompute the intersection of this polygon with another polygon.
Parameters:
- other The other polygon to intersect with
Return: A new polygon representing the intersection, or empty polygon if no intersection
Uses the Sutherland-Hodgman clipping algorithm for polygon intersection. Works well for convex polygons and many simple polygon cases.
function intersects
bool intersects(
Polygon const & other
) constCheck if this polygon intersects with another polygon.
Parameters:
- other The other polygon to check intersection with
Return: True if the polygons intersect (including touching), false otherwise
function differenceWith
Polygon differenceWith(
Polygon const & other
) constCompute the difference of this polygon with another polygon (this - other).
Parameters:
- other The other polygon to subtract from this polygon
Return: A new polygon representing the difference
Updated on 2026-11-13 at 20:25:40 +0000