Unify on GenericPoint type

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
This commit is contained in:
Mark Tolmacs 2024-09-24 18:38:29 +02:00
parent 0c47bd0004
commit 017047d15e
No known key found for this signature in database
7 changed files with 68 additions and 115 deletions

View file

@ -1,14 +1,5 @@
import { degreesToRadians } from "./angle";
import type {
LocalPoint,
GlobalPoint,
Radians,
Degrees,
Vector,
ViewportPoint,
GenericPoint,
Extent,
} from "./types";
import type { Radians, Degrees, Vector, GenericPoint, Extent } from "./types";
import { PRECISION } from "./utils";
import { vectorFromPoint, vectorScale } from "./vector";
@ -19,10 +10,7 @@ import { vectorFromPoint, vectorScale } from "./vector";
* @param y The Y coordinate
* @returns The branded and created point
*/
export function point<Point extends GlobalPoint | LocalPoint | ViewportPoint>(
x: number,
y: number,
): Point {
export function point<Point extends GenericPoint>(x: number, y: number): Point {
return [x, y] as Point;
}
@ -32,9 +20,9 @@ export function point<Point extends GlobalPoint | LocalPoint | ViewportPoint>(
* @param numberArray The number array to check and to convert to Point
* @returns The point instance
*/
export function pointFromArray<
Point extends GlobalPoint | LocalPoint | ViewportPoint,
>(numberArray: number[]): Point | undefined {
export function pointFromArray<Point extends GenericPoint>(
numberArray: number[],
): Point | undefined {
return numberArray.length === 2
? point<Point>(numberArray[0], numberArray[1])
: undefined;
@ -46,9 +34,9 @@ export function pointFromArray<
* @param pair A number pair to convert to Point
* @returns The point instance
*/
export function pointFromPair<
Point extends GlobalPoint | LocalPoint | ViewportPoint,
>(pair: [number, number]): Point {
export function pointFromPair<Point extends GenericPoint>(
pair: [number, number],
): Point {
return pair as Point;
}
@ -58,9 +46,7 @@ export function pointFromPair<
* @param v The vector to convert
* @returns The point the vector points at with origin 0,0
*/
export function pointFromVector<
P extends GlobalPoint | LocalPoint | ViewportPoint,
>(v: Vector): P {
export function pointFromVector<P extends GenericPoint>(v: Vector): P {
return v as unknown as P;
}
@ -70,9 +56,7 @@ export function pointFromVector<
* @param p The value to attempt verification on
* @returns TRUE if the provided value has the shape of a local or global point
*/
export function isPoint(
p: unknown,
): p is LocalPoint | GlobalPoint | ViewportPoint {
export function isPoint(p: unknown): p is GenericPoint {
return (
Array.isArray(p) &&
p.length === 2 &&
@ -91,9 +75,10 @@ export function isPoint(
* @param b Point The second point to compare
* @returns TRUE if the points are sufficiently close to each other
*/
export function pointsEqual<
Point extends GlobalPoint | LocalPoint | ViewportPoint,
>(a: Point, b: Point): boolean {
export function pointsEqual<Point extends GenericPoint>(
a: Point,
b: Point,
): boolean {
const abs = Math.abs;
return abs(a[0] - b[0]) < PRECISION && abs(a[1] - b[1]) < PRECISION;
}
@ -106,9 +91,11 @@ export function pointsEqual<
* @param angle The radians to rotate the point by
* @returns The rotated point
*/
export function pointRotateRads<
Point extends GlobalPoint | LocalPoint | ViewportPoint,
>([x, y]: Point, [cx, cy]: Point, angle: Radians): Point {
export function pointRotateRads<Point extends GenericPoint>(
[x, y]: Point,
[cx, cy]: Point,
angle: Radians,
): Point {
return point(
(x - cx) * Math.cos(angle) - (y - cy) * Math.sin(angle) + cx,
(x - cx) * Math.sin(angle) + (y - cy) * Math.cos(angle) + cy,
@ -123,9 +110,11 @@ export function pointRotateRads<
* @param angle The degree to rotate the point by
* @returns The rotated point
*/
export function pointRotateDegs<
Point extends GlobalPoint | LocalPoint | ViewportPoint,
>(point: Point, center: Point, angle: Degrees): Point {
export function pointRotateDegs<Point extends GenericPoint>(
point: Point,
center: Point,
angle: Degrees,
): Point {
return pointRotateRads(point, center, degreesToRadians(angle));
}
@ -143,8 +132,8 @@ export function pointRotateDegs<
*/
// TODO 99% of use is translating between global and local coords, which need to be formalized
export function pointTranslate<
From extends GlobalPoint | LocalPoint | ViewportPoint,
To extends GlobalPoint | LocalPoint | ViewportPoint,
From extends GenericPoint,
To extends GenericPoint,
>(p: From, v: Vector = [0, 0] as Vector): To {
return point(p[0] + v[0], p[1] + v[1]);
}
@ -156,9 +145,7 @@ export function pointTranslate<
* @param b The other point to create the middle point for
* @returns The middle point
*/
export function pointCenter<P extends LocalPoint | GlobalPoint | ViewportPoint>(
...p: P[]
): P {
export function pointCenter<P extends GenericPoint>(...p: P[]): P {
return pointFromPair(
p
.reduce((mid, x) => [mid[0] + x[0], mid[1] + x[1]], [0, 0])
@ -174,9 +161,10 @@ export function pointCenter<P extends LocalPoint | GlobalPoint | ViewportPoint>(
* @param b The other point to act like the vector to translate by
* @returns
*/
export function pointAdd<
Point extends LocalPoint | GlobalPoint | ViewportPoint,
>(a: Point, b: Point): Point {
export function pointAdd<Point extends GenericPoint>(
a: Point,
b: Point,
): Point {
return point(a[0] + b[0], a[1] + b[1]);
}
@ -188,9 +176,10 @@ export function pointAdd<
* @param b The point which will act like a vector
* @returns The resulting point
*/
export function pointSubtract<
Point extends LocalPoint | GlobalPoint | ViewportPoint,
>(a: Point, b: Point): Point {
export function pointSubtract<Point extends GenericPoint>(
a: Point,
b: Point,
): Point {
return point(a[0] - b[0], a[1] - b[1]);
}
@ -201,9 +190,7 @@ export function pointSubtract<
* @param b Second point
* @returns The euclidean distance between the two points.
*/
export function pointDistance<
P extends LocalPoint | GlobalPoint | ViewportPoint,
>(a: P, b: P): number {
export function pointDistance<P extends GenericPoint>(a: P, b: P): number {
return Math.hypot(b[0] - a[0], b[1] - a[1]);
}
@ -216,9 +203,7 @@ export function pointDistance<
* @param b Second point
* @returns The euclidean distance between the two points.
*/
export function pointDistanceSq<
P extends LocalPoint | GlobalPoint | ViewportPoint,
>(a: P, b: P): number {
export function pointDistanceSq<P extends GenericPoint>(a: P, b: P): number {
return Math.hypot(b[0] - a[0], b[1] - a[1]);
}
@ -230,9 +215,7 @@ export function pointDistanceSq<
* @param multiplier The scaling factor
* @returns
*/
export const pointScaleFromOrigin = <
P extends GlobalPoint | LocalPoint | ViewportPoint,
>(
export const pointScaleFromOrigin = <P extends GenericPoint>(
p: P,
mid: P,
multiplier: number,
@ -247,9 +230,7 @@ export const pointScaleFromOrigin = <
* @param r The other point to compare against
* @returns TRUE if q is indeed between p and r
*/
export const isPointWithinBounds = <
P extends GlobalPoint | LocalPoint | ViewportPoint,
>(
export const isPointWithinBounds = <P extends GenericPoint>(
p: P,
q: P,
r: P,