Skip to content

Turn any idea, plan, or codebase into a beautiful interactive diagram. An agent skill for Claude Code, Codex, and more.

perf(geometry): skip a clearance the axis gap already rules out

goatliamia9cdcdb9d2201
diff --git a/archify/renderers/architecture/labels.mjs b/archify/renderers/architecture/labels.mjs
index 0e4b124a..c5cdafde 100644
--- a/archify/renderers/architecture/labels.mjs
+++ b/archify/renderers/architecture/labels.mjs
@@ -1,4 +1,4 @@
-import { normalizeRoutePoints, rectsOverlap, segmentRectClearance } from '../shared/geometry.mjs';
+import { normalizeRoutePoints, rectsOverlap, segmentRectClearanceWithin } from '../shared/geometry.mjs';
// A bounded fallback for an unpinned label whose usual position collides.
// It never routes an edge, moves a node, expands the canvas, or rewrites input.
@@ -16,7 +16,7 @@ export function placeAutomaticLabels({
&& rect.x + rect.width <= viewBox[0] && rect.y + rect.height <= viewBox[1]
);
const masksRoute = rect => segments.some(segment => segment.relationIndex !== rect.relationIndex
- && segmentRectClearance(segment, rect) + 0.0001 < 4);
+ && segmentRectClearanceWithin(segment, rect, 4) + 0.0001 < 4);
const overlapsLabel = (rect, index, gap = 0) => placed.some((other, otherIndex) => (
otherIndex !== index && rectsOverlap(rect, other, gap)
));
@@ -100,7 +100,7 @@ export function placeAutomaticLabels({
const fallback = baseAnchors.flatMap(([baseX, baseY]) => (
ringOffsets.map(([dx, dy]) => rectAt(label, baseX + dx, baseY + dy))
)).find(rect => clear(rect, index) && (!keepFallbackNearRoute || ownSegments.some(segment => (
- segmentRectClearance(segment, rect) <= label.height * 2
+ segmentRectClearanceWithin(segment, rect, label.height * 2) <= label.height * 2
))));
if (fallback) placed[index] = fallback;
}
diff --git a/archify/renderers/shared/geometry.mjs b/archify/renderers/shared/geometry.mjs
index c1b4a006..8e5547fd 100644
--- a/archify/renderers/shared/geometry.mjs
+++ b/archify/renderers/shared/geometry.mjs
@@ -55,21 +55,39 @@ export function segmentRectClearance(segment, rect) {
if (!segment || !rect) return null;
const { start, end } = segment;
if (!Array.isArray(start) || !Array.isArray(end) || start.length !== 2 || end.length !== 2) return null;
- if (!isFinitePoint(...start, ...end, rect.x, rect.y, rect.width, rect.height)) return null;
+ if (!isFinitePoint(start[0], start[1], end[0], end[1], rect.x, rect.y, rect.width, rect.height)) return null;
if (rect.width < 0 || rect.height < 0) return null;
if (segmentIntersectsRect(segment, rect)) return 0;
- const corners = [
- [rect.x, rect.y],
- [rect.x + rect.width, rect.y],
- [rect.x + rect.width, rect.y + rect.height],
- [rect.x, rect.y + rect.height],
- ];
- return Math.min(
- pointRectDistance(start, rect),
- pointRectDistance(end, rect),
- ...corners.map((corner) => pointSegmentDistance(corner, start, end)),
- );
+ const right = rect.x + rect.width;
+ const bottom = rect.y + rect.height;
+ let clearance = Math.min(pointRectDistance(start, rect), pointRectDistance(end, rect));
+ const corners = [rect.x, rect.y, right, rect.y, right, bottom, rect.x, bottom];
+ for (let index = 0; index < 8; index += 2) {
+ const distance = pointSegmentDistanceXY(corners[index], corners[index + 1], start, end);
+ if (distance < clearance) clearance = distance;
+ }
+ return clearance;
+}
+
+// The same clearance, but the caller already knows the threshold it compares
+// against: when an axis gap alone proves the distance cannot be smaller, the
+// exact distance is never computed and Infinity is returned.
+export function segmentRectClearanceWithin(segment, rect, limit) {
+ if (!segment || !rect) return segmentRectClearance(segment, rect);
+ const { start, end } = segment;
+ if (!Array.isArray(start) || !Array.isArray(end) || start.length !== 2 || end.length !== 2) return null;
+ if (!isFinitePoint(start[0], start[1], end[0], end[1], rect.x, rect.y, rect.width, rect.height)) return null;
+ if (rect.width < 0 || rect.height < 0) return null;
+ if (segmentIntersectsRect(segment, rect)) return 0;
+ const minX = Math.min(start[0], end[0]);
+ const maxX = Math.max(start[0], end[0]);
+ const minY = Math.min(start[1], end[1]);
+ const maxY = Math.max(start[1], end[1]);
+ const gapX = Math.max(rect.x - maxX, minX - (rect.x + rect.width));
+ const gapY = Math.max(rect.y - maxY, minY - (rect.y + rect.height));
+ if (Math.max(gapX, gapY) > limit) return Number.POSITIVE_INFINITY;
+ return segmentRectClearance(segment, rect);
}
export function segmentRectIntersectionLength(segment, rect) {
@@ -1302,6 +1320,14 @@ function pointRectDistance(point, rect) {
return Math.hypot(dx, dy);
}
+function pointSegmentDistanceXY(px, py, start, end) {
+ const dx = end[0] - start[0];
+ const dy = end[1] - start[1];
+ if (dx === 0 && dy === 0) return Math.hypot(px - start[0], py - start[1]);
+ const t = Math.max(0, Math.min(1, ((px - start[0]) * dx + (py - start[1]) * dy) / (dx * dx + dy * dy)));
+ return Math.hypot(px - (start[0] + t * dx), py - (start[1] + t * dy));
+}
+
function pointSegmentDistance(point, start, end) {
const dx = end[0] - start[0];
const dy = end[1] - start[1];