1 | ## Earcut
|
2 |
|
3 | The 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 |
|
10 | The library implements a modified ear slicing algorithm,
|
11 | optimized by [z-order curve](http://en.wikipedia.org/wiki/Z-order_curve) hashing
|
12 | and extended to handle holes, twisted polygons, degeneracies and self-intersections
|
13 | in a way that doesn't _guarantee_ correctness of triangulation,
|
14 | but attempts to always produce acceptable results for practical data.
|
15 |
|
16 | It'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
|
18 | and [Triangulation by Ear Clipping](http://www.geometrictools.com/Documentation/TriangulationByEarClipping.pdf) by David Eberly.
|
19 |
|
20 | #### Why another triangulation library?
|
21 |
|
22 | The aim of this project is to create a JS triangulation library
|
23 | that is **fast enough for real-time triangulation in the browser**,
|
24 | sacrificing triangulation quality for raw speed and simplicity,
|
25 | while being robust enough to handle most practical datasets without crashing or producing garbage.
|
26 | Some benchmarks using Node 0.12:
|
27 |
|
28 | (ops/sec) | pts | earcut | libtess | poly2tri | pnltri
|
29 | ------------------| ---- | --------- | -------- | -------- | ---------
|
30 | OSM building | 15 | _688,671_ | _50,640_ | _61,501_ | _122,966_
|
31 | dude shape | 94 | _34,806_ | _10,339_ | _8,784_ | _11,172_
|
32 | holed dude shape | 104 | _19,553_ | _8,883_ | _7,494_ | _2,130_
|
33 | complex OSM water | 2523 | _537_ | _77.54_ | failure | failure
|
34 | huge OSM water | 5667 | _97.79_ | _29.30_ | failure | failure
|
35 |
|
36 | The original use case it was created for is [Mapbox GL](https://www.mapbox.com/mapbox-gl), WebGL-based interactive maps.
|
37 |
|
38 | If you want to get correct triangulation even on very bad data with lots of self-intersections
|
39 | and earcut is not precise enough, take a look at [libtess.js](https://github.com/brendankenny/libtess.js).
|
40 |
|
41 | #### Usage
|
42 |
|
43 | ```js
|
44 | var 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 |
|
48 | Input should be an array of rings, where the first is outer ring and others are holes;
|
49 | each ring is an array of points, where each point is of the `[x, y]` or `[x, y, z]` form.
|
50 |
|
51 | Each group of three points in the resulting array forms a triangle.
|
52 |
|
53 | Alternatively, you can get triangulation results in the form of flat index and vertex arrays
|
54 | by passing `true` as a second argument to `earcut`
|
55 | (convenient for uploading results directly to WebGL as buffers):
|
56 |
|
57 | ```js
|
58 | var 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 |
|
64 | NPM and Browserify:
|
65 |
|
66 | ```bash
|
67 | npm install earcut
|
68 | ```
|
69 |
|
70 | Browser builds:
|
71 |
|
72 | ```bash
|
73 | npm install
|
74 | npm run build-dev # builds dist/earcut.dev.js, a dev version with a source map
|
75 | npm run build-min # builds dist/earcut.min.js, a minified production build
|
76 | ```
|
77 |
|
78 | Running tests:
|
79 |
|
80 | ```bash
|
81 | npm 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.
|