earcut 2.1.0 → 2.1.1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "earcut",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "The fastest and smallest JavaScript polygon triangulation library for your WebGL apps",
5
5
  "main": "src/earcut.js",
6
6
  "scripts": {
package/src/earcut.js CHANGED
@@ -43,11 +43,18 @@ function earcut(data, holeIndices, dim) {
43
43
  // create a circular doubly linked list from polygon points in the specified winding order
44
44
  function linkedList(data, start, end, dim, clockwise) {
45
45
  var i, last;
46
+
46
47
  if (clockwise === (signedArea(data, start, end, dim) > 0)) {
47
48
  for (i = start; i < end; i += dim) last = insertNode(i, data[i], data[i + 1], last);
48
49
  } else {
49
50
  for (i = end - dim; i >= start; i -= dim) last = insertNode(i, data[i], data[i + 1], last);
50
51
  }
52
+
53
+ if (last && equals(last, last.next)) {
54
+ removeNode(last);
55
+ last = last.next;
56
+ }
57
+
51
58
  return last;
52
59
  }
53
60
 
@@ -195,8 +202,7 @@ function cureLocalIntersections(start, triangles, dim) {
195
202
  var a = p.prev,
196
203
  b = p.next.next;
197
204
 
198
- // a self-intersection where edge (v[i-1],v[i]) intersects (v[i+1],v[i+2])
199
- if (intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) {
205
+ if (!equals(a, b) && intersects(a, p, p.next, b) && locallyInside(a, b) && locallyInside(b, a)) {
200
206
 
201
207
  triangles.push(a.i / dim);
202
208
  triangles.push(p.i / dim);
@@ -292,6 +298,10 @@ function findHoleBridge(hole, outerNode) {
292
298
  var x = p.x + (hy - p.y) * (p.next.x - p.x) / (p.next.y - p.y);
293
299
  if (x <= hx && x > qx) {
294
300
  qx = x;
301
+ if (x === hx) {
302
+ if (hy === p.y) return p;
303
+ if (hy === p.next.y) return p.next;
304
+ }
295
305
  m = p.x < p.next.x ? p : p.next;
296
306
  }
297
307
  }
@@ -300,7 +310,7 @@ function findHoleBridge(hole, outerNode) {
300
310
 
301
311
  if (!m) return null;
302
312
 
303
- if (hole.x === m.x) return m.prev; // hole touches outer segment; pick lower endpoint
313
+ if (hx === qx) return m.prev; // hole touches outer segment; pick lower endpoint
304
314
 
305
315
  // look for points inside the triangle of hole point, segment intersection and endpoint;
306
316
  // if there are no points found, we have a valid connection;
@@ -450,7 +460,7 @@ function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
450
460
 
451
461
  // check if a diagonal between two polygon nodes is valid (lies in polygon interior)
452
462
  function isValidDiagonal(a, b) {
453
- return equals(a, b) || a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) &&
463
+ return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) &&
454
464
  locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b);
455
465
  }
456
466
 
@@ -466,6 +476,8 @@ function equals(p1, p2) {
466
476
 
467
477
  // check if two segments intersect
468
478
  function intersects(p1, q1, p2, q2) {
479
+ if ((equals(p1, q1) && equals(p2, q2)) ||
480
+ (equals(p1, q2) && equals(p2, q1))) return true;
469
481
  return area(p1, q1, p2) > 0 !== area(p1, q1, q2) > 0 &&
470
482
  area(p2, q2, p1) > 0 !== area(p2, q2, q1) > 0;
471
483
  }