When someone snaps a photo of a hand-drawn floor plan or traces an existing PDF listing sheet, the geometry starts out in arbitrary pixel coordinates. Turning that rough sketch into true-scale architectural inches (1/4" = 1'-0") and then letting the user type exact laser-measure dimensions—without warping walls into trapezoids, stretching a 30-inch range into a 41-inch monster, or folding an adjacent bathroom inside-out—is one of the trickiest UX and geometry problems in browser-based CAD.
In PlanCaliber, all three operations—1-wall scale calibration, centerline/clear-interior parametric wall resizing, and collinear wall-chain dragging—run deterministically in pure TypeScript with zero external geometry kernels (src/lib/cad/healer.ts).
Here is the exact math and edge-case protection behind each one.
1. 1-Wall Scale Calibration Without Breaking Standard US Framing & Appliances
After tracing a sketch or PDF, a homeowner or appraiser rarely knows the exact pixel-to-inch scale—they know one real tape-measure reading (for example, "this exterior garage wall is 22'-0"").
Naively multiplying every number in the document by factor = targetLengthIn / curLen breaks physical reality:
- A
4.5"interior partition wall (2x4framing plus two layers of1/2"gypsum board) becomes6.075"thick. - A
30"kitchen range or36"refrigerator scales to non-existent dimensions. - Door widths turn into fractional nonsense like
31.42"instead of standard US door slabs (24",28",30",32",36"). - Base cabinets lose US
3"modular box sizing (18",21",24",27",30",33",36").
Instead, rescalePlanToWall anchors scaling at the plan's bounding-box origin (ox, oy), scales coordinates uniformly, preserves wall thicknesses and fixed appliance sizes, and snaps architectural openings to 2" rough increments and modular cabinets to 3" stock increments:
export function rescalePlanToWall( doc: PlanDocument, wallId: string, targetLengthIn: number ): PlanDocument { const scaled = healPlanDocument(doc); if (!Number.isFinite(targetLengthIn) || targetLengthIn <= 4) return scaled; const vMap = new Map(scaled.vertices.map((v) => [v.id, v])); const wall = scaled.walls.find((w) => w.id === wallId); if (!wall) return scaled; const a = vMap.get(wall.startId); const b = vMap.get(wall.endId); if (!a || !b) return scaled; const curLen = Math.hypot(b.x - a.x, b.y - a.y); if (curLen < 1e-3) return scaled; const factor = targetLengthIn / curLen; const ox = scaled.vertices.reduce((m, v) => Math.min(m, v.x), Infinity); const oy = scaled.vertices.reduce((m, v) => Math.min(m, v.y), Infinity); for (const v of scaled.vertices) { v.x = Number((ox + (v.x - ox) * factor).toFixed(4)); v.y = Number((oy + (v.y - oy) * factor).toFixed(4)); } for (const op of scaled.openings) { op.offsetIn = Number((op.offsetIn * factor).toFixed(4)); // Clamp to physical architectural limits and snap to standard 2" US opening widths op.widthIn = snapInches(Math.min(120, Math.max(18, op.widthIn * factor)), 2); } for (const f of scaled.fixtures) { f.x = Number((ox + (f.x - ox) * factor).toFixed(4)); f.y = Number((oy + (f.y - oy) * factor).toFixed(4)); // Only stretch modular millwork (3" stock increments); keep ranges, tubs, toilets fixed if (f.kind === 'kitchen_island' || f.kind === 'base_cabinet' || f.kind === 'wall_cabinet') { f.widthIn = snapInches(Math.max(18, f.widthIn * factor), 3); } } for (const e of scaled.electrical) { e.x = Number((ox + (e.x - ox) * factor).toFixed(4)); e.y = Number((oy + (e.y - oy) * factor).toFixed(4)); } for (const t of scaled.roomTags) { t.anchorX = Number((ox + (t.anchorX - ox) * factor).toFixed(4)); t.anchorY = Number((oy + (t.anchorY - oy) * factor).toFixed(4)); } scaled.calibrated = true; scaled.calibrationWallId = wallId; clampOpeningsInPlace(scaled); return scaled; } Why anchor around (ox, oy) rather than (0, 0)? Because scaling around the world origin flings the floor plan off-screen if the user panned away from (0, 0) before calibrating. Anchoring to the plan's minimum corner keeps the drawing planted right in the viewport.
2. Clear-Interior vs. Centerline Dimensioning
When a contractor reads a blueprint, walls are typically dimensioned centerline-to-centerline or exterior face-to-face. When a homeowner measures a room with a laser tape measure, they measure clear interior drywall-to-drywall.
If a user types 12'-0" (144") in clear mode on a bedroom wall framed between a 6" exterior wall and a 4.5" interior partition, the true CAD centerline span is not 144"—it is:
144" + (6" / 2) + (4.5" / 2) = 149.25" (12'-5 1/4").
setWallLength inspects every perpendicular wall (|dir · od| < 0.25) attached to either endpoint and accumulates half of each intersecting wall's thickness:
let desiredCenterlineIn = targetLengthIn; if (mode === 'clear') { let endInsets = 0; for (const ow of next.walls) { if (ow.id === wall.id) continue; const oa = vMap.get(ow.startId); const ob = vMap.get(ow.endId); if (!oa || !ob) continue; const olen = Math.hypot(ob.x - oa.x, ob.y - oa.y) || 1; const odx = (ob.x - oa.x) / olen; const ody = (ob.y - oa.y) / olen; const isPerp = Math.abs(dirX * odx + dirY * ody) < 0.25; if (!isPerp) continue; if (ow.startId === wall.startId || ow.endId === wall.startId) { endInsets += ow.thicknessIn / 2; } if (ow.startId === wall.endId || ow.endId === wall.endId) { endInsets += ow.thicknessIn / 2; } } desiredCenterlineIn = targetLengthIn + endInsets; } 3. Preserving 90-Degree Corners & Stopping Downstream Room Collapse
Moving just wall.endId along (dirX, dirY) turns a rectangle into a trapezoid. Even worse: if a bedroom sits next to a 4'-0" hallway and the user widens the bedroom by +5'-0", shifting only the shared partition wall pushes it past the far hallway wall, inverting the hallway polygon into a negative-area self-intersecting bowtie.
PlanCaliber solves both in two clean graph passes:
- Perpendicular BFS (
moveSet): Starting fromwall.endId, traverse every connected perpendicular wall (|dir · od| < 0.15, never crossing back towall.startId) so the entire orthogonal wall face moves together and keeps every corner strictly 90 degrees. - Downstream Inversion Guard (
wouldCollapseDownstream): Simulate movingmoveSetbydelta. If any wall with one moved endpoint and one stationary endpoint flips direction (dot <= 0) or shrinks below18"(too narrow for any viable hallway or closet), promote every vertex with projectionproj >= endProj - 1intomoveSet, sliding the downstream half of the building cleanly outward instead of collapsing it.
const moveSet = new Set<string>([wall.endId]); const queue: string[] = [wall.endId]; while (queue.length > 0) { const curId = queue.shift()!; for (const ow of next.walls) { if (ow.id === wall.id) continue; const otherId = ow.startId === curId ? ow.endId : ow.endId === curId ? ow.startId : null; if (!otherId || otherId === wall.startId || moveSet.has(otherId)) continue; const oa = vMap.get(curId)!; const ob = vMap.get(otherId)!; const olen = Math.hypot(ob.x - oa.x, ob.y - oa.y) || 1; const odx = (ob.x - oa.x) / olen; const ody = (ob.y - oa.y) / olen; if (Math.abs(dirX * odx + dirY * ody) < 0.15) { moveSet.add(otherId); queue.push(otherId); } } } const endProj = (endV.x - startV.x) * dirX + (endV.y - startV.y) * dirY; const wouldCollapseDownstream = next.walls.some((ow) => { if (ow.id === wall.id) return false; const aMoved = moveSet.has(ow.startId); const bMoved = moveSet.has(ow.endId); if (aMoved === bMoved) return false; const a = vMap.get(ow.startId)!; const b = vMap.get(ow.endId)!; const ax = a.x + (aMoved ? dirX * delta : 0); const ay = a.y + (aMoved ? dirY * delta : 0); const bx = b.x + (bMoved ? dirX * delta : 0); const by = b.y + (bMoved ? dirY * delta : 0); const flipped = (bx - ax) * (b.x - a.x) + (by - ay) * (b.y - a.y) <= 0; return flipped || Math.hypot(bx - ax, by - ay) < 18; }); if (wouldCollapseDownstream) { for (const v of next.vertices) { if (v.id === wall.startId) continue; const proj = (v.x - startV.x) * dirX + (v.y - startV.y) * dirY; if (proj >= endProj - 1) { moveSet.add(v.id); } } } Right after applying dirX * delta and dirY * delta, every door and window on a stretched or shrunk wall updates its offset proportionally ((op.offsetIn / oldL) * newL) and runs through clampOpeningsInPlace so no jamb ever overlaps a corner (>= 3" minimum return).
4. Pushing & Pulling Split Exterior Facades via Collinear Chains
In a real planar floor-plan graph, interior partition T-junctions split a 48'-0" exterior south wall into three separate segments (say 16'-0", 14'-0", and 18'-0"). If the user grabs the middle segment to deepen the whole house by 2'-0", dragging only that single segment punches a jagged step into the exterior envelope.
collectCollinearWallChainVertices computes the seed wall's unit normal (nx, ny) = (-dy/len, dx/len) and floods across connected same-kind (exterior or interior) segments whose endpoints lie within 0.5" of the line and whose direction dot-product with the normal is <= 0.02:
export function collectCollinearWallChainVertices( doc: PlanDocument, wallId: string ): { id: string; orig: Point2D }[] { const vMap = new Map(doc.vertices.map((v) => [v.id, v])); const seed = doc.walls.find((w) => w.id === wallId); if (!seed) return []; const sA = vMap.get(seed.startId); const sB = vMap.get(seed.endId); if (!sA || !sB) return []; const dx = sB.x - sA.x; const dy = sB.y - sA.y; const len = Math.hypot(dx, dy); if (len < 1e-3) return []; const nx = -dy / len; const ny = dx / len; const visited = new Set<string>([seed.startId, seed.endId]); let changed = true; while (changed) { changed = false; for (const w of doc.walls) { if (w.kind !== seed.kind) continue; const hasStart = visited.has(w.startId); const hasEnd = visited.has(w.endId); if (hasStart === hasEnd) continue; const wA = vMap.get(w.startId); const wB = vMap.ge...