tree: f7a682dcec5d19a8ee24ed4c6e82e317fc0b5707 [path history] [tgz]
  1. delaunator.js
  2. delaunator.min.js
  3. index.js
  4. LICENSE
  5. package.json
  6. README.md
generate-reference-triangles/node_modules/delaunator/README.md

delaunator

A really fast JavaScript library for Delaunay triangulation of 2D points.

Build Status

Example

const points = [[168, 180], [168, 178], [168, 179], [168, 181], [168, 183], ...];

const delaunay = Delaunator.from(points);
console.log(delaunay.triangles);
// [623, 636, 619,  636, 444, 619, ...]

Install

Install with NPM (npm install delaunator) or Yarn (yarn add delaunator), then:

// import as an ES module
import Delaunator from 'delaunator';

// or require in Node / Browserify
const Delaunator = require('delaunator');

Or use a browser build directly:

<script src="https://unpkg.com/delaunator@2.0.1/delaunator.min.js"></script> <!-- minified build -->
<script src="https://unpkg.com/delaunator@2.0.1/delaunator.js"></script> <!-- dev build -->

API Reference

Delaunator.from(points[, getX, getY])

Constructs a delaunay triangulation object given an array of points ([x, y] by default). getX and getY are optional functions of the form (point) => value for custom point formats. Duplicate points are skipped.

new Delaunator(coords)

Constructs a delaunay triangulation object given a typed array of point coordinates of the form: [x0, y0, x1, y1, ...].

delaunay.triangles

A flat Int32Array array of triangle vertex indices (each group of three numbers forms a triangle). All triangles are directed counterclockwise.

To get the coordinates of all triangles, use:

for (let i = 0; i < triangles.length; i += 3) {
    coordinates.push([
        points[triangles[i]],
        points[triangles[i + 1]],
        points[triangles[i + 2]]
    ]);
}

delaunay.halfedges

A flat Int32Array array of triangle half-edge indices that allows you to traverse the triangulation. i-th half-edge in the array corresponds to vertex triangles[i] the half-edge is coming from. halfedges[i] is the index of a twin half-edge in an adjacent triangle (or -1 for outer half-edges on the convex hull).

The flat array-based data structures might be counterintuitive, but they're one of the key reasons this library is fast.

Performance

Benchmark results against four fastest other libraries (npm run bench on Macbook Pro Retina 15" 2017, Node v8.10.0):

library10,00020,00050,000100,000200,000500,0001,000,000
delaunator31ms17ms59ms125ms232ms613ms1.35s
faster-delaunay48ms88ms243ms447ms1.02s2.72s4.95s
incremental-delaunay56ms131ms309ms577ms1.12s3.01s6.37s
d3-voronoi132ms220ms483ms1s2.27s6.3s12.67s
delaunay-fast150ms343ms1.19s3.35s10.09s41.09s117.53s

Papers

The algorithm is based on ideas from the following papers: