earcut.zig
June 20, 2026 · View on GitHub
Zig port of mapbox/earcut — a fast polygon triangulation algorithm. Takes a flat vertex array and returns triangle indices. Supports concave polygons and holes.
Install
zig fetch --save https://github.com/tenyoru/earcut.zig/archive/refs/heads/main.tar.gz
build.zig:
const earcut = b.dependency("earcut", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("earcut", earcut.module("earcut"));
Usage
const earcut = @import("earcut");
const indices = try earcut.earcut(allocator, &vertices, null, 2);
defer allocator.free(indices);
// with holes:
const indices = try earcut.earcut(allocator, &vertices, &hole_indices, 2);
defer allocator.free(indices);
WASM
Use std.heap.wasm_allocator — no other changes needed:
const indices = try earcut.earcut(std.heap.wasm_allocator, &vertices, null, 2);
Credits
Algorithm by Mapbox (ISC License).