Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

earcut

Package Overview
Dependencies
Maintainers
123
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

earcut - npm Package Compare versions

Comparing version 2.1.5 to 2.2.0

57

dist/earcut.dev.js

@@ -128,3 +128,3 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.earcut = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){

} else if (pass === 1) {
ear = cureLocalIntersections(ear, triangles, dim);
ear = cureLocalIntersections(filterPoints(ear), triangles, dim);
earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);

@@ -236,3 +236,3 @@

return p;
return filterPoints(p);
}

@@ -330,3 +330,3 @@

if (hx === qx) return m.prev; // hole touches outer segment; pick lower endpoint
if (hx === qx) return m; // hole touches outer segment; pick leftmost endpoint

@@ -343,5 +343,5 @@ // look for points inside the triangle of hole point, segment intersection and endpoint;

p = m.next;
p = m;
while (p !== stop) {
do {
if (hx >= p.x && p.x >= mx && hx !== p.x &&

@@ -352,3 +352,4 @@ pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {

if ((tan < tanMin || (tan === tanMin && p.x > m.x)) && locallyInside(p, hole)) {
if (locallyInside(p, hole) &&
(tan < tanMin || (tan === tanMin && (p.x > m.x || (p.x === m.x && sectorContainsSector(m, p)))))) {
m = p;

@@ -360,3 +361,3 @@ tanMin = tan;

p = p.next;
}
} while (p !== stop);

@@ -366,2 +367,10 @@ return m;

// whether sector in vertex m contains sector in vertex p in the same coordinates
function sectorContainsSector(m, p) {
return (
(area(m.prev, m, p.prev) < 0 || area(p.prev, m, m.next) < 0) &&
(area(m.prev, m, p.next) < 0 || area(p.next, m, m.next) < 0)
);
}
// interlink polygon nodes in z-order

@@ -476,4 +485,6 @@ function indexCurve(start, minX, minY, invSize) {

function isValidDiagonal(a, b) {
return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) &&
locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b);
return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) && // dones't intersect other edges
(locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b) && // locally visible
(area(a.prev, a, b.prev) || area(a, b.prev, b)) || // does not create opposite-facing sectors
equals(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0); // special zero-length case
}

@@ -493,8 +504,26 @@

function intersects(p1, q1, p2, q2) {
if ((equals(p1, q1) && equals(p2, q2)) ||
(equals(p1, q2) && equals(p2, q1))) return true;
return area(p1, q1, p2) > 0 !== area(p1, q1, q2) > 0 &&
area(p2, q2, p1) > 0 !== area(p2, q2, q1) > 0;
var o1 = sign(area(p1, q1, p2));
var o2 = sign(area(p1, q1, q2));
var o3 = sign(area(p2, q2, p1));
var o4 = sign(area(p2, q2, q1));
if (o1 !== o2 && o3 !== o4) return true; // general case
if (o1 === 0 && onSegment(p1, p2, q1)) return true; // p1, q1 and p2 are collinear and p2 lies on p1q1
if (o2 === 0 && onSegment(p1, q2, q1)) return true; // p1, q1 and q2 are collinear and q2 lies on p1q1
if (o3 === 0 && onSegment(p2, p1, q2)) return true; // p2, q2 and p1 are collinear and p1 lies on p2q2
if (o4 === 0 && onSegment(p2, q1, q2)) return true; // p2, q2 and q1 are collinear and q1 lies on p2q2
return false;
}
// for collinear points p, q, r, check if point q lies on segment pr
function onSegment(p, q, r) {
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);
}
function sign(num) {
return num > 0 ? 1 : num < 0 ? -1 : 0;
}
// check if a polygon diagonal intersects any polygon segments

@@ -665,2 +694,2 @@ function intersectsPolygon(a, b) {

//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5vZGVfbW9kdWxlcy9icm93c2VyLXBhY2svX3ByZWx1ZGUuanMiLCJzcmMvZWFyY3V0LmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBO0FDQUE7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QU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AgLy8gei1vcmRlciByYW5nZSBmb3IgdGhlIGN1cnJlbnQgdHJpYW5nbGUgYmJveDtcbiAgICB2YXIgbWluWiA9IHpPcmRlcihtaW5UWCwgbWluVFksIG1pblgsIG1pblksIGludlNpemUpLFxuICAgICAgICBtYXhaID0gek9yZGVyKG1heFRYLCBtYXhUWSwgbWluWCwgbWluWSwgaW52U2l6ZSk7XG5cbiAgICB2YXIgcCA9IGVhci5wcmV2WixcbiAgICAgICAgbiA9IGVhci5uZXh0WjtcblxuICAgIC8vIGxvb2sgZm9yIHBvaW50cyBpbnNpZGUgdGhlIHRyaWFuZ2xlIGluIGJvdGggZGlyZWN0aW9uc1xuICAgIHdoaWxlIChwICYmIHAueiA+PSBtaW5aICYmIG4gJiYgbi56IDw9IG1heFopIHtcbiAgICAgICAgaWYgKHAgIT09IGVhci5wcmV2ICYmIHAgIT09IGVhci5uZXh0ICYmXG4gICAgICAgICAgICBwb2ludEluVHJpYW5nbGUoYS54LCBhLnksIGIueCwgYi55LCBjLngsIGMueSwgcC54LCBwLnkpICYmXG4gICAgICAgICAgICBhcmVhKHAucHJldiwgcCwgcC5uZXh0KSA+PSAwKSByZXR1cm4gZmFsc2U7XG4gICAgICAgIHAgPSBwLnByZXZaO1xuXG4gICAgICAgIGlmIChuICE9PSBlYXIucHJldiAmJiBuICE9PSBlYXIubmV4dCAmJlxuICAgICAgICAgICAgcG9pbnRJblRyaWFuZ2xlKGEueCwgYS55LCBiLngsIGIueSwgYy54LCBjLnksIG4ueCwgbi55KSAmJlxuICAgICAgICAgICAgYXJlYShuLnByZXYsIG4sIG4ubmV4dCkgPj0gMCkgcmV0dXJuIGZhbHNlO1xuICAgICAgICBuID0gbi5uZXh0WjtcbiAgICB9XG5cbiAgICAvLyBsb29rIGZvciByZW1haW5pbmcgcG9pbnRzIGluIGRlY3JlYXNpbmcgei1vcmRlclxuICAgIHdoaWxlIChwICYmIHAueiA+PSBtaW5aKSB7XG4gICAgICAgIGlmIChwICE9PSBlYXI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pbmRIb2xlQnJpZGdlKGhvbGUsIG91dGVyTm9kZSkge1xuICAgIHZhciBwID0gb3V0ZXJOb2RlLFxuICAgICAgICBoeCA9IGhvbGUueCxcbiAgICAgICAgaHkgPSBob2xlLnksXG4gICAgICAgIHF4ID0gLUluZmluaXR5LFxuICAgICAgICBtO1xuXG4gICAgLy8gZmluZCBhIHNlZ21lbnQgaW50ZXJzZWN0ZWQgYnkgYSByYXkgZnJvbSB0aGUgaG9sZSdzIGxlZnRtb3N0IHBvaW50IHRvIHRoZSBsZWZ0O1xuICAgIC8vIHNlZ21lbnQncyBlbmRwb2ludCB3aXRoIGxlc3NlciB4IHdpbGwgYmUgcG90ZW50aWFsIGNvbm5lY3Rpb24gcG9pbnRcbiAgICBkbyB7XG4gICAgICAgIGlmIChoeSA8PSBwLnkgJiYgaHkgPj0gcC5uZXh0LnkgJiYgcC5uZXh0LnkgIT09IHAueSkge1xuICAgICAgICAgICAgdmFyIHggPSBwLnggKyAoaHkgLSBwLnkpICogKHAubmV4dC54IC0gcC54KSAvIChwLm5leHQueSAtIHAueSk7XG4gICAgICAgICAgICBpZiAoeCA8PSBoeCAmJiB4ID4gcXgpIHtcbiAgICAgICAgICAgICAgICBxeCA9IHg7XG4gICAgICAgICAgICAgICAgaWYgKHggPT09IGh4KSB7XG4gICAgICAgICAgICAgICAgICAgIGlmIChoeSA9PT0gcC55KSByZXR1cm4gcDtcbiAgICAgICAgICAgICAgICAgICAgaWYgKGh5ID09PSBwLm5leHQueSkgcmV0dXJuIHAubmV4dDtcbiAgICAgICAgICAgICAgICB9XG4gICAgICAgICAgICAgICAgbSA9IHAueCA8IHAubmV4dC54ID8gcCA6IHAubmV4dDtcbiAgICAgICAgICAgIH1cbiAgICAgICAgfVxuICAgICAgICBwID0gcC5uZXh0O1xuICAgIH0gd2hpbGUgKHAgIT09IG91dGVyTm9kZSk7XG5cbiAgICBpZiAoIW0pIHJldHVybiBudWxsO1xuXG4gICAgaWYgKGh4ID09PSBxeCkgcm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cbiAgICAgICAgICAgIH1cbiAgICAgICAgfVxuXG4gICAgICAgIHAgPSBwLm5leHQ7XG4gICAgfVxuXG4gICAgcmV0dXJuIG07XG59XG5cbi8vIGludGVybGluayBwb2x5Z29uIG5vZGVzIGluIHotb3JkZXJcbmZ1bmN0aW9uIGluZGV4Q3VydmUoc3RhcnQsIG1pblgsIG1pblksIGludlNpemUpIHtcbiAgICB2YXIgcCA9IHN0YXJ0O1xuICAgIGRvIHtcbiAgICAgICAgaWYgKHAueiA9PT0gbnVsbCkgcC56ID0gek9yZGVyKHAueCwgcC55LCBtaW5YLCBtaW5ZLCBpbnZTaXplKTtcbiAgICAgICAgcC5wcmV2WiA9IHAucHJldjtcbiAgICAgICAgcC5uZXh0WiA9IHAubmV4dDtcbiAgICAgICAgcCA9IHAubmV4dDtcbiAgICB9IHdoaWxlIChwICE9PSBzdGFydCk7XG5cbiAgICBwLnByZXZaLm5leHRaID0gbnVsbDtcbiAgICBwLnByZXZaID0gbnVsbDtcblxuICAgIHNvcnRMaW5rZWQocCk7XG59XG5cbi8vIFNpbW9uIFRhdGhhbSdzIGxpbmtlZCBsaXN0IG1lcmdlIHNvcnQgYWxnb3JpdGhtXG4vLyBodHRwOi8vd3d3LmNoaWFyay5ncmVlbmVuZC5vcmcudWsvfnNndGF0aGFtL2FsZ29yaXRobXMvbGlzdHNvcnQuaHRtbFxuZnVuY3Rpb24gc29ydExpbmtlZChsaXN0KSB7XG4gICAgdmFyIGksIHAsIHEsIGUsIHRhaWwsIG51bU1lcmdlcywgcFNpemUsIHFTaXplLFxuICAgICAgICBpblNpemUgPSAxO1xuXG4gICAgZG8ge1xuICAgICAgICBwID0gbGlzdDtcbiAgICAgICAgbGlzdCA9IG51bGw7XG4gICAgICAgIHRhaWwgPSBudWxsO1xuICAgICAgICBudW1NZXJnZXMgPSAwO1xuXG4gICAgICAgIHdoaWxlIChwKSB7XG4gICAgICAgICAgICBudW1NZXJnZXMrKztcbiAgICAgICAgICAgIHEgPSBwO1xuICAgICAgICAgICAgcFNpemUgPSAwO1xuICAgICAgICAgICAgZm9yIChpID0gMDsgaSA8IGluU2l6ZTsgaSsrKSB7XG4gICAgICAgICAgICAgICAgcFNpemUrKztcbiAgICAgICAgICAgICAgICBxID0gcS5uZXh0WjtcbiAgICAgICAgICAgICAgICBpZiAoIXEpIGJyZWFrO1xuICAgICAgICAgICAgfVxuICAgICAgICAgICAgcVNpemUgPSBpblNpemU7XG5cbiAgICAgICAgICAgIHdoaWxlIChwU2l6ZSA+IDAgfHwgKHFTaXplID4gMCAmJiBxKSkge1xuXG4gICAgICAgICAgICAgICAgaWYgKHBTaXplICE9PSAwICYmIChxU2l6ZSA9PT0gMCB8fCAhcSB8fCBwLnogPD0gcS56KSkge1xuICAgICAgICAgICAgICAgICAgICBlID0gcDtcbiAgICAgICAgICAgICAgICAgICAgcCA9IHAubmV4dFo7XG4gICAgICAgICAgICAgICAgICAgIHBTaXplLS07XG4gICAgICAgICAgICAgICAgfSBlbHNlIHtcbiAgICAgICAgICAgICAgICAgICAgZSA9IHE7XG4gICAgICAgICAgICAgICAgICAgIHEgPSBxLm5leHRaO1xuICAgICAgICAgICAgICAgICAgICBxU2l6ZS0tO1xuICAgICAgICAgICAgICAgIH1cblxuICAgICAgICAgICAgICAgIGlmICh0YWlsKSB0YWlsLm5leHRaID0gZTtcbiAgICAgICAgICAgICAgICBlbHNlIGxpc3QgPSBlO1xuXG4gICAgICAgICAgICAgICAgZS5wcmV2WiA9IHRhaWw7XG4gICAgICAgICAgICAgICAgdGFpbCA9IGU7XG4gICAgICAgICAgICB9XG5cbiAgICAgICAgICAgIHAgPSBxO1xuICAgICAgICB9XG5cbiAgICAgICAgdGFpbC5uZXh0WiA9IG51bGw7XG4gICAgICAgIGluU2l6ZSAqPSAyO1xuXG4gICAgfSB3aGlsZSAobnVtTWVyZ2VzID4gMSk7XG5cbiAgICByZXR1cm4gbGlzdDtcbn1cblxuLy8gei1vcmRlciBvZiBhIHBvaW50IGdpdmVuIG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VFkgPSBhLnkgPiBiLnkgPyAoYS55ID4gYy55ID8gYS55IDogYy55KSA6IChiLnkgPiBjLnkgPyBiLnkgOiBjLnkpO1xuXG4gICAgLy8gei1vcmRlciByYW5nZSBmb3IgdGhlIGN1cnJlbnQgdHJpYW5nbGUgYmJveDtcbiAgICB2YXIgbWluWiA9IHpPcmRlcihtaW5UWCwgbWluVFksIG1pblgsIG1pblksIGludlNpemUpLFxuICAgICAgICBtYXhaID0gek9yZGVyKG1heFRYLCBtYXhUWSwgbWluWCwgbWluWSwgaW52U2l6ZSk7XG5cbiAgICB2YXIgcCA9IGVhci5wcmV2WixcbiAgICAgICAgbiA9IGVhci5uZXh0WjtcblxuICAgIC8vIGxvb2sgZm9yIHBvaW50cyBpbnNpZGUgdGhlIHRyaWFuZ2xlIGluIGJvdGggZGlyZWN0aW9uc1xuICAgIHdoaWxlIChwICYmIHAueiA+PSBtaW5aICYmIG4gJiYgbi56IDw9IG1heFopIHtcbiAgICAgICAgaWYgKHAgIT09IGVhci5wcmV2ICYmIHAgIT09IGVhci5uZXh0ICYmXG4gICAgICAgICAgICBwb2ludEluVHJpYW5nbGUoYS54LCBhLnksIGIueCwgYi55LCBjLngsIGMueSwgcC54LCBwLnkpICYmXG4gICAgICAgICAgICBhcmVhKHAucHJldiwgcCwgcC5uZXh0KSA+PSAwKSByZXR1cm4gZmFsc2U7XG4gICAgICAgIHAgPSBwLnByZXZaO1xuXG4gICAgICAgIGlmIChuICE9PSBlYXIucHJldiAmJiBuICE9PSBlYXIubmV4dCAmJlxuICAgICAgICAgICAgcG9pbnRJblRyaWFuZ2xlKGEueCwgYS55LCBiLngsIGIueSwgYy54LCBjLnksIG4ueCwgbi55KSAmJlxuICAgICAgICAgICAgYXJlYShuLnByZXYsIG4sIG4ubmV4dCkgPj0gMCkgcmV0dXJuIGZhbHNlO1xuICAgICAgICBuID0gbi5uZXh0WjtcbiAgICB9XG5cbiAgICAvLyBsb29rIGZvciByZW1haW5pbmcgcG9pbnRzIGluIGRlY3JlYXNpbmcgei1vcmRlclxuICAgIHdoaWxlIChwICYmIHAueiA+PSBtaW5aKSB7XG4gICAgICAgIGlmIChwICE9PSBlYXIucHJldiAmJiBwICE9PSBlYXIubmV4dCAmJlxuICAgICAgICAgICAgcG9pbnRJblRyaWFuZ2xlKGEueCwgYS55LCBiLngsIGIueSwgYy54LCBjLnksIHAueCwgcC55KSAmJlxuICAgICAgICAgICAgYXJlYShwLnByZXYsIHAsIHAubmV4dCkgPj0gMCkgcmV0dXJuIGZhbHNlO1xuICAgICAgICBwID0gcC5wcmV2WjtcbiAgICB9XG5cbiAgICAvLyBsb29rIGZvciByZW1haW5pbmcgcG9pbnRzIGluIGluY3JlYXNpbmcgei1vcmRlclxuICAgIHdoaWxlIChuICYmIG4ueiA8PSBtYXhaKSB7XG4gICAgICAgIGlmIChuICE9PSBlYXIucHJldiAmJiBuICE9PSBlYXIubmV4dCAmJlxuICAgICAgICAgICAgcG9pbnRJblRyaWFuZ2xlKGEueCwgYS55LCBiLngsIGIueSwgYy54LCBjLnksIG4ueCwgbi55KSAmJlxuICAgICAgICAgICAgYXJlYShuLnByZXYsIG4sIG4ubmV4dCkgPj0gMCkgcmV0dXJuIGZhbHNlO1xuICAgICAgICBuID0gbi5uZXh0WjtcbiAgICB9XG5cbiAgICByZXR1cm4gdHJ1ZTtcbn1cblxuLy8gZ28gdGhyb3VnaCBhbGwgcG9seWdvbiBub2RlcyBhbmQgY3VyZSBzbWFsbCBsb2NhbCBzZWxmLWludGVyc2VjdGlvbnNcbmZ1bmN0aW9uIGN1cmVMb2NhbEludGVyc2VjdGlvbnMoc3RhcnQsIHRyaWFuZ2xlcywgZGltKSB7XG4gICAgdmFyIHAgPSBzdGFydDtcbiAgICBkbyB7XG4gICAgICAgIHZhciBhID0gcC5wcmV2LFxuICAgICAgICAgICAgYiA9IHAubmV4dC5uZXh0O1xuXG4gICAgICAgIGlmICghZXF1YWxzKGEsIGIpICYmIGludGVyc2VjdHMoYSwgcCwg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5leHQpO1xuICAgIH1cbn1cblxuLy8gRGF2aWQgRWJlcmx5J3MgYWxnb3JpdGhtIGZvciBmaW5kaW5nIGEgYnJpZGdlIGJldHdlZW4gaG9sZSBhbmQgb3V0ZXIgcG9seWdvblxuZnVuY3Rpb24gZmluZEhvbGVCcmlkZ2UoaG9sZSwgb3V0ZXJOb2RlKSB7XG4gICAgdmFyIHAgPSBvdXRlck5vZGUsXG4gICAgICAgIGh4ID0gaG9sZS54LFxuICAgICAgICBoeSA9IGhvbGUueSxcbiAgICAgICAgcXggPSAtSW5maW5pdHksXG4gICAgICAgIG07XG5cbiAgICAvLyBmaW5kIGEgc2VnbWVudCBpbnRlcnNlY3RlZCBieSBhIHJheSBmcm9tIHRoZSBob2xlJ3MgbGVmdG1vc3QgcG9pbnQgdG8gdGhlIGxlZnQ7XG4gICAgLy8gc2VnbWVudCdzIGVuZHBvaW50IHdpdGggbGVzc2VyIHggd2lsbCBiZSBwb3RlbnRpYWwgY29ubmVjdGlvbiBwb2ludFxuICAgIGRvIHtcbiAgICAgICAgaWYgKGh5IDw9IHAueSAmJiBoeSA+PSBwLm5leHQueSAmJiBwLm5leHQueSAhPT0gcC55KSB7XG4gICAgICAgICAgICB2YXIgeCA9IHAueCArIChoeSAtIHAueSkgKiAocC5uZXh0LnggLSBwLngpIC8gKHAubmV4dC55IC0gcC55KTtcbiAgICAgICAgICAgIGlmICh4IDw9IGh4ICYmIHggPiBxeCkge1xuICAgICAgICAgICAgICAgIHF4ID0geDtcbiAgICAgICAgICAgICAgICBpZiAoeCA9PT0gaHgpIHtcbiAgICAgICAgICAgICAgICAgICAgaWYgKGh5ID09PSBwLnkpIHJldHVybiBwO1xuICAgICAgICAgICAgICAgICAgICBpZiAoaHkgPT09IHAubmV4dC55KSByZXR1cm4gcC5uZXh0O1xuICAgICAgICAgICAgICAgIH1cbiAgICAgICAgICAgICAgICBtID0gcC54IDwgcC5uZXh0LnggPyBwIDogcC5uZXh0O1xuICAgICAgICAgICAgfVxu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biAgICBwLnByZXZaLm5leHRaID0gbnVsbDtcbiAgICBwLnByZXZaID0gbnVsbDtcblxuICAgIHNvcnRMaW5rZWQocCk7XG59XG5cbi8vIFNpbW9uIFRhdGhhbSdzIGxpbmtlZCBsaXN0IG1lcmdlIHNvcnQgYWxnb3JpdGhtXG4vLyBodHRwOi8vd3d3LmNoaWFyay5ncmVlbmVuZC5vcmcudWsvfnNndGF0aGFtL2FsZ29yaXRobXMvbGlzdHNvcnQuaHRtbFxuZnVuY3Rpb24gc29ydExpbmtlZChsaXN0KSB7XG4gICAgdmFyIGksIHAsIHEsIGUsIHRhaWwsIG51bU1lcmdlcywgcFNpemUsIHFTaXplLFxuICAgICAgICBpblNpemUgPSAxO1xuXG4gICAgZG8ge1xuICAgICAgICBwID0gbGlzdDtcbiAgICAgICAgbGlzdCA9IG51bGw7XG4gICAgICAgIHRhaWwgPSBudWxsO1xuICAgICAgICBudW1NZXJnZXMgPSAwO1xuXG4gICAgICAgIHdoaWxlIChwKSB7XG4gICAgICAgICAgICBudW1NZXJnZXMrKztcbiAgICAgICAgICAgIHEgPSBwO1xuICAgICAgICAgICAgcFNpemUgPSAwO1xuICAgICAgICAgICAgZm9yIChpID0gMDsgaSA8IGluU2l6ZTsgaSsrKSB7XG4gICAgICAgICAgICAgICAgcFNpemUrKztcbiAgICAgICAgICAgICAgICBxID0gcS5uZXh0WjtcbiAgICAgICAgICAgICAgICBpZiAoIXEpIGJyZWFrO1xuICAgICAgICAgICAgfVxuICAgICAgICAgICAgcVNpemUgPSBpblNpemU7XG5cbiAgICAgICAgICAgIHdoaWxlIChwU2l6ZSA+IDAgfHwgKHFTaXplID4gMCAmJiBxKSkge1xuXG4gICAgICAgICAgICAgICAgaWYgKHBTaXplICE9PSAwICYmIChxU2l6ZSA9PT0gMCB8fCAhcSB8fCBwLnogPD0gcS56KSkge1xuICAgICAgICAgICAgICAgICAgICBlID0gcDtcbiAgICAgICAgICAgICAgICAgICAgcCA9IHAubmV4dFo7XG4gICAgICAgICAgICAgICAgICAgIHBTaXplLS07XG4gICAgICAgICAgICAgICAgfSBlbHNlIHtcbiAgICAgICAgICAgICAgICAgICAgZSA9IHE7XG4gICAgICAgICAgICAgICAgICAgIHEgPSBxLm5leHRaO1xuICAgICAgICAgICAgICAgICAgICBxU2l6ZS0tO1xuICAgICAgICAgICAgICAgIH1cblxuICAgICAgICAgICAgICAgIGlmICh0YWlsKSB0YWlsLm5leHRaID0gZTtcbiAgICAgICAgICAgICAgICBlbHNlIGxpc3QgPSBlO1xuXG4gICAgICAgICAgICAgICAgZS5wcmV2WiA9IHRhaWw7XG4gICAgICAgICAgICAgICAgdGFpbCA9IGU7XG4gICAgICAgICAgICB9XG5cbiAgICAgICAgICAgIHAgPSBxO1xuICAgICAgICB9XG5cbiAgICAgICAgdGFpbC5uZXh0WiA9IG51bGw7XG4gICAgICAgIGluU2l6ZSAqPSAyO1xuXG4gICAgfSB3aGlsZSAobnVtTWVyZ2VzID4gMSk7XG5cbiAgICByZXR1cm4gbGlzdDtcbn1cblxuLy8gei1vcmRlciBvZiBhIHBvaW50IGdpdmVuIGNvb3JkcyBhbmQgaW52ZXJzZSBvZiB0aGUgbG9uZ2VyIHNpZGUgb2YgZGF0YSBiYm94XG5mdW5jdGlvbiB6T3JkZXIoeCwgeSwgbWluWCwgbWluWSwgaW52U2l6ZSkge1xuICAgIC8vIGNvb3JkcyBhcmUgdHJhbnNmb3JtZWQgaW50byBub24tbmVnYXRpdmUgMTUtYml0IGludGVnZXIgcmFuZ2VcbiAgICB4ID0gMzI3NjcgKiAoeCAtIG1pblgpICogaW52U2l6ZTtcbiAgICB5ID0gMzI3NjcgKiAoeSAtIG1pblkpICogaW52U2l6ZTtcblxuICAgIHggPSAoeCB8ICh4IDw8IDgpKSAmIDB4MDBGRjAwRkY7XG4gICAgeCA9ICh4IHwgKHggPDwgNCkpICYgMHgwRjBGMEYwRjtcbiAgICB4ID0gKHggfCAoeCA8PCAyKSkgJiAweDMzMzMzMzMzO1xu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hciBzdW0gPSAwO1xuICAgIGZvciAodmFyIGkgPSBzdGFydCwgaiA9IGVuZCAtIGRpbTsgaSA8IGVuZDsgaSArPSBkaW0pIHtcbiAgICAgICAgc3VtICs9IChkYXRhW2pdIC0gZGF0YVtpXSkgKiAoZGF0YVtpICsgMV0gKyBkYXRhW2ogKyAxXSk7XG4gICAgICAgIGogPSBpO1xuICAgIH1cbiAgICByZXR1cm4gc3VtO1xufVxuXG4vLyB0dXJuIGEgcG9seWdvbiBpbiBhIG11bHRpLWRpbWVuc2lvbmFsIGFycmF5IGZvcm0gKGUuZy4gYXMgaW4gR2VvSlNPTikgaW50byBhIGZvcm0gRWFyY3V0IGFjY2VwdHNcbmVhcmN1dC5mbGF0dGVuID0gZnVuY3Rpb24gKGRhdGEpIHtcbiAgICB2YXIgZGltID0gZGF0YVswXVswXS5sZW5ndGgsXG4gICAgICAgIHJlc3VsdCA9IHt2ZXJ0aWNlczogW10sIGhvbGVzOiBbXSwgZGltZW5zaW9uczogZGltfSxcbiAgICAgICAgaG9sZUluZGV4ID0gMDtcblxuICAgIGZvciAodmFyIGkgPSAwOyBpIDwgZGF0YS5sZW5ndGg7IGkrKykge1xuICAgICAgICBmb3IgKHZhciBqID0gMDsgaiA8IGRhdGFbaV0ubGVuZ3RoOyBqKyspIHtcbiAgICAgICAgICAgIGZvciAodmFyIGQgPSAwOyBkIDwgZGltOyBkKyspIHJlc3VsdC52ZXJ0aWNlcy5wdXNoKGRhdGFbaV1bal1bZF0pO1xuICAgICAgICB9XG4gICAgICAgIGlmIChpID4gMCkge1xuICAgICAgICAgICAgaG9sZUluZGV4ICs9IGRhdGFbaSAtIDFdLmxlbmd0aDtcbiAgICAgICAgICAgIHJlc3VsdC5ob2xlcy5wdXNoKGhvbGVJbmRleCk7XG4gICAgICAgIH1cbiAgICB9XG4gICAgcmV0dXJuIHJlc3VsdDtcbn07XG4iXX0=

@@ -1,1 +0,1 @@

!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).earcut=e()}}(function(){return function i(f,u,o){function v(n,e){if(!u[n]){if(!f[n]){var t="function"==typeof require&&require;if(!e&&t)return t(n,!0);if(y)return y(n,!0);var r=new Error("Cannot find module '"+n+"'");throw r.code="MODULE_NOT_FOUND",r}var x=u[n]={exports:{}};f[n][0].call(x.exports,function(e){return v(f[n][1][e]||e)},x,x.exports,i,f,u,o)}return u[n].exports}for(var y="function"==typeof require&&require,e=0;e<o.length;e++)v(o[e]);return v}({1:[function(e,n,t){"use strict";function r(e,n,t){t=t||2;var r,x,i,f,u,o,v,y=n&&n.length,p=y?n[0]*t:e.length,l=h(e,0,p,t,!0),a=[];if(!l||l.next===l.prev)return a;if(y&&(l=function(e,n,t,r){var x,i,f,u,o,v=[];for(x=0,i=n.length;x<i;x++)f=n[x]*r,u=x<i-1?n[x+1]*r:e.length,(o=h(e,f,u,r,!1))===o.next&&(o.steiner=!0),v.push(b(o));for(v.sort(Z),x=0;x<v.length;x++)g(v[x],t),t=c(t,t.next);return t}(e,n,l,t)),e.length>80*t){r=i=e[0],x=f=e[1];for(var s=t;s<p;s+=t)(u=e[s])<r&&(r=u),(o=e[s+1])<x&&(x=o),i<u&&(i=u),f<o&&(f=o);v=0!==(v=Math.max(i-r,f-x))?1/v:0}return d(l,a,t,r,x,v),a}function h(e,n,t,r,x){var i,f;if(x===0<k(e,n,t,r))for(i=n;i<t;i+=r)f=o(i,e[i],e[i+1],f);else for(i=t-r;n<=i;i-=r)f=o(i,e[i],e[i+1],f);return f&&u(f,f.next)&&(O(f),f=f.next),f}function c(e,n){if(!e)return e;n||(n=e);var t,r=e;do{if(t=!1,r.steiner||!u(r,r.next)&&0!==M(r.prev,r,r.next))r=r.next;else{if(O(r),(r=n=r.prev)===r.next)break;t=!0}}while(t||r!==n);return n}function d(e,n,t,r,x,i,f){if(e){!f&&i&&function(e,n,t,r){var x=e;for(;null===x.z&&(x.z=w(x.x,x.y,n,t,r)),x.prevZ=x.prev,x.nextZ=x.next,x=x.next,x!==e;);x.prevZ.nextZ=null,x.prevZ=null,function(e){var n,t,r,x,i,f,u,o,v=1;do{for(t=e,i=e=null,f=0;t;){for(f++,r=t,n=u=0;n<v&&(u++,r=r.nextZ);n++);for(o=v;0<u||0<o&&r;)0!==u&&(0===o||!r||t.z<=r.z)?(t=(x=t).nextZ,u--):(r=(x=r).nextZ,o--),i?i.nextZ=x:e=x,x.prevZ=i,i=x;t=r}i.nextZ=null,v*=2}while(1<f)}(x)}(e,r,x,i);for(var u,o,v=e;e.prev!==e.next;)if(u=e.prev,o=e.next,i?p(e,r,x,i):y(e))n.push(u.i/t),n.push(e.i/t),n.push(o.i/t),O(e),e=o.next,v=o.next;else if((e=o)===v){f?1===f?d(e=l(e,n,t),n,t,r,x,i,2):2===f&&a(e,n,t,r,x,i):d(c(e),n,t,r,x,i,1);break}}}function y(e){var n=e.prev,t=e,r=e.next;if(0<=M(n,t,r))return!1;for(var x=e.next.next;x!==e.prev;){if(z(n.x,n.y,t.x,t.y,r.x,r.y,x.x,x.y)&&0<=M(x.prev,x,x.next))return!1;x=x.next}return!0}function p(e,n,t,r){var x=e.prev,i=e,f=e.next;if(0<=M(x,i,f))return!1;for(var u=x.x<i.x?x.x<f.x?x.x:f.x:i.x<f.x?i.x:f.x,o=x.y<i.y?x.y<f.y?x.y:f.y:i.y<f.y?i.y:f.y,v=x.x>i.x?x.x>f.x?x.x:f.x:i.x>f.x?i.x:f.x,y=x.y>i.y?x.y>f.y?x.y:f.y:i.y>f.y?i.y:f.y,p=w(u,o,n,t,r),l=w(v,y,n,t,r),a=e.prevZ,s=e.nextZ;a&&a.z>=p&&s&&s.z<=l;){if(a!==e.prev&&a!==e.next&&z(x.x,x.y,i.x,i.y,f.x,f.y,a.x,a.y)&&0<=M(a.prev,a,a.next))return!1;if(a=a.prevZ,s!==e.prev&&s!==e.next&&z(x.x,x.y,i.x,i.y,f.x,f.y,s.x,s.y)&&0<=M(s.prev,s,s.next))return!1;s=s.nextZ}for(;a&&a.z>=p;){if(a!==e.prev&&a!==e.next&&z(x.x,x.y,i.x,i.y,f.x,f.y,a.x,a.y)&&0<=M(a.prev,a,a.next))return!1;a=a.prevZ}for(;s&&s.z<=l;){if(s!==e.prev&&s!==e.next&&z(x.x,x.y,i.x,i.y,f.x,f.y,s.x,s.y)&&0<=M(s.prev,s,s.next))return!1;s=s.nextZ}return!0}function l(e,n,t){var r=e;do{var x=r.prev,i=r.next.next;!u(x,i)&&s(x,r,r.next,i)&&m(x,i)&&m(i,x)&&(n.push(x.i/t),n.push(r.i/t),n.push(i.i/t),O(r),O(r.next),r=e=i),r=r.next}while(r!==e);return r}function a(e,n,t,r,x,i){var f,u,o=e;do{for(var v=o.next.next;v!==o.prev;){if(o.i!==v.i&&(u=v,(f=o).next.i!==u.i&&f.prev.i!==u.i&&!function(e,n){var t=e;do{if(t.i!==e.i&&t.next.i!==e.i&&t.i!==n.i&&t.next.i!==n.i&&s(t,t.next,e,n))return!0;t=t.next}while(t!==e);return!1}(f,u)&&m(f,u)&&m(u,f)&&function(e,n){var t=e,r=!1,x=(e.x+n.x)/2,i=(e.y+n.y)/2;for(;t.y>i!=t.next.y>i&&t.next.y!==t.y&&x<(t.next.x-t.x)*(i-t.y)/(t.next.y-t.y)+t.x&&(r=!r),t=t.next,t!==e;);return r}(f,u))){var y=q(o,v);return o=c(o,o.next),y=c(y,y.next),d(o,n,t,r,x,i),void d(y,n,t,r,x,i)}v=v.next}o=o.next}while(o!==e)}function Z(e,n){return e.x-n.x}function g(e,n){if(n=function(e,n){var t,r=n,x=e.x,i=e.y,f=-1/0;do{if(i<=r.y&&i>=r.next.y&&r.next.y!==r.y){var u=r.x+(i-r.y)*(r.next.x-r.x)/(r.next.y-r.y);if(u<=x&&f<u){if((f=u)===x){if(i===r.y)return r;if(i===r.next.y)return r.next}t=r.x<r.next.x?r:r.next}}r=r.next}while(r!==n);if(!t)return null;if(x===f)return t.prev;var o,v=t,y=t.x,p=t.y,l=1/0;r=t.next;for(;r!==v;)x>=r.x&&r.x>=y&&x!==r.x&&z(i<p?x:f,i,y,p,i<p?f:x,i,r.x,r.y)&&((o=Math.abs(i-r.y)/(x-r.x))<l||o===l&&r.x>t.x)&&m(r,e)&&(t=r,l=o),r=r.next;return t}(e,n)){var t=q(n,e);c(t,t.next)}}function w(e,n,t,r,x){return(e=1431655765&((e=858993459&((e=252645135&((e=16711935&((e=32767*(e-t)*x)|e<<8))|e<<4))|e<<2))|e<<1))|(n=1431655765&((n=858993459&((n=252645135&((n=16711935&((n=32767*(n-r)*x)|n<<8))|n<<4))|n<<2))|n<<1))<<1}function b(e){for(var n=e,t=e;(n.x<t.x||n.x===t.x&&n.y<t.y)&&(t=n),(n=n.next)!==e;);return t}function z(e,n,t,r,x,i,f,u){return 0<=(x-f)*(n-u)-(e-f)*(i-u)&&0<=(e-f)*(r-u)-(t-f)*(n-u)&&0<=(t-f)*(i-u)-(x-f)*(r-u)}function M(e,n,t){return(n.y-e.y)*(t.x-n.x)-(n.x-e.x)*(t.y-n.y)}function u(e,n){return e.x===n.x&&e.y===n.y}function s(e,n,t,r){return!!(u(e,n)&&u(t,r)||u(e,r)&&u(t,n))||0<M(e,n,t)!=0<M(e,n,r)&&0<M(t,r,e)!=0<M(t,r,n)}function m(e,n){return M(e.prev,e,e.next)<0?0<=M(e,n,e.next)&&0<=M(e,e.prev,n):M(e,n,e.prev)<0||M(e,e.next,n)<0}function q(e,n){var t=new f(e.i,e.x,e.y),r=new f(n.i,n.x,n.y),x=e.next,i=n.prev;return(e.next=n).prev=e,(t.next=x).prev=t,(r.next=t).prev=r,(i.next=r).prev=i,r}function o(e,n,t,r){var x=new f(e,n,t);return r?(x.next=r.next,(x.prev=r).next.prev=x,r.next=x):(x.prev=x).next=x,x}function O(e){e.next.prev=e.prev,e.prev.next=e.next,e.prevZ&&(e.prevZ.nextZ=e.nextZ),e.nextZ&&(e.nextZ.prevZ=e.prevZ)}function f(e,n,t){this.i=e,this.x=n,this.y=t,this.prev=null,this.next=null,this.z=null,this.prevZ=null,this.nextZ=null,this.steiner=!1}function k(e,n,t,r){for(var x=0,i=n,f=t-r;i<t;i+=r)x+=(e[f]-e[i])*(e[i+1]+e[f+1]),f=i;return x}n.exports=r,(n.exports.default=r).deviation=function(e,n,t,r){var x=n&&n.length,i=x?n[0]*t:e.length,f=Math.abs(k(e,0,i,t));if(x)for(var u=0,o=n.length;u<o;u++){var v=n[u]*t,y=u<o-1?n[u+1]*t:e.length;f-=Math.abs(k(e,v,y,t))}var p=0;for(u=0;u<r.length;u+=3){var l=r[u]*t,a=r[u+1]*t,s=r[u+2]*t;p+=Math.abs((e[l]-e[s])*(e[a+1]-e[l+1])-(e[l]-e[a])*(e[s+1]-e[l+1]))}return 0===f&&0===p?0:Math.abs((p-f)/f)},r.flatten=function(e){for(var n=e[0][0].length,t={vertices:[],holes:[],dimensions:n},r=0,x=0;x<e.length;x++){for(var i=0;i<e[x].length;i++)for(var f=0;f<n;f++)t.vertices.push(e[x][i][f]);0<x&&(r+=e[x-1].length,t.holes.push(r))}return t}},{}]},{},[1])(1)});
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).earcut=e()}}(function(){return function i(u,f,o){function v(n,e){if(!f[n]){if(!u[n]){var t="function"==typeof require&&require;if(!e&&t)return t(n,!0);if(y)return y(n,!0);var r=new Error("Cannot find module '"+n+"'");throw r.code="MODULE_NOT_FOUND",r}var x=f[n]={exports:{}};u[n][0].call(x.exports,function(e){return v(u[n][1][e]||e)},x,x.exports,i,u,f,o)}return f[n].exports}for(var y="function"==typeof require&&require,e=0;e<o.length;e++)v(o[e]);return v}({1:[function(e,n,t){"use strict";function r(e,n,t){t=t||2;var r,x,i,u,f,o,v,y=n&&n.length,p=y?n[0]*t:e.length,a=s(e,0,p,t,!0),l=[];if(!a||a.next===a.prev)return l;if(y&&(a=function(e,n,t,r){var x,i,u,f,o,v=[];for(x=0,i=n.length;x<i;x++)u=n[x]*r,f=x<i-1?n[x+1]*r:e.length,(o=s(e,u,f,r,!1))===o.next&&(o.steiner=!0),v.push(M(o));for(v.sort(Z),x=0;x<v.length;x++)g(v[x],t),t=c(t,t.next);return t}(e,n,a,t)),e.length>80*t){r=i=e[0],x=u=e[1];for(var h=t;h<p;h+=t)(f=e[h])<r&&(r=f),(o=e[h+1])<x&&(x=o),i<f&&(i=f),u<o&&(u=o);v=0!==(v=Math.max(i-r,u-x))?1/v:0}return d(a,l,t,r,x,v),l}function s(e,n,t,r,x){var i,u;if(x===0<D(e,n,t,r))for(i=n;i<t;i+=r)u=f(i,e[i],e[i+1],u);else for(i=t-r;n<=i;i-=r)u=f(i,e[i],e[i+1],u);return u&&h(u,u.next)&&(k(u),u=u.next),u}function c(e,n){if(!e)return e;n=n||e;var t,r=e;do{if(t=!1,r.steiner||!h(r,r.next)&&0!==m(r.prev,r,r.next))r=r.next;else{if(k(r),(r=n=r.prev)===r.next)break;t=!0}}while(t||r!==n);return n}function d(e,n,t,r,x,i,u){if(e){!u&&i&&function(e,n,t,r){var x=e;for(;null===x.z&&(x.z=w(x.x,x.y,n,t,r)),x.prevZ=x.prev,x.nextZ=x.next,x=x.next,x!==e;);x.prevZ.nextZ=null,x.prevZ=null,function(e){var n,t,r,x,i,u,f,o,v=1;do{for(t=e,i=e=null,u=0;t;){for(u++,r=t,n=f=0;n<v&&(f++,r=r.nextZ);n++);for(o=v;0<f||0<o&&r;)0!==f&&(0===o||!r||t.z<=r.z)?(t=(x=t).nextZ,f--):(r=(x=r).nextZ,o--),i?i.nextZ=x:e=x,x.prevZ=i,i=x;t=r}i.nextZ=null,v*=2}while(1<u)}(x)}(e,r,x,i);for(var f,o,v=e;e.prev!==e.next;)if(f=e.prev,o=e.next,i?p(e,r,x,i):y(e))n.push(f.i/t),n.push(e.i/t),n.push(o.i/t),k(e),e=o.next,v=o.next;else if((e=o)===v){u?1===u?d(e=a(c(e),n,t),n,t,r,x,i,2):2===u&&l(e,n,t,r,x,i):d(c(e),n,t,r,x,i,1);break}}}function y(e){var n=e.prev,t=e,r=e.next;if(0<=m(n,t,r))return!1;for(var x=e.next.next;x!==e.prev;){if(b(n.x,n.y,t.x,t.y,r.x,r.y,x.x,x.y)&&0<=m(x.prev,x,x.next))return!1;x=x.next}return!0}function p(e,n,t,r){var x=e.prev,i=e,u=e.next;if(0<=m(x,i,u))return!1;for(var f=x.x<i.x?x.x<u.x?x.x:u.x:i.x<u.x?i.x:u.x,o=x.y<i.y?x.y<u.y?x.y:u.y:i.y<u.y?i.y:u.y,v=x.x>i.x?x.x>u.x?x.x:u.x:i.x>u.x?i.x:u.x,y=x.y>i.y?x.y>u.y?x.y:u.y:i.y>u.y?i.y:u.y,p=w(f,o,n,t,r),a=w(v,y,n,t,r),l=e.prevZ,h=e.nextZ;l&&l.z>=p&&h&&h.z<=a;){if(l!==e.prev&&l!==e.next&&b(x.x,x.y,i.x,i.y,u.x,u.y,l.x,l.y)&&0<=m(l.prev,l,l.next))return!1;if(l=l.prevZ,h!==e.prev&&h!==e.next&&b(x.x,x.y,i.x,i.y,u.x,u.y,h.x,h.y)&&0<=m(h.prev,h,h.next))return!1;h=h.nextZ}for(;l&&l.z>=p;){if(l!==e.prev&&l!==e.next&&b(x.x,x.y,i.x,i.y,u.x,u.y,l.x,l.y)&&0<=m(l.prev,l,l.next))return!1;l=l.prevZ}for(;h&&h.z<=a;){if(h!==e.prev&&h!==e.next&&b(x.x,x.y,i.x,i.y,u.x,u.y,h.x,h.y)&&0<=m(h.prev,h,h.next))return!1;h=h.nextZ}return!0}function a(e,n,t){var r=e;do{var x=r.prev,i=r.next.next;!h(x,i)&&z(x,r,r.next,i)&&q(x,i)&&q(i,x)&&(n.push(x.i/t),n.push(r.i/t),n.push(i.i/t),k(r),k(r.next),r=e=i),r=r.next}while(r!==e);return c(r)}function l(e,n,t,r,x,i){var u,f,o=e;do{for(var v=o.next.next;v!==o.prev;){if(o.i!==v.i&&(f=v,(u=o).next.i!==f.i&&u.prev.i!==f.i&&!function(e,n){var t=e;do{if(t.i!==e.i&&t.next.i!==e.i&&t.i!==n.i&&t.next.i!==n.i&&z(t,t.next,e,n))return!0;t=t.next}while(t!==e);return!1}(u,f)&&(q(u,f)&&q(f,u)&&function(e,n){var t=e,r=!1,x=(e.x+n.x)/2,i=(e.y+n.y)/2;for(;t.y>i!=t.next.y>i&&t.next.y!==t.y&&x<(t.next.x-t.x)*(i-t.y)/(t.next.y-t.y)+t.x&&(r=!r),t=t.next,t!==e;);return r}(u,f)&&(m(u.prev,u,f.prev)||m(u,f.prev,f))||h(u,f)&&0<m(u.prev,u,u.next)&&0<m(f.prev,f,f.next)))){var y=O(o,v);return o=c(o,o.next),y=c(y,y.next),d(o,n,t,r,x,i),void d(y,n,t,r,x,i)}v=v.next}o=o.next}while(o!==e)}function Z(e,n){return e.x-n.x}function g(e,n){if(n=function(e,n){var t,r=n,x=e.x,i=e.y,u=-1/0;do{if(i<=r.y&&i>=r.next.y&&r.next.y!==r.y){var f=r.x+(i-r.y)*(r.next.x-r.x)/(r.next.y-r.y);if(f<=x&&u<f){if((u=f)===x){if(i===r.y)return r;if(i===r.next.y)return r.next}t=r.x<r.next.x?r:r.next}}r=r.next}while(r!==n);if(!t)return null;if(x===u)return t;var o,v=t,y=t.x,p=t.y,a=1/0;r=t;for(;x>=r.x&&r.x>=y&&x!==r.x&&b(i<p?x:u,i,y,p,i<p?u:x,i,r.x,r.y)&&(o=Math.abs(i-r.y)/(x-r.x),q(r,e)&&(o<a||o===a&&(r.x>t.x||r.x===t.x&&(h=r,(m((l=t).prev,l,h.prev)<0||m(h.prev,l,l.next)<0)&&(m(l.prev,l,h.next)<0||m(h.next,l,l.next)<0))))&&(t=r,a=o)),r=r.next,r!==v;);var l,h;return t}(e,n)){var t=O(n,e);c(t,t.next)}}function w(e,n,t,r,x){return(e=1431655765&((e=858993459&((e=252645135&((e=16711935&((e=32767*(e-t)*x)|e<<8))|e<<4))|e<<2))|e<<1))|(n=1431655765&((n=858993459&((n=252645135&((n=16711935&((n=32767*(n-r)*x)|n<<8))|n<<4))|n<<2))|n<<1))<<1}function M(e){for(var n=e,t=e;(n.x<t.x||n.x===t.x&&n.y<t.y)&&(t=n),(n=n.next)!==e;);return t}function b(e,n,t,r,x,i,u,f){return 0<=(x-u)*(n-f)-(e-u)*(i-f)&&0<=(e-u)*(r-f)-(t-u)*(n-f)&&0<=(t-u)*(i-f)-(x-u)*(r-f)}function m(e,n,t){return(n.y-e.y)*(t.x-n.x)-(n.x-e.x)*(t.y-n.y)}function h(e,n){return e.x===n.x&&e.y===n.y}function z(e,n,t,r){var x=v(m(e,n,t)),i=v(m(e,n,r)),u=v(m(t,r,e)),f=v(m(t,r,n));return x!==i&&u!==f||(!(0!==x||!o(e,t,n))||(!(0!==i||!o(e,r,n))||(!(0!==u||!o(t,e,r))||!(0!==f||!o(t,n,r)))))}function o(e,n,t){return n.x<=Math.max(e.x,t.x)&&n.x>=Math.min(e.x,t.x)&&n.y<=Math.max(e.y,t.y)&&n.y>=Math.min(e.y,t.y)}function v(e){return 0<e?1:e<0?-1:0}function q(e,n){return m(e.prev,e,e.next)<0?0<=m(e,n,e.next)&&0<=m(e,e.prev,n):m(e,n,e.prev)<0||m(e,e.next,n)<0}function O(e,n){var t=new u(e.i,e.x,e.y),r=new u(n.i,n.x,n.y),x=e.next,i=n.prev;return(e.next=n).prev=e,(t.next=x).prev=t,(r.next=t).prev=r,(i.next=r).prev=i,r}function f(e,n,t,r){var x=new u(e,n,t);return r?(x.next=r.next,(x.prev=r).next.prev=x,r.next=x):(x.prev=x).next=x,x}function k(e){e.next.prev=e.prev,e.prev.next=e.next,e.prevZ&&(e.prevZ.nextZ=e.nextZ),e.nextZ&&(e.nextZ.prevZ=e.prevZ)}function u(e,n,t){this.i=e,this.x=n,this.y=t,this.prev=null,this.next=null,this.z=null,this.prevZ=null,this.nextZ=null,this.steiner=!1}function D(e,n,t,r){for(var x=0,i=n,u=t-r;i<t;i+=r)x+=(e[u]-e[i])*(e[i+1]+e[u+1]),u=i;return x}n.exports=r,(n.exports.default=r).deviation=function(e,n,t,r){var x=n&&n.length,i=x?n[0]*t:e.length,u=Math.abs(D(e,0,i,t));if(x)for(var f=0,o=n.length;f<o;f++){var v=n[f]*t,y=f<o-1?n[f+1]*t:e.length;u-=Math.abs(D(e,v,y,t))}var p=0;for(f=0;f<r.length;f+=3){var a=r[f]*t,l=r[f+1]*t,h=r[f+2]*t;p+=Math.abs((e[a]-e[h])*(e[1+l]-e[1+a])-(e[a]-e[l])*(e[1+h]-e[1+a]))}return 0===u&&0===p?0:Math.abs((p-u)/u)},r.flatten=function(e){for(var n=e[0][0].length,t={vertices:[],holes:[],dimensions:n},r=0,x=0;x<e.length;x++){for(var i=0;i<e[x].length;i++)for(var u=0;u<n;u++)t.vertices.push(e[x][i][u]);0<x&&(r+=e[x-1].length,t.holes.push(r))}return t}},{}]},{},[1])(1)});
{
"name": "earcut",
"version": "2.1.5",
"version": "2.2.0",
"description": "The fastest and smallest JavaScript polygon triangulation library for your WebGL apps",

@@ -16,3 +16,3 @@ "main": "src/earcut.js",

"build-dev": "mkdirp dist && browserify -d src/earcut.js -s earcut > dist/earcut.dev.js",
"build-min": "mkdirp dist && browserify src/earcut.js -s earcut | uglifyjs -c warnings=false -m > dist/earcut.min.js",
"build-min": "mkdirp dist && browserify src/earcut.js -s earcut | uglifyjs -c -m > dist/earcut.min.js",
"prepare": "npm run build-dev && npm run build-min",

@@ -26,4 +26,4 @@ "cov": "istanbul cover test/*.js",

"benchmark": "^2.1.4",
"browserify": "^16.2.3",
"coveralls": "^3.0.2",
"browserify": "^16.5.0",
"coveralls": "^3.0.6",
"eslint": "^4.19.1",

@@ -33,5 +33,5 @@ "eslint-config-mourner": "^2.0.3",

"mkdirp": "^0.5.1",
"tape": "^4.9.2",
"uglify-js": "^3.4.9",
"watchify": "^3.11.0"
"tape": "^4.11.0",
"uglify-js": "^3.6.0",
"watchify": "^3.11.1"
},

