This commit is contained in:
parent
171fbf1927
commit
8bf3e3b705
40 changed files with 1443 additions and 1497 deletions
34
src/lib/drawing-geometry.ts
Normal file
34
src/lib/drawing-geometry.ts
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
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 },
|
||||
];
|
||||
Loading…
Reference in a new issue