feat: solve complete junction island contacts

This commit is contained in:
que01 committed 2026-09-14 08:59:32 +08:00
1 parent 7a7912f302
commit 1b91e280bc
5 files changed
+164 -7

No files matched your search

@@ -104,3 +104,94 @@ function crossingArcSupport(ring, halfWidth) {
return candidates.length ? Math.max(...candidates) : null;
}
module.exports.crossingArcSupport = crossingArcSupport;
// Solve three complete landings together. All coordinates are local metres.
// Quadratic connectors round the supporting triangle outside the green hexagon;
// the three contact segments themselves are never shortened by rounding.
function solveIslandContacts(firstSegment, secondSegment, thirdWidth, options = {}) {
const fail = (reason) => ({ status: 'infeasible', reason });
const finitePoint = (p) => Array.isArray(p) && p.length === 2 && p.every(Number.isFinite);
if (
![firstSegment, secondSegment].every((s) => Array.isArray(s) && s.length === 2 && s.every(finitePoint)) ||
!Number.isFinite(thirdWidth) ||
thirdWidth <= 0
)
return fail('invalid-contact');
const sub = (a, b) => a.map((v, i) => v - b[i]);
const add = (a, b, scale = 1) => a.map((v, i) => v + scale * b[i]);
const cross = (a, b) => a[0] * b[1] - a[1] * b[0];
const dot = (a, b) => a[0] * b[0] + a[1] * b[1];
const length = (a) => Math.hypot(...a);
const unit = (a) => a.map((v) => v / length(a));
const d1 = sub(firstSegment[1], firstSegment[0]);
const d2 = sub(secondSegment[1], secondSegment[0]);
if (length(d1) < 1e-8 || length(d2) < 1e-8) return fail('degenerate-contact');
const determinant = cross(d1, d2);
if (Math.abs(determinant) / length(d1) / length(d2) < 1e-6) return fail('parallel-contact-lines');
const corner = add(firstSegment[0], d1, cross(sub(secondSegment[0], firstSegment[0]), d2) / determinant);
const ordered = (segment) => [...segment].sort((a, b) => length(sub(a, corner)) - length(sub(b, corner)));
const first = ordered(firstSegment),
second = ordered(secondSegment);
const u = unit(sub(first[1], corner)),
v = unit(sub(second[1], corner));
// A short edge may begin exactly at the supporting-line intersection; this
// is the intended right-angle landing and is still a complete contact.
const a = length(sub(first[1], corner)),
b = length(sub(second[1], corner));
const chord = sub(second[1], first[1]),
chordLength = length(chord);
const tangent = unit(chord);
// This scale leaves positive connectors on both sides even for unequal depths.
const scale = 1 + thirdWidth / chordLength;
const legLengths = [a * scale, b * scale];
if (
options.maxLegLength !== undefined &&
(!Number.isFinite(options.maxLegLength) || legLengths.some((value) => value > options.maxLegLength))
)
return fail('leg-length-exceeds-space');
const firstTip = add(corner, u, legLengths[0]),
secondTip = add(corner, v, legLengths[1]);
const thirdCenter = firstTip.map((value, i) => (value + secondTip[i]) / 2);
const thirdContact = [add(thirdCenter, tangent, -thirdWidth / 2), add(thirdCenter, tangent, thirdWidth / 2)];
let outward = [tangent[1], -tangent[0]];
if (dot(outward, sub(thirdCenter, corner)) < 0) outward = outward.map((value) => -value);
const contacts = [first, thirdContact, [second[1], second[0]]];
const vertices = contacts.flat();
const hexArea =
Math.abs(
vertices.reduce((sum, point, i) => {
const next = vertices[(i + 1) % 6];
return sum + point[0] * next[1] - next[0] * point[1];
}, 0),
) / 2;
if (hexArea <= 1e-8) return fail('degenerate-contact-hexagon');
const ring = [];
const controls = [firstTip, secondTip, corner];
const steps = 16;
contacts.forEach((contact, index) => {
ring.push(...contact);
const start = contact[1],
end = contacts[(index + 1) % 3][0],
control = controls[index];
for (let step = 1; step < steps; step++) {
const t = step / steps;
ring.push(
start.map((value, axis) => (1 - t) ** 2 * value + 2 * t * (1 - t) * control[axis] + t ** 2 * end[axis]),
);
}
});
ring.push(ring[0]);
return {
status: 'solved',
reason: null,
ring,
greenRing: [...vertices, vertices[0]],
contacts,
thirdContact,
thirdCenter,
outward,
thirdOutward: outward,
legLengths,
};
}
module.exports.solveIslandContacts = solveIslandContacts;