From 1b24afa90bbcc9311cf19ea0aad29dc05f102bac Mon Sep 17 00:00:00 2001 From: dax Date: Tue, 4 Aug 2026 07:17:37 +0000 Subject: pathways: smarter placement - fine-grained nearest fit + snap to neighbor edges, no more coarse tile-width jumps --- public/app.js | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/public/app.js b/public/app.js index b8aeb68..6411bf7 100644 --- a/public/app.js +++ b/public/app.js @@ -78,14 +78,34 @@ function placeRect(w, h, from, dir) { const base = baseRect(w, h, from, dir) if (!collides(base)) return base const horiz = dir === "N" || dir === "S" - for (let k = 1; k < 60; k++) { - const off = k * (w + GAP) - const a = horiz ? { ...base, x: base.x + off } : { ...base, y: base.y + off } - if (!collides(a)) return a - const b = horiz ? { ...base, x: base.x - off } : { ...base, y: base.y - off } - if (!collides(b)) return b + const fixed = horiz ? base.y : base.x + const slide0 = horiz ? base.x : base.y + let best = null + for (let k = 1; k < 1000; k++) { + for (const off of [k * 2, -k * 2]) { + const rect = horiz ? { x: slide0 + off, y: fixed, w, h } : { x: fixed, y: slide0 + off, w, h } + if (!collides(rect)) { + best = rect + break + } + } + if (best) break + } + if (!best) return base + let snap = null + for (const t of tiles.values()) { + const r = t.rect + const lines = horiz + ? [r.x - w - GAP, r.x + r.w + GAP, r.x + (r.w - w) / 2] + : [r.y - h - GAP, r.y + r.h + GAP, r.y + (r.h - h) / 2] + for (const c of lines) { + const at = horiz ? best.x : best.y + if (Math.abs(c - at) > GAP) continue + const rect = horiz ? { x: c, y: fixed, w, h } : { x: fixed, y: c, w, h } + if (!collides(rect)) snap = rect + } } - return base + return snap || best } function addTile(info, rect) { -- cgit v1.3.1