@wentools/spatial
v0.1.0
Published
Pure 2D geometry primitives — points, vectors, rectangles, circles, lines, and distance metrics
Downloads
197
Maintainers
Readme
@wentools/spatial
Pure 2D geometry primitives — points, vectors, rectangles, circles, lines, and distance metrics.
Zero dependencies. Works in Deno, Node, and the browser.
Install
# JSR (recommended)
npx jsr add @wentools/spatial
# Deno
deno add jsr:@wentools/spatialUsage
import {
createPoint,
createRectangle,
isPointInRectangle,
euclideanDistance,
} from '@wentools/spatial'
const point = createPoint(5, 3)
const rect = createRectangle(0, 0, 10, 10)
isPointInRectangle(point, rect) // true
euclideanDistance(point, createPoint(0, 0)) // 5.83...API
Point
| Function | Description |
|----------|-------------|
| createPoint(x, y) | Create a point |
| createOrigin() | Create point at (0, 0) |
| translatePoint(point, dx, dy) | Translate a point by offset |
| pointsEqual(a, b) | Check equality |
| findPointsBounds(points) | Bounding box of points |
Vector
| Function | Description |
|----------|-------------|
| createVector(x, y) | Create a vector |
| createZeroVector() | Create zero vector |
| vectorMagnitude(v) | Length of a vector |
| addVectors(a, b) | Add two vectors |
| subtractVectors(a, b) | Subtract vectors |
| scaleVector(v, scalar) | Scale a vector |
| normalizeVector(v) | Normalize to unit length |
Rectangle
| Function | Description |
|----------|-------------|
| createRectangle(x, y, w, h) | Create a rectangle |
| createRectangleFromPoints(tl, br) | From two corner points |
| getRectangleArea(rect) | Area |
| getRectanglePerimeter(rect) | Perimeter |
| getRectangleCenter(rect) | Center point |
| isPointInRectangle(point, rect) | Point containment |
| rectanglesOverlap(a, b) | Overlap test |
| rectangleContains(outer, inner) | Full containment |
| rectangleIntersection(a, b) | Intersection rectangle |
| rectangleUnion(a, b) | Union bounding box |
| centerRectangleInContainer(container, inner) | Center alignment |
| centerHorizontally(ref, el) | Horizontal centering |
| centerVertically(ref, el) | Vertical centering |
| getBoundingBoxFromPoints(points) | Grid-aware bounding box (+1 width/height) |
Circle
| Function | Description |
|----------|-------------|
| createCircle(x, y, r) | Create a circle |
| createCircleFromPoint(center, r) | From point and radius |
| getCircleArea(circle) | Area |
| getCircleCircumference(circle) | Circumference |
| getCircleCenter(circle) | Center point |
| isPointInCircle(point, circle) | Point containment |
| circlesOverlap(a, b) | Overlap test |
| circleContains(outer, inner) | Full containment |
Line
| Function | Description |
|----------|-------------|
| createLine(start, end) | Create a line segment |
| createLineFromCoords(x1, y1, x2, y2) | From coordinates |
| getLineLength(line) | Length |
| getLineMidpoint(line) | Midpoint |
| getLineSlope(line) | Slope |
| isPointOnLine(point, line, tolerance?) | Point-on-line test |
Distance
| Function | Description |
|----------|-------------|
| manhattanDistance(a, b) | L1 norm |
| euclideanDistance(a, b) | L2 norm |
| euclideanDistanceSquared(a, b) | Squared L2 (no sqrt) |
| chebyshevDistance(a, b) | L∞ norm |
| createDistanceChecker(threshold, metric?) | Partial-applied distance check |
Bounding Box
| Function | Description |
|----------|-------------|
| getBoundingBox(points) | Tight bounding box for points |
License
MIT
