| 1 | # Delaunator [](https://app.travis-ci.com/mapbox/delaunator) [](https://github.com/mourner/projects) [](https://unpkg.com/delaunator)
|
|---|
| 2 |
|
|---|
| 3 | An incredibly fast and robust JavaScript library for
|
|---|
| 4 | [Delaunay triangulation](https://en.wikipedia.org/wiki/Delaunay_triangulation) of 2D points.
|
|---|
| 5 |
|
|---|
| 6 | - [Interactive Demo](https://mapbox.github.io/delaunator/demo.html)
|
|---|
| 7 | - [Guide to data structures](https://mapbox.github.io/delaunator/)
|
|---|
| 8 |
|
|---|
| 9 | <img src="delaunator.png" alt="Delaunay triangulation example" width="600" />
|
|---|
| 10 |
|
|---|
| 11 | ### Projects based on Delaunator
|
|---|
| 12 |
|
|---|
| 13 | - [d3-delaunay](https://github.com/d3/d3-delaunay) for Voronoi diagrams, search, traversal and rendering (a part of [D3](https://d3js.org)).
|
|---|
| 14 | - [d3-geo-voronoi](https://github.com/Fil/d3-geo-voronoi) for Delaunay triangulations and Voronoi diagrams on a sphere (e.g. for geographic locations).
|
|---|
| 15 |
|
|---|
| 16 | ## Example
|
|---|
| 17 |
|
|---|
| 18 | ```js
|
|---|
| 19 | const coords = [168,180, 168,178, 168,179, 168,181, 168,183, ...];
|
|---|
| 20 |
|
|---|
| 21 | const delaunay = new Delaunator(coords);
|
|---|
| 22 | console.log(delaunay.triangles);
|
|---|
| 23 | // [623, 636, 619, 636, 444, 619, ...]
|
|---|
| 24 | ```
|
|---|
| 25 |
|
|---|
| 26 | ## Install
|
|---|
| 27 |
|
|---|
| 28 | Install with NPM (`npm install delaunator`) or Yarn (`yarn add delaunator`), then import as an ES module:
|
|---|
| 29 |
|
|---|
| 30 | ```js
|
|---|
| 31 | import Delaunator from 'delaunator';
|
|---|
| 32 | ```
|
|---|
| 33 |
|
|---|
| 34 | To use as a module in a browser:
|
|---|
| 35 |
|
|---|
| 36 | ```html
|
|---|
| 37 | <script type="module">
|
|---|
| 38 | import Delaunator from 'https://cdn.skypack.dev/delaunator@5.0.0';
|
|---|
| 39 | </script>
|
|---|
| 40 | ```
|
|---|
| 41 |
|
|---|
| 42 | Or use a browser UMD build that exposes a `Delaunator` global variable:
|
|---|
| 43 |
|
|---|
| 44 | ```html
|
|---|
| 45 | <script src="https://unpkg.com/delaunator@5.0.0/delaunator.min.js"></script>
|
|---|
| 46 | ```
|
|---|
| 47 |
|
|---|
| 48 | ## API Reference
|
|---|
| 49 |
|
|---|
| 50 | #### new Delaunator(coords)
|
|---|
| 51 |
|
|---|
| 52 | Constructs a delaunay triangulation object given an array of point coordinates of the form:
|
|---|
| 53 | `[x0, y0, x1, y1, ...]` (use a typed array for best performance).
|
|---|
| 54 |
|
|---|
| 55 | #### Delaunator.from(points[, getX, getY])
|
|---|
| 56 |
|
|---|
| 57 | Constructs a delaunay triangulation object given an array of points (`[x, y]` by default).
|
|---|
| 58 | `getX` and `getY` are optional functions of the form `(point) => value` for custom point formats.
|
|---|
| 59 | Duplicate points are skipped.
|
|---|
| 60 |
|
|---|
| 61 | #### delaunay.triangles
|
|---|
| 62 |
|
|---|
| 63 | A `Uint32Array` array of triangle vertex indices (each group of three numbers forms a triangle).
|
|---|
| 64 | All triangles are directed counterclockwise.
|
|---|
| 65 |
|
|---|
| 66 | To get the coordinates of all triangles, use:
|
|---|
| 67 |
|
|---|
| 68 | ```js
|
|---|
| 69 | for (let i = 0; i < triangles.length; i += 3) {
|
|---|
| 70 | coordinates.push([
|
|---|
| 71 | points[triangles[i]],
|
|---|
| 72 | points[triangles[i + 1]],
|
|---|
| 73 | points[triangles[i + 2]]
|
|---|
| 74 | ]);
|
|---|
| 75 | }
|
|---|
| 76 | ```
|
|---|
| 77 |
|
|---|
| 78 | #### delaunay.halfedges
|
|---|
| 79 |
|
|---|
| 80 | A `Int32Array` array of triangle half-edge indices that allows you to traverse the triangulation.
|
|---|
| 81 | `i`-th half-edge in the array corresponds to vertex `triangles[i]` the half-edge is coming from.
|
|---|
| 82 | `halfedges[i]` is the index of a twin half-edge in an adjacent triangle
|
|---|
| 83 | (or `-1` for outer half-edges on the convex hull).
|
|---|
| 84 |
|
|---|
| 85 | The flat array-based data structures might be counterintuitive,
|
|---|
| 86 | but they're one of the key reasons this library is fast.
|
|---|
| 87 |
|
|---|
| 88 | #### delaunay.hull
|
|---|
| 89 |
|
|---|
| 90 | A `Uint32Array` array of indices that reference points on the convex hull of the input data, counter-clockwise.
|
|---|
| 91 |
|
|---|
| 92 | #### delaunay.coords
|
|---|
| 93 |
|
|---|
| 94 | An array of input coordinates in the form `[x0, y0, x1, y1, ....]`,
|
|---|
| 95 | of the type provided in the constructor (or `Float64Array` if you used `Delaunator.from`).
|
|---|
| 96 |
|
|---|
| 97 | #### delaunay.update()
|
|---|
| 98 |
|
|---|
| 99 | Updates the triangulation if you modified `delaunay.coords` values in place, avoiding expensive memory allocations.
|
|---|
| 100 | Useful for iterative relaxation algorithms such as [Lloyd's](https://en.wikipedia.org/wiki/Lloyd%27s_algorithm).
|
|---|
| 101 |
|
|---|
| 102 | ## Performance
|
|---|
| 103 |
|
|---|
| 104 | Benchmark results against other Delaunay JS libraries
|
|---|
| 105 | (`npm run bench` on Macbook Pro Retina 15" 2017, Node v10.10.0):
|
|---|
| 106 |
|
|---|
| 107 | | uniform 100k | gauss 100k | grid 100k | degen 100k | uniform 1 million | gauss 1 million | grid 1 million | degen 1 million
|
|---|
| 108 | :-- | --: | --: | --: | --: | --: | --: | --: | --:
|
|---|
| 109 | **delaunator** | 82ms | 61ms | 66ms | 25ms | 1.07s | 950ms | 830ms | 278ms
|
|---|
| 110 | [faster‑delaunay](https://github.com/Bathlamos/delaunay-triangulation) | 473ms | 411ms | 272ms | 68ms | 4.27s | 4.62s | 4.3s | 810ms
|
|---|
| 111 | [incremental‑delaunay](https://github.com/mikolalysenko/incremental-delaunay) | 547ms | 505ms | 172ms | 528ms | 5.9s | 6.08s | 2.11s | 6.09s
|
|---|
| 112 | [d3‑voronoi](https://github.com/d3/d3-voronoi) | 972ms | 909ms | 358ms | 720ms | 15.04s | 13.86s | 5.55s | 11.13s
|
|---|
| 113 | [delaunay‑fast](https://github.com/ironwallaby/delaunay) | 3.8s | 4s | 12.57s | timeout | 132s | 138s | 399s | timeout
|
|---|
| 114 | [delaunay](https://github.com/darkskyapp/delaunay) | 4.85s | 5.73s | 15.05s | timeout | 156s | 178s | 326s | timeout
|
|---|
| 115 | [delaunay‑triangulate](https://github.com/mikolalysenko/delaunay-triangulate) | 2.24s | 2.04s | OOM | 1.51s | OOM | OOM | OOM | OOM
|
|---|
| 116 | [cdt2d](https://github.com/mikolalysenko/cdt2d) | 45s | 51s | 118s | 17s | timeout | timeout | timeout | timeout
|
|---|
| 117 |
|
|---|
| 118 | ## Papers
|
|---|
| 119 |
|
|---|
| 120 | The algorithm is based on ideas from the following papers:
|
|---|
| 121 |
|
|---|
| 122 | - [A simple sweep-line Delaunay triangulation algorithm](http://www.academicpub.org/jao/paperInfo.aspx?paperid=15630), 2013, Liu Yonghe, Feng Jinming and Shao Yuehong
|
|---|
| 123 | - [S-hull: a fast radial sweep-hull routine for Delaunay triangulation](http://www.s-hull.org/paper/s_hull.pdf), 2010, David Sinclair
|
|---|
| 124 | - [A faster circle-sweep Delaunay triangulation algorithm](http://cglab.ca/~biniaz/papers/Sweep%20Circle.pdf), 2011, Ahmad Biniaz and Gholamhossein Dastghaibyfard
|
|---|
| 125 |
|
|---|
| 126 | ## Robustness
|
|---|
| 127 |
|
|---|
| 128 | Delaunator should produce valid output even on highly degenerate input. It does so by depending on [robust-predicates](https://github.com/mourner/robust-predicates), a modern port of Jonathan Shewchuk's robust geometric predicates, an industry standard in computational geometry.
|
|---|
| 129 |
|
|---|
| 130 | ## Ports to other languages
|
|---|
| 131 |
|
|---|
| 132 | - [delaunator-rs](https://github.com/mourner/delaunator-rs) (Rust)
|
|---|
| 133 | - [fogleman/delaunay](https://github.com/fogleman/delaunay) (Go)
|
|---|
| 134 | - [delaunator-cpp](https://github.com/abellgithub/delaunator-cpp) (C++)
|
|---|
| 135 | - [delaunator-sharp](https://github.com/nol1fe/delaunator-sharp) (C#)
|
|---|
| 136 | - [delaunator-ruby](https://github.com/hendrixfan/delaunator-ruby) (Ruby)
|
|---|
| 137 | - [Delaunator-Python](https://github.com/HakanSeven12/Delaunator-Python) (Python)
|
|---|
| 138 | - [ricardomatias/delaunator](https://github.com/ricardomatias/delaunator) (Kotlin)
|
|---|
| 139 | - [delaunator-java](https://github.com/waveware4ai/delaunator-java) (Java)
|
|---|
| 140 | - [delaunay-Stata](https://github.com/asjadnaqvi/stata-delaunay-voronoi) (Stata/Mata)
|
|---|
| 141 | - [Delaunator.jl](https://github.com/JuliaGeometry/Delaunator.jl) (Julia)
|
|---|