/* HK Landscape Sheets: plan geometry for the site polygon and the buildings the user places. Plan metres, x right, y down. The site polygon is kept with its bounding box starting at (0, 0), so site.W and site.D are its bounding box and the older bbox-based code keeps working. A polygon is an array of [x, y]; the site polygon is kept with positive shoelace area (clockwise on screen), so the outward normal of edge a->b is (dy, -dx). */ 'use strict'; /* ---------- polygon basics ---------- */ function polyArea(P) { let s = 0; for (let i = 0; i < P.length; i++) { const a = P[i], b = P[(i + 1) % P.length]; s += a[0] * b[1] - b[0] * a[1]; } return s / 2; } function polyPerim(P) { let s = 0; for (let i = 0; i < P.length; i++) { const a = P[i], b = P[(i + 1) % P.length]; s += Math.hypot(b[0] - a[0], b[1] - a[1]); } return s; } function polyCentroid(P) { let A = 0, cx = 0, cy = 0; for (let i = 0; i < P.length; i++) { const a = P[i], b = P[(i + 1) % P.length], f = a[0] * b[1] - b[0] * a[1]; A += f; cx += (a[0] + b[0]) * f; cy += (a[1] + b[1]) * f; } if (Math.abs(A) < 1e-9) { const n = P.length || 1; return [P.reduce((s, p) => s + p[0], 0) / n, P.reduce((s, p) => s + p[1], 0) / n]; } return [cx / (3 * A), cy / (3 * A)]; } function polyBBox(P) { let x0 = 1e9, y0 = 1e9, x1 = -1e9, y1 = -1e9; for (const [x, y] of P) { if (x < x0) x0 = x; if (y < y0) y0 = y; if (x > x1) x1 = x; if (y > y1) y1 = y; } return { x0, y0, x1, y1 }; } function pointInPoly(x, y, P) { let c = false; for (let i = 0, j = P.length - 1; i < P.length; j = i++) { const [xi, yi] = P[i], [xj, yj] = P[j]; if ((yi > y) !== (yj > y) && x < (xj - xi) * (y - yi) / (yj - yi) + xi) c = !c; } return c; } function closestOnSeg(ax, ay, bx, by, x, y) { const dx = bx - ax, dy = by - ay, L2 = dx * dx + dy * dy; const t = L2 ? Math.max(0, Math.min(1, ((x - ax) * dx + (y - ay) * dy) / L2)) : 0; const px = ax + dx * t, py = ay + dy * t; return { x: px, y: py, t, d: Math.hypot(x - px, y - py) }; } /* nearest point on the polygon outline: {x, y, d, edge, t} */ function closestOnPoly(P, x, y) { let best = null; for (let i = 0; i < P.length; i++) { const a = P[i], b = P[(i + 1) % P.length], c = closestOnSeg(a[0], a[1], b[0], b[1], x, y); if (!best || c.d < best.d) best = Object.assign(c, { edge: i }); } return best; } function distToPoly(P, x, y) { return pointInPoly(x, y, P) ? 0 : closestOnPoly(P, x, y).d; } const rectPoly = (x0, y0, x1, y1) => [[x0, y0], [x1, y0], [x1, y1], [x0, y1]]; function translatePoly(P, dx, dy) { return P.map(([x, y]) => [x + dx, y + dy]); } function rotatePoly(P, deg, cx, cy) { if (cx === undefined) [cx, cy] = polyCentroid(P); const a = deg * D2R, c = Math.cos(a), s = Math.sin(a); return P.map(([x, y]) => [cx + (x - cx) * c - (y - cy) * s, cy + (x - cx) * s + (y - cy) * c]); } function orientPos(P) { return polyArea(P) < 0 ? P.slice().reverse() : P; } /* is P the axis-aligned rectangle [0,W]x[0,D] (vertex order free)? */ function isSiteRect(P, W, D) { if (P.length !== 4) return false; const on = (v, a) => Math.abs(v - a) < 1e-6; return P.every(([x, y]) => (on(x, 0) || on(x, W)) && (on(y, 0) || on(y, D))) && on(Math.abs(polyArea(P)), W * D); } /* Sutherland-Hodgman clip of a polygon to an axis rectangle */ function clipPolyRect(P, x0, y0, x1, y1) { let out = P; const edges = [[p => p[0] >= x0, (a, b) => { const t = (x0 - a[0]) / (b[0] - a[0]); return [x0, a[1] + (b[1] - a[1]) * t]; }], [p => p[0] <= x1, (a, b) => { const t = (x1 - a[0]) / (b[0] - a[0]); return [x1, a[1] + (b[1] - a[1]) * t]; }], [p => p[1] >= y0, (a, b) => { const t = (y0 - a[1]) / (b[1] - a[1]); return [a[0] + (b[0] - a[0]) * t, y0]; }], [p => p[1] <= y1, (a, b) => { const t = (y1 - a[1]) / (b[1] - a[1]); return [a[0] + (b[0] - a[0]) * t, y1]; }]]; for (const [inside, cut] of edges) { const src = out; out = []; for (let i = 0; i < src.length; i++) { const a = src[i], b = src[(i + 1) % src.length], ia = inside(a), ib = inside(b); if (ia) out.push(a); if (ia !== ib) out.push(cut(a, b)); } if (!out.length) break; } return out; } /* intervals [lo, hi] of the infinite line p + u*l that lie inside P, sorted */ function linePolyRanges(p, u, P) { const ts = []; for (let i = 0; i < P.length; i++) { const a = P[i], b = P[(i + 1) % P.length], ex = b[0] - a[0], ey = b[1] - a[1], den = u[0] * ey - u[1] * ex; if (Math.abs(den) < 1e-12) continue; const wx = a[0] - p[0], wy = a[1] - p[1], l = (wx * ey - wy * ex) / den, s = (wx * u[1] - wy * u[0]) / den; if (s >= 0 && s < 1) ts.push(l); } ts.sort((a, b) => a - b); const out = []; for (let i = 0; i + 1 < ts.length; i += 2) { const m = (ts[i] + ts[i + 1]) / 2; if (ts[i + 1] - ts[i] > 1e-6 && pointInPoly(p[0] + u[0] * m, p[1] + u[1] * m, P)) out.push([ts[i], ts[i + 1]]); } return out; } /* shortest gap between two disjoint polygons, dimension placed at 82% along parallel overlap (as the v2 rectangles were) */ function polyGap(A, B) { let d = 1e9; const cand = []; const scan = (S, T, flip) => { for (const q of S) { const c = closestOnPoly(T, q[0], q[1]); cand.push(flip ? { a: [c.x, c.y], b: q, d: c.d } : { a: q, b: [c.x, c.y], d: c.d }); if (c.d < d) d = c.d; } }; scan(A, B, false); scan(B, A, true); const near = cand.filter(c => c.d < d + 1e-3); if (near.length < 2) return near[0] ? Object.assign({ d }, near[0]) : null; const n0 = near[0], ux = n0.b[0] - n0.a[0], uy = n0.b[1] - n0.a[1], L = Math.hypot(ux, uy) || 1, tx = -uy / L, ty = ux / L; near.sort((p, q) => (p.a[0] * tx + p.a[1] * ty) - (q.a[0] * tx + q.a[1] * ty)); const lo = near[0], hi = near[near.length - 1], k = 0.82; const a = [lo.a[0] + (hi.a[0] - lo.a[0]) * k, lo.a[1] + (hi.a[1] - lo.a[1]) * k]; return { d, a, b: [a[0] + ux, a[1] + uy] }; } /* ---------- site model ---------- */ function siteEdges(site) { const P = site.poly, out = []; for (let i = 0; i < P.length; i++) { const a = P[i], b = P[(i + 1) % P.length], dx = b[0] - a[0], dy = b[1] - a[1], len = Math.hypot(dx, dy) || 1e-9; out.push({ i, a, b, len, ux: dx / len, uy: dy / len, nx: dy / len, ny: -dx / len }); } return out; } const inSite = (site, x, y) => pointInPoly(x, y, site.poly); /* distance to the site outline for a point inside (0 outside) */ function edgeDistPoly(site, x, y) { return inSite(site, x, y) ? closestOnPoly(site.poly, x, y).d : 0; } const ptLabel = i => 'P' + (i + 1); const edgeLabel = (site, i) => `${ptLabel(i)}–${ptLabel((i + 1) % site.poly.length)}`; function edgeCompass(site, i) { const e = siteEdges(site)[i]; return compass(screenToBearing(site, (Math.atan2(e.nx, -e.ny) / D2R + 360) % 360)); } /* width of the widest circle that fits (the "short side" of an irregular garden) */ function siteInscribed(site) { const st = Math.max(0.2, Math.max(site.W, site.D) / 60); let best = { r: 0, x: site.W / 2, y: site.D / 2 }; for (let x = st / 2; x < site.W; x += st) for (let y = st / 2; y < site.D; y += st) { if (!inSite(site, x, y)) continue; const r = closestOnPoly(site.poly, x, y).d; if (r > best.r) best = { r, x, y }; } return best; } /* the old four-sides model, used only to migrate saves made before polygons */ const LEGACY_NAME = { top: 'A', right: 'B', bottom: 'C', left: 'D' }; function legacyBoxes(site) { const b = [], ext = 6; for (const s of ['top', 'right', 'bottom', 'left']) { const o = site.sides && site.sides[s]; if (!o || !(o.h > 0)) continue; const g = o.gap || 0, dp = o.depth || 12; if (s === 'top') b.push({ x0: -ext, x1: site.W + ext, y0: -g - dp, y1: -g, h: o.h, side: s }); if (s === 'bottom') b.push({ x0: -ext, x1: site.W + ext, y0: site.D + g, y1: site.D + g + dp, h: o.h, side: s }); if (s === 'left') b.push({ x0: -g - dp, x1: -g, y0: -ext, y1: site.D + ext, h: o.h, side: s }); if (s === 'right') b.push({ x0: site.W + g, x1: site.W + g + dp, y0: -ext, y1: site.D + ext, h: o.h, side: s }); } return b; } let _bid = 1; function newBid() { return 'b' + (_bid++) + Math.random().toString(36).slice(2, 5); } /* old saves: rectangle W x D + four sides -> polygon + building polygons; views side/t -> edge/t */ function migrateSite(site) { if (!Array.isArray(site.poly) || site.poly.length < 3) { const W = +site.W || 10, D = +site.D || 7; site.poly = rectPoly(0, 0, W, D); site.buildings = legacyBoxes(Object.assign({}, site, { W, D })).map(b => ({ id: newBid(), name: '現有建築 ' + LEGACY_NAME[b.side], h: b.h, poly: rectPoly(b.x0, b.y0, b.x1, b.y1) })); const E = { top: [0, false], right: [1, false], bottom: [2, true], left: [3, true] }; site.views = (site.views || []).map(v => v.edge !== undefined ? v : { edge: E[v.side] ? E[v.side][0] : 0, t: E[v.side] && E[v.side][1] ? 1 - v.t : v.t, type: v.type }); delete site.sides; } site.buildings = (site.buildings || []).filter(b => b && Array.isArray(b.poly) && b.poly.length >= 3); site.buildings.forEach((b, i) => { if (!b.id) b.id = newBid(); if (!b.name) b.name = '建築 ' + (i + 1); b.h = Math.max(0, +b.h || 0); }); site.views = (site.views || []).filter(v => v.edge >= 0 && v.edge < site.poly.length); return site; } /* keep the polygon positively oriented and its bbox at (0,0); shift everything else with it. Returns the shift [dx, dy] applied (metres). */ function normalizeComp(comp) { const site = comp.site; if (polyArea(site.poly) < 0) { const n = site.poly.length; site.poly = site.poly.slice().reverse(); site.views.forEach(v => { v.edge = (n - 2 - v.edge + n) % n; v.t = 1 - v.t; }); // edge i (a->b) becomes edge n-2-i (b->a) } const bb = polyBBox(site.poly), dx = -bb.x0, dy = -bb.y0; if (Math.abs(dx) > 1e-9 || Math.abs(dy) > 1e-9) { site.poly = translatePoly(site.poly, dx, dy); site.buildings.forEach(b => { b.poly = translatePoly(b.poly, dx, dy); }); (comp.items || []).forEach(i => { i.x += dx; i.y += dy; }); if (comp.eye) { comp.eye.x += dx; comp.eye.y += dy; } if (comp.section) { comp.section.x1 += dx; comp.section.x2 += dx; comp.section.y1 += dy; comp.section.y2 += dy; } } site.poly = site.poly.map(([x, y]) => [Math.round(x * 1000) / 1000, Math.round(y * 1000) / 1000]); site.W = Math.round((bb.x1 - bb.x0) * 1000) / 1000; site.D = Math.round((bb.y1 - bb.y0) * 1000) / 1000; return [dx, dy]; } /* ---------- buildings: shadows from polygon prisms ---------- */ function solidBldgs(site) { return (site.buildings || []).filter(b => b.h > 0 && b.poly.length >= 3).map(b => ({ b, poly: b.poly, h: b.h, bb: polyBBox(b.poly) })); } /* ground shadow of each prism for sun vector sv: footprint + roof image + every swept edge, all oriented the same way so one nonzero fill draws their union */ function shadowPieces(site, sv) { const out = []; if (sv[2] <= 0.01) return out; const k = 1 / sv[2]; for (const B of solidBldgs(site)) { const ox = -sv[0] * k * B.h, oy = -sv[1] * k * B.h, P = orientPos(B.poly); out.push(P, translatePoly(P, ox, oy)); for (let i = 0; i < P.length; i++) { const a = P[i], b = P[(i + 1) % P.length]; out.push(orientPos([a, b, [b[0] + ox, b[1] + oy], [a[0] + ox, a[1] + oy]])); } } return out; } /* is ground point (x, y, z0) shaded? ray towards the sun enters a prism below its roof */ function pointShaded(B, x, y, z0, sv) { const hl = Math.hypot(sv[0], sv[1]); for (const o of B) { if (pointInPoly(x, y, o.poly)) { if (z0 < o.h) return true; continue; } if (hl < 1e-6) continue; const dx = sv[0] / hl, dy = sv[1] / hl, slope = sv[2] / hl, reach = (o.h - z0) / slope; if (reach <= 0) continue; // slab test against the bbox first let t0 = 0, t1 = reach; for (const [pc, dc, lo, hi] of [[x, dx, o.bb.x0, o.bb.x1], [y, dy, o.bb.y0, o.bb.y1]]) { if (Math.abs(dc) < 1e-12) { if (pc < lo || pc > hi) { t0 = 1; t1 = 0; } continue; } let a = (lo - pc) / dc, b = (hi - pc) / dc; if (a > b) [a, b] = [b, a]; t0 = Math.max(t0, a); t1 = Math.min(t1, b); if (t0 > t1) break; } if (t0 > t1) continue; // first edge crossing along the ray const P = o.poly; for (let i = 0; i < P.length; i++) { const a = P[i], b = P[(i + 1) % P.length], ex = b[0] - a[0], ey = b[1] - a[1], den = dx * ey - dy * ex; if (Math.abs(den) < 1e-12) continue; const wx = a[0] - x, wy = a[1] - y, t = (wx * ey - wy * ex) / den, s = (wx * dy - wy * dx) / den; if (t >= 0 && t <= reach && s >= 0 && s <= 1) return true; } } return false; } /* which building is nearest a point, with the distance to its outline */ function nearestBldg(site, x, y) { let best = null; for (const b of site.buildings || []) { if (!(b.h > 0)) continue; const c = closestOnPoly(b.poly, x, y), inside = pointInPoly(x, y, b.poly), d = inside ? 0 : c.d; if (!best || d < best.d) best = { b, d, x: c.x, y: c.y }; } return best; } /* a view marker sits on boundary edge v.edge at fraction v.t */ function viewPos(site, v) { const P = site.poly, n = P.length, a = P[v.edge % n], b = P[(v.edge + 1) % n]; return [a[0] + (b[0] - a[0]) * v.t, a[1] + (b[1] - a[1]) * v.t]; } /* does the outline cross itself? (non-adjacent edges that properly intersect) */ function polySelfX(P) { const n = P.length, cr = (a, b, c) => (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); for (let i = 0; i < n; i++) for (let j = i + 2; j < n; j++) { if (i === 0 && j === n - 1) continue; const a = P[i], b = P[(i + 1) % n], c = P[j], d = P[(j + 1) % n]; const d1 = cr(c, d, a), d2 = cr(c, d, b), d3 = cr(a, b, c), d4 = cr(a, b, d); if (((d1 > 1e-9 && d2 < -1e-9) || (d1 < -1e-9 && d2 > 1e-9)) && ((d3 > 1e-9 && d4 < -1e-9) || (d3 < -1e-9 && d4 > 1e-9))) return true; } return false; }