# dip::Polygon struct#include <diplib/chain_code.h>

A polygon with floating-point vertices.

• Reference

## Public functions

auto BoundingBox() const -> BoundingBoxFloat
Returns the bounding box of the polygon.
auto IsClockWise() const -> bool
Determine the orientation of the polygon (if constructed from a chain code, should return true)
auto Area() const -> dfloat
Computes the (signed) area of the polygon. Default, clockwise polygons have a positive area.
auto Centroid() const -> VertexFloat
Computes the centroid of the polygon.
auto CovarianceMatrix(VertexFloat const& g) const -> dip::CovarianceMatrix
Returns the covariance matrix for the vertices of the polygon, using centroid `g`.
auto CovarianceMatrix() const -> dip::CovarianceMatrix
Returns the covariance matrix for the vertices of the polygon.
auto Length() const -> dfloat
Computes the length of the polygon (i.e. perimeter). If the polygon represents a pixelated object, this function will overestimate the object's perimeter. Use `dip::ChainCode::Length` instead.
Returns statistics on the radii of the polygon. The radii are the distances between the centroid and each of the vertices.
Returns statistics on the radii of the polygon. The radii are the distances between the given centroid and each of the vertices.
auto EllipseVariance() const -> dfloat
Compares a polygon to the ellipse with the same covariance matrix, returning the coefficient of variation of the distance of vertices to the ellipse.
auto EllipseVariance(VertexFloat const& g, dip::CovarianceMatrix const& C) const -> dfloat
Compares a polygon to the ellipse described by the given centroid and covariance matrix, returning the coefficient of variation of the distance of vertices to the ellipse.
auto FractalDimension(dfloat length = 0) const -> dfloat
Computes the fractal dimension of a polygon.
auto BendingEnergy() const -> dfloat
Computes the bending energy of a polygon.
auto Simplify(dfloat tolerance = 0.5) -> Polygon&
Simplifies the polygon using the Douglas-Peucker algorithm.
auto Augment(dfloat distance = 1.0) -> Polygon&
Adds vertices along each edge of the polygon such that the distance between two consecutive vertices is never more than `distance`.
auto Smooth(dfloat sigma = 1.0) -> Polygon&
Locally averages the location of vertices of a polygon so it becomes smoother.
auto ConvexHull() const -> dip::ConvexHull
Returns the convex hull of the polygon.

## Public variables

Vertices vertices
The vertices.

## Function documentation

### dfloat dip::Polygon::EllipseVariance() const

Compares a polygon to the ellipse with the same covariance matrix, returning the coefficient of variation of the distance of vertices to the ellipse.

### dfloat dip::Polygon::FractalDimension(dfloat length = 0) const

Computes the fractal dimension of a polygon.

Fractal dimension is defined as the slope of the polygon length as a function of scale, in a log-log plot. Scale is obtained by smoothing the polygon using `dip::Polygon::Smooth`. Therefore, it is important that the polygon be densely sampled, use `dip::Polygon::Augment` if necessary.

`length` is the length of the polygon (see `dip::Polygon::Length`). It determines the range of scales used to compute the fractal dimension, so a rough estimate is sufficient. If zero is given as length (the default value), then it is computed.

### dfloat dip::Polygon::BendingEnergy() const

Computes the bending energy of a polygon.

The bending energy is the integral along the contour of the square of the curvature. We approximate curvature by, at each vertex, taking the difference in angle between the two edges, and dividing by half the length of the two edges (this is the portion of the boundary associated to the edge).

Note that this approximation is poor when the points are far apart. `dip::Polygon::Augment` should be used to obtain a densely sampled polygon. It is also beneficial to sufficiently smooth the polygon so it better approximates a smooth curve around the object being measured, see `dip::Polygon::Smooth`.

### Polygon& dip::Polygon::Simplify(dfloat tolerance = 0.5)

Simplifies the polygon using the Douglas-Peucker algorithm.

For a polygon derived from a chain code, setting tolerance to 0.5 leads to a maximum-length digital straight segment representation of the object.

### Polygon& dip::Polygon::Smooth(dfloat sigma = 1.0)

Locally averages the location of vertices of a polygon so it becomes smoother.

Uses a Gaussian filter with parameter `sigma`, which is not interpreted as a physical distance between vertices, but as a distance in number of vertices. That is, the neighboring vertex is at a distance of 1, the next one over at a distance of 2, etc. Therefore, it is important that vertices are approximately equally spaced. `dip::Polygon::Augment` modifies any polygon to satisfy that requirement.

A polygon derived from the chain code of an object without high curvature, when smoothed with a `sigma` of 2, will fairly well approximate the original smooth boundary. For objects with higher curvature (including very small objects), choose a smaller `sigma`.