@wentools/spatial
v0.1.1
Published
Pure 2D geometry primitives — points, vectors, rectangles, circles, lines, and distance metrics
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. All functions are pure, operate in continuous 2D coordinate space, and take angles in radians.
Install
# npm
npm install @wentools/spatial
# Deno / JSR
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...Types
Point2D ({ x, y }), Vector2D ({ x, y }), Rectangle ({ x, y, width, height }),
Circle ({ x, y, radius }) and Line ({ start, end }).
API
Point
| Function | Description |
|----------|-------------|
| createPoint(x, y) | Create a point |
| createOrigin() | Create a point at (0, 0) |
| translatePoint(point, dx, dy) | Translate a point by an offset |
| pointsEqual(a, b) | Check equality |
| lerpPoint(a, b, t) | Linear interpolation between two points |
| midpoint(a, b) | Point halfway between two points |
| rotatePoint(point, angle, origin?) | Rotate around an origin (default 0, 0) |
| clampPoint(point, bounds) | Clamp a point inside rectangular bounds |
Vector
| Function | Description |
|----------|-------------|
| createVector(x, y) | Create a vector |
| createZeroVector() | Create a 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 |
| dotProduct(a, b) | Dot product |
| crossProduct2D(a, b) | 2D cross product (a scalar) |
| perpendicular(v) | Vector rotated 90 degrees counter-clockwise |
| vectorFromAngle(angle, magnitude?) | Vector from an angle and length (default 1) |
| vectorToAngle(v) | Angle of a vector |
| rotateVector(v, angle) | Rotate a vector |
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 |
| getRectangleCorners(rect) | The four corner points |
| isPointInRectangle(point, rect) | Point containment |
| rectanglesOverlap(a, b) | Overlap test |
| rectangleContains(outer, inner) | Full containment |
| rectangleIntersection(a, b) | Intersection rectangle, or null |
| rectangleUnion(a, b) | Union bounding box |
| translateRectangle(rect, dx, dy) | Move a rectangle |
| expandRectangle(rect, amount) | Grow on every side |
| insetRectangle(rect, amount) | Shrink on every side |
| scaleRectangle(rect, factor, origin?) | Scale around an origin |
| centerRectangleInContainer(container, inner) | Top-left point that centers inner (whole pixels) |
| centerHorizontally(ref, el) | X position that centers el on ref |
| centerVertically(ref, el) | Y position that centers el on ref |
Circle
| Function | Description |
|----------|-------------|
| createCircle(x, y, r) | Create a circle |
| createCircleFromPoint(center, r) | From a 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
A line is a segment from start to end.
| 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 (Infinity for vertical lines) |
| isPointOnLine(point, line, tolerance?) | Point-on-segment test (default tolerance 0.001) |
Distance
| Function | Description |
|----------|-------------|
| manhattanDistance(a, b) | L1 norm |
| euclideanDistance(a, b) | L2 norm |
| euclideanDistanceSquared(a, b) | Squared L2 (no sqrt) |
| chebyshevDistance(a, b) | L-infinity norm |
| createDistanceChecker(threshold, metric?) | Returns a function that tests whether two points are within a threshold ('euclidean' by default) |
Intersection
| Function | Description |
|----------|-------------|
| circleRectangleOverlap(circle, rect) | Circle and rectangle overlap test |
| lineLineIntersection(a, b) | Intersection point of two segments, or null |
| closestPointOnLine(point, line) | Closest point on a segment |
| closestPointOnRectangle(point, rect) | Closest point on a rectangle's boundary |
Bounding box
| Function | Description |
|----------|-------------|
| getBoundingBox(points) | Tight bounding box for points, or null for an empty list |
License
MIT
