UNPKG

5.44 kBMarkdownView Raw
1## Earcut
2
3The fastest and smallest JavaScript polygon triangulation library. 2.5KB gzipped.
4
5[![Build Status](https://travis-ci.org/mapbox/earcut.svg?branch=master)](https://travis-ci.org/mapbox/earcut)
6[![Coverage Status](https://coveralls.io/repos/mapbox/earcut/badge.svg?branch=master)](https://coveralls.io/r/mapbox/earcut?branch=master)
7
8#### The algorithm
9
10The library implements a modified ear slicing algorithm,
11optimized by [z-order curve](http://en.wikipedia.org/wiki/Z-order_curve) hashing
12and extended to handle holes, twisted polygons, degeneracies and self-intersections
13in a way that doesn't _guarantee_ correctness of triangulation,
14but attempts to always produce acceptable results for practical data.
15
16It's based on ideas from
17[FIST: Fast Industrial-Strength Triangulation of Polygons](http://www.cosy.sbg.ac.at/~held/projects/triang/triang.html) by Martin Held
18and [Triangulation by Ear Clipping](http://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf) by David Eberly.
19
20#### Why another triangulation library?
21
22The aim of this project is to create a JS triangulation library
23that is **fast enough for real-time triangulation in the browser**,
24sacrificing triangulation quality for raw speed and simplicity,
25while being robust enough to handle most practical datasets without crashing or producing garbage.
26Some benchmarks using Node 0.12:
27
28(ops/sec) | pts | earcut | libtess | poly2tri | pnltri
29------------------| ---- | --------- | -------- | -------- | ---------
30OSM building | 15 | _648,935_ | _50,640_ | _61,501_ | _122,966_
31dude shape | 94 | _34,379_ | _10,339_ | _8,784_ | _11,172_
32holed dude shape | 104 | _26,849_ | _8,883_ | _7,494_ | _2,130_
33complex OSM water | 2523 | _564_ | _77.54_ | failure | failure
34huge OSM water | 5667 | _116_ | _29.30_ | failure | failure
35
36The original use case it was created for is [Mapbox GL](https://www.mapbox.com/mapbox-gl), WebGL-based interactive maps.
37
38If you want to get correct triangulation even on very bad data with lots of self-intersections
39and earcut is not precise enough, take a look at [libtess.js](https://github.com/brendankenny/libtess.js).
40
41#### Usage
42
43```js
44var triangles = earcut([10,0, 0,50, 60,60, 70,10]); // returns [1,0,3, 3,2,1]
45```
46
47Signature: `earcut(coords[, holeIndices, numDimensions = 2])`.
48
49* `coords` is a flat array of vertice coordinates like `[x0,y0, x1,y1, x2,y2, ...]`.
50* `holeIndices` is an array of hole indices if any
51 (e.g. `[5, 8]` for a 12-vertice input would mean one hole with vertices 5–7 and another with 8–11).
52* `numDimensions` is the number of coordinates per vertice in the input array (`2` by default).
53
54Each group of three vertice indices in the resulting array forms a triangle.
55
56```js
57// triangulating a polygon with a hole
58earcut([0,0, 100,0, 100,100, 0,100, 20,20, 80,20, 80,80, 20,80], [4]);
59// [3,0,4, 5,4,0, 3,4,7, 5,0,1, 2,3,7, 6,5,1, 2,7,6, 6,1,2]
60
61// triangulating a polygon with 3d coords
62earcut([10,0,1, 0,50,2, 60,60,3, 70,10,4], null, 3);
63// [1,0,3, 3,2,1]
64```
65
66If your input is a multi-dimensional array (e.g. [GeoJSON Polygon](http://geojson.org/geojson-spec.html#polygon)),
67you can convert it to the format expected by Earcut with [a couple lines of codes](viz/viz.js#L99-L115).
68
69#### Install
70
71NPM and Browserify:
72
73```bash
74npm install earcut
75```
76
77Browser builds:
78
79```bash
80npm install
81npm run build-dev # builds dist/earcut.dev.js, a dev version with a source map
82npm run build-min # builds dist/earcut.min.js, a minified production build
83```
84
85Running tests:
86
87```bash
88npm test
89```
90
91![](https://cloud.githubusercontent.com/assets/25395/5778431/e8ec0c10-9da3-11e4-8d4e-a2ced6a7d2b7.png)
92
93#### Ports to other languages
94
95- [mapbox/earcut.hpp](https://github.com/mapbox/earcut.hpp) (C++11)
96- [Cawfree/earcut-j](https://github.com/Cawfree/earcut-j) (Java)
97
98#### Changelog
99
100##### 2.0.0 (Apr 30, 2015)
101
102- **Breaking**: changed the API to accept a flat input array of vertices with hole indices and return triangle indices.
103 It makes the indexed output much faster than it was before (up to 30%) and improves memory footprint.
104
105##### 1.4.2 (Mar 18, 2015)
106
107- Fixed another rare edge case with a tiny hole in a huge polygon.
108
109##### 1.4.1 (Mar 17, 2015)
110
111- Fixed a rare edge case that led to incomplete triangulation.
112
113##### 1.4.0 (Mar 9, 2015)
114
115- Fixed indexed output to produce indices not multiplied by dimension and work with any number of dimensions.
116
117##### 1.3.0 (Feb 24, 2015)
118
119- Added a second argument to `earcut` that switches output format to flat vertex and index arrays if set to `true`.
120
121##### 1.2.3 (Feb 10, 2015)
122
123- Improved performance (especially on recent v8) by avoiding `Array` `push` with multiple arguments.
124
125##### 1.2.2 (Jan 27, 2015)
126
127- Significantly improved performance for polygons with self-intersections
128 (e.g. big OSM water polygons are now handled 2-3x faster)
129
130##### 1.2.1 (Jan 26, 2015)
131
132- Significantly improved performance on polygons with high number of vertices
133 by using z-order curve hashing for vertice lookup.
134- Slightly improved overall performance with better point filtering.
135
136##### 1.1.0 (Jan 21, 2015)
137
138- Improved performance on polygons with holes by switching from Held to Eberly hole elimination algorithm
139- More robustness fixes and tests
140
141##### 1.0.1 — 1.0.6 (Jan 20, 2015)
142
143- Various robustness improvements and fixes.
144
145##### 1.0.0 (Jan 18, 2015)
146
147- Initial release.