94 lines
2 KiB
TypeScript
94 lines
2 KiB
TypeScript
import { Bounds } from 'types'
|
|
import { pointInBounds } from './hitTests'
|
|
import { intersectPolygonBounds } from './intersections'
|
|
|
|
/**
|
|
* Get whether two bounds collide.
|
|
* @param a Bounds
|
|
* @param b Bounds
|
|
* @returns
|
|
*/
|
|
export function boundsCollide(a: Bounds, b: Bounds): boolean {
|
|
return !(
|
|
a.maxX < b.minX ||
|
|
a.minX > b.maxX ||
|
|
a.maxY < b.minY ||
|
|
a.minY > b.maxY
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Get whether the bounds of A contain the bounds of B. A perfect match will return true.
|
|
* @param a Bounds
|
|
* @param b Bounds
|
|
* @returns
|
|
*/
|
|
export function boundsContain(a: Bounds, b: Bounds): boolean {
|
|
return (
|
|
a.minX < b.minX && a.minY < b.minY && a.maxY > b.maxY && a.maxX > b.maxX
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Get whether the bounds of A are contained by the bounds of B.
|
|
* @param a Bounds
|
|
* @param b Bounds
|
|
* @returns
|
|
*/
|
|
export function boundsContained(a: Bounds, b: Bounds): boolean {
|
|
return boundsContain(b, a)
|
|
}
|
|
|
|
/**
|
|
* Get whether a set of points are all contained by a bounding box.
|
|
* @returns
|
|
*/
|
|
export function boundsContainPolygon(a: Bounds, points: number[][]): boolean {
|
|
return points.every((point) => pointInBounds(point, a))
|
|
}
|
|
|
|
/**
|
|
* Get whether a polygon collides a bounding box.
|
|
* @param points
|
|
* @param b
|
|
*/
|
|
export function boundsCollidePolygon(a: Bounds, points: number[][]): boolean {
|
|
return intersectPolygonBounds(points, a).length > 0
|
|
}
|
|
|
|
/**
|
|
* Get whether two bounds are identical.
|
|
* @param a Bounds
|
|
* @param b Bounds
|
|
* @returns
|
|
*/
|
|
export function boundsAreEqual(a: Bounds, b: Bounds): boolean {
|
|
return !(
|
|
b.maxX !== a.maxX ||
|
|
b.minX !== a.minX ||
|
|
b.maxY !== a.maxY ||
|
|
b.minY !== a.minY
|
|
)
|
|
}
|
|
|
|
export function getRotatedEllipseBounds(
|
|
x: number,
|
|
y: number,
|
|
rx: number,
|
|
ry: number,
|
|
rotation: number
|
|
): Bounds {
|
|
const c = Math.cos(rotation)
|
|
const s = Math.sin(rotation)
|
|
const w = Math.hypot(rx * c, ry * s)
|
|
const h = Math.hypot(rx * s, ry * c)
|
|
|
|
return {
|
|
minX: x + rx - w,
|
|
minY: y + ry - h,
|
|
maxX: x + rx + w,
|
|
maxY: y + ry + h,
|
|
width: w * 2,
|
|
height: h * 2,
|
|
}
|
|
}
|