/* HK Landscape Sheets: drafting primitives and the drawings on the sheets (site plan, planting plan, section A-A). Look only: the engines live in site.js, tree.js and board.js. */ 'use strict'; const INK = '#2e2e30', GRAPH = '#5b5955', LIGHT = '#a29e94', HAIR = '#d8d4c9', PAPER = '#fbfaf6', ACCENT = '#c4763c', REDPEN = '#cf2e22'; const F_MONO = '"IBM Plex Mono", ui-monospace, Consolas, monospace'; const F_SANS = '"IBM Plex Sans", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif'; const fnt = (px, w = 400, fam = F_SANS) => `${w} ${px}px ${fam}`; const mmTxt = m => String(Math.round(m * 1000)); /* plan metres -> canvas px, with px padding for bubbles/dims and metres of context around the garden */ function sheetTx(site, w, h, pad, ext) { const EW = site.W + ext.l + ext.r, ED = site.D + ext.t + ext.b; const aw = w - pad.l - pad.r, ah = h - pad.t - pad.b; const sc = Math.max(1, Math.min(aw / EW, ah / ED)); const ox = pad.l + (aw - EW * sc) / 2 + ext.l * sc, oy = pad.t + (ah - ED * sc) / 2 + ext.t * sc; return txFrom(sc, ox, oy); } function txFrom(sc, ox, oy) { return { sc, x: v => ox + v * sc, y: v => oy + v * sc, ix: px => (px - ox) / sc, iy: py => (py - oy) / sc, ox, oy }; } /* polygon (plan metres) as a closed sub-path in canvas px */ function polyPathTx(ctx, P, tx) { P.forEach(([x, y], i) => i ? ctx.lineTo(tx.x(x), tx.y(y)) : ctx.moveTo(tx.x(x), tx.y(y))); ctx.closePath(); } /* ---------- primitives ---------- */ function hatchIn(ctx, pathFn, { sp = 6, ang = 45, col = LIGHT, lw = 0.6 } = {}) { ctx.save(); ctx.beginPath(); pathFn(ctx); ctx.clip(); const L = Math.hypot(ctx.canvas.width, ctx.canvas.height) + 50, a = ang * D2R, dx = Math.cos(a), dy = Math.sin(a), nx = -dy, ny = dx; ctx.strokeStyle = col; ctx.lineWidth = lw; ctx.beginPath(); for (let s = -L; s <= L; s += sp) { const cx = nx * s, cy = ny * s; ctx.moveTo(cx - dx * L, cy - dy * L); ctx.lineTo(cx + dx * L, cy + dy * L); } ctx.stroke(); ctx.restore(); } function stippleIn(ctx, pathFn, x0, y0, x1, y1, { dens = 0.02, col = GRAPH, seed = 5, r = 0.7 } = {}) { ctx.save(); ctx.beginPath(); pathFn(ctx); ctx.clip(); ctx.fillStyle = col; const R = rng(seed), n = Math.min(4000, Math.abs((x1 - x0) * (y1 - y0)) * dens); for (let i = 0; i < n; i++) { const x = x0 + R() * (x1 - x0), y = y0 + R() * (y1 - y0); ctx.beginPath(); ctx.arc(x, y, r * (0.6 + R() * 0.8), 0, 6.283); ctx.fill(); } ctx.restore(); } /* dimension line A-B offset `off` px to the left of A->B; 45-degree ticks; text above in mm */ function dimLine(ctx, ax, ay, bx, by, txt, off = 0, { col = GRAPH, size = 9 } = {}) { const L = Math.hypot(bx - ax, by - ay); if (L < 4) return; const ux = (bx - ax) / L, uy = (by - ay) / L, nx = uy, ny = -ux; const A = [ax + nx * off, ay + ny * off], B = [bx + nx * off, by + ny * off], s = Math.sign(off) || 1; ctx.save(); ctx.strokeStyle = col; ctx.fillStyle = col; ctx.lineWidth = 0.6; ctx.beginPath(); if (off) { ctx.moveTo(ax + nx * 3 * s, ay + ny * 3 * s); ctx.lineTo(A[0] + nx * 3 * s, A[1] + ny * 3 * s); ctx.moveTo(bx + nx * 3 * s, by + ny * 3 * s); ctx.lineTo(B[0] + nx * 3 * s, B[1] + ny * 3 * s); } ctx.moveTo(A[0] - ux * 4, A[1] - uy * 4); ctx.lineTo(B[0] + ux * 4, B[1] + uy * 4); ctx.stroke(); ctx.lineWidth = 1.5; ctx.beginPath(); for (const P of [A, B]) { const tx = (ux + nx) * 2.8, ty = (uy + ny) * 2.8; ctx.moveTo(P[0] - tx, P[1] - ty); ctx.lineTo(P[0] + tx, P[1] + ty); } ctx.stroke(); ctx.translate((A[0] + B[0]) / 2, (A[1] + B[1]) / 2); let ang = Math.atan2(uy, ux); if (ang > Math.PI / 2 - 1e-6 || ang < -Math.PI / 2 - 1e-6) ang -= Math.PI; ctx.rotate(ang); ctx.font = fnt(size, 400, F_MONO); ctx.textAlign = 'center'; ctx.textBaseline = 'bottom'; const tw = ctx.measureText(txt).width; ctx.fillStyle = PAPER; ctx.fillRect(-tw / 2 - 1.5, -size - 2, tw + 3, size); ctx.fillStyle = col; ctx.fillText(txt, 0, -1.5); ctx.restore(); } function bubble(ctx, x, y, r, label, { col = INK, fill = PAPER, lw = 1, size } = {}) { ctx.save(); ctx.beginPath(); ctx.arc(x, y, r, 0, 6.283); ctx.fillStyle = fill; ctx.fill(); ctx.strokeStyle = col; ctx.lineWidth = lw; ctx.stroke(); ctx.fillStyle = col; ctx.font = fnt(size || Math.round(r * 1.05), 500, F_MONO); ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText(label, x, y + 0.5); ctx.restore(); } function northPoint(ctx, x, y, r, north) { ctx.save(); ctx.translate(x, y); ctx.strokeStyle = INK; ctx.lineWidth = 1; ctx.beginPath(); ctx.arc(0, 0, r, 0, 6.283); ctx.stroke(); ctx.rotate((north || 0) * D2R); ctx.beginPath(); ctx.moveTo(0, -r * 1.3); ctx.lineTo(-r * 0.38, r * 0.62); ctx.lineTo(0, r * 0.3); ctx.closePath(); ctx.fillStyle = INK; ctx.fill(); ctx.beginPath(); ctx.moveTo(0, -r * 1.3); ctx.lineTo(r * 0.38, r * 0.62); ctx.lineTo(0, r * 0.3); ctx.closePath(); ctx.fillStyle = PAPER; ctx.fill(); ctx.lineWidth = 0.8; ctx.stroke(); ctx.fillStyle = INK; ctx.font = fnt(Math.max(8, Math.round(r * 0.75)), 600, F_MONO); ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText('N', 0, -r * 1.3 - Math.max(6, r * 0.5)); ctx.restore(); } /* graphic scale: alternating blocks, labelled in metres */ function scaleBar(ctx, x, y, pxPerM, maxPx = 150) { const opts = [[20, 5], [10, 5], [5, 5], [2, 4], [1, 4]]; let T = 1, n = 4; for (const [t, k] of opts) if (t * pxPerM <= maxPx) { T = t; n = k; break; } const seg = T / n * pxPerM; ctx.save(); ctx.strokeStyle = INK; ctx.lineWidth = 0.8; for (let i = 0; i < n; i++) { ctx.fillStyle = i % 2 ? PAPER : INK; ctx.fillRect(x + i * seg, y - 4, seg, 4); ctx.strokeRect(x + i * seg, y - 4, seg, 4); } ctx.fillStyle = INK; ctx.font = fnt(8.5, 400, F_MONO); ctx.textAlign = 'center'; ctx.textBaseline = 'top'; ctx.fillText('0', x, y + 2); ctx.fillText(String(T / 2).replace(/\.0$/, ''), x + n * seg / 2, y + 2); ctx.textAlign = 'left'; ctx.fillText(T + ' m', x + n * seg - 3, y + 2); ctx.restore(); return n * seg; } /* revision-cloud path around an ellipse (arcs bulge outward) */ function cloudPath(ctx, cx, cy, rx, ry, arc = 6) { const per = Math.PI * (3 * (rx + ry) - Math.sqrt((3 * rx + ry) * (rx + 3 * ry))), n = Math.max(7, Math.round(per / (arc * 1.7))); ctx.beginPath(); for (let i = 0; i < n; i++) { const a0 = i / n * 6.2832, a1 = (i + 1) / n * 6.2832; const p0 = [cx + rx * Math.cos(a0), cy + ry * Math.sin(a0)], p1 = [cx + rx * Math.cos(a1), cy + ry * Math.sin(a1)]; const mx = (p0[0] + p1[0]) / 2, my = (p0[1] + p1[1]) / 2, d = Math.hypot(p1[0] - p0[0], p1[1] - p0[1]), ang = Math.atan2(p1[1] - p0[1], p1[0] - p0[0]); ctx.arc(mx, my, d / 2, ang + Math.PI, ang + 2 * Math.PI, false); } } function noteTag(ctx, x, y, txt, on) { ctx.save(); ctx.beginPath(); ctx.arc(x, y, 7.5, 0, 6.283); ctx.fillStyle = on ? REDPEN : PAPER; ctx.fill(); ctx.strokeStyle = REDPEN; ctx.lineWidth = 1.2; ctx.stroke(); ctx.fillStyle = on ? '#fff' : REDPEN; ctx.font = fnt(txt.length > 1 ? 8 : 9, 600, F_MONO); ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText(txt, x, y + 0.5); ctx.restore(); } function keyTagBox(ctx, x, y, txt, strong) { ctx.font = fnt(8.5, strong ? 600 : 500, F_MONO); const tw = ctx.measureText(txt).width + 7; ctx.fillStyle = PAPER; ctx.fillRect(x - tw / 2, y - 6.5, tw, 13); ctx.strokeStyle = strong ? INK : GRAPH; ctx.lineWidth = strong ? 1 : 0.7; ctx.strokeRect(x - tw / 2, y - 6.5, tw, 13); ctx.fillStyle = INK; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText(txt, x, y + 0.5); return tw; } /* grid spacing so there are at most 6 x 5 grid lines */ function niceGrid(W, D) { for (const g of [1, 2, 2.5, 3, 4, 5, 10, 20]) if (Math.floor(W / g) + 1 <= 6 && Math.floor(D / g) + 1 <= 5) return g; return 20; } function gridLines(site) { const g = niceGrid(site.W, site.D), xs = [], ys = []; for (let v = 0; v <= site.W + 1e-6; v += g) xs.push(v); for (let v = 0; v <= site.D + 1e-6; v += g) ys.push(v); return { g, xs: xs.map((v, i) => ({ v, lab: String(i + 1) })), ys: ys.map((v, i) => ({ v, lab: String.fromCharCode(65 + i) })) }; } /* standard scale such that `metres` fits `paperMm` on the printed A1 sheet */ function niceScale(metres, paperMm) { for (const s of [20, 25, 50, 100, 200, 250, 500, 1000]) if (metres * 1000 / s <= paperMm) return s; return 1250; } /* grid with bubbles (top: 1,2,3 / left: A,B,C) and dimension strings (bottom, right) */ function drawPlanFrame(ctx, w, h, site, tx, opt = {}) { const G = gridLines(site), gx0 = tx.x(0), gy0 = tx.y(0), gx1 = tx.x(site.W), gy1 = tx.y(site.D); if (opt.grid !== false) { ctx.save(); ctx.strokeStyle = LIGHT; ctx.lineWidth = 0.6; ctx.setLineDash([12, 3, 2, 3]); ctx.beginPath(); for (const { v } of G.xs) { ctx.moveTo(tx.x(v), 24); ctx.lineTo(tx.x(v), gy1 + 10); } for (const { v } of G.ys) { ctx.moveTo(24, tx.y(v)); ctx.lineTo(gx1 + 10, tx.y(v)); } ctx.stroke(); ctx.restore(); for (const { v, lab } of G.xs) bubble(ctx, tx.x(v), 14, 9, lab); for (const { v, lab } of G.ys) bubble(ctx, 14, tx.y(v), 9, lab); } if (opt.dims !== false) { const yb = h - 30, yb2 = h - 14, xr = w - 30, xr2 = w - 14; ctx.save(); ctx.strokeStyle = LIGHT; ctx.lineWidth = 0.5; ctx.beginPath(); for (const x of [gx0, gx1, ...G.xs.map(o => tx.x(o.v))]) { ctx.moveTo(x, gy1 + 4); ctx.lineTo(x, yb2 + 3); } for (const y of [gy0, gy1, ...G.ys.map(o => tx.y(o.v))]) { ctx.moveTo(gx1 + 4, y); ctx.lineTo(xr2 + 3, y); } ctx.stroke(); ctx.restore(); const xsAll = [...new Set([0, ...G.xs.map(o => o.v), site.W].map(v => Math.round(v * 1000) / 1000))].sort((a, b) => a - b); const ysAll = [...new Set([0, ...G.ys.map(o => o.v), site.D].map(v => Math.round(v * 1000) / 1000))].sort((a, b) => a - b); for (let i = 0; i + 1 < xsAll.length; i++) dimLine(ctx, tx.x(xsAll[i]), yb, tx.x(xsAll[i + 1]), yb, mmTxt(xsAll[i + 1] - xsAll[i])); dimLine(ctx, gx0, yb2, gx1, yb2, mmTxt(site.W), 0, { col: INK }); for (let i = 0; i + 1 < ysAll.length; i++) dimLine(ctx, xr, tx.y(ysAll[i]), xr, tx.y(ysAll[i + 1]), mmTxt(ysAll[i + 1] - ysAll[i])); dimLine(ctx, xr2, gy0, xr2, gy1, mmTxt(site.D), 0, { col: INK }); // an irregular site also gets a dimension string on every boundary edge, offset outward if (site.poly && !isSiteRect(site.poly, site.W, site.D)) for (const e of siteEdges(site)) if (e.len * tx.sc > 22) dimLine(ctx, tx.x(e.a[0]), tx.y(e.a[1]), tx.x(e.b[0]), tx.y(e.b[1]), mmTxt(e.len), opt.edgeOff || 17); } return G; } /* buildings (the user's polygons): hatched, name and height, gap to the site, or an off-sheet arrow */ function drawContext(ctx, w, h, site, tx, ui = {}) { const R = [30, 30, w - 44, h - 44], M = [tx.ix(R[0]), tx.iy(R[1]), tx.ix(R[2]), tx.iy(R[3])]; const C = polyCentroid(site.poly); for (const b of site.buildings || []) { const vis = clipPolyRect(b.poly, M[0], M[1], M[2], M[3]), visA = vis.length >= 3 ? Math.abs(polyArea(vis)) * tx.sc * tx.sc : 0; const label = `${b.name} +${b.h} m`, sel = ui.selB === b.id; const touches = b.poly.some(([x, y]) => inSite(site, x, y)) || site.poly.some(([x, y]) => pointInPoly(x, y, b.poly)); const gap = touches ? null : polyGap(b.poly, site.poly); const vbb = vis.length >= 3 ? polyBBox(vis) : null, big = vbb && visA > 400 && Math.min(vbb.x1 - vbb.x0, vbb.y1 - vbb.y0) * tx.sc >= 16; ctx.save(); if (visA > 60) { ctx.save(); ctx.beginPath(); ctx.rect(R[0], R[1], R[2] - R[0], R[3] - R[1]); ctx.clip(); if (b.h > 0) { ctx.fillStyle = '#f2f0ea'; ctx.beginPath(); polyPathTx(ctx, b.poly, tx); ctx.fill(); hatchIn(ctx, c => polyPathTx(c, b.poly, tx), { sp: 6, col: '#d0ccc2', lw: 0.5 }); ctx.strokeStyle = INK; ctx.lineWidth = 1.5; ctx.lineJoin = 'miter'; ctx.beginPath(); polyPathTx(ctx, b.poly, tx); ctx.stroke(); } else { ctx.strokeStyle = GRAPH; ctx.lineWidth = 0.9; ctx.setLineDash([6, 3]); ctx.beginPath(); polyPathTx(ctx, b.poly, tx); ctx.stroke(); ctx.setLineDash([]); } if (sel) { ctx.strokeStyle = ACCENT; ctx.lineWidth = 2; ctx.setLineDash([6, 3]); ctx.beginPath(); polyPathTx(ctx, b.poly, tx); ctx.stroke(); ctx.setLineDash([]); } ctx.restore(); } if (big) { // label at the middle of the visible part, turned if the part is tall and narrow const vb = vbb, [lx, ly] = polyCentroid(vis), pw = (vb.x1 - vb.x0) * tx.sc, ph = (vb.y1 - vb.y0) * tx.sc; ctx.font = fnt(9, 500, F_MONO); ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; const tw = ctx.measureText(label).width + 8, vert = tw > pw - 6 && ph > pw * 1.2; ctx.translate(tx.x(lx), tx.y(ly)); if (vert) ctx.rotate(-Math.PI / 2); ctx.fillStyle = PAPER; ctx.fillRect(-tw / 2, -7, tw, 14); ctx.fillStyle = INK; ctx.fillText(label, 0, 0.5); ctx.restore(); ctx.save(); // drop the label transform and the frame clip if (gap && gap.d > 0.2 && gap.d < 40) { const A = [tx.x(gap.a[0]), tx.y(gap.a[1])], B = [tx.x(gap.b[0]), tx.y(gap.b[1])]; if ([A, B].every(([x, y]) => x > R[0] && x < R[2] && y > R[1] && y < R[3])) dimLine(ctx, A[0], A[1], B[0], B[1], mmTxt(gap.d)); } } else { // building beyond the drawn area: arrow on the frame, pointing at it const bc = polyCentroid(b.poly), sx = tx.x(C[0]), sy = tx.y(C[1]), dx = tx.x(bc[0]) - sx, dy = tx.y(bc[1]) - sy, L = Math.hypot(dx, dy) || 1, ux = dx / L, uy = dy / L; let t = 1e9; const I = [R[0] + 8, R[1] + 8, R[2] - 8, R[3] - 8]; if (ux > 1e-6) t = Math.min(t, (I[2] - sx) / ux); if (ux < -1e-6) t = Math.min(t, (I[0] - sx) / ux); if (uy > 1e-6) t = Math.min(t, (I[3] - sy) / uy); if (uy < -1e-6) t = Math.min(t, (I[1] - sy) / uy); const px = sx + ux * t, py = sy + uy * t, txt = `${label},距 ${gap ? fmt(gap.d) : 0} m`; ctx.strokeStyle = INK; ctx.fillStyle = INK; ctx.lineWidth = 1; ctx.beginPath(); ctx.moveTo(px - ux * 16, py - uy * 16); ctx.lineTo(px, py); ctx.stroke(); ctx.beginPath(); ctx.moveTo(px + ux * 5, py + uy * 5); ctx.lineTo(px - uy * 4, py + ux * 4); ctx.lineTo(px + uy * 4, py - ux * 4); ctx.fill(); ctx.font = fnt(9, 500, F_MONO); ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; // text runs along the frame beside the arrow, clear of the dimension strings round the garden const tw = ctx.measureText(txt).width + 8, side = Math.abs(ux) > Math.abs(uy), tgx = side ? 0 : 1, tgy = side ? -1 : 0; ctx.translate(px - ux * 9 + tgx * (tw / 2 + 10), py - uy * 9 + tgy * (tw / 2 + 10)); if (side) ctx.rotate(-Math.PI / 2); ctx.fillStyle = PAPER; ctx.fillRect(-tw / 2, -7, tw, 14); ctx.fillStyle = INK; ctx.fillText(txt, 0, 0.5); } ctx.restore(); } } function gardenEdge(ctx, site, tx) { if (site.podium) { // planter upstand, cut in plan: poché band just outside the outline, square corners const t = Math.max(3, 0.2 * tx.sc); ctx.save(); ctx.beginPath(); ctx.rect(-1e4, -1e4, 2e4, 2e4); polyPathTx(ctx, site.poly, tx); ctx.clip('evenodd'); ctx.strokeStyle = '#3f3f41'; ctx.lineWidth = 2 * t; ctx.lineJoin = 'miter'; ctx.miterLimit = 12; ctx.beginPath(); polyPathTx(ctx, site.poly, tx); ctx.stroke(); ctx.restore(); } else { ctx.strokeStyle = INK; ctx.lineWidth = 2; ctx.lineJoin = 'miter'; ctx.beginPath(); polyPathTx(ctx, site.poly, tx); ctx.stroke(); } } /* metres of context to show round the garden on each side, from the buildings beyond that side */ function ctxExt(site, near, none, cap) { const e = { l: none, r: none, t: none, b: none }, W = site.W, D = site.D, eps = 0.01, perp = { l: ['t', 'b'], r: ['t', 'b'], t: ['l', 'r'], b: ['l', 'r'] }; for (const o of solidBldgs(site)) { const bb = o.bb, bey = { l: bb.x1 <= eps, r: bb.x0 >= W - eps, t: bb.y1 <= eps, b: bb.y0 >= D - eps }; const ext = { l: -bb.x0, r: bb.x1 - W, t: -bb.y0, b: bb.y1 - D }, gap = { l: -bb.x1, r: bb.x0 - W, t: -bb.y1, b: bb.y0 - D }; for (const k of ['l', 'r', 't', 'b']) { if (ext[k] <= 0) continue; if (!bey[k] && perp[k].some(q => bey[q])) continue; // e.g. a building beyond the top edge that also overhangs the left const g = Math.max(0, gap[k]); e[k] = Math.max(e[k], bey[k] && ext[k] + 1.5 <= cap ? ext[k] + 1.5 : Math.min(g + near, cap, ext[k] + 1.5)); } } return e; } /* CAD-style editing overlay: handles, rubber band, crosshair and a length / angle tooltip */ function drawEdit(ctx, w, h, tx, ed) { if (!ed) return; ctx.save(); for (const H of ed.handles || []) { const P = H.P, n = P.length; ctx.strokeStyle = ACCENT; ctx.lineWidth = 1; ctx.setLineDash([5, 3]); ctx.beginPath(); polyPathTx(ctx, P, tx); ctx.stroke(); ctx.setLineDash([]); for (let i = 0; i < n; i++) { // edge midpoints: insert a vertex const a = P[i], b = P[(i + 1) % n], mx = tx.x((a[0] + b[0]) / 2), my = tx.y((a[1] + b[1]) / 2); if (Math.hypot(tx.x(b[0]) - tx.x(a[0]), tx.y(b[1]) - tx.y(a[1])) < 26) continue; ctx.fillStyle = PAPER; ctx.strokeStyle = ACCENT; ctx.lineWidth = 1; ctx.beginPath(); ctx.arc(mx, my, H.r * 0.6, 0, 6.283); ctx.fill(); ctx.stroke(); ctx.beginPath(); ctx.moveTo(mx - 2.5, my); ctx.lineTo(mx + 2.5, my); ctx.moveTo(mx, my - 2.5); ctx.lineTo(mx, my + 2.5); ctx.stroke(); } P.forEach(([x, y], i) => { const px = tx.x(x), py = tx.y(y), on = H.selV === i, r = H.r; ctx.fillStyle = on ? ACCENT : PAPER; ctx.strokeStyle = on ? INK : ACCENT; ctx.lineWidth = 1.2; ctx.fillRect(px - r / 2, py - r / 2, r, r); ctx.strokeRect(px - r / 2, py - r / 2, r, r); if (on && n > 3) { // small delete box const bx = px + r / 2 + 5, by = py - r / 2 - 15; ctx.fillStyle = INK; ctx.fillRect(bx, by, 13, 13); ctx.strokeStyle = PAPER; ctx.lineWidth = 1.3; ctx.beginPath(); ctx.moveTo(bx + 3.5, by + 3.5); ctx.lineTo(bx + 9.5, by + 9.5); ctx.moveTo(bx + 9.5, by + 3.5); ctx.lineTo(bx + 3.5, by + 9.5); ctx.stroke(); } }); } const D = ed.draft; if (D && D.pts.length) { const pts = D.pts.map(([x, y]) => [tx.x(x), tx.y(y)]); if (D.rect && D.cur) { // rectangle by two corners const [x0, y0] = pts[0], x1 = tx.x(D.cur[0]), y1 = tx.y(D.cur[1]); ctx.fillStyle = 'rgba(196,118,60,0.08)'; ctx.fillRect(Math.min(x0, x1), Math.min(y0, y1), Math.abs(x1 - x0), Math.abs(y1 - y0)); ctx.strokeStyle = ACCENT; ctx.lineWidth = 1.2; ctx.setLineDash([6, 3]); ctx.strokeRect(Math.min(x0, x1), Math.min(y0, y1), Math.abs(x1 - x0), Math.abs(y1 - y0)); ctx.setLineDash([]); dimLine(ctx, Math.min(x0, x1), Math.max(y0, y1), Math.max(x0, x1), Math.max(y0, y1), mmTxt(Math.abs(D.cur[0] - D.pts[0][0])), -14, { col: ACCENT }); dimLine(ctx, Math.max(x0, x1), Math.min(y0, y1), Math.max(x0, x1), Math.max(y0, y1), mmTxt(Math.abs(D.cur[1] - D.pts[0][1])), -14, { col: ACCENT }); } else { ctx.strokeStyle = ACCENT; ctx.lineWidth = 1.6; ctx.beginPath(); pts.forEach(([x, y], i) => i ? ctx.lineTo(x, y) : ctx.moveTo(x, y)); ctx.stroke(); for (let i = 0; i + 1 < D.pts.length; i++) { const a = D.pts[i], b = D.pts[i + 1]; dimLine(ctx, pts[i][0], pts[i][1], pts[i + 1][0], pts[i + 1][1], mmTxt(Math.hypot(b[0] - a[0], b[1] - a[1])), 14, { col: ACCENT }); } if (D.cur) { // rubber band const L = pts[pts.length - 1]; ctx.setLineDash([6, 4]); ctx.lineWidth = 1.1; ctx.beginPath(); ctx.moveTo(L[0], L[1]); ctx.lineTo(tx.x(D.cur[0]), tx.y(D.cur[1])); if (D.pts.length >= 2) ctx.lineTo(pts[0][0], pts[0][1]); ctx.stroke(); ctx.setLineDash([]); } pts.forEach(([x, y], i) => { ctx.fillStyle = i === 0 && D.closeHint ? ACCENT : PAPER; ctx.strokeStyle = INK; ctx.lineWidth = 1; ctx.fillRect(x - 3.5, y - 3.5, 7, 7); ctx.strokeRect(x - 3.5, y - 3.5, 7, 7); }); if (D.closeHint) { ctx.strokeStyle = ACCENT; ctx.lineWidth = 1.4; ctx.beginPath(); ctx.arc(pts[0][0], pts[0][1], 9, 0, 6.283); ctx.stroke(); } } } if (ed.cursor) { // crosshair across the drawing, pick box const [cx, cy] = ed.cursor; ctx.strokeStyle = 'rgba(46,46,48,0.45)'; ctx.lineWidth = 0.6; ctx.beginPath(); ctx.moveTo(30, cy); ctx.lineTo(w - 44, cy); ctx.moveTo(cx, 30); ctx.lineTo(cx, h - 44); ctx.stroke(); ctx.strokeStyle = INK; ctx.lineWidth = 1; ctx.strokeRect(cx - 4, cy - 4, 8, 8); if (ed.snapped) { ctx.strokeStyle = ACCENT; ctx.lineWidth = 1.4; ctx.beginPath(); ctx.moveTo(cx - 6, cy - 6); ctx.lineTo(cx + 6, cy + 6); ctx.moveTo(cx + 6, cy - 6); ctx.lineTo(cx - 6, cy + 6); ctx.stroke(); } } if (ed.tip && ed.cursor) { const lines = ed.tip.split('\n'); ctx.font = fnt(9.5, 500, F_MONO); const tw = Math.max(...lines.map(l => ctx.measureText(l).width)) + 10, th = lines.length * 13 + 5; let x = ed.cursor[0] + 16, y = ed.cursor[1] + 16; if (x + tw > w - 46) x = ed.cursor[0] - tw - 12; if (y + th > h - 46) y = ed.cursor[1] - th - 12; ctx.fillStyle = 'rgba(251,250,246,0.95)'; ctx.fillRect(x, y, tw, th); ctx.strokeStyle = INK; ctx.lineWidth = 0.8; ctx.strokeRect(x, y, tw, th); ctx.fillStyle = INK; ctx.textAlign = 'left'; ctx.textBaseline = 'top'; lines.forEach((l, i) => ctx.fillText(l, x + 5, y + 4 + i * 13)); } ctx.restore(); } /* ---------- LP-01 site analysis plan ---------- */ function siteExt(site) { return ctxExt(site, 3.5, 2.5, 12); } function drawSitePlan(ctx, w, h, site, sun, ui) { const tx = ui.tx || sheetTx(site, w, h, { l: 34, t: 34, r: 46, b: 46 }, siteExt(site)); ctx.fillStyle = PAPER; ctx.fillRect(0, 0, w, h); const [m, d] = ui.md, hr = ui.time, layer = ui.layer, sp = sunPos(m, d, hr); let legend = ''; if (layer === 'shadow') { // Shadows are exact polygons: each prism's footprint, its roof image and every swept wall, filled as one union. // (The v2 row strips sampled a grid; a polygon per wall is both exact and far cheaper.) if (sp.alt > 0) { const pieces = shadowPieces(site, sunVector(site, sp)); ctx.save(); ctx.beginPath(); ctx.rect(30, 30, w - 74, h - 74); ctx.clip(); ctx.fillStyle = 'rgba(46,46,48,0.20)'; ctx.beginPath(); for (const P of pieces) polyPathTx(ctx, P, tx); ctx.fill('nonzero'); ctx.restore(); legend = `太陽高度 ${sp.alt.toFixed(1)}°,方位 ${sp.az.toFixed(0)}°(${compass(sp.az)})。灰色為建築物陰影。`; } drawContext(ctx, w, h, site, tx, ui); if (sp.alt <= 0) { ctx.fillStyle = 'rgba(46,46,48,0.28)'; ctx.beginPath(); polyPathTx(ctx, site.poly, tx); ctx.fill(); legend = '太陽在地平線以下。'; } } else { drawContext(ctx, w, h, site, tx, ui); if (ui.dragging) legend = '編輯中:放手後重新計算日照時數。'; else if (layer === 'dec' || layer === 'jun') { const G = sun[layer], mx = layer === 'dec' ? 11 : 13.5; ctx.save(); ctx.beginPath(); polyPathTx(ctx, site.poly, tx); ctx.clip(); for (let j = 0; j < G.ny; j++) for (let i = 0; i < G.nx; i++) { const v = G.g[j * G.nx + i]; if (isNaN(v)) continue; ctx.fillStyle = heatColor(v, mx); ctx.fillRect(tx.x(i * site.W / G.nx), tx.y(j * site.D / G.ny), site.W / G.nx * tx.sc + 0.6, site.D / G.ny * tx.sc + 0.6); } ctx.restore(); ctx.font = fnt(8.5, 500, F_MONO); ctx.textAlign = 'center'; ctx.fillStyle = 'rgba(20,20,20,0.78)'; const stI = Math.max(1, Math.round(G.nx / 6)), stJ = Math.max(1, Math.round(G.ny / 4)); for (let j = Math.floor(stJ / 2); j < G.ny; j += stJ) for (let i = Math.floor(stI / 2); i < G.nx; i += stI) { const v = G.g[j * G.nx + i]; if (!isNaN(v)) ctx.fillText(v.toFixed(1), tx.x((i + 0.5) * site.W / G.nx), tx.y((j + 0.5) * site.D / G.ny) + 3); } const all = sunVals(G), avg = all.reduce((a, b) => a + b, 0) / (all.length || 1); legend = `${KEY_DATES[layer][2]}:直射日照時數(只計地盤範圍內)。平均 ${avg.toFixed(1)} h,最少 ${Math.min(...all).toFixed(1)} h,最多 ${Math.max(...all).toFixed(1)} h。`; } else { drawZones(ctx, site, sun, tx, 1.3); const all = sunVals(sun.avg), cnt = { sun: 0, part: 0, shade: 0 }; for (const v of all) cnt[zoneOfHours(v)]++; legend = Object.keys(cnt).map(k => `${ZONE_ZH[k]} ${Math.round(cnt[k] / (all.length || 1) * 100)}%`).join(',') + '(全年平均,只計地盤範圍內)'; } } // area tag (bottom left, inside the frame; the top edge is kept for off-sheet building arrows) { const A = Math.abs(polyArea(site.poly)), Pm = polyPerim(site.poly), t = `地盤 SITE ${A.toFixed(1)} m² 周界 ${Pm.toFixed(1)} m`; ctx.save(); ctx.font = fnt(9, 600, F_MONO); const tw = ctx.measureText(t).width + 10; const ty = h - 66; ctx.fillStyle = PAPER; ctx.fillRect(36, ty, tw, 16); ctx.strokeStyle = INK; ctx.lineWidth = 0.8; ctx.strokeRect(36, ty, tw, 16); ctx.fillStyle = INK; ctx.textAlign = 'left'; ctx.textBaseline = 'middle'; ctx.fillText(t, 41, ty + 8.5); ctx.restore(); } gardenEdge(ctx, site, tx); drawPlanFrame(ctx, w, h, site, tx, { edgeOff: 17 }); // boundary points P1, P2 ... outside each corner (they name the edges in the notes) { const P = site.poly, n = P.length; ctx.save(); ctx.font = fnt(8.5, 600, F_MONO); ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; for (let i = 0; i < n; i++) { const a = P[(i + n - 1) % n], b = P[i], c = P[(i + 1) % n], la = Math.hypot(b[0] - a[0], b[1] - a[1]) || 1, lc = Math.hypot(c[0] - b[0], c[1] - b[1]) || 1; let bx = (b[0] - a[0]) / la - (c[0] - b[0]) / lc, by = (b[1] - a[1]) / la - (c[1] - b[1]) / lc; const bl = Math.hypot(bx, by) || 1; bx /= bl; by /= bl; if (inSite(site, b[0] + bx * 0.05, b[1] + by * 0.05)) { bx = -bx; by = -by; } // always outside the garden const x = tx.x(b[0]) + bx * 11, y = tx.y(b[1]) + by * 11, t = ptLabel(i), tw = ctx.measureText(t).width + 4; ctx.fillStyle = PAPER; ctx.fillRect(x - tw / 2, y - 6, tw, 12); ctx.fillStyle = INK; ctx.fillText(t, x, y + 0.5); } ctx.restore(); } // views, on their boundary edge const E = siteEdges(site); for (const v of site.views) { const e = E[v.edge]; if (!e) continue; const [x, y] = viewPos(site, v), px = tx.x(x), py = tx.y(y); const nrm = [e.nx, e.ny], ang = Math.atan2(nrm[1], nrm[0]); ctx.save(); if (v.type === 'good') { ctx.strokeStyle = '#4d7a52'; ctx.fillStyle = 'rgba(77,122,82,0.10)'; ctx.lineWidth = 1; ctx.setLineDash([4, 3]); ctx.beginPath(); ctx.moveTo(px, py); ctx.arc(px, py, 44, ang - 0.55, ang + 0.55); ctx.closePath(); ctx.fill(); ctx.stroke(); ctx.setLineDash([]); ctx.fillStyle = '#35603a'; ctx.font = fnt(9, 600, F_MONO); ctx.textAlign = 'center'; ctx.fillText('好景 VIEW', px + nrm[0] * 56, py + nrm[1] * 56 + 3); } else { const tv = [nrm[1], -nrm[0]]; ctx.strokeStyle = ACCENT; ctx.lineWidth = 1.6; ctx.beginPath(); for (let k = -4; k <= 4; k++) { const q = k * 4.5, zz = (k % 2 ? 5 : -5); const X = px + tv[0] * q + nrm[0] * zz, Y = py + tv[1] * q + nrm[1] * zz; k === -4 ? ctx.moveTo(X, Y) : ctx.lineTo(X, Y); } ctx.stroke(); ctx.fillStyle = ACCENT; ctx.font = fnt(9, 600, F_MONO); ctx.textAlign = 'center'; ctx.fillText('遮擋 SCREEN', px + nrm[0] * 26, py + nrm[1] * 26 + 3); } ctx.fillStyle = PAPER; ctx.strokeStyle = INK; ctx.lineWidth = 1; ctx.beginPath(); ctx.arc(px, py, 3.5, 0, 6.283); ctx.fill(); ctx.stroke(); ctx.restore(); } // prevailing wind (HKO climatology, approximate): arrows aimed at the middle of the garden, stopping at its outline if (ui.wind) { let [mx, my] = polyCentroid(site.poly); if (!inSite(site, mx, my)) { const ic = siteInscribed(site); mx = ic.x; my = ic.y; } const cx = tx.x(mx), cy = tx.y(my), Rr = Math.min(Math.hypot(site.W, site.D) / 2 * tx.sc + 44, Math.min(w, h) / 2 - 30); for (const wd of WIND) { const [sx, sy] = bearingToScreen(site, wd.from), rg = linePolyRanges([mx, my], [sx, sy], site.poly), exitM = Math.max(0, ...rg.map(r => r[1])); const tip = Math.max(Rr - 50, exitM * tx.sc + 10), tail = tip + 50; const x0 = cx + sx * tail, y0 = cy + sy * tail, x1 = cx + sx * tip, y1 = cy + sy * tip; ctx.save(); ctx.strokeStyle = wd.col; ctx.fillStyle = wd.col; ctx.lineWidth = 2; ctx.setLineDash([7, 3]); ctx.beginPath(); ctx.moveTo(x0, y0); ctx.lineTo(x1, y1); ctx.stroke(); ctx.setLineDash([]); const a = Math.atan2(y1 - y0, x1 - x0); ctx.beginPath(); ctx.moveTo(x1 + Math.cos(a) * 3, y1 + Math.sin(a) * 3); ctx.lineTo(x1 - 10 * Math.cos(a - 0.4), y1 - 10 * Math.sin(a - 0.4)); ctx.lineTo(x1 - 10 * Math.cos(a + 0.4), y1 - 10 * Math.sin(a + 0.4)); ctx.closePath(); ctx.fill(); ctx.font = fnt(9, 600, F_MONO); ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; const txt = wd.label + '風 ' + (wd.id === 'winter' ? '9–5月' : '6–8月'), lx = Math.max(60, Math.min(w - 90, x0 + sx * 10)), ly = Math.max(40, Math.min(h - 56, y0 + sy * 10)), tw = ctx.measureText(txt).width + 8; ctx.fillStyle = PAPER; ctx.fillRect(lx - tw / 2, ly - 7, tw, 14); ctx.strokeRect(lx - tw / 2, ly - 7, tw, 14); ctx.fillStyle = wd.col; ctx.fillText(txt, lx, ly + 0.5); ctx.restore(); } } drawEdit(ctx, w, h, tx, ui.edit); return { tx, legend, sp }; } /* ---------- plant codes and schedule ---------- */ const CODE_PRE = { tree: 'T', palm: 'P', shrub: 'S', climber: 'C', ground: 'G' }; function plantCodes(comp) { const order = ['tree', 'palm', 'shrub', 'climber', 'ground'], map = {}, rows = [], cnt = {}; const ids = [...new Set(comp.items.filter(i => i.kind === 'plant').map(i => i.id))].filter(id => libById(id)); ids.sort((a, b) => order.indexOf(libById(a).type) - order.indexOf(libById(b).type)); for (const id of ids) { const p = libById(id), k = CODE_PRE[p.type] || 'X'; cnt[k] = (cnt[k] || 0) + 1; const code = k + String(cnt[k]).padStart(2, '0'); map[id] = code; rows.push({ code, p, qty: comp.items.filter(i => i.id === id).length }); } return { map, rows }; } /* ---------- LP-02 planting plan ---------- */ function planExt(site) { return ctxExt(site, 1.4, 1, 3); } function crownOutline(ctx, cx, cy, r, R) { // scalloped canopy line const n = Math.max(8, Math.round(r / 3.2)); ctx.beginPath(); for (let i = 0; i < n; i++) { const a0 = i / n * 6.2832, a1 = (i + 1) / n * 6.2832, rr = r * (0.94 + R() * 0.08); const p0 = [cx + rr * Math.cos(a0), cy + rr * Math.sin(a0)], p1 = [cx + r * Math.cos(a1), cy + r * Math.sin(a1)]; const mx = (p0[0] + p1[0]) / 2, my = (p0[1] + p1[1]) / 2, d = Math.hypot(p1[0] - p0[0], p1[1] - p0[1]), ang = Math.atan2(p1[1] - p0[1], p1[0] - p0[0]); ctx.arc(mx, my, d / 2, ang + Math.PI, ang + 2 * Math.PI, false); } } function drawPlantingPlan(ctx, w, h, comp, sun, ui) { const site = comp.site, tx = sheetTx(site, w, h, { l: 34, t: 34, r: 46, b: 46 }, planExt(site)); ctx.fillStyle = PAPER; ctx.fillRect(0, 0, w, h); drawContext(ctx, w, h, site, tx); if (ui.zones && sun) drawZones(ctx, site, sun, tx, 0.75); gardenEdge(ctx, site, tx); const G = drawPlanFrame(ctx, w, h, site, tx, { grid: ui.grid !== false, dims: ui.dims !== false }); const R = rng(3), hl = new Set(ui.highlight || []), codes = ui.codes || plantCodes(comp); // view cone V1 const e = comp.eye, Lc = Math.hypot(site.W, site.D); ctx.save(); ctx.beginPath(); polyPathTx(ctx, site.poly, tx); ctx.clip(); ctx.fillStyle = 'rgba(196,118,60,0.06)'; ctx.strokeStyle = ACCENT; ctx.lineWidth = 0.8; ctx.setLineDash([5, 4]); ctx.beginPath(); ctx.moveTo(tx.x(e.x), tx.y(e.y)); for (let a = -e.fov / 2; a <= e.fov / 2 + 0.01; a += 4) { const r = (e.dir + a) * D2R; ctx.lineTo(tx.x(e.x + Math.sin(r) * Lc), tx.y(e.y - Math.cos(r) * Lc)); } ctx.closePath(); ctx.fill(); for (const s of [-1, 1]) { const r = (e.dir + s * e.fov / 2) * D2R; ctx.beginPath(); ctx.moveTo(tx.x(e.x), tx.y(e.y)); ctx.lineTo(tx.x(e.x + Math.sin(r) * Lc), tx.y(e.y - Math.cos(r) * Lc)); ctx.stroke(); } ctx.restore(); // plants: ground cover, shrubs, then canopies on top const rank = { ground: 0, climber: 0, shrub: 1, palm: 2, tree: 3 }; const plants = comp.items.filter(i => plantOf(i)).sort((a, b) => rank[plantOf(a).type] - rank[plantOf(b).type] || matureH(plantOf(a)) - matureH(plantOf(b))); const geo = {}; for (const it of plants) { const p = plantOf(it), sz = sizeAtYears(p, comp.years), st = phenology(p, comp.month), cx = tx.x(it.x), cy = tx.y(it.y), r = Math.max(3, sz.S / 2 * tx.sc); geo[it.uid] = { cx, cy, r }; const sp = paramsFor(p), col = st === 'A' ? (sp.autumn || '#c2552f') : st === 'N' ? mix(sp.leaf, '#c6de8a', 0.4) : sp.leaf, bare = st === 'B' || st === 'F'; const Rl = rng((it.seed || 1) * 13 + 1); if (p.type === 'ground' || p.type === 'climber') { ctx.fillStyle = rgba(col, 0.28); blobPath(ctx, cx, cy, r, Rl, 14, 0.12); ctx.fill(); stippleIn(ctx, c => blobPath(c, cx, cy, r, rng((it.seed || 1) * 13 + 1), 14, 0.12), cx - r, cy - r, cx + r, cy + r, { dens: 0.05, col: rgba(sp.leafDark, 0.8), seed: it.seed || 3, r: 0.6 }); ctx.save(); ctx.setLineDash([2, 2]); ctx.strokeStyle = GRAPH; ctx.lineWidth = 0.6; blobPath(ctx, cx, cy, r, rng((it.seed || 1) * 13 + 1), 14, 0.12); ctx.stroke(); ctx.restore(); } else if (p.type === 'shrub') { ctx.fillStyle = rgba(col, 0.42); crownOutline(ctx, cx, cy, r, Rl); ctx.fill(); ctx.strokeStyle = GRAPH; ctx.lineWidth = 0.8; crownOutline(ctx, cx, cy, r, rng((it.seed || 1) * 13 + 1)); ctx.stroke(); ctx.fillStyle = GRAPH; ctx.beginPath(); ctx.arc(cx, cy, 1.1, 0, 6.283); ctx.fill(); } else if (p.type === 'palm') { ctx.fillStyle = rgba(col, 0.22); ctx.beginPath(); ctx.arc(cx, cy, r, 0, 6.283); ctx.fill(); ctx.strokeStyle = rgba(sp.leafDark, 0.95); ctx.lineWidth = 0.8; for (let k = 0; k < 11; k++) { const a = k / 11 * 6.283 + Rl() * 0.2; ctx.beginPath(); ctx.moveTo(cx, cy); ctx.lineTo(cx + Math.cos(a) * r, cy + Math.sin(a) * r); ctx.stroke(); for (let q = 0.35; q < 0.95; q += 0.15) { const bx = cx + Math.cos(a) * r * q, by = cy + Math.sin(a) * r * q, l = r * 0.12; ctx.beginPath(); ctx.moveTo(bx, by); ctx.lineTo(bx + Math.cos(a + 0.7) * l, by + Math.sin(a + 0.7) * l); ctx.moveTo(bx, by); ctx.lineTo(bx + Math.cos(a - 0.7) * l, by + Math.sin(a - 0.7) * l); ctx.stroke(); } } } else { // tree ctx.save(); ctx.setLineDash([2, 3]); ctx.strokeStyle = LIGHT; ctx.lineWidth = 0.6; ctx.beginPath(); ctx.arc(cx, cy, matureS(p) / 2 * tx.sc, 0, 6.283); ctx.stroke(); ctx.restore(); if (!bare) { const gr = ctx.createRadialGradient(cx - r * 0.3, cy - r * 0.3, r * 0.1, cx, cy, r); gr.addColorStop(0, rgba(shade(col, 0.35), 0.30)); gr.addColorStop(1, rgba(sp.leafDark, 0.42)); ctx.fillStyle = gr; blobPath(ctx, cx, cy, r * 0.98, Rl, 18, 0.06); ctx.fill(); } else { // bare crown: branch symbol ctx.strokeStyle = GRAPH; ctx.lineWidth = 0.7; for (let k = 0; k < 7; k++) { const a = k / 7 * 6.283 + Rl(); ctx.beginPath(); ctx.moveTo(cx, cy); const mx = cx + Math.cos(a) * r * 0.55, my = cy + Math.sin(a) * r * 0.55; ctx.lineTo(mx, my); ctx.lineTo(cx + Math.cos(a - 0.25) * r * 0.92, cy + Math.sin(a - 0.25) * r * 0.92); ctx.moveTo(mx, my); ctx.lineTo(cx + Math.cos(a + 0.3) * r * 0.9, cy + Math.sin(a + 0.3) * r * 0.9); ctx.stroke(); } } if (sp.flower && (st === 'F' || st === 'LF')) { ctx.fillStyle = rgba(sp.flower, 0.8); for (let k = 0; k < Math.min(10, 3 + r / 6); k++) { const a = Rl() * 6.283, dd = Math.sqrt(Rl()) * r * 0.85; ctx.beginPath(); ctx.arc(cx + Math.cos(a) * dd, cy + Math.sin(a) * dd, Math.max(1.2, Math.min(3.2, r * 0.05)), 0, 6.283); ctx.fill(); } } ctx.strokeStyle = INK; ctx.lineWidth = 1; crownOutline(ctx, cx, cy, r, rng((it.seed || 1) * 7 + 2)); ctx.stroke(); ctx.lineWidth = 0.8; ctx.beginPath(); ctx.moveTo(cx - 5, cy); ctx.lineTo(cx + 5, cy); ctx.moveTo(cx, cy - 5); ctx.lineTo(cx, cy + 5); ctx.stroke(); ctx.fillStyle = PAPER; ctx.beginPath(); ctx.arc(cx, cy, 2.2, 0, 6.283); ctx.fill(); ctx.stroke(); } if (ui.sel === it.uid) { ctx.save(); ctx.strokeStyle = ACCENT; ctx.lineWidth = 2; ctx.setLineDash([6, 3]); ctx.beginPath(); ctx.arc(cx, cy, r + 4, 0, 6.283); ctx.stroke(); ctx.restore(); } } // figures (plan symbol: shoulders and head) for (const it of comp.items.filter(i => !plantOf(i))) { const cx = tx.x(it.x), cy = tx.y(it.y), s = Math.max(4, 0.25 * tx.sc); geo[it.uid] = { cx, cy, r: s + 2 }; ctx.save(); ctx.strokeStyle = INK; ctx.lineWidth = 0.9; ctx.fillStyle = it.id === 'mascot_egret' ? PAPER : '#e4e0d6'; if (it.id === 'mascot_egret') { ctx.beginPath(); ctx.ellipse(cx, cy, s * 0.8, s * 0.45, -0.5, 0, 6.283); ctx.fill(); ctx.stroke(); ctx.beginPath(); ctx.moveTo(cx + s * 0.6, cy - s * 0.35); ctx.lineTo(cx + s * 1.2, cy - s * 0.8); ctx.stroke(); } else { ctx.beginPath(); ctx.ellipse(cx, cy, s, s * 0.55, 0, 0, 6.283); ctx.fill(); ctx.stroke(); ctx.fillStyle = INK; ctx.beginPath(); ctx.arc(cx, cy, s * 0.36, 0, 6.283); ctx.fill(); } if (ui.sel === it.uid) { ctx.strokeStyle = ACCENT; ctx.lineWidth = 2; ctx.beginPath(); ctx.arc(cx, cy, s + 5, 0, 6.283); ctx.stroke(); } ctx.restore(); } // keynote tags with leaders if (ui.keys !== false) { const placed = [], dirs = [-0.785, -2.356, 0.785, 2.356, -1.571, 1.571, 0, 3.1416]; const tags = plants.slice().sort((a, b) => geo[b.uid].r - geo[a.uid].r); ctx.font = fnt(8.5, 600, F_MONO); for (const it of tags) { const code = codes.map[it.id]; if (!code) continue; const g = geo[it.uid], tw = ctx.measureText(code).width + 7, big = isTreeLike(plantOf(it)); let best = null; for (const a of dirs) { const d = g.r + (big ? 12 : 9), x = g.cx + Math.cos(a) * d + Math.cos(a) * tw / 2, y = g.cy + Math.sin(a) * d; const box = [x - tw / 2 - 2, y - 8, x + tw / 2 + 2, y + 8]; const hit = placed.some(b => b[0] < box[2] && box[0] < b[2] && b[1] < box[3] && box[1] < b[3]) || box[0] < 28 || box[2] > w - 48 || box[1] < 28 || box[3] > h - 48; if (!hit) { best = { a, x, y, box }; break; } if (!best) best = { a, x, y, box }; } placed.push(best.box); ctx.strokeStyle = GRAPH; ctx.lineWidth = 0.6; ctx.beginPath(); ctx.moveTo(g.cx + Math.cos(best.a) * g.r * 0.7, g.cy + Math.sin(best.a) * g.r * 0.7); ctx.lineTo(best.x - Math.cos(best.a) * tw / 2, best.y); ctx.stroke(); ctx.fillStyle = GRAPH; ctx.beginPath(); ctx.arc(g.cx + Math.cos(best.a) * g.r * 0.7, g.cy + Math.sin(best.a) * g.r * 0.7, 1.3, 0, 6.283); ctx.fill(); keyTagBox(ctx, best.x, best.y, code, big); } } // section A-A marker if (ui.section) sectionMarker(ctx, tx, ui.section, 'A', ui.sheetNo || 'LP-02'); // measure (accent) if (ui.measure) { const M = ui.measure, L = Math.hypot(M.x2 - M.x1, M.y2 - M.y1); ctx.save(); dimLine(ctx, tx.x(M.x1), tx.y(M.y1), tx.x(M.x2), tx.y(M.y2), mmTxt(L), 0, { col: ACCENT, size: 10 }); ctx.fillStyle = ACCENT; for (const [x, y] of [[M.x1, M.y1], [M.x2, M.y2]]) { ctx.beginPath(); ctx.arc(tx.x(x), tx.y(y), 2.5, 0, 6.283); ctx.fill(); } ctx.restore(); } // red-pen mark-up: clouds and note numbers const tagsAt = {}; for (const nt of ui.notes || []) { const its = [...new Set(nt.items || [])].map(u => geo[u]).filter(Boolean); if (!its.length) continue; const on = ui.hoverNote === nt.n; ctx.save(); ctx.strokeStyle = REDPEN; ctx.lineWidth = on ? 2 : nt.status === 'fail' ? 1.2 : 0.9; ctx.globalAlpha = on ? 1 : ui.hoverNote == null ? 0.85 : 0.3; const groups = its.slice(0, 4).map(g => ({ cx: g.cx, cy: g.cy, rx: g.r + 6, ry: g.r + 6 })); for (const c of groups) { cloudPath(ctx, c.cx, c.cy, c.rx, c.ry, Math.max(4, Math.min(7, c.rx / 4))); ctx.stroke(); const key = Math.round(c.cx) + ',' + Math.round(c.cy), k = tagsAt[key] = (tagsAt[key] || 0) + 1; const ta = -0.785 + (k - 1) * 0.62; noteTag(ctx, c.cx + Math.cos(ta) * (c.rx + 4), c.cy + Math.sin(ta) * (c.ry + 4), String(nt.n), on); } ctx.restore(); } if (ui.ghost) { const gx = tx.x(ui.ghost.x), gy = tx.y(ui.ghost.y); ctx.save(); ctx.strokeStyle = REDPEN; ctx.lineWidth = 1.5; ctx.setLineDash([5, 3]); ctx.beginPath(); ctx.arc(gx, gy, Math.max(10, (ui.ghost.r || 0.6) * tx.sc), 0, 6.283); ctx.stroke(); if (ui.ghostFrom) { const f = geo[ui.ghostFrom]; if (f) { ctx.beginPath(); ctx.moveTo(f.cx, f.cy); ctx.lineTo(gx, gy); ctx.stroke(); ctx.setLineDash([]); const a = Math.atan2(gy - f.cy, gx - f.cx); ctx.fillStyle = REDPEN; ctx.beginPath(); ctx.moveTo(gx - Math.cos(a) * 10, gy - Math.sin(a) * 10); ctx.lineTo(gx - Math.cos(a) * 18 - Math.sin(a) * 4, gy - Math.sin(a) * 18 + Math.cos(a) * 4); ctx.lineTo(gx - Math.cos(a) * 18 + Math.sin(a) * 4, gy - Math.sin(a) * 18 - Math.cos(a) * 4); ctx.fill(); } } ctx.setLineDash([]); ctx.fillStyle = REDPEN; ctx.font = fnt(9, 600, F_MONO); ctx.textAlign = 'left'; ctx.fillText('建議位置', gx + 12, gy - 10); ctx.restore(); } // eye point V1 and direction handle const ex = tx.x(e.x), ey = tx.y(e.y), hx = e.x + Math.sin(e.dir * D2R) * 2, hy = e.y - Math.cos(e.dir * D2R) * 2; ctx.save(); ctx.strokeStyle = ACCENT; ctx.fillStyle = ACCENT; ctx.lineWidth = 1.4; ctx.beginPath(); ctx.moveTo(ex, ey); ctx.lineTo(tx.x(hx), tx.y(hy)); ctx.stroke(); ctx.beginPath(); ctx.arc(tx.x(hx), tx.y(hy), 4.5, 0, 6.283); ctx.fill(); ctx.fillStyle = PAPER; ctx.beginPath(); ctx.arc(ex, ey, 7, 0, 6.283); ctx.fill(); ctx.stroke(); ctx.fillStyle = ACCENT; ctx.font = fnt(7.5, 700, F_MONO); ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.fillText('V1', ex, ey + 0.5); ctx.font = fnt(8.5, 500, F_MONO); ctx.textAlign = 'left'; ctx.fillText(`視點 +${e.h} m`, ex + 10, ey + 12); ctx.restore(); return { tx, handle: { x: hx, y: hy }, geo, grid: G }; } function sectionMarker(ctx, tx, sec, letter, sheetNo) { const A = [tx.x(sec.x1), tx.y(sec.y1)], B = [tx.x(sec.x2), tx.y(sec.y2)], L = Math.hypot(B[0] - A[0], B[1] - A[1]); if (L < 6) return; const u = [(B[0] - A[0]) / L, (B[1] - A[1]) / L], n = [u[1], -u[0]]; ctx.save(); ctx.strokeStyle = INK; ctx.lineWidth = 0.8; ctx.setLineDash([14, 4, 3, 4]); ctx.beginPath(); ctx.moveTo(A[0], A[1]); ctx.lineTo(B[0], B[1]); ctx.stroke(); ctx.setLineDash([]); ctx.lineWidth = 2.6; ctx.beginPath(); ctx.moveTo(A[0], A[1]); ctx.lineTo(A[0] + u[0] * 14, A[1] + u[1] * 14); ctx.moveTo(B[0], B[1]); ctx.lineTo(B[0] - u[0] * 14, B[1] - u[1] * 14); ctx.stroke(); for (const [P, s] of [[A, -1], [B, 1]]) { const c = [P[0] + u[0] * s * 13, P[1] + u[1] * s * 13]; ctx.fillStyle = INK; ctx.beginPath(); ctx.moveTo(c[0] + n[0] * 19, c[1] + n[1] * 19); ctx.lineTo(c[0] + u[0] * 10, c[1] + u[1] * 10); ctx.lineTo(c[0] - u[0] * 10, c[1] - u[1] * 10); ctx.closePath(); ctx.fill(); ctx.beginPath(); ctx.arc(c[0], c[1], 10, 0, 6.283); ctx.fillStyle = PAPER; ctx.fill(); ctx.lineWidth = 1; ctx.stroke(); ctx.beginPath(); ctx.moveTo(c[0] - 10, c[1]); ctx.lineTo(c[0] + 10, c[1]); ctx.lineWidth = 0.6; ctx.stroke(); ctx.fillStyle = INK; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; ctx.font = fnt(8.5, 600, F_MONO); ctx.fillText(letter, c[0], c[1] - 4.5); ctx.font = fnt(5.5, 500, F_MONO); ctx.fillText(sheetNo.replace('-', ''), c[0], c[1] + 4.5); } ctx.restore(); } /* ---------- LP-02 section A-A ---------- */ function lineRectRange(p, u, x0, y0, x1, y1) { // infinite line p + u*l, returns [lo, hi] inside the rectangle or null let lo = -1e9, hi = 1e9; for (const [pc, uc, a, b] of [[p[0], u[0], x0, x1], [p[1], u[1], y0, y1]]) { if (Math.abs(uc) < 1e-9) { if (pc < a || pc > b) return null; continue; } let t0 = (a - pc) / uc, t1 = (b - pc) / uc; if (t0 > t1) [t0, t1] = [t1, t0]; lo = Math.max(lo, t0); hi = Math.min(hi, t1); } return lo < hi ? [lo, hi] : null; } function drawSection(ctx, w, h, comp, sec, ui = {}) { const site = comp.site, codes = ui.codes || plantCodes(comp); ctx.fillStyle = PAPER; ctx.fillRect(0, 0, w, h); const len = Math.hypot(sec.x2 - sec.x1, sec.y2 - sec.y1) || 1, u = [(sec.x2 - sec.x1) / len, (sec.y2 - sec.y1) / len], n = [u[1], -u[0]], p1 = [sec.x1, sec.y1]; const toL = (x, y) => (x - p1[0]) * u[0] + (y - p1[1]) * u[1], toF = (x, y) => (x - p1[0]) * n[0] + (y - p1[1]) * n[1]; const gRs = linePolyRanges(p1, u, site.poly), gR = gRs.length ? [gRs[0][0], gRs[gRs.length - 1][1]] : [0, len]; const lmin = Math.min(gR[0], 0) - 2.2, lmax = Math.max(gR[1], len) + 2.2; const items = comp.items.map(it => { const p = plantOf(it), sz = p ? sizeAtYears(p, comp.years) : null; return { it, p, sz, l: toL(it.x, it.y), f: toF(it.x, it.y) }; }) .filter(o => o.f >= -(o.sz ? o.sz.S / 2 : 0.4)).sort((a, b) => b.f - a.f); const maxZ = Math.max(4, ...items.filter(o => o.p).map(o => o.sz.H)) * 1.12; const soil = site.podium ? site.soil : 0, zb = site.podium ? soil + 0.35 : 0.7; const padL = 62, padR = 14, padT = 44, padB = 10; const sc = Math.max(2, Math.min((w - padL - padR) / (lmax - lmin), (h - padT - padB) / (maxZ + zb))); const X = l => padL + (l - lmin) * sc, Y = z => padT + (maxZ - z) * sc, gy = Y(0), xl = X(lmin), xr = X(lmax); // level scale const step = maxZ > 14 ? 5 : maxZ > 7 ? 2 : 1; ctx.font = fnt(8, 400, F_MONO); ctx.textAlign = 'right'; ctx.textBaseline = 'middle'; for (let z = step; z < maxZ; z += step) { ctx.strokeStyle = HAIR; ctx.lineWidth = 0.5; ctx.setLineDash([2, 4]); ctx.beginPath(); ctx.moveTo(xl, Y(z)); ctx.lineTo(xr, Y(z)); ctx.stroke(); ctx.setLineDash([]); ctx.fillStyle = LIGHT; ctx.fillText('+' + z.toFixed(1), padL - 6, Y(z)); } // ground, soil, slab const gA = X(gR[0]), segs = (gRs.length ? gRs : [gR]).map(r => [X(r[0]), X(r[1])]); if (site.podium) { const ys = Y(-soil), yb = Y(-soil - 0.35); ctx.fillStyle = '#d9d6cd'; ctx.fillRect(xl, ys, xr - xl, yb - ys); hatchIn(ctx, c => c.rect(xl, ys, xr - xl, yb - ys), { sp: 3.5, col: GRAPH, lw: 0.5 }); ctx.strokeStyle = INK; ctx.lineWidth = 1.2; ctx.strokeRect(xl, ys, xr - xl, yb - ys); const t = Math.max(3, 0.2 * sc); for (const [a, b] of segs) { // each stretch of garden the cut crosses (an L or U shape can give several) ctx.fillStyle = '#efe8d8'; ctx.fillRect(a, gy, b - a, ys - gy); stippleIn(ctx, c => c.rect(a, gy, b - a, ys - gy), a, gy, b, ys, { dens: 0.08, col: '#8b7d62', seed: 9, r: 0.6 }); ctx.fillStyle = INK; ctx.fillRect(a - t, Y(0.45), t, ys - Y(0.45)); ctx.fillRect(b, Y(0.45), t, ys - Y(0.45)); } ctx.strokeStyle = INK; ctx.lineWidth = 1.2; ctx.beginPath(); ctx.moveTo(xl, ys); ctx.lineTo(segs[0][0] - t, ys); for (let k = 0; k + 1 < segs.length; k++) { ctx.moveTo(segs[k][1] + t, ys); ctx.lineTo(segs[k + 1][0] - t, ys); } ctx.moveTo(segs[segs.length - 1][1] + t, ys); ctx.lineTo(xr, ys); ctx.stroke(); dimLine(ctx, gA + 12, gy, gA + 12, ys, mmTxt(soil), 0, { col: INK, size: 8.5 }); ctx.font = fnt(8, 500, F_MONO); ctx.textAlign = 'left'; ctx.fillStyle = INK; ctx.textBaseline = 'middle'; ctx.fillText('種植土 SOIL', gA + 22, (gy + ys) / 2); ctx.fillText(`結構板 SSL +${(site.level || 0).toFixed(2)}`, 4 + xl, (ys + yb) / 2 + (yb - ys) / 2 + 8); } else { ctx.fillStyle = '#efe8d8'; ctx.fillRect(xl, gy, xr - xl, Y(-zb) - gy); hatchIn(ctx, c => c.rect(xl, gy, xr - xl, Y(-zb) - gy), { sp: 6, ang: -45, col: '#8b7d62', lw: 0.5 }); } ctx.strokeStyle = INK; ctx.lineWidth = 2; ctx.beginPath(); ctx.moveTo(xl, gy); ctx.lineTo(xr, gy); ctx.stroke(); // datum const fgl = (site.podium ? (site.level || 0) + soil : 0).toFixed(2); ctx.fillStyle = INK; ctx.beginPath(); ctx.moveTo(padL - 12, gy - 7); ctx.lineTo(padL - 4, gy - 7); ctx.lineTo(padL - 8, gy - 1); ctx.closePath(); ctx.fill(); ctx.font = fnt(8, 600, F_MONO); ctx.textAlign = 'right'; ctx.textBaseline = 'bottom'; ctx.fillText('FGL', padL - 14, gy - 7); ctx.textBaseline = 'top'; ctx.font = fnt(8, 400, F_MONO); ctx.fillText('+' + fgl, padL - 4, gy + 2); // buildings cut by the section line for (const b of solidBldgs(site)) for (const r of linePolyRanges(p1, u, b.poly)) { const a = Math.max(lmin, r[0]), c = Math.min(lmax, r[1]); if (c - a < 0.05) continue; const top = Math.min(b.h, maxZ - 0.3), x0 = X(a), x1 = X(c), y0 = Y(top); ctx.fillStyle = '#e6e3db'; ctx.fillRect(x0, y0, x1 - x0, gy - y0); hatchIn(ctx, k => k.rect(x0, y0, x1 - x0, gy - y0), { sp: 4, col: GRAPH, lw: 0.5 }); ctx.strokeStyle = INK; ctx.lineWidth = 1.6; ctx.strokeRect(x0, y0, x1 - x0, gy - y0); if (b.h > maxZ - 0.3) { // break line ctx.fillStyle = PAPER; ctx.fillRect(x0 - 2, y0 - 7, x1 - x0 + 4, 9); ctx.strokeStyle = INK; ctx.lineWidth = 0.8; ctx.beginPath(); const mxp = (x0 + x1) / 2; ctx.moveTo(x0 - 4, y0 - 2); ctx.lineTo(mxp - 3, y0 - 2); ctx.lineTo(mxp, y0 - 7); ctx.lineTo(mxp + 2, y0 + 3); ctx.lineTo(mxp + 5, y0 - 2); ctx.lineTo(x1 + 4, y0 - 2); ctx.stroke(); } ctx.save(); ctx.translate((x0 + x1) / 2, (y0 + gy) / 2); ctx.rotate(-Math.PI / 2); ctx.font = fnt(8.5, 500, F_MONO); ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; const t = `${b.b.name} +${b.h} m`, tw = ctx.measureText(t).width + 6; ctx.fillStyle = PAPER; ctx.fillRect(-tw / 2, -6.5, tw, 13); ctx.fillStyle = INK; ctx.fillText(t, 0, 0.5); ctx.restore(); } // grid lines crossing the cut const G = gridLines(site), gbub = []; if (Math.abs(u[0]) > 0.25) for (const o of G.xs) { const l = (o.v - p1[0]) / u[0], y = p1[1] + u[1] * l; if (y >= -0.01 && y <= site.D + 0.01) gbub.push({ l, lab: o.lab }); } if (Math.abs(u[1]) > 0.25) for (const o of G.ys) { const l = (o.v - p1[1]) / u[1], x = p1[0] + u[0] * l; if (x >= -0.01 && x <= site.W + 0.01) gbub.push({ l, lab: o.lab }); } ctx.save(); ctx.strokeStyle = LIGHT; ctx.lineWidth = 0.6; ctx.setLineDash([12, 3, 2, 3]); for (const g of gbub) { ctx.beginPath(); ctx.moveTo(X(g.l), 22); ctx.lineTo(X(g.l), Y(-zb) + 4); ctx.stroke(); } ctx.restore(); for (const g of gbub) bubble(ctx, X(g.l), 12, 8.5, g.lab); // items, far first; depth fades them const fmax = Math.max(1, ...items.map(o => o.f)), tags = []; for (const o of items) { const cut = o.p && Math.abs(o.f) <= o.sz.S / 2; ctx.globalAlpha = cut ? 1 : 0.42 + 0.5 * (1 - Math.min(1, Math.max(0, o.f) / fmax)); if (o.p) { const s = plantSprite(o.p, comp.years, comp.month, comp.style, o.it.seed, sc), k = sc / s.px; ctx.drawImage(s.cv, X(o.l) - s.ax * k, gy - s.ay * k, s.cv.width * k, s.cv.height * k); if (isTreeLike(o.p) || !tags.some(t => t.code === codes.map[o.it.id])) tags.push({ x: X(o.l), y: gy - s.H * sc, code: codes.map[o.it.id], H: s.H, big: isTreeLike(o.p) }); } else { const f = FIGURES.find(f => f.id === o.it.id), im = FIG_IMG[o.it.id]; if (f && im && im.complete && im.naturalWidth) { const hh = f.h * sc, ww = im.naturalWidth / im.naturalHeight * hh; ctx.drawImage(im, X(o.l) - ww / 2, gy - hh, ww, hh); } } } ctx.globalAlpha = 1; // keynote tags above const placed = []; ctx.font = fnt(8.5, 600, F_MONO); tags.filter(t => t.code).sort((a, b) => a.x - b.x).forEach(t => { const txt = t.big ? `${t.code} ${fmt(t.H)}m` : t.code, tw = ctx.measureText(txt).width + 8; let y = Math.max(padT - 10, Math.min(t.y - 12, gy - 20)), x = Math.max(xl + tw / 2, Math.min(xr - tw / 2, t.x)), g = 0; while (placed.some(P => Math.abs(P.x - x) < (P.w + tw) / 2 + 2 && Math.abs(P.y - y) < 15) && g++ < 30) y -= 15; if (y < 8) { y = 8; } placed.push({ x, y, w: tw }); ctx.strokeStyle = GRAPH; ctx.lineWidth = 0.6; ctx.beginPath(); ctx.moveTo(x, y + 6.5); ctx.lineTo(t.x, Math.max(y + 6.5, t.y)); ctx.stroke(); keyTagBox(ctx, x, y, txt, t.big); }); return { sc }; }