What is @types/earcut?
@types/earcut provides TypeScript type definitions for the earcut library, which is a fast, portable, and robust polygon triangulation library. It is used to convert 2D polygons into triangles, which is a common requirement in computer graphics and game development.
What are @types/earcut's main functionalities?
Triangulate a simple polygon
This feature allows you to triangulate a simple polygon by providing an array of vertices. The output is an array of triangle indices.
const earcut = require('earcut');
const vertices = [10, 0, 0, 50, 60, 60, 70, 10];
const triangles = earcut(vertices);
console.log(triangles); // [1, 0, 3, 3, 2, 1]
Triangulate a polygon with holes
This feature allows you to triangulate a polygon with holes by providing an array of vertices and an array of hole indices. The output is an array of triangle indices.
const earcut = require('earcut');
const vertices = [0, 0, 50, 0, 50, 50, 0, 50, 10, 10, 40, 10, 40, 40, 10, 40];
const holeIndices = [4];
const triangles = earcut(vertices, holeIndices);
console.log(triangles); // [1, 0, 3, 3, 2, 1, 5, 4, 7, 7, 6, 5]
Triangulate a polygon with 3D coordinates
This feature allows you to triangulate a polygon with 3D coordinates by providing an array of vertices and specifying the dimension. The output is an array of triangle indices.
const earcut = require('earcut');
const vertices = [10, 0, 0, 0, 50, 0, 60, 60, 0, 70, 10, 0];
const dim = 3;
const triangles = earcut(vertices, null, dim);
console.log(triangles); // [1, 0, 3, 3, 2, 1]
Other packages similar to @types/earcut
delaunator
Delaunator is a fast library for Delaunay triangulation of 2D points. It is similar to earcut in that it performs triangulation, but it specifically focuses on Delaunay triangulation, which is useful for creating meshes from scattered points.
poly2tri
Poly2tri is a library for polygon triangulation that supports complex polygons with holes. It is similar to earcut in its ability to handle polygons with holes, but it uses a different algorithm (sweep line algorithm) for triangulation.
trianglify
Trianglify is a library for generating low-poly art and triangle meshes. While it also performs triangulation, its primary focus is on creating aesthetically pleasing patterns and designs, rather than general-purpose polygon triangulation.