UNPKG

4.81 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 | _688,671_ | _50,640_ | _61,501_ | _122,966_
31dude shape | 94 | _34,806_ | _10,339_ | _8,784_ | _11,172_
32holed dude shape | 104 | _19,553_ | _8,883_ | _7,494_ | _2,130_
33complex OSM water | 2523 | _537_ | _77.54_ | failure | failure
34huge OSM water | 5667 | _97.79_ | _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]]]);
45// [[0,50],[10,0],[70,10], [70,10],[60,60],[0,50]]
46```
47
48Input should be an array of rings, where the first is outer ring and others are holes;
49each ring is an array of points, where each point is of the `[x, y]` or `[x, y, z]` form.
50
51Each group of three points in the resulting array forms a triangle.
52
53Alternatively, you can get triangulation results in the form of flat index and vertex arrays
54by passing `true` as a second argument to `earcut`
55(convenient for uploading results directly to WebGL as buffers):
56
57```js
58var triangles = earcut([[[10,0],[0,50],[60,60],[70,10]]], true);
59// {vertices: [0,50, 10,0, 70,10, 60,60], indices: [1,0,2, 3,2,1]}
60```
61
62#### Install
63
64NPM and Browserify:
65
66```bash
67npm install earcut
68```
69
70Browser builds:
71
72```bash
73npm install
74npm run build-dev # builds dist/earcut.dev.js, a dev version with a source map
75npm run build-min # builds dist/earcut.min.js, a minified production build
76```
77
78Running tests:
79
80```bash
81npm test
82```
83
84![](https://cloud.githubusercontent.com/assets/25395/5778431/e8ec0c10-9da3-11e4-8d4e-a2ced6a7d2b7.png)
85
86#### Ports to other languages
87
88- [mapbox/earcut.hpp](https://github.com/mapbox/earcut.hpp) (C++11)
89- [Cawfree/earcut-j](https://github.com/Cawfree/earcut-j) (Java)
90
91#### Changelog
92
93##### 1.4.2 (Mar 18, 2015)
94
95- Fixed another rare edge case with a tiny hole in a huge polygon.
96
97##### 1.4.1 (Mar 17, 2015)
98
99- Fixed a rare edge case that led to incomplete triangulation.
100
101##### 1.4.0 (Mar 9, 2015)
102
103- Fixed indexed output to produce indices not multiplied by dimension and work with any number of dimensions.
104
105##### 1.3.0 (Feb 24, 2015)
106
107- Added a second argument to `earcut` that switches output format to flat vertex and index arrays if set to `true`.
108
109##### 1.2.3 (Feb 10, 2015)
110
111- Improved performance (especially on recent v8) by avoiding `Array` `push` with multiple arguments.
112
113##### 1.2.2 (Jan 27, 2015)
114
115- Significantly improved performance for polygons with self-intersections
116 (e.g. big OSM water polygons are now handled 2-3x faster)
117
118##### 1.2.1 (Jan 26, 2015)
119
120- Significantly improved performance on polygons with high number of vertices
121 by using z-order curve hashing for vertice lookup.
122- Slightly improved overall performance with better point filtering.
123
124##### 1.1.0 (Jan 21, 2015)
125
126- Improved performance on polygons with holes by switching from Held to Eberly hole elimination algorithm
127- More robustness fixes and tests
128
129##### 1.0.1 — 1.0.6 (Jan 20, 2015)
130
131- Various robustness improvements and fixes.
132
133##### 1.0.0 (Jan 18, 2015)
134
135- Initial release.