diff --git a/packages/math/line.ts b/packages/math/line.ts index c646e04d46..6ff8a66eeb 100644 --- a/packages/math/line.ts +++ b/packages/math/line.ts @@ -1,5 +1,5 @@ import { pointCenter, pointRotateRads } from "./point"; -import type { GlobalPoint, Line, LocalPoint, Radians } from "./types"; +import type { GenericPoint, Line, Radians } from "./types"; /** * Create a line from two points. @@ -7,7 +7,7 @@ import type { GlobalPoint, Line, LocalPoint, Radians } from "./types"; * @param points The two points lying on the line * @returns The line on which the points lie */ -export function line

(a: P, b: P): Line

{ +export function line

(a: P, b: P): Line

{ return [a, b] as Line

; } @@ -17,7 +17,7 @@ export function line

(a: P, b: P): Line

{ * @param param0 The array with the two points to convert to a line * @returns The created line */ -export function lineFromPointPair

([a, b]: [ +export function lineFromPointPair

([a, b]: [ P, P, ]): Line

{ @@ -30,7 +30,7 @@ export function lineFromPointPair

([a, b]: [ * @param pointArray * @returns */ -export function lineFromPointArray

( +export function lineFromPointArray

( pointArray: P[], ): Line

| undefined { return pointArray.length === 2 @@ -40,7 +40,7 @@ export function lineFromPointArray

( // return the coordinates resulting from rotating the given line about an origin by an angle in degrees // note that when the origin is not given, the midpoint of the given line is used as the origin -export const lineRotate = ( +export const lineRotate = ( l: Line, angle: Radians, origin?: Point, diff --git a/packages/math/point.ts b/packages/math/point.ts index 659d3b8820..471eab8765 100644 --- a/packages/math/point.ts +++ b/packages/math/point.ts @@ -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( - x: number, - y: number, -): Point { +export function point(x: number, y: number): Point { return [x, y] as Point; } @@ -32,9 +20,9 @@ export function point( * @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( + numberArray: number[], +): Point | undefined { return numberArray.length === 2 ? 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( + 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

(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( + 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( + [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: 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: P[] -): P { +export function pointCenter

(...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

( * @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( + 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( + 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

(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

(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: 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: P, q: P, r: P, diff --git a/packages/math/polygon.ts b/packages/math/polygon.ts index f844ab8d0c..557dd78692 100644 --- a/packages/math/polygon.ts +++ b/packages/math/polygon.ts @@ -1,23 +1,17 @@ import { pointsEqual } from "./point"; import { lineSegment, pointOnLineSegment } from "./segment"; -import type { GlobalPoint, LocalPoint, Polygon, ViewportPoint } from "./types"; +import type { GenericPoint, Polygon } from "./types"; import { PRECISION } from "./utils"; -export function polygon( - ...points: Point[] -) { +export function polygon(...points: Point[]) { return polygonClose(points) as Polygon; } -export function polygonFromPoints< - Point extends GlobalPoint | LocalPoint | ViewportPoint, ->(points: Point[]) { +export function polygonFromPoints(points: Point[]) { return polygonClose(points) as Polygon; } -export const polygonIncludesPoint = < - Point extends LocalPoint | GlobalPoint | ViewportPoint, ->( +export const polygonIncludesPoint = ( point: Point, polygon: Polygon, ) => { @@ -42,9 +36,7 @@ export const polygonIncludesPoint = < return inside; }; -export const pointOnPolygon = < - Point extends LocalPoint | GlobalPoint | ViewportPoint, ->( +export const pointOnPolygon = ( p: Point, poly: Polygon, threshold = PRECISION, @@ -61,16 +53,12 @@ export const pointOnPolygon = < return on; }; -function polygonClose( - polygon: Point[], -) { +function polygonClose(polygon: Point[]) { return polygonIsClosed(polygon) ? polygon : ([...polygon, polygon[0]] as Polygon); } -function polygonIsClosed< - Point extends LocalPoint | GlobalPoint | ViewportPoint, ->(polygon: Point[]) { +function polygonIsClosed(polygon: Point[]) { return pointsEqual(polygon[0], polygon[polygon.length - 1]); } diff --git a/packages/math/segment.ts b/packages/math/segment.ts index 3f91a1deef..a3fa629b91 100644 --- a/packages/math/segment.ts +++ b/packages/math/segment.ts @@ -4,13 +4,7 @@ import { pointFromVector, pointRotateRads, } from "./point"; -import type { - GlobalPoint, - LineSegment, - LocalPoint, - Radians, - ViewportPoint, -} from "./types"; +import type { GenericPoint, LineSegment, Radians } from "./types"; import { PRECISION } from "./utils"; import { vectorAdd, @@ -26,14 +20,14 @@ import { * @param points The two points delimiting the line segment on each end * @returns The line segment delineated by the points */ -export function lineSegment

( +export function lineSegment

( a: P, b: P, ): LineSegment

{ return [a, b] as LineSegment

; } -export function lineSegmentFromPointArray

( +export function lineSegmentFromPointArray

( pointArray: P[], ): LineSegment

| undefined { return pointArray.length === 2 @@ -46,7 +40,7 @@ export function lineSegmentFromPointArray

( * @param segment * @returns */ -export const isLineSegment = ( +export const isLineSegment = ( segment: unknown, ): segment is LineSegment => Array.isArray(segment) && @@ -63,9 +57,7 @@ export const isLineSegment = ( * @param origin * @returns */ -export const lineSegmentRotate = < - Point extends LocalPoint | GlobalPoint | ViewportPoint, ->( +export const lineSegmentRotate = ( l: LineSegment, angle: Radians, origin?: Point, @@ -80,9 +72,7 @@ export const lineSegmentRotate = < * Calculates the point two line segments with a definite start and end point * intersect at. */ -export const segmentsIntersectAt = < - Point extends GlobalPoint | LocalPoint | ViewportPoint, ->( +export const segmentsIntersectAt = ( a: Readonly>, b: Readonly>, ): Point | null => { @@ -115,9 +105,7 @@ export const segmentsIntersectAt = < return null; }; -export const pointOnLineSegment = < - Point extends LocalPoint | GlobalPoint | ViewportPoint, ->( +export const pointOnLineSegment = ( point: Point, line: LineSegment, threshold = PRECISION, @@ -131,9 +119,7 @@ export const pointOnLineSegment = < return distance < threshold; }; -export const distanceToLineSegment = < - Point extends LocalPoint | GlobalPoint | ViewportPoint, ->( +export const distanceToLineSegment = ( point: Point, line: LineSegment, ) => { diff --git a/packages/math/triangle.ts b/packages/math/triangle.ts index bc74372b7c..8e6144e83a 100644 --- a/packages/math/triangle.ts +++ b/packages/math/triangle.ts @@ -1,4 +1,4 @@ -import type { GlobalPoint, LocalPoint, Triangle } from "./types"; +import type { GenericPoint, Triangle } from "./types"; // Types @@ -11,7 +11,7 @@ import type { GlobalPoint, LocalPoint, Triangle } from "./types"; * @param p The point to test whether is in the triangle * @returns TRUE if the point is inside of the triangle */ -export function triangleIncludesPoint

( +export function triangleIncludesPoint

( [a, b, c]: Triangle

, p: P, ): boolean { diff --git a/packages/math/types.ts b/packages/math/types.ts index 107cfd2c62..6f84c9c7b3 100644 --- a/packages/math/types.ts +++ b/packages/math/types.ts @@ -58,7 +58,7 @@ export type GenericPoint = GlobalPoint | LocalPoint | ViewportPoint; /** * A line is an infinitely long object with no width, depth, or curvature. */ -export type Line

= [p: P, q: P] & { +export type Line

= [p: P, q: P] & { _brand: "excalimath_line"; }; @@ -67,10 +67,7 @@ export type Line

= [p: P, q: P] & { * line that is bounded by two distinct end points, and * contains every point on the line that is between its endpoints. */ -export type LineSegment

= [ - a: P, - b: P, -] & { +export type LineSegment

= [a: P, b: P] & { _brand: "excalimath_linesegment"; }; diff --git a/packages/math/vector.ts b/packages/math/vector.ts index 94849d0e38..d987ea1b00 100644 --- a/packages/math/vector.ts +++ b/packages/math/vector.ts @@ -1,4 +1,4 @@ -import type { GlobalPoint, LocalPoint, Vector, ViewportPoint } from "./types"; +import type { GenericPoint, Vector } from "./types"; /** * Create a vector from the x and y coordiante elements. @@ -23,9 +23,10 @@ export function vector( * @param origin The origin point in a given coordiante system * @returns The created vector from the point and the origin */ -export function vectorFromPoint< - Point extends GlobalPoint | LocalPoint | ViewportPoint, ->(p: Point, origin: Point = [0, 0] as Point): Vector { +export function vectorFromPoint( + p: Point, + origin: Point = [0, 0] as Point, +): Vector { return vector(p[0] - origin[0], p[1] - origin[1]); }