@@ -38,0 +38,0 @@ "eslintConfig": {

@@ -98,4 +98,4 @@ ## Earcut

- [development build](https://unpkg.com/earcut@2.1.4/dist/earcut.dev.js)
- [minified production build](https://unpkg.com/earcut@2.1.4/dist/earcut.min.js)
- [development build](https://unpkg.com/earcut@2.1.5/dist/earcut.dev.js)
- [minified production build](https://unpkg.com/earcut@2.1.5/dist/earcut.min.js)

@@ -117,2 +117,6 @@ Running tests:

##### 2.1.5 (Feb 5, 2019)
- Fixed a race condition with coincident holes that could lead to bad triangulation.
##### 2.1.4 (Dec 4, 2018)

@@ -119,0 +123,0 @@

@@ -127,3 +127,3 @@ 'use strict';

} else if (pass === 1) {
ear = cureLocalIntersections(ear, triangles, dim);
ear = cureLocalIntersections(filterPoints(ear), triangles, dim);
earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);

@@ -235,3 +235,3 @@

return p;
return filterPoints(p);
}

@@ -329,3 +329,3 @@

if (hx === qx) return m.prev; // hole touches outer segment; pick lower endpoint
if (hx === qx) return m; // hole touches outer segment; pick leftmost endpoint

@@ -342,5 +342,5 @@ // look for points inside the triangle of hole point, segment intersection and endpoint;

p = m.next;
p = m;
while (p !== stop) {
do {
if (hx >= p.x && p.x >= mx && hx !== p.x &&

@@ -351,3 +351,4 @@ pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {

if ((tan < tanMin || (tan === tanMin && p.x > m.x)) && locallyInside(p, hole)) {
if (locallyInside(p, hole) &&
(tan < tanMin || (tan === tanMin && (p.x > m.x || (p.x === m.x && sectorContainsSector(m, p)))))) {
m = p;

@@ -359,3 +360,3 @@ tanMin = tan;

p = p.next;
}
} while (p !== stop);

@@ -365,2 +366,10 @@ return m;

// whether sector in vertex m contains sector in vertex p in the same coordinates
function sectorContainsSector(m, p) {
return (
(area(m.prev, m, p.prev) < 0 || area(p.prev, m, m.next) < 0) &&
(area(m.prev, m, p.next) < 0 || area(p.next, m, m.next) < 0)
);
}
// interlink polygon nodes in z-order

@@ -475,4 +484,6 @@ function indexCurve(start, minX, minY, invSize) {

function isValidDiagonal(a, b) {
return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) &&
locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b);
return a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(a, b) && // dones't intersect other edges
(locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b) && // locally visible
(area(a.prev, a, b.prev) || area(a, b.prev, b)) || // does not create opposite-facing sectors
equals(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0); // special zero-length case
}

@@ -492,8 +503,26 @@

function intersects(p1, q1, p2, q2) {
if ((equals(p1, q1) && equals(p2, q2)) ||
(equals(p1, q2) && equals(p2, q1))) return true;
return area(p1, q1, p2) > 0 !== area(p1, q1, q2) > 0 &&
area(p2, q2, p1) > 0 !== area(p2, q2, q1) > 0;
var o1 = sign(area(p1, q1, p2));
var o2 = sign(area(p1, q1, q2));
var o3 = sign(area(p2, q2, p1));
var o4 = sign(area(p2, q2, q1));
if (o1 !== o2 && o3 !== o4) return true; // general case
if (o1 === 0 && onSegment(p1, p2, q1)) return true; // p1, q1 and p2 are collinear and p2 lies on p1q1
if (o2 === 0 && onSegment(p1, q2, q1)) return true; // p1, q1 and q2 are collinear and q2 lies on p1q1
if (o3 === 0 && onSegment(p2, p1, q2)) return true; // p2, q2 and p1 are collinear and p1 lies on p2q2
if (o4 === 0 && onSegment(p2, q1, q2)) return true; // p2, q2 and q1 are collinear and q1 lies on p2q2
return false;
}
// for collinear points p, q, r, check if point q lies on segment pr
function onSegment(p, q, r) {
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);
}
function sign(num) {
return num > 0 ? 1 : num < 0 ? -1 : 0;
}
// check if a polygon diagonal intersects any polygon segments

@@ -500,0 +529,0 @@ function intersectsPolygon(a, b) {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc