Class EarClipping
Triangulates a simple 2D polygon by ear clipping.
public static class EarClipping
- Inheritance
-
EarClipping
Remarks
Ear clipping repeatedly finds a corner whose triangle lies entirely inside the polygon (an "ear"), emits that triangle, and removes the corner - always terminating with n-2 triangles for an n-vertex polygon. It handles any simple polygon, convex or concave, which is what the extruded letter glyphs need: X, Y and Z are all concave.
It does not handle self-intersecting polygons, and it does not handle holes directly - a glyph with a hole, such as O or 8, is authored as a single outline with a bridge cut connecting the hole to the outside.
Methods
SignedArea(IReadOnlyList<Vector2>)
Returns the polygon's signed area: positive for counter-clockwise winding.
public static float SignedArea(IReadOnlyList<Vector2> polygon)
Parameters
polygonIReadOnlyList<Vector2>The polygon's corners, in order.
Returns
- float
The signed area, by the shoelace formula.
Triangulate(IReadOnlyList<Vector2>)
Triangulates a simple polygon into a list of vertex-index triples.
public static List<int> Triangulate(IReadOnlyList<Vector2> polygon)
Parameters
polygonIReadOnlyList<Vector2>The polygon's corners, in order, without repeating the first corner at the end. Either winding is accepted.
Returns
- List<int>
Indices into
polygon, three per triangle, (corner count - 2) triangles in total. Triangles are always wound counter-clockwise, whichever way the input was wound.
Exceptions
- ArgumentNullException
The polygon is null.
- ArgumentException
The polygon has fewer than three corners.
- InvalidOperationException
No ear could be found, which means the polygon self-intersects.