Skip to content

Commit

Permalink
fix race condition with coincident holes
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Sep 18, 2019
1 parent 2de038e commit c4c216e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/earcut.js
Expand Up @@ -336,26 +336,35 @@ function findHoleBridge(hole, outerNode) {
tanMin = Infinity,
tan;

p = m.next;
p = m;

while (p !== stop) {
do {
if (hx >= p.x && p.x >= mx && hx !== p.x &&
pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)) {

tan = Math.abs(hy - p.y) / (hx - p.x); // tangential

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;
tanMin = tan;
}
}

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

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
function indexCurve(start, minX, minY, invSize) {
var p = start;
Expand Down
3 changes: 2 additions & 1 deletion test/expected.json
Expand Up @@ -34,7 +34,8 @@
"issue107": 0,
"issue111": 19,
"boxy": 57,
"collinear-diagonal": 14
"collinear-diagonal": 14,
"issue119": 18
},
"errors": {
"dude": 2e-15,
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/issue119.json
@@ -0,0 +1,7 @@
[
[[2,12],[2,20],[25,20],[25,12]],
[[7,18],[7,15],[5,15]],
[[19,18],[19,17],[17,17]],
[[19,17],[21,17],[19,16]],
[[7,15],[9,15],[7,13]]
]

0 comments on commit c4c216e

Please sign in to comment.