jmgraph 3.2.16 → 3.2.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +251 -428
  3. package/build/gulpfile.js +142 -142
  4. package/build/package-lock.json +10666 -0
  5. package/build/package.json +71 -71
  6. package/dev.js +9 -9
  7. package/dist/jmgraph.core.min.js +1 -1
  8. package/dist/jmgraph.core.min.js.map +1 -1
  9. package/dist/jmgraph.js +3500 -2668
  10. package/dist/jmgraph.min.js +1 -1
  11. package/example/ball.html +216 -216
  12. package/example/base.html +111 -111
  13. package/example/canvas.html +53 -53
  14. package/example/cell.html +283 -283
  15. package/example/controls/arc.html +128 -128
  16. package/example/controls/arrowline.html +77 -77
  17. package/example/controls/bezier.html +298 -298
  18. package/example/controls/img.html +96 -96
  19. package/example/controls/label.html +86 -86
  20. package/example/controls/line.html +172 -172
  21. package/example/controls/prismatic.html +62 -62
  22. package/example/controls/rect.html +63 -63
  23. package/example/controls/resize.html +111 -111
  24. package/example/controls/test.html +359 -359
  25. package/example/es.html +69 -69
  26. package/example/es5module.html +62 -63
  27. package/example/heartarc.html +115 -115
  28. package/example/index.html +46 -46
  29. package/example/js/require.js +4 -4
  30. package/example/love/img/bling/bling.tps +265 -265
  31. package/example/love/img/bling.json +87 -87
  32. package/example/love/img/bling.tps +295 -295
  33. package/example/love/img/love.json +95 -95
  34. package/example/love/img/love.tps +315 -315
  35. package/example/love/img/qq/qq.tps +399 -399
  36. package/example/love/img/qq.json +242 -242
  37. package/example/love/index.html +40 -40
  38. package/example/love/js/game.js +558 -558
  39. package/example/music.html +210 -210
  40. package/example/node/test.js +137 -137
  41. package/example/pdf.html +186 -186
  42. package/example/progress.html +172 -172
  43. package/example/pso.html +147 -147
  44. package/example/sort.html +804 -815
  45. package/example/tweenjs.html +83 -83
  46. package/example/webgl.html +278 -278
  47. package/example/xfj/index.html +331 -331
  48. package/example/xfj/shake.js +48 -48
  49. package/example/xfj/testori.html +75 -75
  50. package/index.js +99 -99
  51. package/package.json +58 -56
  52. package/src/core/jmControl.js +1376 -1531
  53. package/src/core/jmEvents.js +240 -281
  54. package/src/core/jmGradient.js +231 -231
  55. package/src/core/jmGraph.js +569 -569
  56. package/src/core/jmList.js +92 -157
  57. package/src/core/jmObject.js +83 -103
  58. package/src/core/jmPath.js +35 -35
  59. package/src/core/jmProperty.js +71 -110
  60. package/src/core/jmShadow.js +65 -65
  61. package/src/core/jmUtils.js +906 -919
  62. package/src/lib/earcut.js +680 -680
  63. package/src/lib/earcut.md +73 -73
  64. package/src/lib/webgl/base.js +522 -452
  65. package/src/lib/webgl/core/buffer.js +48 -48
  66. package/src/lib/webgl/core/mapSize.js +40 -40
  67. package/src/lib/webgl/core/mapType.js +43 -43
  68. package/src/lib/webgl/core/program.js +138 -138
  69. package/src/lib/webgl/core/shader.js +13 -13
  70. package/src/lib/webgl/core/texture.js +60 -60
  71. package/src/lib/webgl/gradient.js +168 -168
  72. package/src/lib/webgl/index.js +137 -11
  73. package/src/lib/webgl/path.js +568 -561
  74. package/src/shapes/jmArrowLine.js +36 -36
  75. package/src/shapes/jmImage.js +244 -244
  76. package/src/shapes/jmLabel.js +271 -271
  77. package/src/shapes/jmResize.js +332 -330
