tldraw/utils/hitTests.ts

49 lines
1 KiB
TypeScript
Raw Normal View History

import { Bounds } from 'types'
import vec from './vec'
2021-05-14 12:44:23 +00:00
/**
* Get whether a point is inside of a bounds.
* @param A
* @param b
* @returns
*/
2021-06-21 21:35:28 +00:00
export function pointInBounds(A: number[], b: Bounds): boolean {
2021-05-14 12:44:23 +00:00
return !(A[0] < b.minX || A[0] > b.maxX || A[1] < b.minY || A[1] > b.maxY)
}
/**
* Get whether a point is inside of a circle.
* @param A
* @param b
* @returns
*/
2021-06-21 21:35:28 +00:00
export function pointInCircle(A: number[], C: number[], r: number): boolean {
2021-05-14 12:44:23 +00:00
return vec.dist(A, C) <= r
}
/**
* Get whether a point is inside of an ellipse.
* @param point
* @param center
* @param rx
* @param ry
* @param rotation
* @returns
*/
export function pointInEllipse(
A: number[],
C: number[],
rx: number,
ry: number,
rotation = 0
2021-06-21 21:35:28 +00:00
): boolean {
2021-05-14 12:44:23 +00:00
rotation = rotation || 0
const cos = Math.cos(rotation)
const sin = Math.sin(rotation)
const delta = vec.sub(A, C)
const tdx = cos * delta[0] + sin * delta[1]
const tdy = sin * delta[0] - cos * delta[1]
return (tdx * tdx) / (rx * rx) + (tdy * tdy) / (ry * ry) <= 1
}