Skip to content

Commit

Permalink
fix cases of bad diagonals in the split routine
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Mar 17, 2016
1 parent 7bd7a0b commit d13c8dc
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/earcut.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {

// check if a diagonal between two polygon nodes is valid (lies in polygon interior)
function isValidDiagonal(a, b) {
return equals(a, b) || a.next.i !== b.i && a.prev.i !== b.i && !intersectsPolygon(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);
}

Expand All @@ -476,6 +476,8 @@ function equals(p1, p2) {

// check if two segments intersect
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;
}
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/bad-diagonals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[[440,4152],[440,4208],[296,4192],[368,4192],[400,4200],[400,4176],[368,4192],[296,4192],[264,4200],[288,4160],[296,4192]]]
3 changes: 2 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ areaTest('water2', 1212);
areaTest('water3', 197);
areaTest('water3b', 25);
areaTest('water4', 705);
areaTest('water-huge', 5173, 0.0011);
areaTest('water-huge', 5174, 0.0011);
areaTest('water-huge2', 4461, 0.0028);
areaTest('degenerate', 0);
areaTest('bad-hole', 42, 0.019);
Expand All @@ -34,6 +34,7 @@ areaTest('eberly-3', 73);
areaTest('eberly-6', 1429);
areaTest('issue52', 109);
areaTest('shared-points', 4);
areaTest('bad-diagonals', 7);

test('indices-2d', function (t) {
var indices = earcut([10, 0, 0, 50, 60, 60, 70, 10]);
Expand Down

0 comments on commit d13c8dc

Please sign in to comment.