package/src/lib/earcut.js CHANGED
@@ -1,680 +1,680 @@
1
- 'use strict';
2
-
3
- export default earcut;
4
-
5
- function earcut(data, holeIndices, dim) {
6
-
7
- dim = dim || 2;
8
-
9
- var hasHoles = holeIndices && holeIndices.length,
10
- outerLen = hasHoles ? holeIndices[0] * dim : data.length,
11
- outerNode = linkedList(data, 0, outerLen, dim, true),
12
- triangles = [];
13
-
14
- if (!outerNode || outerNode.next === outerNode.prev) return triangles;
15
-
16
- var minX, minY, maxX, maxY, x, y, invSize;
17
-
18
- if (hasHoles) outerNode = eliminateHoles(data, holeIndices, outerNode, dim);
19
-
20
- // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
21
- if (data.length > 80 * dim) {
22
- minX = maxX = data[0];
23
- minY = maxY = data[1];
24
-
25
- for (var i = dim; i < outerLen; i += dim) {
26
- x = data[i];
27
- y = data[i + 1];
28
- if (x < minX) minX = x;
29
- if (y < minY) minY = y;
30
- if (x > maxX) maxX = x;
31
- if (y > maxY) maxY = y;
32
- }
33
-
34
- // minX, minY and invSize are later used to transform coords into integers for z-order calculation
35
- invSize = Math.max(maxX - minX, maxY - minY);
36
- invSize = invSize !== 0 ? 32767 / invSize : 0;
37
- }
38
-
39
- earcutLinked(outerNode, triangles, dim, minX, minY, invSize, 0);
40
-
41
- return triangles;
42
- }
43
-
44
- // create a circular doubly linked list from polygon points in the specified winding order
45
- function linkedList(data, start, end, dim, clockwise) {
46
- var i, last;
47
-
48
- if (clockwise === (signedArea(data, start, end, dim) > 0)) {
49
- for (i = start; i < end; i += dim) last = insertNode(i, data[i], data[i + 1], last);
50
- } else {
51
- for (i = end - dim; i >= start; i -= dim) last = insertNode(i, data[i], data[i + 1], last);
52
- }
53
-
54
- if (last && equals(last, last.next)) {
55
- removeNode(last);
56
- last = last.next;
57
- }
58
-
59
- return last;
60
- }
61
-
62
- // eliminate colinear or duplicate points
63
- function filterPoints(start, end) {
64
- if (!start) return start;
65
- if (!end) end = start;
66
-
67
- var p = start,
68
- again;
69
- do {
70
- again = false;
71
-
72
- if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) {
73
- removeNode(p);
74
- p = end = p.prev;
75
- if (p === p.next) break;
76
- again = true;
77
-
78
- } else {
79
- p = p.next;
80
- }
81
- } while (again || p !== end);
82
-
83
- return end;
84
- }
85
-
86
- // main ear slicing loop which triangulates a polygon (given as a linked list)
87
- function earcutLinked(ear, triangles, dim, minX, minY, invSize, pass) {
88
- if (!ear) return;
89
-
90
- // interlink polygon nodes in z-order
91
- if (!pass && invSize) indexCurve(ear, minX, minY, invSize);
92
-
93
- var stop = ear,
94
- prev, next;
95
-
96
- // iterate through ears, slicing them one by one
97
- while (ear.prev !== ear.next) {
98
- prev = ear.prev;
99
- next = ear.next;
100
-
101
- if (invSize ? isEarHashed(ear, minX, minY, invSize) : isEar(ear)) {
102
- // cut off the triangle
103
- triangles.push(prev.i / dim | 0);
104
- triangles.push(ear.i / dim | 0);
105
- triangles.push(next.i / dim | 0);
106
-
107
- removeNode(ear);
108
-
109
- // skipping the next vertex leads to less sliver triangles
110
- ear = next.next;
111
- stop = next.next;
112
-
113
- continue;
114
- }
115
-
116
- ear = next;
117
-
118
- // if we looped through the whole remaining polygon and can't find any more ears
119
- if (ear === stop) {
120
- // try filtering points and slicing again
121
- if (!pass) {
122
- earcutLinked(filterPoints(ear), triangles, dim, minX, minY, invSize, 1);
123
-
124
- // if this didn't work, try curing all small self-intersections locally
125
- } else if (pass === 1) {
126
- ear = cureLocalIntersections(filterPoints(ear), triangles, dim);
127
- earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);
128
-
129
- // as a last resort, try splitting the remaining polygon into two
130
- } else if (pass === 2) {
131
- splitEarcut(ear, triangles, dim, minX, minY, invSize);
132
- }
133
-
134
- break;
135
- }
136
- }
137
- }
138
-
139
- // check whether a polygon node forms a valid ear with adjacent nodes
140
- function isEar(ear) {
141
- var a = ear.prev,
142
- b = ear,
143
- c = ear.next;
144
-
145
- if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
146
-
147
- // now make sure we don't have other points inside the potential ear
148
- var ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
149
-
150
- // triangle bbox; min & max are calculated like this for speed
151
- var x0 = ax < bx ? (ax < cx ? ax : cx) : (bx < cx ? bx : cx),
152
- y0 = ay < by ? (ay < cy ? ay : cy) : (by < cy ? by : cy),
153
- x1 = ax > bx ? (ax > cx ? ax : cx) : (bx > cx ? bx : cx),
154
- y1 = ay > by ? (ay > cy ? ay : cy) : (by > cy ? by : cy);
155
-
156
- var p = c.next;
157
- while (p !== a) {
158
- if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 &&
159
- pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) &&
160
- area(p.prev, p, p.next) >= 0) return false;
161
- p = p.next;
162
- }
163
-
164
- return true;
165
- }
166
-
167
- function isEarHashed(ear, minX, minY, invSize) {
168
- var a = ear.prev,
169
- b = ear,
170
- c = ear.next;
171
-
172
- if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
173
-
174
- var ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
175
-
176
- // triangle bbox; min & max are calculated like this for speed
177
- var x0 = ax < bx ? (ax < cx ? ax : cx) : (bx < cx ? bx : cx),
178
- y0 = ay < by ? (ay < cy ? ay : cy) : (by < cy ? by : cy),
179
- x1 = ax > bx ? (ax > cx ? ax : cx) : (bx > cx ? bx : cx),
180
- y1 = ay > by ? (ay > cy ? ay : cy) : (by > cy ? by : cy);
181
-
182
- // z-order range for the current triangle bbox;
183
- var minZ = zOrder(x0, y0, minX, minY, invSize),
184
- maxZ = zOrder(x1, y1, minX, minY, invSize);
185
-
186
- var p = ear.prevZ,
187
- n = ear.nextZ;
188
-
189
- // look for points inside the triangle in both directions
190
- while (p && p.z >= minZ && n && n.z <= maxZ) {
191
- if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c &&
192
- pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0) return false;
193
- p = p.prevZ;
194
-
195
- if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c &&
196
- pointInTriangle(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0) return false;
197
- n = n.nextZ;
198
- }
199
-
200
- // look for remaining points in decreasing z-order
201
- while (p && p.z >= minZ) {
202
- if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c &&
203
- pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0) return false;
204
- p = p.prevZ;
205
- }
206
-
207
- // look for remaining points in increasing z-order
208
- while (n && n.z <= maxZ) {
209
- if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c &&
210
- pointInTriangle(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0) return false;
211
- n = n.nextZ;
212
- }
213
-
214
- return true;
215
- }
216
-
217
- // go through all polygon nodes and cure small local self-intersections
218
- function cureLocalIntersections(start, triangles, dim) {
219
- var p = start;
220
- do {
221
- var a = p.prev,
222
- b = p.next.next;
223
-
224
- if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) {
225
-
226
- triangles.push(a.i / dim | 0);
227
- triangles.push(p.i / dim | 0);
228
- triangles.push(b.i / dim | 0);
229
-
230
- // remove two nodes involved
231
- removeNode(p);
232
- removeNode(p.next);
233
-
234
- p = start = b;
235
- }
236
- p = p.next;
237
- } while (p !== start);
238
-
239
- return filterPoints(p);
240
- }
241
-
242
- // try splitting polygon into two and triangulate them independently
243
- function splitEarcut(start, triangles, dim, minX, minY, invSize) {
244
- // look for a valid diagonal that divides the polygon into two
245
- var a = start;
246
- do {
247
- var b = a.next.next;
248
- while (b !== a.prev) {
249
- if (a.i !== b.i && isValidDiagonal(a, b)) {
250
- // split the polygon in two by the diagonal
251
- var c = splitPolygon(a, b);
252
-
253
- // filter colinear points around the cuts
254
- a = filterPoints(a, a.next);
255
- c = filterPoints(c, c.next);
256
-
257
- // run earcut on each half
258
- earcutLinked(a, triangles, dim, minX, minY, invSize, 0);
259
- earcutLinked(c, triangles, dim, minX, minY, invSize, 0);
260
- return;
261
- }
262
- b = b.next;
263
- }
264
- a = a.next;
265
- } while (a !== start);
266
- }
267
-
268
- // link every hole into the outer loop, producing a single-ring polygon without holes
269
- function eliminateHoles(data, holeIndices, outerNode, dim) {
270
- var queue = [],
271
- i, len, start, end, list;
272
-
273
- for (i = 0, len = holeIndices.length; i < len; i++) {
274
- start = holeIndices[i] * dim;
275
- end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
276
- list = linkedList(data, start, end, dim, false);
277
- if (list === list.next) list.steiner = true;
278
- queue.push(getLeftmost(list));
279
- }
280
-
281
- queue.sort(compareX);
282
-
283
- // process holes from left to right
284
- for (i = 0; i < queue.length; i++) {
285
- outerNode = eliminateHole(queue[i], outerNode);
286
- }
287
-
288
- return outerNode;
289
- }
290
-
291
- function compareX(a, b) {
292
- return a.x - b.x;
293
- }
294
-
295
- // find a bridge between vertices that connects hole with an outer ring and and link it
296
- function eliminateHole(hole, outerNode) {
297
- var bridge = findHoleBridge(hole, outerNode);
298
- if (!bridge) {
299
- return outerNode;
300
- }
301
-
302
- var bridgeReverse = splitPolygon(bridge, hole);
303
-
304
- // filter collinear points around the cuts
305
- filterPoints(bridgeReverse, bridgeReverse.next);
306
- return filterPoints(bridge, bridge.next);
307
- }
308
-
309
- // David Eberly's algorithm for finding a bridge between hole and outer polygon
310
- function findHoleBridge(hole, outerNode) {
311
- var p = outerNode,
312
- hx = hole.x,
313
- hy = hole.y,
314
- qx = -Infinity,
315
- m;
316
-
317
- // find a segment intersected by a ray from the hole's leftmost point to the left;
318
- // segment's endpoint with lesser x will be potential connection point
319
- do {
320
- if (hy <= p.y && hy >= p.next.y && p.next.y !== p.y) {
321
- var x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y);
322
- if (x <= hx && x > qx) {
323
- qx = x;
324
- m = p.x < p.next.x ? p : p.next;
325
- if (x === hx) return m; // hole touches outer segment; pick leftmost endpoint
326
- }
327
- }
328
- p = p.next;
329
- } while (p !== outerNode);
330
-
331
- if (!m) return null;
332
-
333
- // look for points inside the triangle of hole point, segment intersection and endpoint;
334
- // if there are no points found, we have a valid connection;
335
- // otherwise choose the point of the minimum angle with the ray as connection point
336
-
337
- var stop = m,
338
- mx = m.x,
339
- my = m.y,
340
- tanMin = Infinity,
341
- tan;
342
-
343
- p = m;
344
-
345
- do {
346
- if (hx >= p.x && p.x >= mx && hx !== p.x &&
347
- pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {
348
-
349
- tan = Math.abs(hy - p.y) / (hx - p.x); // tangential
350
-
351
- if (locallyInside(p, hole) &&
352
- (tan < tanMin || (tan === tanMin && (p.x > m.x || (p.x === m.x && sectorContainsSector(m, p)))))) {
353
- m = p;
354
- tanMin = tan;
355
- }
356
- }
357
-
358
- p = p.next;
359
- } while (p !== stop);
360
-
361
- return m;
362
- }
363
-
364
- // whether sector in vertex m contains sector in vertex p in the same coordinates
365
- function sectorContainsSector(m, p) {
366
- return area(m.prev, m, p.prev) < 0 && area(p.next, m, m.next) < 0;
367
- }
368
-
369
- // interlink polygon nodes in z-order
370
- function indexCurve(start, minX, minY, invSize) {
371
- var p = start;
372
- do {
373
- if (p.z === 0) p.z = zOrder(p.x, p.y, minX, minY, invSize);
374
- p.prevZ = p.prev;
375
- p.nextZ = p.next;
376
- p = p.next;
377
- } while (p !== start);
378
-
379
- p.prevZ.nextZ = null;
380
- p.prevZ = null;
381
-
382
- sortLinked(p);
383
- }
384
-
385
- // Simon Tatham's linked list merge sort algorithm
386
- // http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
387
- function sortLinked(list) {
388
- var i, p, q, e, tail, numMerges, pSize, qSize,
389
- inSize = 1;
390
-
391
- do {
392
- p = list;
393
- list = null;
394
- tail = null;
395
- numMerges = 0;
396
-
397
- while (p) {
398
- numMerges++;
399
- q = p;
400
- pSize = 0;
401
- for (i = 0; i < inSize; i++) {
402
- pSize++;
403
- q = q.nextZ;
404
- if (!q) break;
405
- }
406
- qSize = inSize;
407
-
408
- while (pSize > 0 || (qSize > 0 && q)) {
409
-
410
- if (pSize !== 0 && (qSize === 0 || !q || p.z <= q.z)) {
411
- e = p;
412
- p = p.nextZ;
413
- pSize--;
414
- } else {
415
- e = q;
416
- q = q.nextZ;
417
- qSize--;
418
- }
419
-
420
- if (tail) tail.nextZ = e;
421
- else list = e;
422
-
423
- e.prevZ = tail;
424
- tail = e;
425
- }
426
-
427
- p = q;
428
- }
429
-
430
- tail.nextZ = null;
431
- inSize *= 2;
432
-
433
- } while (numMerges > 1);
434
-
435
- return list;
436
- }
437
-
438
- // z-order of a point given coords and inverse of the longer side of data bbox
439
- function zOrder(x, y, minX, minY, invSize) {
440
- // coords are transformed into non-negative 15-bit integer range
441
- x = (x - minX) * invSize | 0;
442
- y = (y - minY) * invSize | 0;
443
-
444
- x = (x | (x << 8)) & 0x00FF00FF;
445
- x = (x | (x << 4)) & 0x0F0F0F0F;
446
- x = (x | (x << 2)) & 0x33333333;
447
- x = (x | (x << 1)) & 0x55555555;
448
-
449
- y = (y | (y << 8)) & 0x00FF00FF;
450
- y = (y | (y << 4)) & 0x0F0F0F0F;
451
- y = (y | (y << 2)) & 0x33333333;
452
- y = (y | (y << 1)) & 0x55555555;
453
-
454
- return x | (y << 1);
455
- }
456
-
457
- // find the leftmost node of a polygon ring
458
- function getLeftmost(start) {
459
- var p = start,
460
- leftmost = start;
461
- do {
462
- if (p.x < leftmost.x || (p.x === leftmost.x && p.y < leftmost.y)) leftmost = p;
463
- p = p.next;
464
- } while (p !== start);
465
-
466
- return leftmost;
467
- }
468
-
469
- // check if a point lies within a convex triangle
470
- function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
471
- return (cx - px) * (ay - py) >= (ax - px) * (cy - py) &&
472
- (ax - px) * (by - py) >= (bx - px) * (ay - py) &&
473
- (bx - px) * (cy - py) >= (cx - px) * (by - py);
474
- }
475
-
476
- // check if a diagonal between two polygon nodes is valid (lies in polygon interior)
477
- function isValidDiagonal(a, b) {
478
- return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) && // dones't intersect other edges
479
- (locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b) && // locally visible
480
- (area(a.prev, a, b.prev) || area(a, b.prev, b)) || // does not create opposite-facing sectors
481
- equals(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0); // special zero-length case
482
- }
483
-
484
- // signed area of a triangle
485
- function area(p, q, r) {
486
- return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
487
- }
488
-
489
- // check if two points are equal
490
- function equals(p1, p2) {
491
- return p1.x === p2.x && p1.y === p2.y;
492
- }
493
-
494
- // check if two segments intersect
495
- function intersects(p1, q1, p2, q2) {
496
- var o1 = sign(area(p1, q1, p2));
497
- var o2 = sign(area(p1, q1, q2));
498
- var o3 = sign(area(p2, q2, p1));
499
- var o4 = sign(area(p2, q2, q1));
500
-
501
- if (o1 !== o2 && o3 !== o4) return true; // general case
502
-
503
- if (o1 === 0 && onSegment(p1, p2, q1)) return true; // p1, q1 and p2 are collinear and p2 lies on p1q1
504
- if (o2 === 0 && onSegment(p1, q2, q1)) return true; // p1, q1 and q2 are collinear and q2 lies on p1q1
505
- if (o3 === 0 && onSegment(p2, p1, q2)) return true; // p2, q2 and p1 are collinear and p1 lies on p2q2
506
- if (o4 === 0 && onSegment(p2, q1, q2)) return true; // p2, q2 and q1 are collinear and q1 lies on p2q2
507
-
508
- return false;
509
- }
510
-
511
- // for collinear points p, q, r, check if point q lies on segment pr
512
- function onSegment(p, q, r) {
513
- return q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) && q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y);
514
- }
515
-
516
- function sign(num) {
517
- return num > 0 ? 1 : num < 0 ? -1 : 0;
518
- }
519
-
520
- // check if a polygon diagonal intersects any polygon segments
521
- function intersectsPolygon(a, b) {
522
- var p = a;
523
- do {
524
- if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i &&
525
- intersects(p, p.next, a, b)) return true;
526
- p = p.next;
527
- } while (p !== a);
528
-
529
- return false;
530
- }
531
-
532
- // check if a polygon diagonal is locally inside the polygon
533
- function locallyInside(a, b) {
534
- return area(a.prev, a, a.next) < 0 ?
535
- area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0 :
536
- area(a, b, a.prev) < 0 || area(a, a.next, b) < 0;
537
- }
538
-
539
- // check if the middle point of a polygon diagonal is inside the polygon
540
- function middleInside(a, b) {
541
- var p = a,
542
- inside = false,
543
- px = (a.x + b.x) / 2,
544
- py = (a.y + b.y) / 2;
545
- do {
546
- if (((p.y > py) !== (p.next.y > py)) && p.next.y !== p.y &&
547
- (px < (p.next.x - p.x) * (py - p.y) / (p.next.y - p.y) + p.x))
548
- inside = !inside;
549
- p = p.next;
550
- } while (p !== a);
551
-
552
- return inside;
553
- }
554
-
555
- // link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
556
- // if one belongs to the outer ring and another to a hole, it merges it into a single ring
557
- function splitPolygon(a, b) {
558
- var a2 = new Node(a.i, a.x, a.y),
559
- b2 = new Node(b.i, b.x, b.y),
560
- an = a.next,
561
- bp = b.prev;
562
-
563
- a.next = b;
564
- b.prev = a;
565
-
566
- a2.next = an;
567
- an.prev = a2;
568
-
569
- b2.next = a2;
570
- a2.prev = b2;
571
-
572
- bp.next = b2;
573
- b2.prev = bp;
574
-
575
- return b2;
576
- }
577
-
578
- // create a node and optionally link it with previous one (in a circular doubly linked list)
579
- function insertNode(i, x, y, last) {
580
- var p = new Node(i, x, y);
581
-
582
- if (!last) {
583
- p.prev = p;
584
- p.next = p;
585
-
586
- } else {
587
- p.next = last.next;
588
- p.prev = last;
589
- last.next.prev = p;
590
- last.next = p;
591
- }
592
- return p;
593
- }
594
-
595
- function removeNode(p) {
596
- p.next.prev = p.prev;
597
- p.prev.next = p.next;
598
-
599
- if (p.prevZ) p.prevZ.nextZ = p.nextZ;
600
- if (p.nextZ) p.nextZ.prevZ = p.prevZ;
601
- }
602
-
603
- function Node(i, x, y) {
604
- // vertex index in coordinates array
605
- this.i = i;
606
-
607
- // vertex coordinates
608
- this.x = x;
609
- this.y = y;
610
-
611
- // previous and next vertex nodes in a polygon ring
612
- this.prev = null;
613
- this.next = null;
614
-
615
- // z-order curve value
616
- this.z = 0;
617
-
618
- // previous and next nodes in z-order
619
- this.prevZ = null;
620
- this.nextZ = null;
621
-
622
- // indicates whether this is a steiner point
623
- this.steiner = false;
624
- }
625
-
626
- // return a percentage difference between the polygon area and its triangulation area;
627
- // used to verify correctness of triangulation
628
- earcut.deviation = function (data, holeIndices, dim, triangles) {
629
- var hasHoles = holeIndices && holeIndices.length;
630
- var outerLen = hasHoles ? holeIndices[0] * dim : data.length;
631
-
632
- var polygonArea = Math.abs(signedArea(data, 0, outerLen, dim));
633
- if (hasHoles) {
634
- for (var i = 0, len = holeIndices.length; i < len; i++) {
635
- var start = holeIndices[i] * dim;
636
- var end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
637
- polygonArea -= Math.abs(signedArea(data, start, end, dim));
638
- }
639
- }
640
-
641
- var trianglesArea = 0;
642
- for (i = 0; i < triangles.length; i += 3) {
643
- var a = triangles[i] * dim;
644
- var b = triangles[i + 1] * dim;
645
- var c = triangles[i + 2] * dim;
646
- trianglesArea += Math.abs(
647
- (data[a] - data[c]) * (data[b + 1] - data[a + 1]) -
648
- (data[a] - data[b]) * (data[c + 1] - data[a + 1]));
649
- }
650
-
651
- return polygonArea === 0 && trianglesArea === 0 ? 0 :
652
- Math.abs((trianglesArea - polygonArea) / polygonArea);
653
- };
654
-
655
- function signedArea(data, start, end, dim) {
656
- var sum = 0;
657
- for (var i = start, j = end - dim; i < end; i += dim) {
658
- sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
659
- j = i;
660
- }
661
- return sum;
662
- }
663
-
664
- // turn a polygon in a multi-dimensional array form (e.g. as in GeoJSON) into a form Earcut accepts
665
- earcut.flatten = function (data) {
666
- var dim = data[0][0].length,
667
- result = {vertices: [], holes: [], dimensions: dim},
668
- holeIndex = 0;
669
-
670
- for (var i = 0; i < data.length; i++) {
671
- for (var j = 0; j < data[i].length; j++) {
672
- for (var d = 0; d < dim; d++) result.vertices.push(data[i][j][d]);
673
- }
674
- if (i > 0) {
675
- holeIndex += data[i - 1].length;
676
- result.holes.push(holeIndex);
677
- }
678
- }
679
- return result;
680
- };
1
+ 'use strict';
2
+
3
+ export default earcut;
4
+
5
+ function earcut(data, holeIndices, dim) {
6
+
7
+ dim = dim || 2;
8
+
9
+ var hasHoles = holeIndices && holeIndices.length,
10
+ outerLen = hasHoles ? holeIndices[0] * dim : data.length,
11
+ outerNode = linkedList(data, 0, outerLen, dim, true),
12
+ triangles = [];
13
+
14
+ if (!outerNode || outerNode.next === outerNode.prev) return triangles;
15
+
16
+ var minX, minY, maxX, maxY, x, y, invSize;
17
+
18
+ if (hasHoles) outerNode = eliminateHoles(data, holeIndices, outerNode, dim);
19
+
20
+ // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
21
+ if (data.length > 80 * dim) {
22
+ minX = maxX = data[0];
23
+ minY = maxY = data[1];
24
+
25
+ for (var i = dim; i < outerLen; i += dim) {
26
+ x = data[i];
27
+ y = data[i + 1];
28
+ if (x < minX) minX = x;
29
+ if (y < minY) minY = y;
30
+ if (x > maxX) maxX = x;
31
+ if (y > maxY) maxY = y;
32
+ }
33
+
34
+ // minX, minY and invSize are later used to transform coords into integers for z-order calculation
35
+ invSize = Math.max(maxX - minX, maxY - minY);
36
+ invSize = invSize !== 0 ? 32767 / invSize : 0;
37
+ }
38
+
39
+ earcutLinked(outerNode, triangles, dim, minX, minY, invSize, 0);
40
+
41
+ return triangles;
42
+ }
43
+
44
+ // create a circular doubly linked list from polygon points in the specified winding order
45
+ function linkedList(data, start, end, dim, clockwise) {
46
+ var i, last;
47
+
48
+ if (clockwise === (signedArea(data, start, end, dim) > 0)) {
49
+ for (i = start; i < end; i += dim) last = insertNode(i, data[i], data[i + 1], last);
50
+ } else {
51
+ for (i = end - dim; i >= start; i -= dim) last = insertNode(i, data[i], data[i + 1], last);
52
+ }
53
+
54
+ if (last && equals(last, last.next)) {
55
+ removeNode(last);
56
+ last = last.next;
57
+ }
58
+
59
+ return last;
60
+ }
61
+
62
+ // eliminate colinear or duplicate points
63
+ function filterPoints(start, end) {
64
+ if (!start) return start;
65
+ if (!end) end = start;
66
+
67
+ var p = start,
68
+ again;
69
+ do {
70
+ again = false;
71
+
72
+ if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) {
73
+ removeNode(p);
74
+ p = end = p.prev;
75
+ if (p === p.next) break;
76
+ again = true;
77
+
78
+ } else {
79
+ p = p.next;
80
+ }
81
+ } while (again || p !== end);
82
+
83
+ return end;
84
+ }
85
+
86
+ // main ear slicing loop which triangulates a polygon (given as a linked list)
87
+ function earcutLinked(ear, triangles, dim, minX, minY, invSize, pass) {
88
+ if (!ear) return;
89
+
90
+ // interlink polygon nodes in z-order
91
+ if (!pass && invSize) indexCurve(ear, minX, minY, invSize);
92
+
93
+ var stop = ear,
94
+ prev, next;
95
+
96
+ // iterate through ears, slicing them one by one
97
+ while (ear.prev !== ear.next) {
98
+ prev = ear.prev;
99
+ next = ear.next;
100
+
101
+ if (invSize ? isEarHashed(ear, minX, minY, invSize) : isEar(ear)) {
102
+ // cut off the triangle
103
+ triangles.push(prev.i / dim | 0);
104
+ triangles.push(ear.i / dim | 0);
105
+ triangles.push(next.i / dim | 0);
106
+
107
+ removeNode(ear);
108
+
109
+ // skipping the next vertex leads to less sliver triangles
110
+ ear = next.next;
111
+ stop = next.next;
112
+
113
+ continue;
114
+ }
115
+
116
+ ear = next;
117
+
118
+ // if we looped through the whole remaining polygon and can't find any more ears
119
+ if (ear === stop) {
120
+ // try filtering points and slicing again
121
+ if (!pass) {
122
+ earcutLinked(filterPoints(ear), triangles, dim, minX, minY, invSize, 1);
123
+
124
+ // if this didn't work, try curing all small self-intersections locally
125
+ } else if (pass === 1) {
126
+ ear = cureLocalIntersections(filterPoints(ear), triangles, dim);
127
+ earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);
128
+
129
+ // as a last resort, try splitting the remaining polygon into two
130
+ } else if (pass === 2) {
131
+ splitEarcut(ear, triangles, dim, minX, minY, invSize);
132
+ }
133
+
134
+ break;
135
+ }
136
+ }
137
+ }
138
+
139
+ // check whether a polygon node forms a valid ear with adjacent nodes
140
+ function isEar(ear) {
141
+ var a = ear.prev,
142
+ b = ear,
143
+ c = ear.next;
144
+
145
+ if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
146
+
147
+ // now make sure we don't have other points inside the potential ear
148
+ var ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
149
+
150
+ // triangle bbox; min & max are calculated like this for speed
151
+ var x0 = ax < bx ? (ax < cx ? ax : cx) : (bx < cx ? bx : cx),
152
+ y0 = ay < by ? (ay < cy ? ay : cy) : (by < cy ? by : cy),
153
+ x1 = ax > bx ? (ax > cx ? ax : cx) : (bx > cx ? bx : cx),
154
+ y1 = ay > by ? (ay > cy ? ay : cy) : (by > cy ? by : cy);
155
+
156
+ var p = c.next;
157
+ while (p !== a) {
158
+ if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 &&
159
+ pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) &&
160
+ area(p.prev, p, p.next) >= 0) return false;
161
+ p = p.next;
162
+ }
163
+
164
+ return true;
165
+ }
166
+
167
+ function isEarHashed(ear, minX, minY, invSize) {
168
+ var a = ear.prev,
169
+ b = ear,
170
+ c = ear.next;
171
+
172
+ if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
173
+
174
+ var ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
175
+
176
+ // triangle bbox; min & max are calculated like this for speed
177
+ var x0 = ax < bx ? (ax < cx ? ax : cx) : (bx < cx ? bx : cx),
178
+ y0 = ay < by ? (ay < cy ? ay : cy) : (by < cy ? by : cy),
179
+ x1 = ax > bx ? (ax > cx ? ax : cx) : (bx > cx ? bx : cx),
180
+ y1 = ay > by ? (ay > cy ? ay : cy) : (by > cy ? by : cy);
181
+
182
+ // z-order range for the current triangle bbox;
183
+ var minZ = zOrder(x0, y0, minX, minY, invSize),
184
+ maxZ = zOrder(x1, y1, minX, minY, invSize);
185
+
186
+ var p = ear.prevZ,
187
+ n = ear.nextZ;
188
+
189
+ // look for points inside the triangle in both directions
190
+ while (p && p.z >= minZ && n && n.z <= maxZ) {
191
+ if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c &&
192
+ pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0) return false;
193
+ p = p.prevZ;
194
+
195
+ if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c &&
196
+ pointInTriangle(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0) return false;
197
+ n = n.nextZ;
198
+ }
199
+
200
+ // look for remaining points in decreasing z-order
201
+ while (p && p.z >= minZ) {
202
+ if (p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c &&
203
+ pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) && area(p.prev, p, p.next) >= 0) return false;
204
+ p = p.prevZ;
205
+ }
206
+
207
+ // look for remaining points in increasing z-order
208
+ while (n && n.z <= maxZ) {
209
+ if (n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c &&
210
+ pointInTriangle(ax, ay, bx, by, cx, cy, n.x, n.y) && area(n.prev, n, n.next) >= 0) return false;
211
+ n = n.nextZ;
212
+ }
213
+
214
+ return true;
215
+ }
216
+
217
+ // go through all polygon nodes and cure small local self-intersections
218
+ function cureLocalIntersections(start, triangles, dim) {
219
+ var p = start;
220
+ do {
221
+ var a = p.prev,
222
+ b = p.next.next;
223
+
224
+ if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) {
225
+
226
+ triangles.push(a.i / dim | 0);
227
+ triangles.push(p.i / dim | 0);
228
+ triangles.push(b.i / dim | 0);
229
+
230
+ // remove two nodes involved
231
+ removeNode(p);
232
+ removeNode(p.next);
233
+
234
+ p = start = b;
235
+ }
236
+ p = p.next;
237
+ } while (p !== start);
238
+
239
+ return filterPoints(p);
240
+ }
241
+
242
+ // try splitting polygon into two and triangulate them independently
243
+ function splitEarcut(start, triangles, dim, minX, minY, invSize) {
244
+ // look for a valid diagonal that divides the polygon into two
245
+ var a = start;
246
+ do {
247
+ var b = a.next.next;
248
+ while (b !== a.prev) {
249
+ if (a.i !== b.i && isValidDiagonal(a, b)) {
250
+ // split the polygon in two by the diagonal
251
+ var c = splitPolygon(a, b);
252
+
253
+ // filter colinear points around the cuts
254
+ a = filterPoints(a, a.next);
255
+ c = filterPoints(c, c.next);
256
+
257
+ // run earcut on each half
258
+ earcutLinked(a, triangles, dim, minX, minY, invSize, 0);
259
+ earcutLinked(c, triangles, dim, minX, minY, invSize, 0);
260
+ return;
261
+ }
262
+ b = b.next;
263
+ }
264
+ a = a.next;
265
+ } while (a !== start);
266
+ }
267
+
268
+ // link every hole into the outer loop, producing a single-ring polygon without holes
269
+ function eliminateHoles(data, holeIndices, outerNode, dim) {
270
+ var queue = [],
271
+ i, len, start, end, list;
272
+
273
+ for (i = 0, len = holeIndices.length; i < len; i++) {
274
+ start = holeIndices[i] * dim;
275
+ end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
276
+ list = linkedList(data, start, end, dim, false);
277
+ if (list === list.next) list.steiner = true;
278
+ queue.push(getLeftmost(list));
279
+ }
280
+
281
+ queue.sort(compareX);
282
+
283
+ // process holes from left to right
284
+ for (i = 0; i < queue.length; i++) {
285
+ outerNode = eliminateHole(queue[i], outerNode);
286
+ }
287
+
288
+ return outerNode;
289
+ }
290
+
291
+ function compareX(a, b) {
292
+ return a.x - b.x;
293
+ }
294
+
295
+ // find a bridge between vertices that connects hole with an outer ring and and link it
296
+ function eliminateHole(hole, outerNode) {
297
+ var bridge = findHoleBridge(hole, outerNode);
298
+ if (!bridge) {
299
+ return outerNode;
300
+ }
301
+
302
+ var bridgeReverse = splitPolygon(bridge, hole);
303
+
304
+ // filter collinear points around the cuts
305
+ filterPoints(bridgeReverse, bridgeReverse.next);
306
+ return filterPoints(bridge, bridge.next);
307
+ }
308
+
309
+ // David Eberly's algorithm for finding a bridge between hole and outer polygon
310
+ function findHoleBridge(hole, outerNode) {
311
+ var p = outerNode,
312
+ hx = hole.x,
313
+ hy = hole.y,
314
+ qx = -Infinity,
315
+ m;
316
+
317
+ // find a segment intersected by a ray from the hole's leftmost point to the left;
318
+ // segment's endpoint with lesser x will be potential connection point
319
+ do {
320
+ if (hy <= p.y && hy >= p.next.y && p.next.y !== p.y) {
321
+ var x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y);
322
+ if (x <= hx && x > qx) {
323
+ qx = x;
324
+ m = p.x < p.next.x ? p : p.next;
325
+ if (x === hx) return m; // hole touches outer segment; pick leftmost endpoint
326
+ }
327
+ }
328
+ p = p.next;
329
+ } while (p !== outerNode);
330
+
331
+ if (!m) return null;
332
+
333
+ // look for points inside the triangle of hole point, segment intersection and endpoint;
334
+ // if there are no points found, we have a valid connection;
335
+ // otherwise choose the point of the minimum angle with the ray as connection point
336
+
337
+ var stop = m,
338
+ mx = m.x,
339
+ my = m.y,
340
+ tanMin = Infinity,
341
+ tan;
342
+
343
+ p = m;
344
+
345
+ do {
346
+ if (hx >= p.x && p.x >= mx && hx !== p.x &&
347
+ pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {
348
+
349
+ tan = Math.abs(hy - p.y) / (hx - p.x); // tangential
350
+
351
+ if (locallyInside(p, hole) &&
352
+ (tan < tanMin || (tan === tanMin && (p.x > m.x || (p.x === m.x && sectorContainsSector(m, p)))))) {
353
+ m = p;
354
+ tanMin = tan;
355
+ }
356
+ }
357
+
358
+ p = p.next;
359
+ } while (p !== stop);
360
+
361
+ return m;
362
+ }
363
+
364
+ // whether sector in vertex m contains sector in vertex p in the same coordinates
365
+ function sectorContainsSector(m, p) {
366
+ return area(m.prev, m, p.prev) < 0 && area(p.next, m, m.next) < 0;
367
+ }
368
+
369
+ // interlink polygon nodes in z-order
370
+ function indexCurve(start, minX, minY, invSize) {
371
+ var p = start;
372
+ do {
373
+ if (p.z === 0) p.z = zOrder(p.x, p.y, minX, minY, invSize);
374
+ p.prevZ = p.prev;
375
+ p.nextZ = p.next;
376
+ p = p.next;
377
+ } while (p !== start);
378
+
379
+ p.prevZ.nextZ = null;
380
+ p.prevZ = null;
381
+
382
+ sortLinked(p);
383
+ }
384
+
385
+ // Simon Tatham's linked list merge sort algorithm
386
+ // http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
387
+ function sortLinked(list) {
388
+ var i, p, q, e, tail, numMerges, pSize, qSize,
389
+ inSize = 1;
390
+
391
+ do {
392
+ p = list;
393
+ list = null;
394
+ tail = null;
395
+ numMerges = 0;
396
+
397
+ while (p) {
398
+ numMerges++;
399
+ q = p;
400
+ pSize = 0;
401
+ for (i = 0; i < inSize; i++) {
402
+ pSize++;
403
+ q = q.nextZ;
404
+ if (!q) break;
405
+ }
406
+ qSize = inSize;
407
+
408
+ while (pSize > 0 || (qSize > 0 && q)) {
409
+
410
+ if (pSize !== 0 && (qSize === 0 || !q || p.z <= q.z)) {
411
+ e = p;
412
+ p = p.nextZ;
413
+ pSize--;
414
+ } else {
415
+ e = q;
416
+ q = q.nextZ;
417
+ qSize--;
418
+ }
419
+
420
+ if (tail) tail.nextZ = e;
421
+ else list = e;
422
+
423
+ e.prevZ = tail;
424
+ tail = e;
425
+ }
426
+
427
+ p = q;
428
+ }
429
+
430
+ tail.nextZ = null;
431
+ inSize *= 2;
432
+
433
+ } while (numMerges > 1);
434
+
435
+ return list;
436
+ }
437
+
438
+ // z-order of a point given coords and inverse of the longer side of data bbox
439
+ function zOrder(x, y, minX, minY, invSize) {
440
+ // coords are transformed into non-negative 15-bit integer range
441
+ x = (x - minX) * invSize | 0;
442
+ y = (y - minY) * invSize | 0;
443
+
444
+ x = (x | (x << 8)) & 0x00FF00FF;
445
+ x = (x | (x << 4)) & 0x0F0F0F0F;
446
+ x = (x | (x << 2)) & 0x33333333;
447
+ x = (x | (x << 1)) & 0x55555555;
448
+
449
+ y = (y | (y << 8)) & 0x00FF00FF;
450
+ y = (y | (y << 4)) & 0x0F0F0F0F;
451
+ y = (y | (y << 2)) & 0x33333333;
452
+ y = (y | (y << 1)) & 0x55555555;
453
+
454
+ return x | (y << 1);
455
+ }
456
+
457
+ // find the leftmost node of a polygon ring
458
+ function getLeftmost(start) {
459
+ var p = start,
460
+ leftmost = start;
461
+ do {
462
+ if (p.x < leftmost.x || (p.x === leftmost.x && p.y < leftmost.y)) leftmost = p;
463
+ p = p.next;
464
+ } while (p !== start);
465
+
466
+ return leftmost;
467
+ }
468
+
469
+ // check if a point lies within a convex triangle
470
+ function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
471
+ return (cx - px) * (ay - py) >= (ax - px) * (cy - py) &&
472
+ (ax - px) * (by - py) >= (bx - px) * (ay - py) &&
473
+ (bx - px) * (cy - py) >= (cx - px) * (by - py);
474
+ }
475
+
476
+ // check if a diagonal between two polygon nodes is valid (lies in polygon interior)
477
+ function isValidDiagonal(a, b) {
478
+ return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) && // dones't intersect other edges
479
+ (locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b) && // locally visible
480
+ (area(a.prev, a, b.prev) || area(a, b.prev, b)) || // does not create opposite-facing sectors
481
+ equals(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0); // special zero-length case
482
+ }
483
+
484
+ // signed area of a triangle
485
+ function area(p, q, r) {
486
+ return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
487
+ }
488
+
489
+ // check if two points are equal
490
+ function equals(p1, p2) {
491
+ return p1.x === p2.x && p1.y === p2.y;
492
+ }
493
+
494
+ // check if two segments intersect
495
+ function intersects(p1, q1, p2, q2) {
496
+ var o1 = sign(area(p1, q1, p2));
497
+ var o2 = sign(area(p1, q1, q2));
498
+ var o3 = sign(area(p2, q2, p1));
499
+ var o4 = sign(area(p2, q2, q1));
500
+
501
+ if (o1 !== o2 && o3 !== o4) return true; // general case
502
+
503
+ if (o1 === 0 && onSegment(p1, p2, q1)) return true; // p1, q1 and p2 are collinear and p2 lies on p1q1
504
+ if (o2 === 0 && onSegment(p1, q2, q1)) return true; // p1, q1 and q2 are collinear and q2 lies on p1q1
505
+ if (o3 === 0 && onSegment(p2, p1, q2)) return true; // p2, q2 and p1 are collinear and p1 lies on p2q2
506
+ if (o4 === 0 && onSegment(p2, q1, q2)) return true; // p2, q2 and q1 are collinear and q1 lies on p2q2
507
+
508
+ return false;
509
+ }
510
+
511
+ // for collinear points p, q, r, check if point q lies on segment pr
512
+ function onSegment(p, q, r) {
513
+ return q.x <= Math.max(p.x, r.x) && q.x >= Math.min(p.x, r.x) && q.y <= Math.max(p.y, r.y) && q.y >= Math.min(p.y, r.y);
514
+ }
515
+
516
+ function sign(num) {
517
+ return num > 0 ? 1 : num < 0 ? -1 : 0;
518
+ }
519
+
520
+ // check if a polygon diagonal intersects any polygon segments
521
+ function intersectsPolygon(a, b) {
522
+ var p = a;
523
+ do {
524
+ if (p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i &&
525
+ intersects(p, p.next, a, b)) return true;
526
+ p = p.next;
527
+ } while (p !== a);
528
+
529
+ return false;
530
+ }
531
+
532
+ // check if a polygon diagonal is locally inside the polygon
533
+ function locallyInside(a, b) {
534
+ return area(a.prev, a, a.next) < 0 ?
535
+ area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0 :
536
+ area(a, b, a.prev) < 0 || area(a, a.next, b) < 0;
537
+ }
538
+
539
+ // check if the middle point of a polygon diagonal is inside the polygon
540
+ function middleInside(a, b) {
541
+ var p = a,
542
+ inside = false,
543
+ px = (a.x + b.x) / 2,
544
+ py = (a.y + b.y) / 2;
545
+ do {
546
+ if (((p.y > py) !== (p.next.y > py)) && p.next.y !== p.y &&
547
+ (px < (p.next.x - p.x) * (py - p.y) / (p.next.y - p.y) + p.x))
548
+ inside = !inside;
549
+ p = p.next;
550
+ } while (p !== a);
551
+
552
+ return inside;
553
+ }
554
+
555
+ // link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
556
+ // if one belongs to the outer ring and another to a hole, it merges it into a single ring
557
+ function splitPolygon(a, b) {
558
+ var a2 = new Node(a.i, a.x, a.y),
559
+ b2 = new Node(b.i, b.x, b.y),
560
+ an = a.next,
561
+ bp = b.prev;
562
+
563
+ a.next = b;
564
+ b.prev = a;
565
+
566
+ a2.next = an;
567
+ an.prev = a2;
568
+
569
+ b2.next = a2;
570
+ a2.prev = b2;
571
+
572
+ bp.next = b2;
573
+ b2.prev = bp;
574
+
575
+ return b2;
576
+ }
577
+
578
+ // create a node and optionally link it with previous one (in a circular doubly linked list)
579
+ function insertNode(i, x, y, last) {
580
+ var p = new Node(i, x, y);
581
+
582
+ if (!last) {
583
+ p.prev = p;
584
+ p.next = p;
585
+
586
+ } else {
587
+ p.next = last.next;
588
+ p.prev = last;
589
+ last.next.prev = p;
590
+ last.next = p;
591
+ }
592
+ return p;
593
+ }
594
+
595
+ function removeNode(p) {
596
+ p.next.prev = p.prev;
597
+ p.prev.next = p.next;
598
+
599
+ if (p.prevZ) p.prevZ.nextZ = p.nextZ;
600
+ if (p.nextZ) p.nextZ.prevZ = p.prevZ;
601
+ }
602
+
603
+ function Node(i, x, y) {
604
+ // vertex index in coordinates array
605
+ this.i = i;
606
+
607
+ // vertex coordinates
608
+ this.x = x;
609
+ this.y = y;
610
+
611
+ // previous and next vertex nodes in a polygon ring
612
+ this.prev = null;
613
+ this.next = null;
614
+
615
+ // z-order curve value
616
+ this.z = 0;
617
+
618
+ // previous and next nodes in z-order
619
+ this.prevZ = null;
620
+ this.nextZ = null;
621
+
622
+ // indicates whether this is a steiner point
623
+ this.steiner = false;
624
+ }
625
+
626
+ // return a percentage difference between the polygon area and its triangulation area;
627
+ // used to verify correctness of triangulation
628
+ earcut.deviation = function (data, holeIndices, dim, triangles) {
629
+ var hasHoles = holeIndices && holeIndices.length;
630
+ var outerLen = hasHoles ? holeIndices[0] * dim : data.length;
631
+
632
+ var polygonArea = Math.abs(signedArea(data, 0, outerLen, dim));
633
+ if (hasHoles) {
634
+ for (var i = 0, len = holeIndices.length; i < len; i++) {
635
+ var start = holeIndices[i] * dim;
636
+ var end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
637
+ polygonArea -= Math.abs(signedArea(data, start, end, dim));
638
+ }
639
+ }
640
+
641
+ var trianglesArea = 0;
642
+ for (i = 0; i < triangles.length; i += 3) {
643
+ var a = triangles[i] * dim;
644
+ var b = triangles[i + 1] * dim;
645
+ var c = triangles[i + 2] * dim;
646
+ trianglesArea += Math.abs(
647
+ (data[a] - data[c]) * (data[b + 1] - data[a + 1]) -
648
+ (data[a] - data[b]) * (data[c + 1] - data[a + 1]));
649
+ }
650
+
651
+ return polygonArea === 0 && trianglesArea === 0 ? 0 :
652
+ Math.abs((trianglesArea - polygonArea) / polygonArea);
653
+ };
654
+
655
+ function signedArea(data, start, end, dim) {
656
+ var sum = 0;
657
+ for (var i = start, j = end - dim; i < end; i += dim) {
658
+ sum += (data[j] - data[i]) * (data[i + 1] + data[j + 1]);
659
+ j = i;
660
+ }
661
+ return sum;
662
+ }
663
+
664
+ // turn a polygon in a multi-dimensional array form (e.g. as in GeoJSON) into a form Earcut accepts
665
+ earcut.flatten = function (data) {
666
+ var dim = data[0][0].length,
667
+ result = {vertices: [], holes: [], dimensions: dim},
668
+ holeIndex = 0;
669
+
670
+ for (var i = 0; i < data.length; i++) {
671
+ for (var j = 0; j < data[i].length; j++) {
672
+ for (var d = 0; d < dim; d++) result.vertices.push(data[i][j][d]);
673
+ }
674
+ if (i > 0) {
675
+ holeIndex += data[i - 1].length;
676
+ result.holes.push(holeIndex);
677
+ }
678
+ }
679
+ return result;
680
+ };