improve gap routing. add adaptive shell fallback on failure

This commit is contained in:
Stewart Allen 2025-09-28 22:39:56 -04:00
commit f53d6da12b
3 changed files with 42 additions and 13 deletions

View file

@ -1323,15 +1323,15 @@ export class Polygon {
pp2.pos = ip2;
}
});
list.sort(function(p1, p2) {
return util.distSq(lp1, p1) - util.distSq(lp1, p2);
});
if (deep && this.inner) {
this.inner.forEach(p => {
let ints = p.intersections(lp1, lp2);
if (ints) list.appendAll(ints);
});
}
list.sort(function(p1, p2) {
return util.distSq(lp1, p1) - util.distSq(lp1, p2);
});
return list;
}
@ -1392,6 +1392,23 @@ export class Polygon {
return poly;
}
/**
* emit the shortest poly that connects two points on a closed CW poly
* because traveling around the 'end point' may yield a shorter route.
* if the poly is open, use emitSegment()
*/
emitShortestSegment(i1, i2) {
if (this.open) {
return this.emitSegment(i1, i2);
}
let start = i1.p2.pos,
end = i2.p1.pos,
points = this.points,
seg0 = new Polygon([ i1, ...points.slice(start, end + 1), i2 ]).setOpen(),
seg1 = new Polygon([ i2, ...points.slice(end +1 ), ...points.slice(0, start), i1 ]).setOpen();
return seg0.perimeter() < seg1.perimeter() ? seg0 : seg1;
}
/**
* @param {Polygon} poly
* @param {number} [tolerance]