Polygon.jl
Polygon provides a ConvexHull
type, which represents the convex hull of a set of 2D points by its extreme points. Functionality includes:
- convexity test
- construction of a convex hull given a set of points
- area
- centroid
- point-in-convex-hull test
- closest point within convex hull
- equivalent halfspace representation of the convex hull
Types
The ConvexHull
type
Polygon.ConvexHull
— Typestruct ConvexHull{O<:Polygon.VertexOrder, T, P<:StaticArraysCore.StaticArray{Tuple{2}, T, 1}, V<:AbstractArray{P<:StaticArraysCore.StaticArray{Tuple{2}, T, 1}, 1}}
Represents the convex hull of a set of 2D points by its extreme points (vertices), which are stored according to the VertexOrder
given by the first type parameter.
Polygon.SConvexHull
— TypeSConvexHull{N, T}
The default statically-sized ConvexHull
type. Backed by an SVector{N, SVector{2, T}}
with vertices ordered counter-clockwise.
Polygon.DConvexHull
— TypeDConvexHull{N, T}
The default dynamically-sized ConvexHull
type. Backed by a Vector{SVector{2, T}}
with vertices ordered counter-clockwise.
Polygon.vertices
— Functionvertices(hull)
Return the ConvexHull
's (ordered) vector of vertices.
Polygon.num_vertices
— Functionnum_vertices(hull)
Return the number of vertices of the given ConvexHull
.
VertexOrder
s
Polygon.VertexOrder
— Typeabstract type VertexOrder
A VertexOrder
represents the order in which the vertices of a ConvexHull
are stored.
Polygon.CCW
— Typestruct CCW <: Polygon.VertexOrder
Counterclockwise vertex order.
Polygon.CW
— Typestruct CW <: Polygon.VertexOrder
Clockwise vertex order.
Algorithms
Polygon.is_ordered_and_strongly_convex
— Functionis_ordered_and_strongly_convex(vertices, order)
Return whether vertices
are ordered according to vertex order type O
(a subtype of VertexOrder
), and as a result strongly convex (see e.g. CGAL documentation for a definition of strong convexity).
Polygon.jarvis_march!
— Functionjarvis_march!(hull, points)
Compute the convex hull of points
and store the result in hull
using the Jarvis march (gift wrapping) algorithm. This algorithm has $O(nh)$ complexity, where $n$ is the number of points and $h$ is the number of vertices of the convex hull.
Polygon.area
— Functionarea(hull)
Compute the area of the given ConvexHull
using the shoelace formula.
Polygon.centroid
— Functioncentroid(hull)
Compute the centroid or geometric center of the given ConvexHull
using the formulas given here.
Base.in
— Methodin(point, hull)
Return whether point
is in hull
.
Polygon.closest_point
— Functionclosest_point(p, hull)
Find the closest point to p
within hull
. If p
is inside hull
, p
itself is returned.
Polygon.hrep
— Functionhrep(hull)
Return the equivalent halfspace representation of the convex hull, i.e. matrix $A$ and vector $b$ such that the set of points inside the hull is
\[\left\{ x \mid A x \le b \right\}\]
If hull
is backed by a statically sized vector of vertices, the output (A, b)
will be statically sized as well. If the vector of vertices is additionally immutable (e.g., a StaticArrays.SVector
), then hrep
will not perform any dynamic memory allocation.
Polygon.hrep!
— Functionhrep!(A, b, hull)
Return the equivalent halfspace representation of the convex hull, i.e. matrix $A$ and vector $b$ such that the set of points inside the hull is
\[\left\{ x \mid A x \le b \right\}\]
This function stores its output in the (mutable) matrix A
and vector b
.