min-rect
v0.1.1
Published
compute the minimum bounding box
Downloads
286
Readme
min-rect
Computes the minimum-area oriented bounding rectangle (2D) using the rotating calipers algorithm.
Install
npm install min-rectUsage
import { MBR, Vector2 } from 'min-rect'
const points = [
[0, 0],
[1, 0],
[0, 1],
[1, 1],
].map(([x, y]) => new Vector2(x, y))
const mbr = new MBR()
mbr.fromPoints(points)
console.log(mbr)
// {
// halfSize: Vector2 { x: 0.5, y: 0.5 },
// center: Vector2 { x: 0.5, y: 0.5 },
// rotation: 0
// }API
new MBR()
The minimum bounding rectangle instance. Call fromPoints() to compute.
| Property | Type | Description |
|----------|------|-------------|
| halfSize | Vector2 | Half width and half height of the rectangle |
| center | Vector2 | Geometric center of the rectangle |
| rotation | number | Orientation in radians |
| Method | Description |
|--------|-------------|
| fromPoints(points: Vector2[]) | Compute the MBR from a set of 2D points |
| grahamScan(points: Vector2[]) | Compute the convex hull (Graham scan) |
| rotatingCalipers(hull: Vector2[]) | Find the minimum-area rectangle for a convex hull |
new Vector2(x, y)
2D vector with dot, cross, sub, add, scale, normalize, angle, length, set, copyFrom, setFromSub.
Utility functions
getProj(point, edge), getMinMaxProj(points, edge), getCenter(points) — exported for convenience.
