logigate/app/lib/draw.ts

140 lines
4.5 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// SVG geometry for gate symbols, in pixels relative to the node origin.
import { GRID as G, type Pt, type Shape } from './logic/geometry'
import type { CrossingStyle, SymbolKind } from './logic/types'
const NEGATED = new Set<SymbolKind>(['nand', 'nor', 'xnor', 'not'])
const IEC_LABEL: Record<SymbolKind, string> = {
and: '&',
nand: '&',
or: '≥1',
nor: '≥1',
xor: '=1',
xnor: '=1',
not: '1',
buf: '1',
box: '',
}
export interface GateDrawing {
stubs: string
body: string
/** Extra stroke-only path (second curve of an ANSI XOR). */
extra?: string
bubble?: { cx: number; cy: number; r: number }
label?: { x: number; y: number; text: string }
}
export function gateDrawing(shape: Shape, style: 'iec' | 'ansi'): GateDrawing {
const symbol = shape.gate!
const { body } = shape
const W = body.w * G
const out = shape.outputs[0]!
const oy = out.dy * G
const negated = NEGATED.has(symbol)
const r = 0.22 * G
const one = shape.inputs.length === 1
const top = (one ? body.y + 0.25 : body.y) * G
const bottom = (one ? body.y + body.h - 0.25 : body.y + body.h) * G
const mid = (top + bottom) / 2
const x0 = 0.5 * G
const x1 = W - 0.5 * G - (negated ? 2 * r : 0)
const stubs = [
...shape.inputs.map(p => `M0 ${p.dy * G}H${x0 + 0.5 * G}`),
`M${x1 - 0.3 * G} ${oy}H${W}`,
].join('')
const bubble = negated ? { cx: x1 + r, cy: oy, r } : undefined
if (style === 'iec') {
return {
stubs,
body: `M${x0} ${top}H${x1}V${bottom}H${x0}Z`,
bubble,
label: { x: (x0 + x1) / 2, y: top + 0.62 * G, text: IEC_LABEL[symbol] },
}
}
const h = bottom - top
switch (symbol) {
case 'and':
case 'nand': {
const rad = h / 2
const xm = Math.max(x0, x1 - rad)
return { stubs, bubble, body: `M${x0} ${top}H${xm}A${rad} ${rad} 0 0 1 ${xm} ${bottom}H${x0}Z` }
}
case 'or':
case 'nor':
case 'xor':
case 'xnor': {
const xs = symbol.startsWith('x') ? x0 + 0.3 * G : x0
const w = x1 - xs
const path = `M${xs} ${top}Q${xs + 0.28 * w} ${mid} ${xs} ${bottom}Q${xs + 0.62 * w} ${bottom} ${x1} ${mid}Q${xs + 0.62 * w} ${top} ${xs} ${top}Z`
const extra = xs !== x0 ? `M${x0} ${top}Q${x0 + 0.28 * w} ${mid} ${x0} ${bottom}` : undefined
return { stubs, bubble, body: path, extra }
}
default:
return { stubs, bubble, body: `M${x0} ${top}L${x1} ${mid}L${x0} ${bottom}Z` }
}
}
// 7-segment patterns for 0–F, segments a..g as bits 0..6.
const SEGMENTS = [0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71]
export function sevenSegment(value: number) {
const xa = 1.15 * G
const xb = 2.35 * G
const y0 = -0.05 * G
const y1 = 1.5 * G
const y2 = 3.05 * G
const g = 3
const segs: [number, number, number, number][] = [
[xa + g, y0, xb - g, y0],
[xb, y0 + g, xb, y1 - g],
[xb, y1 + g, xb, y2 - g],
[xa + g, y2, xb - g, y2],
[xa, y1 + g, xa, y2 - g],
[xa, y0 + g, xa, y1 - g],
[xa + g, y1, xb - g, y1],
]
const bits = SEGMENTS[value & 15]!
return segs.map((s, i) => ({ d: `M${s[0]} ${s[1]}L${s[2]} ${s[3]}`, on: (bits >> i) & 1 }))
}
const HOP = 5
/**
* SVG path for a wire (points in grid units), with a hop drawn on its
* horizontal segments wherever another signal crosses (`hops`: segment
* index to crossing x positions). `scale` is pixels per grid unit.
*/
export function wirePath(pts: Pt[], hops: Map<number, number[]> | undefined, style: CrossingStyle, scale = G): string {
const r = HOP
let d = `M${pts[0]![0] * scale} ${pts[0]![1] * scale}`
for (let i = 0; i < pts.length - 1; i++) {
const [ax, ay] = pts[i]!
const [bx, by] = pts[i + 1]!
const xs = hops?.get(i)
if (xs && style !== 'plain' && ay === by) {
const dir = Math.sign(bx - ax)
const y = ay * scale
let prev = ax * scale
for (const hx of xs.map(x => x * scale).sort((p, q) => dir * (p - q))) {
// Skip hops too close to a corner or to the previous hop.
if (Math.abs(hx - prev) < r + 1 || Math.abs(bx * scale - hx) < r + 1) continue
const s = hx - dir * r
const e = hx + dir * r
d += `L${s} ${y}`
if (style === 'bow') d += `A${r} ${r} 0 0 ${dir > 0 ? 1 : 0} ${e} ${y}`
else if (style === 'triangle') d += `L${hx} ${y - r * 1.3}L${e} ${y}`
else if (style === 'bridge') d += `L${s} ${y - r}L${e} ${y - r}L${e} ${y}`
else d += `M${e} ${y}`
prev = e
}
}
d += `L${bx * scale} ${by * scale}`
}
return d
}