/* HK Planting Board: 地盤分析: sun path, shadows, sun-hours, wind, views, design direction. Solar position: NOAA simplified equations (see SOURCES.noaa), Hong Kong 22.3°N 114.17°E, HKT (UTC+8). */ 'use strict'; const HK = { lat: 22.3, lon: 114.17, tz: 8 }; const D2R = Math.PI / 180; function dayOfYear(m, d) { return Math.round((Date.UTC(2026, m - 1, d) - Date.UTC(2026, 0, 0)) / 864e5); } function sunPos(m, d, hour) { // returns {alt, az} degrees; az clockwise from true north const doy = dayOfYear(m, d); const g = 2 * Math.PI / 365 * (doy - 1 + (hour - 12) / 24); const eqt = 229.18 * (0.000075 + 0.001868 * Math.cos(g) - 0.032077 * Math.sin(g) - 0.014615 * Math.cos(2 * g) - 0.040849 * Math.sin(2 * g)); const dec = 0.006918 - 0.399912 * Math.cos(g) + 0.070257 * Math.sin(g) - 0.006758 * Math.cos(2 * g) + 0.000907 * Math.sin(2 * g) - 0.002697 * Math.cos(3 * g) + 0.00148 * Math.sin(3 * g); const tst = hour * 60 + eqt + 4 * HK.lon - 60 * HK.tz; const ha = (tst / 4 - 180) * D2R; const lat = HK.lat * D2R; const cz = Math.sin(lat) * Math.sin(dec) + Math.cos(lat) * Math.cos(dec) * Math.cos(ha); const zen = Math.acos(Math.max(-1, Math.min(1, cz))); let caz = (Math.sin(dec) - Math.sin(lat) * Math.cos(zen)) / (Math.cos(lat) * Math.sin(zen) || 1e-9); let az = Math.acos(Math.max(-1, Math.min(1, caz))) / D2R; if (ha > 0) az = 360 - az; return { alt: 90 - zen / D2R, az }; } const SIDES = ['top', 'right', 'bottom', 'left']; const SIDE_ZH = { top: '上', right: '右', bottom: '下', left: '左' }; const SIDE_SCREEN_ANGLE = { top: 0, right: 90, bottom: 180, left: 270 }; const COMPASS8 = ['北', '東北', '東', '東南', '南', '西南', '西', '西北']; function compass(bearing) { return COMPASS8[Math.round(((bearing % 360) + 360) % 360 / 45) % 8]; } function screenToBearing(site, screenAng) { return ((screenAng - site.north) % 360 + 360) % 360; } function sideCompass(site, side) { return compass(screenToBearing(site, SIDE_SCREEN_ANGLE[side])); } /* screen direction vector (x right, y down) for a true bearing */ function bearingToScreen(site, bearing) { const a = (bearing + site.north) * D2R; return [Math.sin(a), -Math.cos(a)]; } /* the v2 example was a 10 x 7 m rectangle with four fixed sides; it is now built by migrating that into a polygon and three building polygons, so it looks the same */ function defaultSite() { return migrateSite({ W: 10, D: 7, north: 20, soil: 1.2, podium: true, level: 12, sides: { top: { h: 36, gap: 0, depth: 14 }, right: { h: 6, gap: 4, depth: 10 }, bottom: { h: 0, gap: 0, depth: 10 }, left: { h: 60, gap: 8, depth: 16 } }, views: [{ side: 'bottom', t: 0.55, type: 'good' }, { side: 'right', t: 0.4, type: 'screen' }], }); } /* second example: an L-shaped podium garden wrapped round three towers of different heights */ function lShapeSite() { return { north: 15, soil: 1.2, podium: true, level: 15, poly: [[0, 0], [24, 0], [24, 9], [11, 9], [11, 20], [0, 20]], buildings: [ { id: newBid(), name: '第一座 Tower 1', h: 120, poly: [[13.5, 11.5], [25, 11.5], [25, 23], [13.5, 23]] }, { id: newBid(), name: '第二座 Tower 2', h: 75, poly: rotatePoly(rectPoly(4, -15, 16, -4.5), 12) }, { id: newBid(), name: '第三座 Tower 3', h: 45, poly: [[-13, 4], [-4.5, 4], [-4.5, 17], [-9, 21], [-13, 21]] }, ], views: [{ edge: 1, t: 0.8, type: 'good' }, { edge: 4, t: 0.5, type: 'screen' }], }; } function sunVector(site, sp) { // plan (x right, y down, z up) const [sx, sy] = bearingToScreen(site, sp.az); const c = Math.cos(sp.alt * D2R); return [sx * c, sy * c, Math.sin(sp.alt * D2R)]; } function inShadow(site, B, x, y, sv) { return pointShaded(B, x, y, 0.3, sv); } const KEY_DATES = { dec: [12, 21, '冬至 12月21日'], mar: [3, 20, '春分 3月20日'], jun: [6, 21, '夏至 6月21日'] }; function sunHoursGrid(site, m, d, res) { // only cells whose centre lies inside the site polygon and outside every building are computed; the rest stay NaN const nx = Math.max(1, Math.round(site.W / res)), ny = Math.max(1, Math.round(site.D / res)); const g = new Float32Array(nx * ny), B = solidBldgs(site), step = 0.25, cells = []; for (let j = 0; j < ny; j++) for (let i = 0; i < nx; i++) { const x = (i + 0.5) * site.W / nx, y = (j + 0.5) * site.D / ny, k = j * nx + i; if (inSite(site, x, y) && !B.some(o => pointInPoly(x, y, o.poly))) cells.push([k, x, y]); else g[k] = NaN; } for (let h = 5; h <= 19.5; h += step) { const sp = sunPos(m, d, h); if (sp.alt < 4) continue; const sv = sunVector(site, sp); for (const [k, x, y] of cells) if (!inShadow(site, B, x, y, sv)) g[k] += step; } return { nx, ny, g, n: cells.length }; } function computeSun(site) { const res = Math.max(0.25, Math.min(1, Math.max(site.W, site.D) / 40)); const out = { res }; for (const k of Object.keys(KEY_DATES)) { const [m, d] = KEY_DATES[k]; out[k] = sunHoursGrid(site, m, d, res); } const { nx, ny } = out.dec; const avg = new Float32Array(nx * ny); for (let i = 0; i < avg.length; i++) avg[i] = (out.dec.g[i] + 2 * out.mar.g[i] + out.jun.g[i]) / 4; // Mar stands for Mar+Sep out.avg = { nx, ny, g: avg, n: out.dec.n }; return out; } /* zone thresholds: common horticultural convention (full sun ≥ 6 h, part shade 3–6 h, shade < 3 h) */ function zoneOfHours(h) { return h >= 6 ? 'sun' : h >= 3 ? 'part' : 'shade'; } function sunAt(site, sun, x, y, key = 'avg') { const G = sun[key]; const i = Math.min(G.nx - 1, Math.max(0, Math.floor(x / site.W * G.nx))), j = Math.min(G.ny - 1, Math.max(0, Math.floor(y / site.D * G.ny))); const v = G.g[j * G.nx + i]; if (!isNaN(v)) return v; // a point on the outline or just outside: nearest computed cell let best = null; for (let r = 1; r <= 3 && !best; r++) for (let jj = j - r; jj <= j + r; jj++) for (let ii = i - r; ii <= i + r; ii++) { if (ii < 0 || jj < 0 || ii >= G.nx || jj >= G.ny) continue; const w = G.g[jj * G.nx + ii]; if (isNaN(w)) continue; const d = Math.hypot(ii - i, jj - j); if (!best || d < best.d) best = { d, w }; } return best ? best.w : 0; } /* the values of the computed (inside) cells only */ function sunVals(G) { return Array.from(G.g).filter(v => !isNaN(v)); } /* ---------- wind (HKO climatology, approximate) ---------- */ const WIND = [ { id: 'winter', zh: '東北季候風及東風(約 9 月至翌年 5 月)', from: 65, spread: 40, col: '#4f6d8f', label: '東北偏東/東' }, { id: 'summer', zh: '夏季西南季候風(約 6 月至 8 月)', from: 225, spread: 30, col: '#c4763c', label: '西南' }, ]; /* for each site edge: which way it faces, whether it is open (no building in front, a low one, or one further off than twice its height) and which prevailing wind it meets. Probes 3 points along the edge. */ function edgeExposure(site) { const B = solidBldgs(site); return siteEdges(site).map(e => { let near = null; for (const f of [0.2, 0.5, 0.8]) { const px = e.a[0] + (e.b[0] - e.a[0]) * f + e.nx * 0.05, py = e.a[1] + (e.b[1] - e.a[1]) * f + e.ny * 0.05; for (const o of B) for (const r of linePolyRanges([px, py], [e.nx, e.ny], o.poly)) { if (r[1] <= 0) continue; const g = Math.max(0, r[0]); if (g < 60 && (!near || g < near.g - 0.5 || (Math.abs(g - near.g) <= 0.5 && o.h > near.h))) near = { g, h: o.h, b: o.b }; } } const open = !near || near.h < 8 || near.g > near.h * 2; const br = screenToBearing(site, (Math.atan2(e.nx, -e.ny) / D2R + 360) % 360); const faces = WIND.filter(w => { const d = Math.abs(((br - w.from + 540) % 360) - 180); return d <= 45 + w.spread / 2; }).map(w => w.id); return { edge: e.i, open, bearing: br, dir: compass(br), faces, near, len: e.len }; }); } /* ---------- design direction (plain language) ---------- */ function designDirection(site, sun) { const out = []; const q = [['上左', 0, 0], ['上右', 1, 0], ['下左', 0, 1], ['下右', 1, 1]]; const qStat = q.map(([n, qx, qy]) => { let s = { dec: 0, jun: 0, avg: 0, n: 0 }; const G = sun.dec, nx = G.nx, ny = G.ny; for (let j = 0; j < ny; j++) for (let i = 0; i < nx; i++) { if ((i < nx / 2 ? 0 : 1) !== qx || (j < ny / 2 ? 0 : 1) !== qy) continue; const k = j * nx + i; if (isNaN(sun.avg.g[k])) continue; s.dec += sun.dec.g[k]; s.jun += sun.jun.g[k]; s.avg += sun.avg.g[k]; s.n++; } if (!s.n) return null; const ang = Math.atan2(qx ? 1 : -1, qy ? 1 : -1); // screen angle (x right, y down) const screenAng = (Math.atan2(qx ? 1 : -1, qy ? -1 : 1) / D2R + 360) % 360; // from up, clockwise return { name: n, dir: compass(screenToBearing(site, screenAng)), dec: s.dec / s.n, jun: s.jun / s.n, avg: s.avg / s.n, ang }; }).filter(Boolean); const shadeQ = qStat.filter(s => s.dec < 2); shadeQ.forEach(s => out.push({ kind: 'sun', level: 'warn', text: `${s.dir}角(圖${s.name})冬至日照只有約 ${s.dec.toFixed(1)} 小時,少於 2 小時:宜用耐陰植物,例如九節、梔子、含笑、麥冬、腎蕨。` })); const sunny = qStat.filter(s => s.avg >= 6).sort((a, b) => b.avg - a.avg); if (sunny.length) out.push({ kind: 'sun', level: 'ok', text: `${sunny.map(s => s.dir + '角').join('、')}全年平均日照約 ${sunny[0].avg.toFixed(1)} 小時,屬全日照:適合開花喬木,例如洋紫荊、黃花風鈴木、宮粉羊蹄甲。` }); const hot = qStat.filter(s => s.jun >= 9); if (hot.length) out.push({ kind: 'sun', level: 'info', text: `夏至時${hot.map(s => s.dir + '角').join('、')}曬足約 ${hot[0].jun.toFixed(0)} 小時:座位區宜用大樹冠樹種遮蔭,例如樟樹、水黃皮、細葉榕(遠離鋪地)。` }); if (!shadeQ.length && !sunny.length) out.push({ kind: 'sun', level: 'info', text: '整個花園日照屬半日照(全年平均 3–6 小時):大部分常見品種都適合,避免只喜全日照的開花樹。' }); // wind: open edges grouped by the way they face (edges shorter than 1 m are ignored) const ex = edgeExposure(site).filter(e => e.len >= 1), grp = wid => { const by = {}; ex.filter(e => e.open && e.faces.includes(wid)).forEach(e => (by[e.dir] = by[e.dir] || []).push(edgeLabel(site, e.edge))); return by; }; for (const [dir, eds] of Object.entries(grp('winter'))) out.push({ kind: 'wind', level: 'warn', text: `${dir}邊(${eds.join('、')})開揚,正對東北季候風及東風:宜沿邊種耐風常綠灌木作擋風,例如草海桐、石斑木、桃金娘。` }); for (const [dir, eds] of Object.entries(grp('summer'))) out.push({ kind: 'wind', level: 'ok', text: `${dir}邊(${eds.join('、')})開揚,迎夏季西南風:保留通風廊,這邊不要種密實屏障。` }); const openDirs = [...new Set(ex.filter(e => e.open).map(e => e.dir))]; if (openDirs.length || site.podium) out.push({ kind: 'wind', level: 'warn', text: `颱風:${openDirs.length ? openDirs.join('、') + '邊開揚' : '平台花園'}${site.podium ? `(平台約 ${site.level} m 高)` : ''},颱風時風力較大。避免 GLTMS 列為木質脆弱的品種(鳳凰木、石栗、鐵刀木),新種樹木要做好支撐。` }); for (const v of site.views) { const dir = edgeCompass(site, v.edge), el = edgeLabel(site, v.edge); if (v.type === 'screen') out.push({ kind: 'view', level: 'warn', text: `${dir}邊(${el})有需要遮擋的景觀:沿邊種常綠灌木或窄冠樹作屏障,例如羅漢松、龍柏、含笑。` }); else out.push({ kind: 'view', level: 'ok', text: `${dir}邊(${el})景觀好:保留視線,前景只種低地被,或用樹冠底高過 2.5 m 的樹框景。` }); } if (site.podium) { if (site.soil < 1.2) out.push({ kind: 'soil', level: 'warn', text: `平台種植土深 ${site.soil} m,少於喬木參考值 1.2 m(屋宇署 APP-152「供參考」數值):只宜種灌木及地被,或局部加高種植槽。` }); else out.push({ kind: 'soil', level: 'ok', text: `平台種植土深 ${site.soil} m,達喬木參考值 1.2 m:可以種喬木,但要確認結構荷載及排水。` }); } return { list: out, qStat }; } /* ---------- drawing ---------- */ function heatColor(h, max) { // 0 → deep blue-grey (shade) … max → warm yellow (sun) const t = Math.max(0, Math.min(1, h / max)); const stops = [[0, [58, 76, 110]], [0.3, [96, 138, 160]], [0.55, [168, 196, 140]], [0.8, [236, 206, 110]], [1, [242, 150, 70]]]; for (let i = 1; i < stops.length; i++) if (t <= stops[i][0]) { const [a, ca] = stops[i - 1], [b, cb] = stops[i], u = (t - a) / (b - a); return `rgb(${ca.map((v, k) => Math.round(v + (cb[k] - v) * u)).join(',')})`; } return 'rgb(242,150,70)'; } const ZONE_COL = { sun: 'rgba(240,196,80,0.33)', part: 'rgba(150,180,120,0.30)', shade: 'rgba(70,95,130,0.33)' }; const ZONE_ZH = { sun: '全日照 ≥6h', part: '半日照 3–6h', shade: '陰 <3h' }; function drawZones(ctx, site, sun, tx, alpha = 1) { const G = sun.avg; ctx.save(); ctx.globalAlpha = alpha; ctx.beginPath(); site.poly.forEach(([x, y], i) => i ? ctx.lineTo(tx.x(x), tx.y(y)) : ctx.moveTo(tx.x(x), tx.y(y))); ctx.closePath(); 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; const z = zoneOfHours(v); ctx.fillStyle = ZONE_COL[z]; const x0 = tx.x(i * site.W / G.nx), y0 = tx.y(j * site.D / G.ny), x1 = tx.x((i + 1) * site.W / G.nx), y1 = tx.y((j + 1) * site.D / G.ny); ctx.fillRect(x0, y0, x1 - x0 + 0.5, y1 - y0 + 0.5); } ctx.restore(); } function drawNorth(ctx, x, y, r, north) { ctx.save(); ctx.translate(x, y); ctx.rotate(north * D2R); ctx.strokeStyle = '#333'; ctx.fillStyle = '#333'; ctx.lineWidth = 1; ctx.beginPath(); ctx.arc(0, 0, r, 0, 6.283); ctx.stroke(); ctx.beginPath(); ctx.moveTo(0, -r * 1.1); ctx.lineTo(r * 0.35, r * 0.4); ctx.lineTo(0, r * 0.15); ctx.closePath(); ctx.fill(); ctx.rotate(-north * D2R); ctx.font = '600 11px system-ui'; ctx.textAlign = 'center'; const a = north * D2R; ctx.fillText('N', Math.sin(a) * (r + 10), -Math.cos(a) * (r + 10) + 4); ctx.restore(); } function drawSunPath(ctx, w, h, site, cur) { ctx.fillStyle = '#fbfaf6'; ctx.fillRect(0, 0, w, h); const cx = w / 2, cy = h / 2, R = Math.min(w, h) / 2 - 18; ctx.save(); ctx.lineWidth = 0.6; ctx.fillStyle = '#5b5955'; ctx.font = '500 8.5px "IBM Plex Mono", ui-monospace, monospace'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle'; for (const a of [0, 15, 30, 45, 60, 75]) { ctx.strokeStyle = a % 30 ? '#e4e0d6' : '#cfcabd'; ctx.beginPath(); ctx.arc(cx, cy, R * (90 - a) / 90, 0, 6.283); ctx.stroke(); } ctx.strokeStyle = '#2e2e30'; ctx.lineWidth = 1.2; ctx.beginPath(); ctx.arc(cx, cy, R, 0, 6.283); ctx.stroke(); ctx.lineWidth = 0.6; for (let b = 0; b < 360; b += 15) { const [sx, sy] = bearingToScreen(site, b); ctx.strokeStyle = b % 45 ? '#e4e0d6' : '#cfcabd'; ctx.beginPath(); ctx.moveTo(cx + sx * (b % 45 ? R * 0.2 : 0), cy + sy * (b % 45 ? R * 0.2 : 0)); ctx.lineTo(cx + sx * R, cy + sy * R); ctx.stroke(); ctx.strokeStyle = '#2e2e30'; ctx.beginPath(); ctx.moveTo(cx + sx * R, cy + sy * R); ctx.lineTo(cx + sx * (R + (b % 45 ? 3 : 6)), cy + sy * (R + (b % 45 ? 3 : 6))); ctx.stroke(); if (b % 45 === 0) ctx.fillText(COMPASS8[b / 45], cx + sx * (R + 12), cy + sy * (R + 12)); } for (const a of [30, 60]) { const [sx, sy] = bearingToScreen(site, 90); ctx.fillStyle = '#a29e94'; ctx.fillText(a + '°', cx + sx * R * (90 - a) / 90 + 8, cy + sy * R * (90 - a) / 90 - 6); } const cols = { dec: '#4f6d8f', mar: '#5b5955', jun: '#c4763c' }; for (const k of ['dec', 'mar', 'jun']) { const [m, d] = KEY_DATES[k]; ctx.strokeStyle = cols[k]; ctx.lineWidth = 1.8; ctx.beginPath(); let first = true; for (let hh = 5; hh <= 19.5; hh += 0.1) { const sp = sunPos(m, d, hh); if (sp.alt < 0) { first = true; continue; } const [sx, sy] = bearingToScreen(site, sp.az), rr = R * (90 - sp.alt) / 90; first ? ctx.moveTo(cx + sx * rr, cy + sy * rr) : ctx.lineTo(cx + sx * rr, cy + sy * rr); first = false; } ctx.stroke(); ctx.fillStyle = cols[k]; for (let hh = 7; hh <= 17; hh += 2) { const sp = sunPos(m, d, hh); if (sp.alt <= 0) continue; const [sx, sy] = bearingToScreen(site, sp.az), rr = R * (90 - sp.alt) / 90; ctx.beginPath(); ctx.arc(cx + sx * rr, cy + sy * rr, 1.8, 0, 6.283); ctx.fill(); } } if (cur) { const sp = sunPos(cur.m, cur.d, cur.h); if (sp.alt > 0) { const [sx, sy] = bearingToScreen(site, sp.az), rr = R * (90 - sp.alt) / 90; ctx.fillStyle = '#fbfaf6'; ctx.strokeStyle = '#2e2e30'; ctx.lineWidth = 1.2; ctx.beginPath(); ctx.arc(cx + sx * rr, cy + sy * rr, 5.5, 0, 6.283); ctx.fill(); ctx.stroke(); ctx.fillStyle = '#c4763c'; ctx.beginPath(); ctx.arc(cx + sx * rr, cy + sy * rr, 2.6, 0, 6.283); ctx.fill(); } } ctx.restore(); }