@@ -177,9 +177,9 @@ void Earcut<N>::operator()(const Polygon& points) {
177
177
p = p->next ;
178
178
} while (p != outerNode);
179
179
180
- // minX, minY and size are later used to transform coords into integers for z-order calculation
180
+ // minX, minY and inv_size are later used to transform coords into integers for z-order calculation
181
181
inv_size = std::max<double >(maxX - minX, maxY - minY);
182
- inv_size = inv_size != .0 ? (1 . / inv_size) : .0 ;
182
+ inv_size = inv_size != .0 ? (32767 . / inv_size) : .0 ;
183
183
}
184
184
185
185
earcutLinked (outerNode);
@@ -443,7 +443,6 @@ Earcut<N>::eliminateHoles(const Polygon& points, Node* outerNode) {
443
443
// process holes from left to right
444
444
for (size_t i = 0 ; i < queue.size (); i++) {
445
445
outerNode = eliminateHole (queue[i], outerNode);
446
- outerNode = filterPoints (outerNode, outerNode->next );
447
446
}
448
447
449
448
return outerNode;
@@ -461,11 +460,10 @@ Earcut<N>::eliminateHole(Node* hole, Node* outerNode) {
461
460
Node* bridgeReverse = splitPolygon (bridge, hole);
462
461
463
462
// filter collinear points around the cuts
464
- Node* filteredBridge = filterPoints (bridge, bridge->next );
465
463
filterPoints (bridgeReverse, bridgeReverse->next );
466
464
467
465
// Check if input node was removed by the filtering
468
- return outerNode == bridge ? filteredBridge : outerNode ;
466
+ return filterPoints ( bridge, bridge-> next ) ;
469
467
}
470
468
471
469
// David Eberly's algorithm for finding a bridge between hole and outer polygon
@@ -485,20 +483,15 @@ Earcut<N>::findHoleBridge(Node* hole, Node* outerNode) {
485
483
double x = p->x + (hy - p->y ) * (p->next ->x - p->x ) / (p->next ->y - p->y );
486
484
if (x <= hx && x > qx) {
487
485
qx = x;
488
- if (x == hx) {
489
- if (hy == p->y ) return p;
490
- if (hy == p->next ->y ) return p->next ;
491
- }
492
486
m = p->x < p->next ->x ? p : p->next ;
487
+ if (x == hx) return m; // hole touches outer segment; pick leftmost endpoint
493
488
}
494
489
}
495
490
p = p->next ;
496
491
} while (p != outerNode);
497
492
498
493
if (!m) return 0 ;
499
494
500
- if (hx == qx) return m; // hole touches outer segment; pick leftmost endpoint
501
-
502
495
// look for points inside the triangle of hole Vertex, segment intersection and endpoint;
503
496
// if there are no points found, we have a valid connection;
504
497
// otherwise choose the Vertex of the minimum angle with the ray as connection Vertex
@@ -628,8 +621,8 @@ Earcut<N>::sortLinked(Node* list) {
628
621
template <typename N>
629
622
int32_t Earcut<N>::zOrder(const double x_, const double y_) {
630
623
// coords are transformed into non-negative 15-bit integer range
631
- int32_t x = static_cast <int32_t >(32767.0 * (x_ - minX) * inv_size);
632
- int32_t y = static_cast <int32_t >(32767.0 * (y_ - minY) * inv_size);
624
+ int32_t x = static_cast <int32_t >((x_ - minX) * inv_size);
625
+ int32_t y = static_cast <int32_t >((y_ - minY) * inv_size);
633
626
634
627
x = (x | (x << 8 )) & 0x00FF00FF ;
635
628
x = (x | (x << 4 )) & 0x0F0F0F0F ;
@@ -662,9 +655,9 @@ Earcut<N>::getLeftmost(Node* start) {
662
655
// check if a point lies within a convex triangle
663
656
template <typename N>
664
657
bool Earcut<N>::pointInTriangle(double ax, double ay, double bx, double by, double cx, double cy, double px, double py) const {
665
- return (cx - px) * (ay - py) - (ax - px) * (cy - py) >= 0 &&
666
- (ax - px) * (by - py) - (bx - px) * (ay - py) >= 0 &&
667
- (bx - px) * (cy - py) - (cx - px) * (by - py) >= 0 ;
658
+ return (cx - px) * (ay - py) >= (ax - px) * (cy - py) &&
659
+ (ax - px) * (by - py) >= (bx - px) * (ay - py) &&
660
+ (bx - px) * (cy - py) >= (cx - px) * (by - py);
668
661
}
669
662
670
663
// check if a diagonal between two polygon nodes is valid (lies in polygon interior)
0 commit comments