Refactoring points

Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
This commit is contained in:
Mark Tolmacs 2024-09-20 21:32:32 +02:00
parent 8ca4cf3260
commit b4cb314090
No known key found for this signature in database
40 changed files with 746 additions and 783 deletions

View file

@ -4,7 +4,13 @@ import {
pointFromVector,
pointRotateRads,
} from "./point";
import type { GlobalPoint, LineSegment, LocalPoint, Radians } from "./types";
import type {
GlobalPoint,
LineSegment,
LocalPoint,
Radians,
ViewportPoint,
} from "./types";
import { PRECISION } from "./utils";
import {
vectorAdd,
@ -20,7 +26,7 @@ import {
* @param points The two points delimiting the line segment on each end
* @returns The line segment delineated by the points
*/
export function lineSegment<P extends GlobalPoint | LocalPoint>(
export function lineSegment<P extends GlobalPoint | LocalPoint | ViewportPoint>(
a: P,
b: P,
): LineSegment<P> {
@ -57,7 +63,9 @@ export const isLineSegment = <Point extends GlobalPoint | LocalPoint>(
* @param origin
* @returns
*/
export const lineSegmentRotate = <Point extends LocalPoint | GlobalPoint>(
export const lineSegmentRotate = <
Point extends LocalPoint | GlobalPoint | ViewportPoint,
>(
l: LineSegment<Point>,
angle: Radians,
origin?: Point,
@ -72,7 +80,9 @@ export const lineSegmentRotate = <Point extends LocalPoint | GlobalPoint>(
* Calculates the point two line segments with a definite start and end point
* intersect at.
*/
export const segmentsIntersectAt = <Point extends GlobalPoint | LocalPoint>(
export const segmentsIntersectAt = <
Point extends GlobalPoint | LocalPoint | ViewportPoint,
>(
a: Readonly<LineSegment<Point>>,
b: Readonly<LineSegment<Point>>,
): Point | null => {
@ -105,7 +115,9 @@ export const segmentsIntersectAt = <Point extends GlobalPoint | LocalPoint>(
return null;
};
export const pointOnLineSegment = <Point extends LocalPoint | GlobalPoint>(
export const pointOnLineSegment = <
Point extends LocalPoint | GlobalPoint | ViewportPoint,
>(
point: Point,
line: LineSegment<Point>,
threshold = PRECISION,
@ -119,7 +131,9 @@ export const pointOnLineSegment = <Point extends LocalPoint | GlobalPoint>(
return distance < threshold;
};
export const distanceToLineSegment = <Point extends LocalPoint | GlobalPoint>(
export const distanceToLineSegment = <
Point extends LocalPoint | GlobalPoint | ViewportPoint,
>(
point: Point,
line: LineSegment<Point>,
) => {