|
@@ -0,0 +1,406 @@
|
|
|
|
|
+import type { Board } from 'jsxgraph'
|
|
|
|
|
+import type { SpeakerParams, CalcResult } from '../types'
|
|
|
|
|
+import type { ChartPalette } from './palette'
|
|
|
|
|
+import { DEG, splAt } from '../calc'
|
|
|
|
|
+
|
|
|
|
|
+export interface DiagramChartLabels {
|
|
|
|
|
+ empty: string
|
|
|
|
|
+ horizontalDistance: string
|
|
|
|
|
+ height: string
|
|
|
|
|
+ upperBound: string
|
|
|
|
|
+ lowerBound: string
|
|
|
|
|
+ acousticAxis: string
|
|
|
|
|
+ coverage: string
|
|
|
|
|
+ vertical: string
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+let currentObjects: any[] = []
|
|
|
|
|
+
|
|
|
|
|
+function nice(rough: number) {
|
|
|
|
|
+ const p = Math.pow(10, Math.floor(Math.log10(Math.max(rough, 1e-9))))
|
|
|
|
|
+ const n = rough / p
|
|
|
|
|
+ if (n <= 1) return p
|
|
|
|
|
+ if (n <= 2) return 2 * p
|
|
|
|
|
+ if (n <= 5) return 5 * p
|
|
|
|
|
+ return 10 * p
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function drawEmpty(board: Board, text: string, pal: ChartPalette): void {
|
|
|
|
|
+ board.suspendUpdate()
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (currentObjects.length > 0) {
|
|
|
|
|
+ board.removeObject(currentObjects)
|
|
|
|
|
+ currentObjects = []
|
|
|
|
|
+ }
|
|
|
|
|
+ const t = board.create('text', [0, 0, text], {
|
|
|
|
|
+ fontSize: 14,
|
|
|
|
|
+ color: pal.dim,
|
|
|
|
|
+ anchorX: 'center',
|
|
|
|
|
+ anchorY: 'middle',
|
|
|
|
|
+ fixed: true
|
|
|
|
|
+ })
|
|
|
|
|
+ currentObjects.push(t)
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ board.unsuspendUpdate()
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export function drawDiagram(
|
|
|
|
|
+ board: Board,
|
|
|
|
|
+ p: SpeakerParams & { maxD?: number; maxZ?: number },
|
|
|
|
|
+ result: CalcResult | null,
|
|
|
|
|
+ labels: DiagramChartLabels,
|
|
|
|
|
+ pal: ChartPalette
|
|
|
|
|
+): void {
|
|
|
|
|
+ const { z, d1, d2, d3, angle, vCov } = p
|
|
|
|
|
+ const cov = vCov
|
|
|
|
|
+ const t6 = cov / 2
|
|
|
|
|
+
|
|
|
|
|
+ const maxD = p.maxD || Math.max(d1, d2, d3, 5) * 1.13
|
|
|
|
|
+ const maxZ = p.maxZ || Math.max(z * 1.15, 3.5)
|
|
|
|
|
+
|
|
|
|
|
+ if (!result) {
|
|
|
|
|
+ drawEmpty(board, labels.empty, pal)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const aRad = angle * DEG
|
|
|
|
|
+ const t6Rad = t6 * DEG
|
|
|
|
|
+ const upperAngle = aRad + t6Rad
|
|
|
|
|
+ const lowerAngle = aRad - t6Rad
|
|
|
|
|
+
|
|
|
|
|
+ const dFar = upperAngle < Math.PI / 2 - 0.01 ? z / Math.tan(upperAngle) : maxD * 3
|
|
|
|
|
+ const dNear = lowerAngle > 0.01 ? z / Math.tan(lowerAngle) : -1
|
|
|
|
|
+
|
|
|
|
|
+ const nearX = dNear >= 0 ? Math.min(maxD, Math.max(0, dNear)) : null
|
|
|
|
|
+ const farX = dFar > 0 ? Math.min(maxD, dFar) : null
|
|
|
|
|
+
|
|
|
|
|
+ board.suspendUpdate()
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (currentObjects.length > 0) {
|
|
|
|
|
+ board.removeObject(currentObjects)
|
|
|
|
|
+ currentObjects = []
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const addObj = (type: string, args: any[], attrs: any = {}) => {
|
|
|
|
|
+ if (type === 'point') attrs.withLabel = false
|
|
|
|
|
+ const obj = board.create(type as any, args, attrs)
|
|
|
|
|
+ currentObjects.push(obj)
|
|
|
|
|
+ return obj
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const pt = (x: number, y: number) => addObj('point', [x, y], { visible: false, fixed: true })
|
|
|
|
|
+
|
|
|
|
|
+ // ================= 一、网格和坐标轴 =================
|
|
|
|
|
+ const gD = nice(maxD / 6)
|
|
|
|
|
+ const gZ = nice(maxZ / 5)
|
|
|
|
|
+
|
|
|
|
|
+ for (let d = gD; d < maxD; d += gD) {
|
|
|
|
|
+ addObj(
|
|
|
|
|
+ 'segment',
|
|
|
|
|
+ [
|
|
|
|
|
+ [d, 0],
|
|
|
|
|
+ [d, maxZ]
|
|
|
|
|
+ ],
|
|
|
|
|
+ { strokeColor: pal.border, strokeWidth: 1, dash: 2, strokeOpacity: 0.5, layer: 0 }
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ for (let h = gZ; h < maxZ; h += gZ) {
|
|
|
|
|
+ addObj(
|
|
|
|
|
+ 'segment',
|
|
|
|
|
+ [
|
|
|
|
|
+ [0, h],
|
|
|
|
|
+ [maxD, h]
|
|
|
|
|
+ ],
|
|
|
|
|
+ { strokeColor: pal.border, strokeWidth: 1, dash: 2, strokeOpacity: 0.5, layer: 0 }
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ addObj(
|
|
|
|
|
+ 'segment',
|
|
|
|
|
+ [
|
|
|
|
|
+ [0, 0],
|
|
|
|
|
+ [0, maxZ]
|
|
|
|
|
+ ],
|
|
|
|
|
+ { strokeColor: pal.dim, strokeWidth: 1.5, lastArrow: true, layer: 1 }
|
|
|
|
|
+ )
|
|
|
|
|
+ addObj(
|
|
|
|
|
+ 'segment',
|
|
|
|
|
+ [
|
|
|
|
|
+ [0, 0],
|
|
|
|
|
+ [maxD, 0]
|
|
|
|
|
+ ],
|
|
|
|
|
+ { strokeColor: pal.dim, strokeWidth: 1.5, lastArrow: true, layer: 1 }
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ // X 轴刻度和数字
|
|
|
|
|
+ for (let d = gD; d < maxD; d += gD) {
|
|
|
|
|
+ addObj(
|
|
|
|
|
+ 'segment',
|
|
|
|
|
+ [
|
|
|
|
|
+ [d, 0],
|
|
|
|
|
+ [d, -maxZ * 0.015]
|
|
|
|
|
+ ],
|
|
|
|
|
+ { strokeColor: pal.dim, strokeWidth: 1, layer: 1 }
|
|
|
|
|
+ )
|
|
|
|
|
+ addObj('text', [d, -maxZ * 0.03, d.toFixed(0)], {
|
|
|
|
|
+ fontSize: 9,
|
|
|
|
|
+ color: pal.dim,
|
|
|
|
|
+ anchorX: 'center',
|
|
|
|
|
+ anchorY: 'top',
|
|
|
|
|
+ layer: 1
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Y 轴刻度和数字
|
|
|
|
|
+ for (let h = gZ; h < maxZ; h += gZ) {
|
|
|
|
|
+ addObj(
|
|
|
|
|
+ 'segment',
|
|
|
|
|
+ [
|
|
|
|
|
+ [0, h],
|
|
|
|
|
+ [-maxD * 0.015, h]
|
|
|
|
|
+ ],
|
|
|
|
|
+ { strokeColor: pal.dim, strokeWidth: 1, layer: 1 }
|
|
|
|
|
+ )
|
|
|
|
|
+ addObj('text', [-maxD * 0.025, h, h.toFixed(1)], {
|
|
|
|
|
+ fontSize: 9,
|
|
|
|
|
+ color: pal.dim,
|
|
|
|
|
+ anchorX: 'right',
|
|
|
|
|
+ anchorY: 'middle',
|
|
|
|
|
+ layer: 1
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 轴名称:精确防裁切
|
|
|
|
|
+ addObj('text', [maxD, -maxZ * 0.05, labels.horizontalDistance], {
|
|
|
|
|
+ fontSize: 10,
|
|
|
|
|
+ color: pal.dim,
|
|
|
|
|
+ anchorX: 'right',
|
|
|
|
|
+ anchorY: 'top',
|
|
|
|
|
+ layer: 1
|
|
|
|
|
+ })
|
|
|
|
|
+ addObj('text', [0, maxZ + maxZ * 0.02, labels.height], {
|
|
|
|
|
+ fontSize: 10,
|
|
|
|
|
+ color: pal.dim,
|
|
|
|
|
+ anchorX: 'middle',
|
|
|
|
|
+ anchorY: 'bottom',
|
|
|
|
|
+ layer: 1
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ // ================= 二、覆盖区域和声学线 =================
|
|
|
|
|
+ if (nearX !== null && farX !== null) {
|
|
|
|
|
+ const leftX = Math.min(nearX, farX)
|
|
|
|
|
+ const rightX = Math.max(nearX, farX)
|
|
|
|
|
+ addObj('polygon', [pt(0, z), pt(leftX, 0), pt(rightX, 0)], {
|
|
|
|
|
+ fillColor: pal.accent,
|
|
|
|
|
+ fillOpacity: 0.1,
|
|
|
|
|
+ strokeColor: 'none',
|
|
|
|
|
+ layer: 2
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (nearX !== null && farX !== null && farX > nearX) {
|
|
|
|
|
+ addObj(
|
|
|
|
|
+ 'segment',
|
|
|
|
|
+ [
|
|
|
|
|
+ [nearX, 0],
|
|
|
|
|
+ [farX, 0]
|
|
|
|
|
+ ],
|
|
|
|
|
+ { strokeColor: pal.accent, strokeWidth: 4, strokeOpacity: 0.35, layer: 2 }
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (farX !== null) {
|
|
|
|
|
+ addObj(
|
|
|
|
|
+ 'segment',
|
|
|
|
|
+ [
|
|
|
|
|
+ [0, z],
|
|
|
|
|
+ [farX, 0]
|
|
|
|
|
+ ],
|
|
|
|
|
+ { strokeColor: pal.accent, strokeWidth: 1.2, dash: 2, strokeOpacity: 0.6, layer: 3 }
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ if (nearX !== null) {
|
|
|
|
|
+ addObj(
|
|
|
|
|
+ 'segment',
|
|
|
|
|
+ [
|
|
|
|
|
+ [0, z],
|
|
|
|
|
+ [nearX, 0]
|
|
|
|
|
+ ],
|
|
|
|
|
+ { strokeColor: pal.accent, strokeWidth: 1.2, dash: 2, strokeOpacity: 0.6, layer: 3 }
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const d3Clamped = Math.min(maxD, d3)
|
|
|
|
|
+ addObj(
|
|
|
|
|
+ 'segment',
|
|
|
|
|
+ [
|
|
|
|
|
+ [0, z],
|
|
|
|
|
+ [d3Clamped, 0]
|
|
|
|
|
+ ],
|
|
|
|
|
+ { strokeColor: pal.accent, strokeWidth: 1.5, dash: 2, layer: 3 }
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ // ================= 三、点与标签(防重叠、防溢出、强制拆行) =================
|
|
|
|
|
+ addObj('point', [0, z], {
|
|
|
|
|
+ face: '[]',
|
|
|
|
|
+ size: 6,
|
|
|
|
|
+ fillColor: pal.accent,
|
|
|
|
|
+ strokeColor: '#ffffff',
|
|
|
|
|
+ strokeWidth: 1,
|
|
|
|
|
+ layer: 5
|
|
|
|
|
+ })
|
|
|
|
|
+ addObj('text', [maxD * 0.02, z, 'SPK'], {
|
|
|
|
|
+ fontSize: 10,
|
|
|
|
|
+ color: pal.accent,
|
|
|
|
|
+ anchorX: 'left',
|
|
|
|
|
+ anchorY: 'bottom',
|
|
|
|
|
+ layer: 5
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ const pts = [
|
|
|
|
|
+ { d: d1, lbl: 'D1', c: pal.cyan },
|
|
|
|
|
+ { d: d2, lbl: 'D2', c: pal.cyan },
|
|
|
|
|
+ { d: d3, lbl: 'D3', c: pal.accent }
|
|
|
|
|
+ ]
|
|
|
|
|
+
|
|
|
|
|
+ const sortedPts = [...pts].sort((a, b) => a.d - b.d)
|
|
|
|
|
+ const textBaseY = maxZ * 0.03
|
|
|
|
|
+
|
|
|
|
|
+ let lastX = -999
|
|
|
|
|
+ let lastTier = -1
|
|
|
|
|
+ let currentTier = 0
|
|
|
|
|
+
|
|
|
|
|
+ sortedPts.forEach((ptDef) => {
|
|
|
|
|
+ const x = ptDef.d
|
|
|
|
|
+ const spl = splAt(x, p)
|
|
|
|
|
+
|
|
|
|
|
+ addObj('point', [x, 0], {
|
|
|
|
|
+ size: 4,
|
|
|
|
|
+ fillColor: ptDef.c,
|
|
|
|
|
+ strokeColor: pal.bg,
|
|
|
|
|
+ strokeWidth: 1,
|
|
|
|
|
+ layer: 4
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ const labelWidth = maxD * 0.15
|
|
|
|
|
+
|
|
|
|
|
+ if (lastTier === -1) {
|
|
|
|
|
+ currentTier = 0
|
|
|
|
|
+ } else if (x - lastX < labelWidth) {
|
|
|
|
|
+ currentTier = lastTier + 1
|
|
|
|
|
+ if (currentTier > 2) currentTier = 0
|
|
|
|
|
+ } else {
|
|
|
|
|
+ currentTier = 0
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ lastX = x
|
|
|
|
|
+ lastTier = currentTier
|
|
|
|
|
+
|
|
|
|
|
+ const textY = textBaseY + currentTier * (maxZ * 0.08)
|
|
|
|
|
+
|
|
|
|
|
+ // 靠右边界时,文字自动向左对齐防止切出屏幕
|
|
|
|
|
+ const isNearRightEdge = x > maxD * 0.85
|
|
|
|
|
+ const anchorX = isNearRightEdge ? 'right' : 'center'
|
|
|
|
|
+
|
|
|
|
|
+ // 强制拆成两个文本对象画,避免 \n 在某些环境下失效变成一行超长文本
|
|
|
|
|
+ addObj('text', [x, textY, `${ptDef.lbl} = ${x.toFixed(1)}m`], {
|
|
|
|
|
+ fontSize: 10,
|
|
|
|
|
+ color: ptDef.c,
|
|
|
|
|
+ anchorX: anchorX,
|
|
|
|
|
+ anchorY: 'bottom',
|
|
|
|
|
+ layer: 4
|
|
|
|
|
+ })
|
|
|
|
|
+ addObj('text', [x, textY + maxZ * 0.1, `${spl.toFixed(1)} dB`], {
|
|
|
|
|
+ fontSize: 10,
|
|
|
|
|
+ color: ptDef.c,
|
|
|
|
|
+ anchorX: anchorX,
|
|
|
|
|
+ anchorY: 'top',
|
|
|
|
|
+ layer: 4
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ // ================= 四、弧线与角度 =================
|
|
|
|
|
+
|
|
|
|
|
+ // 1. 水平向右的基准虚线,标定 A 角的参考系
|
|
|
|
|
+ const baseRefLineLength = maxD * 0.18
|
|
|
|
|
+ addObj(
|
|
|
|
|
+ 'segment',
|
|
|
|
|
+ [
|
|
|
|
|
+ [0, z],
|
|
|
|
|
+ [baseRefLineLength, z]
|
|
|
|
|
+ ],
|
|
|
|
|
+ {
|
|
|
|
|
+ strokeColor: pal.dim,
|
|
|
|
|
+ strokeWidth: 1,
|
|
|
|
|
+ dash: 2,
|
|
|
|
|
+ strokeOpacity: 0.8,
|
|
|
|
|
+ layer: 3
|
|
|
|
|
+ }
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 覆盖角 V 的圆弧(外圈)
|
|
|
|
|
+ if (dFar > 0.01) {
|
|
|
|
|
+ // 半径与 maxZ 挂钩,确保在不同拉伸比例下都足够清晰
|
|
|
|
|
+ const vRadius = maxZ * 0.3
|
|
|
|
|
+ addObj(
|
|
|
|
|
+ 'curve',
|
|
|
|
|
+ [
|
|
|
|
|
+ (t: number) => Math.cos(t) * vRadius,
|
|
|
|
|
+ (t: number) => z - Math.sin(t) * vRadius,
|
|
|
|
|
+ lowerAngle, // 起始角(独立参数)
|
|
|
|
|
+ upperAngle // 终止角(独立参数)
|
|
|
|
|
+ ],
|
|
|
|
|
+ { strokeColor: '#eab308', strokeWidth: 2.5, layer: 4 } // 加粗让它更显眼
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ // V 标签放在外圈
|
|
|
|
|
+ const midA = (lowerAngle + upperAngle) / 2
|
|
|
|
|
+ const vLblR = vRadius + maxZ * 0.05
|
|
|
|
|
+ addObj(
|
|
|
|
|
+ 'text',
|
|
|
|
|
+ [Math.cos(midA) * vLblR, z - Math.sin(midA) * vLblR, `V = ${cov.toFixed(0)}°`],
|
|
|
|
|
+ {
|
|
|
|
|
+ fontSize: 11,
|
|
|
|
|
+ color: '#ca8a04',
|
|
|
|
|
+ anchorX: 'left',
|
|
|
|
|
+ anchorY: 'middle',
|
|
|
|
|
+ layer: 4
|
|
|
|
|
+ }
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 安装倾斜角 A 的圆弧(内圈)
|
|
|
|
|
+ if (angle > 0.01) {
|
|
|
|
|
+ const aRadius = maxZ * 0.16 // 内圈半径
|
|
|
|
|
+ addObj(
|
|
|
|
|
+ 'curve',
|
|
|
|
|
+ [
|
|
|
|
|
+ (t: number) => Math.cos(t) * aRadius,
|
|
|
|
|
+ (t: number) => z - Math.sin(t) * aRadius,
|
|
|
|
|
+ 0, // 起始角(独立参数)
|
|
|
|
|
+ aRad // 终止角(独立参数)
|
|
|
|
|
+ ],
|
|
|
|
|
+ { strokeColor: pal.text, strokeWidth: 1.5, strokeOpacity: 0.9, layer: 4 }
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+ // A 标签放在内圈
|
|
|
|
|
+ const midA = aRad / 2
|
|
|
|
|
+ const aLblR = aRadius + maxZ * 0.04
|
|
|
|
|
+ addObj(
|
|
|
|
|
+ 'text',
|
|
|
|
|
+ [Math.cos(midA) * aLblR, z - Math.sin(midA) * aLblR, `A = ${angle.toFixed(1)}°`],
|
|
|
|
|
+ {
|
|
|
|
|
+ fontSize: 11,
|
|
|
|
|
+ color: pal.text,
|
|
|
|
|
+ anchorX: 'left',
|
|
|
|
|
+ anchorY: 'bottom',
|
|
|
|
|
+ layer: 10
|
|
|
|
|
+ }
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ board.unsuspendUpdate()
|
|
|
|
|
+ board.update()
|
|
|
|
|
+ }
|
|
|
|
|
+}
|