Skip to content

Commit c0c5ecb

Browse files
authored
Guard against degenerate edges (#134)
JS Array#Sort doesn't bomb on NaN, but std::sort does
1 parent f917bfc commit c0c5ecb

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

include/mapbox/earcut.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,9 +561,12 @@ typename Earcut<N>::Node* Earcut<N>::eliminateHoles(const Polygon& points, Node*
561561
std::sort(holeQueue.begin(), holeQueue.end(), [](const Node* a, const Node* b) {
562562
if (a->x != b->x) return a->x < b->x;
563563
if (a->y != b->y) return a->y < b->y;
564-
const double aSlope = (a->next->y - a->y) / (a->next->x - a->x);
565-
const double bSlope = (b->next->y - b->y) / (b->next->x - b->x);
566-
return aSlope < bSlope;
564+
const double adx = a->next->x - a->x, ady = a->next->y - a->y;
565+
const double bdx = b->next->x - b->x, bdy = b->next->y - b->y;
566+
const bool aDegenerate = adx == 0 && ady == 0;
567+
const bool bDegenerate = bdx == 0 && bdy == 0;
568+
if (aDegenerate != bDegenerate) return aDegenerate;
569+
return ady * bdx < bdy * adx;
567570
});
568571

569572
// block-bbox index for findHoleBridge, grown append-only as holes merge. Seed it with the

0 commit comments

Comments
 (0)