placeCut function
Where an opening centred at c with width w cuts (spec 08 D8), among
stretches (sorted, as stretchesOf gives them):
- the candidates are the stretches at least
wlong; - none: no fit, null;
- the chosen one is the candidate nearest
c(the distance fromcto the interval, 0 inside it); ties go to the lowera; - the cut is
[x, x + w]withx = clamp(c − w/2, a, b − w), and it isclampedwhenx ≠ c − w/2: exact, since an unclampedxisc − w/2itself.
Null too for a degenerate opening (D6): a width not greater than
wallJoin.linear, or a non-finite width or centre.
Implementation
Cut? placeCut(List<(double, double)> stretches, double c, double w) {
if (!c.isFinite || !w.isFinite || !(w > wallJoin.linear)) return null;
(double, double)? best;
var bestDistance = double.infinity;
for (final s in stretches) {
final (a, b) = s;
if (!(w <= b - a)) continue;
final distance = c < a ? a - c : (c > b ? c - b : 0.0);
if (distance < bestDistance) {
best = s;
bestDistance = distance;
}
}
if (best == null) return null;
final (a, b) = best;
final want = c - w / 2;
var x = want;
if (x < a) x = a;
if (x > b - w) x = b - w;
return (a: x, b: x + w, clamped: x != want);
}