/* HK Landscape Sheets: composition model, the 14-rule checker, and the view from the eye point (V1). */ 'use strict'; const ROLE_ZH = { main: '主景', sec: '次景', ter: '配景' }; const ROLE_COL = { main: '#c0392b', sec: '#3f8a4a', ter: '#3d6fb0' }; const RULE_TAG = { design: '設計原則', hk: '香港指引', site: '地盤分析' }; /* soil depth reference values for planting over structures (see SOURCES.podium; verify) */ const SOIL_REQ = { tree: 1.2, palm: 1.2, shrub: 0.6, ground: 0.3, climber: 0.3 }; let _uid = 1; function newUid() { return 'p' + (_uid++) + Math.random().toString(36).slice(2, 5); } function exampleComposition() { const it = (id, x, y, role, seed) => ({ uid: newUid(), kind: 'plant', id, x, y, role, seed: seed || Math.floor(Math.random() * 1e4) }); const fg = (id, x, y) => ({ uid: newUid(), kind: 'figure', id, x, y }); return { name: '示範:10×7 m 平台庭園', years: 5, month: 3, style: 'wash', site: defaultSite(), eye: { x: 5.0, y: 6.85, dir: 0, fov: 64, h: 1.6 }, items: [ it('blakeana', 6.2, 2.4, 'main', 11), it('camphor', 1.8, 1.5, 'sec', 12), it('ficus', 8.9, 5.4, 'sec', 13), it('handroanthus', 3.2, 3.5, 'sec', 14), it('handroanthus', 4.2, 3.9, 'sec', 15), it('rhodosimsii', 1.0, 5.8, 'ter', 21), it('rhodosimsii', 1.8, 6.3, 'ter', 22), it('rhodosimsii', 0.7, 6.5, 'ter', 23), it('gardenia', 1.0, 0.9, 'ter', 24), it('psychotria', 0.6, 3.0, 'ter', 25), it('nerium', 9.3, 0.9, 'ter', 26), it('ixora', 7.9, 6.4, 'ter', 27), it('ophiopogon', 3.4, 5.7, 'ter', 28), it('ophiopogon', 5.9, 5.4, 'ter', 29), it('nephrolepis', 1.4, 3.8, 'ter', 30), fg('office_milktea', 5.7, 4.3), fg('grandma_trolley', 7.3, 4.9), fg('mascot_egret', 4.6, 5.5), ], }; } /* example 2: L-shaped podium garden (337 m²) round a 120 m tower, with 75 m and 45 m towers to the north and west */ function exampleComposition2() { const it = (id, x, y, role, seed) => ({ uid: newUid(), kind: 'plant', id, x, y, role, seed: seed || Math.floor(Math.random() * 1e4) }); const fg = (id, x, y) => ({ uid: newUid(), kind: 'figure', id, x, y }); const c = { name: '示範二:L 形平台花園(三座大廈)', years: 5, month: 3, style: 'wash', site: lShapeSite(), eye: { x: 5.5, y: 19.4, dir: 10, fov: 64, h: 1.6 }, items: [ it('blakeana', 6.0, 12.5, 'main', 31), it('camphor', 3.5, 3.5, 'sec', 32), it('handroanthus', 13.0, 3.0, 'sec', 33), it('handroanthus', 20.5, 4.5, 'sec', 34), it('ficus', 7.0, 5.8, 'sec', 35), it('psychotria', 1.2, 16.5, 'ter', 41), it('psychotria', 1.4, 18.6, 'ter', 42), it('psychotria', 2.4, 17.6, 'ter', 43), it('gardenia', 9.8, 16.8, 'ter', 44), it('ixora', 16.5, 7.8, 'ter', 45), it('rhodosimsii', 22.8, 1.2, 'ter', 46), it('ophiopogon', 4.5, 8.8, 'ter', 47), it('nephrolepis', 9.6, 11.2, 'ter', 48), it('ophiopogon', 17.5, 1.3, 'ter', 49), fg('office_milktea', 8.2, 14.5), fg('grandma_trolley', 11.0, 5.2), fg('mascot_egret', 4.2, 15.8), ], }; normalizeComp(c); return c; } function plantOf(item) { return item.kind === 'plant' ? libById(item.id) : null; } function isTreeLike(p) { return p && (p.type === 'tree' || p.type === 'palm'); } function matureS(p) { return (p.s[0] + p.s[1]) / 2; } function matureH(p) { return (p.h[0] + p.h[1]) / 2; } function nameOf(item) { const p = plantOf(item); if (p) return p.zh; const f = FIGURES.find(f => f.id === item.id); return f ? f.zh : item.id; } const fmt = v => (Math.round(v * 10) / 10).toFixed(1); const ptxt = (x, y) => `(${fmt(x)}, ${fmt(y)})`; /* nearest point inside the site polygon at least m from its outline (m = 0: on or inside the outline) */ function clampIn(site, x, y, m = 0.3) { const P = site.poly; if (!P) return [Math.max(m, Math.min(site.W - m, x)), Math.max(m, Math.min(site.D - m, y))]; if (m <= 0) { if (pointInPoly(x, y, P)) return [x, y]; const c = closestOnPoly(P, x, y); return [c.x, c.y]; } const E = siteEdges(site); let p = [x, y]; for (let k = 0; k < 8; k++) { const c = closestOnPoly(P, p[0], p[1]), ins = pointInPoly(p[0], p[1], P); if (ins && c.d >= m - 1e-6) return k ? [Math.round(p[0] * 1000) / 1000, Math.round(p[1] * 1000) / 1000] : p; const e = E[c.edge]; p = ins && c.d > 1e-6 ? [c.x + (p[0] - c.x) / c.d * m, c.y + (p[1] - c.y) / c.d * m] : [c.x - e.nx * m, c.y - e.ny * m]; } let best = null; // tight corner: nearest grid point that fits for (let gx = m; gx <= site.W - m; gx += 0.1) for (let gy = m; gy <= site.D - m; gy += 0.1) { if (!pointInPoly(gx, gy, P) || closestOnPoly(P, gx, gy).d < m) continue; const d = Math.hypot(gx - x, gy - y); if (!best || d < best.d) best = { d, p: [gx, gy] }; } return best ? best.p : p; } /* a candidate spot is usable if it is inside the garden, m clear of the outline and not inside a building */ function spotOk(site, x, y, m = 0.6) { return inSite(site, x, y) && closestOnPoly(site.poly, x, y).d >= m - 1e-9 && !(site.buildings || []).some(b => b.h > 0 && pointInPoly(x, y, b.poly)); } function inCone(eye, x, y, maxD) { const dx = x - eye.x, dy = y - eye.y, d = Math.hypot(dx, dy); if (d < 0.2 || d > maxD) return false; const a = Math.atan2(dx, -dy) / D2R, da = Math.abs(((a - eye.dir) % 360 + 540) % 360 - 180); return da <= eye.fov / 2; } function edgeDist(site, x, y) { return edgeDistPoly(site, x, y); } /* find replacement species */ function alternatives(p, opt = {}) { const bad = new Set(opt.avoid || ['invasive', 'flagged', 'brittle', 'toxic']); const H = matureH(p); return LIB.filter(q => q.id !== p.id && (isTreeLike(p) ? isTreeLike(q) : q.type === p.type) && !q.warn.some(w => bad.has(w.k)) && (!opt.light || q.light.includes(opt.light)) && (!opt.maxS || matureS(q) <= opt.maxS) && (!opt.minDH || Math.abs(matureH(q) - H) / H >= opt.minDH)) .sort((a, b) => (b.native === true) - (a.native === true) || Math.abs(matureH(a) - (opt.targetH || H)) - Math.abs(matureH(b) - (opt.targetH || H))); } /* ---------- checker ---------- */ const pairNeedS = (sa, sb) => Math.min(6, Math.max(3.0, 0.3 * (sa + sb))); /* nearest spot for a tree of spread S that clears the given trees; null if none */ function findTreeSpot(site, trees, S, from) { let best = null; for (let gx = 0.6; gx <= site.W - 0.6; gx += 0.25) for (let gy = 0.6; gy <= site.D - 0.6; gy += 0.25) { if (!spotOk(site, gx, gy)) continue; const slack = trees.length ? Math.min(...trees.map(o => Math.hypot(o.x - gx, o.y - gy) - pairNeedS(matureS(plantOf(o)), S))) : 9; if (slack < 0.1) continue; const d = Math.hypot(gx - from[0], gy - from[1]); if (!best || d < best.d) best = { x: gx, y: gy, d }; } return best; } const removeFix = (it, why) => ({ verb: '移除', label: `移除 ${nameOf(it)} ${ptxt(it.x, it.y)}${why ? ':' + why : ''}`, ghost: { x: it.x, y: it.y, r: 0.6 }, apply: c => { c.items = c.items.filter(q => q.uid !== it.uid); } }); function runChecks(comp, sun) { const site = comp.site, R = []; const plants = comp.items.filter(i => i.kind === 'plant'); const trees = plants.filter(i => isTreeLike(plantOf(i))); const zoneAt = i => zoneOfHours(sunAt(site, sun, i.x, i.y, 'avg')); const add = r => R.push(r); const moveFix = (it, nx, ny, why) => { [nx, ny] = clampIn(site, nx, ny); return { verb: '移動', label: `把 ${nameOf(it)} 由 ${ptxt(it.x, it.y)} 移到 ${ptxt(nx, ny)}${why ? ':' + why : ''}`, ghost: { x: nx, y: ny, r: 0.6 }, from: it.uid, apply: c => { const t = c.items.find(q => q.uid === it.uid); if (t) { t.x = nx; t.y = ny; } } }; }; const replFix = (it, q, why) => q ? { verb: '替換', label: `把 ${nameOf(it)} 換成 ${window.LANG === 'en' ? q.en : q.zh + '(' + q.en + ')'}${why ? ':' + why : ''}`, apply: c => { const t = c.items.find(z => z.uid === it.uid); if (t) t.id = q.id; } } : null; /* a spot for item t that suits its sun need, clears other trees and (for the main plant) sits in the view cone */ const spotFor = (t, opt = {}) => { const p = plantOf(t), G = sun.avg, maxD = Math.hypot(site.W, site.D) * 1.2, others = trees.filter(o => o.uid !== t.uid); let best = null; for (let j = 0; j < G.ny; j++) for (let k = 0; k < G.nx; k++) { const x = (k + 0.5) * site.W / G.nx, y = (j + 0.5) * site.D / G.ny; if (!spotOk(site, x, y, 0.5)) continue; const sunOk = p.light.includes(zoneOfHours(G.g[j * G.nx + k])); if (opt.needSun && !sunOk) continue; if (isTreeLike(p) && others.some(o => Math.hypot(o.x - x, o.y - y) < pairNeedS(matureS(plantOf(o)), matureS(p)) - 0.05)) continue; const cone = inCone(comp.eye, x, y, maxD) && Math.hypot(x - comp.eye.x, y - comp.eye.y) >= 2; if ((t.role === 'main' || opt.needCone) && !cone) continue; const d = Math.hypot(x - (opt.near || [t.x, t.y])[0], y - (opt.near || [t.x, t.y])[1]) - (sunOk ? 3 : 0); if (opt.maxMove && Math.hypot(x - t.x, y - t.y) > opt.maxMove) continue; if (!best || d < best.d) best = { x, y, d }; } return best; }; // D1 layering { const has = { tree: trees.length > 0, shrub: plants.some(i => plantOf(i).type === 'shrub'), ground: plants.some(i => ['ground', 'climber'].includes(plantOf(i).type)) }; const miss = Object.keys(has).filter(k => !has[k]); const r = { id: 'D1', tag: 'design', src: 'design', title: '高度分層:喬木、灌木、地被都要有', items: [] }; if (!miss.length) { r.status = 'pass'; r.msg = `有 ${trees.length} 棵喬木/棕櫚、${plants.filter(i => plantOf(i).type === 'shrub').length} 棵灌木及地被,分層完整。`; } else { r.status = 'warn'; const zh = { tree: '喬木', shrub: '灌木', ground: '地被' }; r.msg = `欠缺:${miss.map(k => zh[k]).join('、')}。只有一兩層會令構圖單薄。`; const k = miss[0], pick = LIB.find(q => q.type === (k === 'tree' ? 'tree' : k) && !q.warn.length && q.light.includes('part')); const [x, y] = clampIn(site, site.W * 0.3, site.D * 0.7, 0.6); if (pick) r.fix = { verb: '加入', label: `在 ${ptxt(x, y)} 加一棵 ${pick.zh}`, ghost: { x, y, r: 0.6 }, apply: c => c.items.push({ uid: newUid(), kind: 'plant', id: pick.id, x, y, role: k === 'tree' ? 'sec' : 'ter', seed: 5 }) }; } add(r); } // D2 tall trees not all the same height { const r = { id: 'D2', tag: 'design', src: 'design', title: '喬木不要全部一樣高', items: trees.map(i => i.uid) }; if (trees.length < 2) { r.status = 'pass'; r.msg = '只有一棵喬木,不適用。'; } else { const hs = trees.map(i => ({ i, h: sizeAtYears(plantOf(i), comp.years).H })); const mx = Math.max(...hs.map(h => h.h)), mn = Math.min(...hs.map(h => h.h)); if ((mx - mn) / mx < 0.15) { r.status = 'warn'; r.msg = `${comp.years} 年後喬木高度都在 ${fmt(mn)}–${fmt(mx)} m 之間,天際線太平。`; const t = hs[hs.length - 1].i, p = plantOf(t), q = alternatives(p, { minDH: 0.35, light: zoneAt(t) })[0]; r.fix = replFix(t, q, `成熟高度 ${fmt(matureH(q || p))} m,拉開高低`); } else { r.status = 'pass'; r.msg = `喬木高度 ${fmt(mn)}–${fmt(mx)} m,有高有低。`; } } add(r); } // D3 odd numbers { const g = {}; plants.forEach(i => { const p = plantOf(i); if (p.type === 'ground') return; (g[i.id] = g[i.id] || []).push(i); }); const even = Object.values(g).filter(a => a.length >= 2 && a.length % 2 === 0); const r = { id: 'D3', tag: 'design', src: 'design', title: '同種植物用奇數成組(1、3、5)', items: even.flat().map(i => i.uid) }; if (!even.length) { r.status = 'pass'; r.msg = '成組的品種都是奇數。'; } else { const a = even[0], p = plantOf(a[0]); r.status = 'warn'; r.msg = `${even.map(a => plantOf(a[0]).zh + ' × ' + a.length).join('、')}:雙數容易變成對稱排列,看起來呆板。`; const cx = a.reduce((s, i) => s + i.x, 0) / a.length, cy = a.reduce((s, i) => s + i.y, 0) / a.length; const off = isTreeLike(p) ? Math.min(2.5, matureS(p) * 0.35) : 0.9; let [nx, ny] = clampIn(site, cx + off * 0.7, cy - off * 0.8); if (isTreeLike(p)) { const spot = findTreeSpot(site, trees, matureS(p), [cx, cy]); if (spot) { nx = spot.x; ny = spot.y; } else { const rm = a.find(i => i.role !== 'main') || a[0]; r.fix = removeFix(rm, `花園放不下第 ${a.length + 1} 棵,改為 ${a.length - 1} 棵`); } } if (!r.fix) r.fix = { verb: '加入', label: `在 ${ptxt(nx, ny)} 多種一棵 ${p.zh},變成 ${a.length + 1} 棵`, ghost: { x: nx, y: ny, r: 0.6 }, apply: c => c.items.push({ uid: newUid(), kind: 'plant', id: p.id, x: nx, y: ny, role: a[0].role, seed: Math.floor(Math.random() * 1e4) }) }; } add(r); } // D4 spacing vs mature spread { const pairNeed = (a, b) => pairNeedS(matureS(plantOf(a)), matureS(plantOf(b))); const bad = []; for (let i = 0; i < trees.length; i++) for (let j = i + 1; j < trees.length; j++) { const a = trees[i], b = trees[j], need = pairNeed(a, b), d = Math.hypot(a.x - b.x, a.y - b.y); if (d < need - 0.05) bad.push({ a, b, d, need }); } const r = { id: 'D4', tag: 'design', src: 'rprp', title: '喬木間距(至少 3 m,冠幅大的要更遠)', items: bad.flatMap(x => [x.a.uid, x.b.uid]) }; if (!bad.length) { r.status = 'pass'; r.msg = '喬木之間距離足夠。'; } else { const x = bad[0]; r.status = 'fail'; r.msg = bad.slice(0, 3).map(x => `${nameOf(x.a)} 與 ${nameOf(x.b)} 相距 ${fmt(x.d)} m(建議 ≥ ${fmt(x.need)} m)`).join(';') + (bad.length > 3 ? `;另有 ${bad.length - 3} 對` : '') + '。樹冠會互相擠壓,長歪或要大修剪。'; const mv = x.b.role === 'main' ? x.a : x.b, others = trees.filter(t => t !== mv); const pairNeed = (o, m) => pairNeedS(matureS(plantOf(o)), matureS(plantOf(m))); // search the plan for the nearest spot that clears every other tree (or the best available) let best = null; for (let gx = 0.6; gx <= site.W - 0.6; gx += 0.25) for (let gy = 0.6; gy <= site.D - 0.6; gy += 0.25) { if (!spotOk(site, gx, gy)) continue; const slack = Math.min(...others.map(o => Math.hypot(o.x - gx, o.y - gy) - pairNeed(o, mv))); const move = Math.hypot(gx - mv.x, gy - mv.y); const score = slack >= 0.1 ? 100 - move : slack - 50; if (!best || score > best.score) best = { x: gx, y: gy, score, slack }; } if (best.slack >= 0.1 && Math.hypot(best.x - mv.x, best.y - mv.y) > 0.1) r.fix = moveFix(mv, best.x, best.y, '與其他喬木都保持足夠距離'); else { // no room anywhere: swap the bigger crown for a smaller species const cand = [x.a, x.b].filter(t => t.role !== 'main'); const big = (cand.length ? cand : [x.a, x.b]).sort((u, v) => matureS(plantOf(v)) - matureS(plantOf(u)))[0]; if (matureS(plantOf(big)) > 6) r.fix = replFix(big, alternatives(plantOf(big), { maxS: 6, light: zoneAt(big), targetH: 7 })[0], '花園放不下,改用冠幅較小的品種'); else r.fix = removeFix(big, '花園放不下這麼多喬木,改用灌木填補'); } } add(r); } // D5 mature crown vs courtyard size ("short side" = the widest circle that fits, which is the short side of a rectangle) { const shortW = site.poly ? Math.round(siteInscribed(site).r * 2 * 10) / 10 : Math.min(site.W, site.D); const big = trees.filter(i => matureS(plantOf(i)) > shortW * 1.3); const r = { id: 'D5', tag: 'design', src: 'rprp', title: '成熟冠幅要配合花園大小(適當植物種在適當位置)', items: big.map(i => i.uid) }; if (!big.length) { r.status = 'pass'; r.msg = '所有喬木的成熟冠幅都與花園大小相稱。'; } else { r.status = 'warn'; const t = big[0], p = plantOf(t); r.msg = big.map(i => `${nameOf(i)} 成熟冠幅約 ${fmt(matureS(plantOf(i)))} m`).join('、') + `,比花園可用闊度約 ${fmt(shortW)} m 大得多。`; const q = alternatives(p, { maxS: shortW * 0.9, light: zoneAt(t), targetH: 7 })[0]; r.fix = replFix(t, q, q ? `成熟冠幅約 ${fmt(matureS(q))} m` : ''); } add(r); } // D6 focal point in view cone { const maxD = Math.hypot(site.W, site.D) * 1.2; const mains = plants.filter(i => i.role === 'main'), seen = mains.filter(i => inCone(comp.eye, i.x, i.y, maxD)); const r = { id: 'D6', tag: 'design', src: 'design', title: '主景要在視線範圍內', items: mains.map(i => i.uid) }; if (seen.length) { r.status = 'pass'; r.msg = `${seen.map(nameOf).join('、')} 在視錐內,是畫面焦點。`; } else if (!mains.length) { r.status = 'warn'; r.msg = '沒有任何植物設為「主景」。'; const t = trees[0]; if (t) r.fix = { verb: '設定', label: `把 ${nameOf(t)} 設為主景`, apply: c => { const z = c.items.find(q => q.uid === t.uid); if (z) z.role = 'main'; } }; } else { r.status = 'fail'; const t = mains[0]; r.msg = `主景 ${nameOf(t)} 不在視點的視錐內,站在視點看不到焦點。`; const a = comp.eye.dir * D2R; let L = 0; for (let s = 0.5; s < maxD; s += 0.25) { const x = comp.eye.x + Math.sin(a) * s, y = comp.eye.y - Math.cos(a) * s; if (!inSite(site, x, y)) break; L = s; } const tgt = [comp.eye.x + Math.sin(a) * L * 0.6, comp.eye.y - Math.cos(a) * L * 0.6]; const sp = spotFor(t, { needCone: true, near: tgt }); r.fix = sp ? moveFix(t, sp.x, sp.y, '放進視錐內,並顧及日照及間距') : moveFix(t, tgt[0], tgt[1], '放在視線中央、約 60% 深度'); } add(r); } // D7 role ratio { const n = plants.filter(i => plantOf(i).type !== 'ground').length, m = plants.filter(i => i.role === 'main'); const r = { id: 'D7', tag: 'design', src: 'design', title: '主景要少:主景不超過全部(地被除外)的兩成', items: m.map(i => i.uid) }; if (m.length <= Math.max(1, Math.round(n * 0.2))) { r.status = 'pass'; r.msg = `主景 ${m.length}、次景 ${plants.filter(i => i.role === 'sec').length}、配景 ${plants.filter(i => i.role === 'ter').length}。`; } else { r.status = 'warn'; const t = m[m.length - 1]; r.msg = `主景有 ${m.length} 個,焦點太多會互相搶鏡。`; r.fix = { verb: '設定', label: `把 ${nameOf(t)} 改為次景`, apply: c => { const z = c.items.find(q => q.uid === t.uid); if (z) z.role = 'sec'; } }; } add(r); } // H1 soil depth on podium { const bad = site.podium ? plants.filter(i => site.soil < SOIL_REQ[plantOf(i).type]) : []; const r = { id: 'H1', tag: 'hk', src: 'podium', title: `平台種植土深度(喬木參考 ${SOIL_REQ.tree} m、灌木 ${SOIL_REQ.shrub} m、地被 ${SOIL_REQ.ground} m)`, items: bad.map(i => i.uid) }; if (!site.podium) { r.status = 'pass'; r.msg = '不是平台花園(直接種在地面),不適用。'; } else if (!bad.length) { r.status = 'pass'; r.msg = `種植土深 ${site.soil} m,足夠目前所有植物。`; } else { r.status = 'fail'; const need = Math.max(...bad.map(i => SOIL_REQ[plantOf(i).type])); r.msg = `種植土只有 ${site.soil} m,${bad.map(nameOf).slice(0, 5).join('、')}${bad.length > 5 ? ' 等' : ''}需要更深的泥土。`; r.fix = { verb: '加深', label: `把種植土深度改為 ${need} m(要結構工程師確認荷載)`, apply: c => { c.site.soil = need; } }; } add(r); } // H2 crown clearance from buildings: distance to the nearest building polygon edge { const bad = []; for (const i of trees) { const p = plantOf(i), rr = matureS(p) / 2 * 0.6, nb = nearestBldg(site, i.x, i.y); // allow some crown lift / pruning if (nb && nb.d < Math.min(rr, 3)) bad.push({ i, b: nb.b, d: nb.d, px: nb.x, py: nb.y, need: Math.min(rr, 3) }); } const r = { id: 'H2', tag: 'design', src: 'rprp', title: '成熟樹冠與建築物外牆保持距離', items: bad.map(b => b.i.uid) }; if (!bad.length) { r.status = 'pass'; r.msg = '喬木離建築物外牆有足夠距離。'; } else { const b = bad[0]; r.status = 'warn'; r.msg = bad.map(b => `${nameOf(b.i)} 離${b.b.name}外牆約 ${fmt(b.d)} m`).join(';') + '。樹冠會頂住外牆、窗戶,要經常修剪。'; // move straight away from the nearest wall point until clear, then keep it inside the garden const dx = b.i.x - b.px, dy = b.i.y - b.py, L = Math.hypot(dx, dy) || 1, go = b.need + 0.2; let [nx, ny] = clampIn(site, b.px + dx / L * go, b.py + dy / L * go, 0.6); if ((nearestBldg(site, nx, ny) || { d: 99 }).d < b.need) { // clamping pulled it back: nearest spot that clears every building let best = null; for (let gx = 0.6; gx <= site.W - 0.6; gx += 0.25) for (let gy = 0.6; gy <= site.D - 0.6; gy += 0.25) { if (!spotOk(site, gx, gy) || (nearestBldg(site, gx, gy) || { d: 99 }).d < b.need + 0.1) continue; const d = Math.hypot(gx - b.i.x, gy - b.i.y); if (!best || d < best.d) best = { d, x: gx, y: gy }; } if (best) { nx = best.x; ny = best.y; } } r.fix = moveFix(b.i, nx, ny, `離牆約 ${fmt(b.need)} m`); } add(r); } // H3 typhoon / brittle { const bad = plants.filter(i => plantOf(i).warn.some(w => w.k === 'brittle')); const open = edgeExposure(site).some(e => e.open && e.len >= 1); const r = { id: 'H3', tag: 'hk', src: 'brittle', title: '颱風風險:易斷枝品種', items: bad.map(i => i.uid) }; if (!bad.length) { r.status = 'pass'; r.msg = '沒有易斷枝品種。'; } else { r.status = (site.podium || open) ? 'fail' : 'warn'; const t = bad[0], p = plantOf(t); r.msg = `${bad.map(nameOf).join('、')} 木質較脆或根淺${site.podium ? ',而且在平台上風大' : ''}${open ? ',地盤有開揚面' : ''}。`; r.fix = replFix(t, alternatives(p, { light: zoneAt(t), maxS: Math.max(8, Math.min(site.W, site.D)) })[0], '較耐風'); } add(r); } // H4 invasive / flagged { const bad = plants.filter(i => plantOf(i).warn.some(w => w.k === 'invasive' || w.k === 'flagged')); const r = { id: 'H4', tag: 'hk', src: 'afcdIas', title: '入侵及不理想品種', items: bad.map(i => i.uid) }; if (!bad.length) { r.status = 'pass'; r.msg = '沒有已知入侵或不理想品種。'; } else { const t = bad[0], p = plantOf(t), w = p.warn.find(w => w.k === 'invasive' || w.k === 'flagged'); r.status = 'fail'; r.src = w.src; r.msg = bad.map(i => `${nameOf(i)}:${plantOf(i).warn.find(w => w.k === 'invasive' || w.k === 'flagged').note}`).join(' '); r.fix = replFix(t, alternatives(p, { light: zoneAt(t) })[0], '改用本地品種'); } add(r); } // H5 toxic { const bad = plants.filter(i => plantOf(i).warn.some(w => w.k === 'toxic')); const r = { id: 'H5', tag: 'hk', src: 'toxic', title: '有毒植物(兒童、長者、寵物會接觸的地方)', items: bad.map(i => i.uid) }; if (!bad.length) { r.status = 'pass'; r.msg = '沒有已標示的有毒植物。'; } else { const t = bad[0], p = plantOf(t); r.status = 'warn'; r.msg = bad.map(i => `${nameOf(i)}:${plantOf(i).warn.find(w => w.k === 'toxic').note}`).join(' '); r.fix = replFix(t, alternatives(p, { light: zoneAt(t) })[0], '無毒、同類型'); } add(r); } // H6 surface roots near paving / podium edge { const bad = plants.filter(i => plantOf(i).warn.some(w => w.k === 'roots') && edgeDist(site, i.x, i.y) < 3); const r = { id: 'H6', tag: 'hk', src: 'roots', title: '根系空間:細葉榕不宜貼近花園邊或種在細小種植槽(3 m 為本工具假設)', items: bad.map(i => i.uid) }; if (!bad.length) { r.status = 'pass'; r.msg = '沒有根系問題品種貼近邊界。'; } else { const t = bad[0]; r.status = 'warn'; r.msg = `${bad.map(i => `${nameOf(i)} 離邊只有 ${fmt(edgeDist(site, i.x, i.y))} m`).join(';')}。有氣根的樹不宜種在細小種植槽,根亦可能頂起鋪地。`; const room = siteInscribed(site).r >= 3.1; if (room) r.fix = moveFix(t, ...clampIn(site, t.x, t.y, 3.05), '離邊 3 m'); else r.fix = replFix(t, alternatives(plantOf(t), { light: zoneAt(t), maxS: 8 })[0], '花園太小,改用細根品種'); } add(r); } // S1 sun need vs zone { const bad = plants.filter(i => !plantOf(i).light.includes(zoneAt(i))); const r = { id: 'S1', tag: 'site', src: 'noaa', title: '日照需要與地盤日照分區相符', items: bad.map(i => i.uid) }; if (!bad.length) { r.status = 'pass'; r.msg = '所有植物都種在合適的日照分區。'; } else { const t = bad[0], p = plantOf(t); r.status = 'warn'; r.msg = bad.slice(0, 5).map(i => `${nameOf(i)} 需要${plantOf(i).light.map(l => LIGHT_ZH[l]).join('/')},但所在位置屬「${ZONE_ZH[zoneAt(i)]}」(全年平均 ${fmt(sunAt(site, sun, i.x, i.y))} h)`).join(';') + (bad.length > 5 ? ` 等 ${bad.length} 項` : '') + '。'; const best = spotFor(t, { needSun: true, maxMove: t.role === 'main' ? 99 : 3.5 }); r.fix = best ? moveFix(t, best.x, best.y, `移到${ZONE_ZH[zoneOfHours(sunAt(site, sun, best.x, best.y))]}`) : replFix(t, alternatives(p, { light: zoneAt(t), maxS: isTreeLike(p) ? Math.max(8, matureS(p)) : 9 })[0], t.role === 'main' ? `視線範圍內沒有合適日照位置,改用適合${ZONE_ZH[zoneAt(t)]}的主景品種` : `適合${ZONE_ZH[zoneAt(t)]}`); } add(r); } return R; } /* ---------- drawing helpers ---------- */ const FIG_IMG = {}; function loadFigures(onload) { let n = 0; FIGURES.forEach(f => { const im = new Image(); im.onload = () => { if (++n === FIGURES.length && onload) onload(); }; im.onerror = im.onload; im.src = `assets/figures/${f.id}.png`; FIG_IMG[f.id] = im; }); } function planTransform(site, w, h, pad = 34, ext) { // ext: metres of context around the garden {l,r,t,b} ext = ext || { l: 0, r: 0, t: 0, b: 0 }; const EW = site.W + ext.l + ext.r, ED = site.D + ext.t + ext.b; const sc = Math.min((w - pad * 2) / EW, (h - pad * 2) / ED); const ox = (w - EW * sc) / 2 + ext.l * sc, oy = (h - ED * sc) / 2 + ext.t * sc; return { sc, x: v => ox + v * sc, y: v => oy + v * sc, ix: px => (px - ox) / sc, iy: py => (py - oy) / sc, ox, oy }; } /* plan and section drawing moved to sheet.js (drawPlantingPlan, drawSection) */ /* perspective from the eye point */ function drawPerspective(ctx, w, h, comp, ui) { const site = comp.site, e = comp.eye, a = e.dir * D2R, fwd = [Math.sin(a), -Math.cos(a)], right = [Math.cos(a), Math.sin(a)]; const F = (w / 2) / Math.tan(e.fov / 2 * D2R), hy = h * 0.52, cx = w / 2; const toCam = (x, y) => ({ l: (x - e.x) * right[0] + (y - e.y) * right[1], f: (x - e.x) * fwd[0] + (y - e.y) * fwd[1] }); const proj = (l, f, z) => [cx + l / f * F, hy + (e.h - z) / f * F]; // sky & ground ctx.fillStyle = PAPER; ctx.fillRect(0, 0, w, hy); ctx.fillStyle = '#f1eee6'; ctx.fillRect(0, hy, w, h - hy); const near = 0.3; function clipPoly(pts) { // pts [{l,f,z}] clip f >= near const out = []; for (let i = 0; i < pts.length; i++) { const A = pts[i], B = pts[(i + 1) % pts.length], ia = A.f >= near, ib = B.f >= near; if (ia) out.push(A); if (ia !== ib) { const t = (near - A.f) / (B.f - A.f); out.push({ l: A.l + (B.l - A.l) * t, f: near, z: A.z + (B.z - A.z) * t }); } } return out; } function fillPoly(pts, fill, stroke) { const c = clipPoly(pts); if (c.length < 3) return; ctx.beginPath(); c.forEach((p, i) => { const [x, y] = proj(p.l, p.f, p.z); i ? ctx.lineTo(x, y) : ctx.moveTo(x, y); }); ctx.closePath(); if (fill) { ctx.fillStyle = fill; ctx.fill(); } if (stroke) { ctx.strokeStyle = stroke; ctx.lineWidth = 1; ctx.stroke(); } } // buildings: one face per polygon edge, all faces far first const P3 = (x, y, z) => Object.assign(toCam(x, y), { z }), faces = []; for (const o of solidBldgs(site)) { const Q = o.poly; for (let i = 0; i < Q.length; i++) { const a = Q[i], b = Q[(i + 1) % Q.length], c = toCam((a[0] + b[0]) / 2, (a[1] + b[1]) / 2); faces.push({ f: Math.hypot(c.l, c.f), pts: [P3(a[0], a[1], 0), P3(b[0], b[1], 0), P3(b[0], b[1], o.h), P3(a[0], a[1], o.h)] }); } } faces.sort((A, B) => B.f - A.f).forEach(fc => fillPoly(fc.pts, 'rgba(226,223,215,0.9)', 'rgba(46,46,48,0.55)')); // garden ground const G = site.poly.map(([x, y]) => P3(x, y, 0)); fillPoly(G, '#e9e4d6', INK); if (site.podium) { // podium edge upstand hint const Gp = site.poly.map(([x, y]) => P3(x, y, 0.45)); ctx.setLineDash([3, 3]); fillPoly(Gp, null, 'rgba(90,85,75,0.5)'); ctx.setLineDash([]); } // horizon ctx.strokeStyle = GRAPH; ctx.lineWidth = 0.7; ctx.setLineDash([12, 3, 2, 3]); ctx.beginPath(); ctx.moveTo(0, hy); ctx.lineTo(w, hy); ctx.stroke(); ctx.setLineDash([]); ctx.fillStyle = GRAPH; ctx.font = fnt(8.5, 500, F_MONO); ctx.textAlign = 'left'; ctx.fillText(`HL 視平線 +${e.h} m`, 6, hy - 4); // items const items = comp.items.map(it => Object.assign({ it, p: plantOf(it) }, toCam(it.x, it.y))).filter(o => o.f > 0.6).sort((A, B) => B.f - A.f); for (const o of items) { const k = F / o.f, [sx, sy] = proj(o.l, o.f, 0); if (sx < -w || sx > 2 * w) continue; if (o.p) { if (o.f < 1.2) continue; // too close to the eye const s = plantSprite(o.p, comp.years, comp.month, comp.style, o.it.seed, k), m = k / s.px; ctx.drawImage(s.cv, sx - s.ax * m, sy - s.ay * m, s.cv.width * m, s.cv.height * m); } else { const f = FIGURES.find(f => f.id === o.it.id), im = FIG_IMG[o.it.id]; if (im && im.complete && im.naturalWidth) { const hh = f.h * k, ww = im.naturalWidth / im.naturalHeight * hh; ctx.drawImage(im, sx - ww / 2, sy - hh, ww, hh); } } } }