Triangulate

ceramic.Triangulate (Class)

Utility class for triangulating polygons into triangles.

Triangulate converts complex polygons (defined by vertices) into a set of triangles by generating appropriate indices. This is essential for rendering filled shapes on the GPU, which typically only supports triangle primitives.

The triangulation uses the ear-clipping algorithm, which:

  • Works with both convex and concave polygons
  • Handles clockwise and counter-clockwise winding
  • Produces a valid triangulation for simple polygons (no self-intersections)

Common uses:

  • Converting Shape paths to renderable triangles
  • Filling complex polygons
  • Creating meshes from outline data
  • Processing vector graphics
// Triangulate a square
var vertices = [
    0, 0,    // Top-left
    100, 0,  // Top-right
    100, 100, // Bottom-right
    0, 100   // Bottom-left
];
var indices = [];
Triangulate.triangulate(vertices, indices);
// indices now contains [0, 1, 2, 0, 2, 3]
See: Shape For automatic triangulation of visual shapes, EarClippingTriangulator The underlying triangulation implementation

Static Members

triangulate(vertices: Array<Float>, indices: Array<Int>): Void

Triangulates a polygon defined by vertices and fills the indices array.

Takes a list of 2D vertices (as x,y pairs) and generates triangle indices that define how to connect those vertices into triangles. The polygon should be simple (no self-intersections) for best results.

Name Type Description
vertices Array<Float> Array of vertex coordinates as [x0,y0, x1,y1, x2,y2, ...] Must contain at least 6 values (3 vertices).
indices Array<Int> Output array to fill with triangle indices. Will be cleared before adding new indices. Result length will be 3 × (numVertices - 2). * haxe var vertices = [0,0, 100,0, 50,100]; // Triangle var indices = []; Triangulate.triangulate(vertices, indices); // indices = [0, 1, 2]

Private Members

tmpVertices: Array<Float>

tmpIndices: Array<Int>