Skip to main content

triangulate_polygon

Function triangulate_polygon 

Source
pub fn triangulate_polygon(coords: &[GeoCoord]) -> Vec<u32>
Expand description

Triangulate a simple polygon (no holes) into a list of triangle indices.

Uses ear-clipping which handles both convex and concave polygons correctly. For polygons with holes, use triangulate_polygon_with_holes instead.

§Returns

Indices into the input coords slice, grouped in triples.

§Closing vertex

If the last coordinate duplicates the first (within 1e-12 degrees), it is treated as a ring-closing sentinel and excluded from triangulation. This matches the GeoJSON convention where polygon rings repeat the first vertex.

§Edge cases

InputResult
Fewer than 3 coordinatesempty Vec
Exactly 3 unique coordinates1 triangle (3 indices)
Closing duplicate reducing count below 3empty Vec

§Example

use rustial_engine::triangulate_polygon;
use rustial_engine::GeoCoord;

let square = vec![
    GeoCoord::from_lat_lon(0.0, 0.0),
    GeoCoord::from_lat_lon(0.0, 1.0),
    GeoCoord::from_lat_lon(1.0, 1.0),
    GeoCoord::from_lat_lon(1.0, 0.0),
];
let indices = triangulate_polygon(&square);
assert_eq!(indices.len(), 6); // 2 triangles