logigate/app/lib/logic/wires.ts

163 lines
6.2 KiB
TypeScript

// Editing wire routes. A route is the full point list from the output pin
// to the input pin; a wire stores only the corners in between (`pts`).
import { simplify, type Pt } from './geometry'
const sign = (n: number) => (n > 0 ? 1 : n < 0 ? -1 : 0)
const copy = (pts: Pt[]) => pts.map(p => [p[0], p[1]] as Pt)
/** Axis a segment moves along when dragged: x (0) for vertical segments, y (1) for horizontal ones. */
export function dragAxis(a: Pt, b: Pt): 0 | 1 {
return a[0] === b[0] ? 0 : 1
}
/**
* Prepares dragging segment `i` sideways. A segment that touches a pin gets
* a one-unit stub at that end, so the pin stays where it is and the wire
* jogs right next to it. Returns null for segments too short to move.
*/
export function segmentDrag(route: Pt[], i: number): { base: Pt[]; k: number; axis: 0 | 1 } | null {
const last = route.length - 1
const a = route[i]!
const b = route[i + 1]!
const axis = dragAxis(a, b)
const u: Pt = [sign(b[0] - a[0]), sign(b[1] - a[1])]
const len = Math.abs(b[0] - a[0]) + Math.abs(b[1] - a[1])
const base = copy(route)
const fromPin = i === 0
const toPin = i + 1 === last
if (fromPin && toPin) {
if (len < 2) return null
base.splice(1, 0, [a[0] + u[0], a[1] + u[1]], [b[0] - u[0], b[1] - u[1]])
return { base, k: 1, axis }
}
if (toPin) base.splice(last, 0, [b[0] - u[0], b[1] - u[1]])
if (fromPin) {
base.splice(1, 0, [a[0] + u[0], a[1] + u[1]])
return { base, k: 1, axis }
}
return { base, k: i, axis }
}
/** Corner points after shifting segment `k` of `base` by `delta` along `axis`. */
export function moveSegment(base: Pt[], k: number, axis: 0 | 1, delta: number): Pt[] {
const pts = copy(base)
pts[k]![axis] += delta
pts[k + 1]![axis] += delta
return simplify(pts).slice(1, -1)
}
/**
* Corner points after moving corner `i` of a route. Its neighbours follow
* so the wire stays square; next to a pin, a one-unit stub keeps the wire
* leaving the pin straight.
*/
export function moveCorner(route: Pt[], i: number, to: Pt): Pt[] {
const pts = copy(route)
const last = route.length - 1
const old = route[i]!
pts[i] = [to[0], to[1]]
for (const j of [i - 1, i + 1]) {
if (j <= 0 || j >= last) continue
if (route[j]![1] === old[1]) pts[j]![1] = to[1]
else pts[j]![0] = to[0]
}
const off = to[1] !== old[1]
if (off && i === last - 1 && route[last]![1] === old[1]) {
const [px, py] = route[last]!
pts.splice(last, 0, [px - 1, to[1]], [px - 1, py])
}
if (off && i === 1 && route[0]![1] === old[1]) {
const [px, py] = route[0]!
pts.splice(1, 0, [px + 1, py], [px + 1, to[1]])
}
return simplify(pts).slice(1, -1)
}
/** The grid point on segment `i` closest to `at`. */
export function pointOnSegment(route: Pt[], i: number, at: Pt): Pt {
const a = route[i]!
const b = route[i + 1]!
const clamp = (v: number, p: number, q: number) => Math.min(Math.max(v, Math.min(p, q)), Math.max(p, q))
return a[0] === b[0] ? [a[0], clamp(Math.round(at[1]), a[1], b[1])] : [clamp(Math.round(at[0]), a[0], b[0]), a[1]]
}
/**
* Splits segment `i` at the grid point nearest `at` and prepares to drag the
* part after it sideways, which pulls a new step into the wire.
*/
export function splitDrag(route: Pt[], i: number, at: Pt) {
const c = pointOnSegment(route, i, at)
const b = route[i + 1]!
if (c[0] === b[0] && c[1] === b[1]) return segmentDrag(route, i)
const pts = [...copy(route.slice(0, i + 1)), c, [c[0], c[1]] as Pt, ...copy(route.slice(i + 1))]
return segmentDrag(pts, i + 2)
}
/**
* Finishes a branch toward its target pin: a branch that leaves a vertical
* segment steps sideways first, so it forms a clear T instead of running on
* along the wire it came from.
*/
export function branchTo(corners: Pt[], from: Pt, to: Pt): Pt[] {
const c = corners[corners.length - 1]
const prev = corners[corners.length - 2] ?? from
if (!c || prev[0] !== c[0] || c[1] === to[1]) return corners
return [...corners, [Math.round((c[0] + to[0]) / 2), c[1]]]
}
/** Corners for a new wire that leaves an existing route at the point nearest `at` on segment `i`. */
export function branchCorners(route: Pt[], i: number, at: Pt): Pt[] {
const c = pointOnSegment(route, i, at)
return simplify([...copy(route.slice(0, i + 1)), c]).slice(1)
}
/** True when any segment of the route passes through the box (edges included). */
export function routeTouchesBox(pts: Pt[], box: { x0: number; y0: number; x1: number; y1: number }): boolean {
for (let i = 0; i < pts.length - 1; i++) {
const [ax, ay] = pts[i]!
const [bx, by] = pts[i + 1]!
const lx = Math.min(ax, bx)
const hx = Math.max(ax, bx)
const ly = Math.min(ay, by)
const hy = Math.max(ay, by)
// Axis-aligned segment against an axis-aligned box: their extents overlap on both axes.
if (hx >= box.x0 && lx <= box.x1 && hy >= box.y0 && ly <= box.y1) return true
}
return false
}
/** The segment of a route passing within `tol` of a point, or -1. */
export function segmentAt(pts: Pt[], p: Pt, tol = 0.4): number {
for (let i = 0; i < pts.length - 1; i++) {
const [ax, ay] = pts[i]!
const [bx, by] = pts[i + 1]!
if (ax === bx && ay === by) continue
if (p[0] >= Math.min(ax, bx) - tol && p[0] <= Math.max(ax, bx) + tol && p[1] >= Math.min(ay, by) - tol && p[1] <= Math.max(ay, by) + tol) return i
}
return -1
}
/**
* Where to grab a wire's end to move it to another pin: a little way along
* the route from that pin, on its first (or last) segment.
*/
export function endGrip(pts: Pt[], end: 'from' | 'to'): Pt | null {
if (pts.length < 2) return null
const [a, b] = end === 'from' ? [pts[0]!, pts[1]!] : [pts[pts.length - 1]!, pts[pts.length - 2]!]
const len = Math.abs(b[0] - a[0]) + Math.abs(b[1] - a[1])
if (!len) return null
const t = Math.min(0.9, len / 2) / len
return [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]
}
/** The end of a route a point is close to (within `reach`, measured to the pin), if any. */
export function nearEnd(pts: Pt[], p: Pt, reach = 1.1): 'from' | 'to' | null {
if (pts.length < 2) return null
const d = (q: Pt) => Math.hypot(q[0] - p[0], q[1] - p[1])
const from = d(pts[0]!)
const to = d(pts[pts.length - 1]!)
if (Math.min(from, to) > reach) return null
return from <= to ? 'from' : 'to'
}