34 lines
970 B
TypeScript
34 lines
970 B
TypeScript
import type { Point } from './types';
|
|
|
|
export interface Bounds {
|
|
minX: number;
|
|
maxX: number;
|
|
minY: number;
|
|
maxY: number;
|
|
}
|
|
export interface Rect {
|
|
x: number;
|
|
y: number;
|
|
width: number;
|
|
height: number;
|
|
}
|
|
export const bounds = (points: Point[]): Bounds => ({
|
|
minX: Math.min(...points.map((point) => point.x)),
|
|
maxX: Math.max(...points.map((point) => point.x)),
|
|
minY: Math.min(...points.map((point) => point.y)),
|
|
maxY: Math.max(...points.map((point) => point.y)),
|
|
});
|
|
export const rectBounds = (box: Rect): Bounds => ({
|
|
minX: box.x,
|
|
minY: box.y,
|
|
maxX: box.x + box.width,
|
|
maxY: box.y + box.height,
|
|
});
|
|
export const overlaps = (a: Bounds, b: Bounds, gap = 0) =>
|
|
a.minX < b.maxX + gap && a.maxX > b.minX - gap && a.minY < b.maxY + gap && a.maxY > b.minY - gap;
|
|
export const corners = (box: Bounds): Point[] => [
|
|
{ x: box.minX, y: box.minY },
|
|
{ x: box.maxX, y: box.minY },
|
|
{ x: box.maxX, y: box.maxY },
|
|
{ x: box.minX, y: box.maxY },
|
|
];
|