calculate-elbow
A TypeScript library that computes "elbow" paths between two points. Each point can optionally declare a facing direction—"x+"
, "x-"
, "y+"
, or "y-"
—which influences how the path enters or exits the point. This is handy when routing wires or connectors in diagrams.
Installation
bun add calculate-elbow
Usage
import { calculateElbow } from "calculate-elbow"
const start = { x: 0, y: 0, facingDirection: "x+" }
const end = { x: 100, y: 50, facingDirection: "y-" }
const path = calculateElbow(start, end, { overshoot: 20 })
console.log(path)
// [
// { x: 0, y: 0 },
// { x: 20, y: 0 },
// { x: 20, y: 50 },
// { x: 100, y: 50 }
// ]
calculateElbow
returns an array of points representing the orthogonal segments from start to end. The optional overshoot
parameter controls how far the path extends beyond the end point when aligning with its facing direction.
Testing
Run the test suite with Bun:
bun test