mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
Unify on GenericPoint type
Signed-off-by: Mark Tolmacs <mark@lazycat.hu>
This commit is contained in:
parent
0c47bd0004
commit
017047d15e
7 changed files with 68 additions and 115 deletions
|
@ -1,5 +1,5 @@
|
||||||
import { pointCenter, pointRotateRads } from "./point";
|
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.
|
* 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
|
* @param points The two points lying on the line
|
||||||
* @returns The line on which the points lie
|
* @returns The line on which the points lie
|
||||||
*/
|
*/
|
||||||
export function line<P extends GlobalPoint | LocalPoint>(a: P, b: P): Line<P> {
|
export function line<P extends GenericPoint>(a: P, b: P): Line<P> {
|
||||||
return [a, b] as Line<P>;
|
return [a, b] as Line<P>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ export function line<P extends GlobalPoint | LocalPoint>(a: P, b: P): Line<P> {
|
||||||
* @param param0 The array with the two points to convert to a line
|
* @param param0 The array with the two points to convert to a line
|
||||||
* @returns The created line
|
* @returns The created line
|
||||||
*/
|
*/
|
||||||
export function lineFromPointPair<P extends GlobalPoint | LocalPoint>([a, b]: [
|
export function lineFromPointPair<P extends GenericPoint>([a, b]: [
|
||||||
P,
|
P,
|
||||||
P,
|
P,
|
||||||
]): Line<P> {
|
]): Line<P> {
|
||||||
|
@ -30,7 +30,7 @@ export function lineFromPointPair<P extends GlobalPoint | LocalPoint>([a, b]: [
|
||||||
* @param pointArray
|
* @param pointArray
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export function lineFromPointArray<P extends GlobalPoint | LocalPoint>(
|
export function lineFromPointArray<P extends GenericPoint>(
|
||||||
pointArray: P[],
|
pointArray: P[],
|
||||||
): Line<P> | undefined {
|
): Line<P> | undefined {
|
||||||
return pointArray.length === 2
|
return pointArray.length === 2
|
||||||
|
@ -40,7 +40,7 @@ export function lineFromPointArray<P extends GlobalPoint | LocalPoint>(
|
||||||
|
|
||||||
// return the coordinates resulting from rotating the given line about an origin by an angle in degrees
|
// 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
|
// note that when the origin is not given, the midpoint of the given line is used as the origin
|
||||||
export const lineRotate = <Point extends LocalPoint | GlobalPoint>(
|
export const lineRotate = <Point extends GenericPoint>(
|
||||||
l: Line<Point>,
|
l: Line<Point>,
|
||||||
angle: Radians,
|
angle: Radians,
|
||||||
origin?: Point,
|
origin?: Point,
|
||||||
|
|
|
@ -1,14 +1,5 @@
|
||||||
import { degreesToRadians } from "./angle";
|
import { degreesToRadians } from "./angle";
|
||||||
import type {
|
import type { Radians, Degrees, Vector, GenericPoint, Extent } from "./types";
|
||||||
LocalPoint,
|
|
||||||
GlobalPoint,
|
|
||||||
Radians,
|
|
||||||
Degrees,
|
|
||||||
Vector,
|
|
||||||
ViewportPoint,
|
|
||||||
GenericPoint,
|
|
||||||
Extent,
|
|
||||||
} from "./types";
|
|
||||||
import { PRECISION } from "./utils";
|
import { PRECISION } from "./utils";
|
||||||
import { vectorFromPoint, vectorScale } from "./vector";
|
import { vectorFromPoint, vectorScale } from "./vector";
|
||||||
|
|
||||||
|
@ -19,10 +10,7 @@ import { vectorFromPoint, vectorScale } from "./vector";
|
||||||
* @param y The Y coordinate
|
* @param y The Y coordinate
|
||||||
* @returns The branded and created point
|
* @returns The branded and created point
|
||||||
*/
|
*/
|
||||||
export function point<Point extends GlobalPoint | LocalPoint | ViewportPoint>(
|
export function point<Point extends GenericPoint>(x: number, y: number): Point {
|
||||||
x: number,
|
|
||||||
y: number,
|
|
||||||
): Point {
|
|
||||||
return [x, y] as 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
|
* @param numberArray The number array to check and to convert to Point
|
||||||
* @returns The point instance
|
* @returns The point instance
|
||||||
*/
|
*/
|
||||||
export function pointFromArray<
|
export function pointFromArray<Point extends GenericPoint>(
|
||||||
Point extends GlobalPoint | LocalPoint | ViewportPoint,
|
numberArray: number[],
|
||||||
>(numberArray: number[]): Point | undefined {
|
): Point | undefined {
|
||||||
return numberArray.length === 2
|
return numberArray.length === 2
|
||||||
? point<Point>(numberArray[0], numberArray[1])
|
? point<Point>(numberArray[0], numberArray[1])
|
||||||
: undefined;
|
: undefined;
|
||||||
|
@ -46,9 +34,9 @@ export function pointFromArray<
|
||||||
* @param pair A number pair to convert to Point
|
* @param pair A number pair to convert to Point
|
||||||
* @returns The point instance
|
* @returns The point instance
|
||||||
*/
|
*/
|
||||||
export function pointFromPair<
|
export function pointFromPair<Point extends GenericPoint>(
|
||||||
Point extends GlobalPoint | LocalPoint | ViewportPoint,
|
pair: [number, number],
|
||||||
>(pair: [number, number]): Point {
|
): Point {
|
||||||
return pair as Point;
|
return pair as Point;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,9 +46,7 @@ export function pointFromPair<
|
||||||
* @param v The vector to convert
|
* @param v The vector to convert
|
||||||
* @returns The point the vector points at with origin 0,0
|
* @returns The point the vector points at with origin 0,0
|
||||||
*/
|
*/
|
||||||
export function pointFromVector<
|
export function pointFromVector<P extends GenericPoint>(v: Vector): P {
|
||||||
P extends GlobalPoint | LocalPoint | ViewportPoint,
|
|
||||||
>(v: Vector): P {
|
|
||||||
return v as unknown as P;
|
return v as unknown as P;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,9 +56,7 @@ export function pointFromVector<
|
||||||
* @param p The value to attempt verification on
|
* @param p The value to attempt verification on
|
||||||
* @returns TRUE if the provided value has the shape of a local or global point
|
* @returns TRUE if the provided value has the shape of a local or global point
|
||||||
*/
|
*/
|
||||||
export function isPoint(
|
export function isPoint(p: unknown): p is GenericPoint {
|
||||||
p: unknown,
|
|
||||||
): p is LocalPoint | GlobalPoint | ViewportPoint {
|
|
||||||
return (
|
return (
|
||||||
Array.isArray(p) &&
|
Array.isArray(p) &&
|
||||||
p.length === 2 &&
|
p.length === 2 &&
|
||||||
|
@ -91,9 +75,10 @@ export function isPoint(
|
||||||
* @param b Point The second point to compare
|
* @param b Point The second point to compare
|
||||||
* @returns TRUE if the points are sufficiently close to each other
|
* @returns TRUE if the points are sufficiently close to each other
|
||||||
*/
|
*/
|
||||||
export function pointsEqual<
|
export function pointsEqual<Point extends GenericPoint>(
|
||||||
Point extends GlobalPoint | LocalPoint | ViewportPoint,
|
a: Point,
|
||||||
>(a: Point, b: Point): boolean {
|
b: Point,
|
||||||
|
): boolean {
|
||||||
const abs = Math.abs;
|
const abs = Math.abs;
|
||||||
return abs(a[0] - b[0]) < PRECISION && abs(a[1] - b[1]) < PRECISION;
|
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
|
* @param angle The radians to rotate the point by
|
||||||
* @returns The rotated point
|
* @returns The rotated point
|
||||||
*/
|
*/
|
||||||
export function pointRotateRads<
|
export function pointRotateRads<Point extends GenericPoint>(
|
||||||
Point extends GlobalPoint | LocalPoint | ViewportPoint,
|
[x, y]: Point,
|
||||||
>([x, y]: Point, [cx, cy]: Point, angle: Radians): Point {
|
[cx, cy]: Point,
|
||||||
|
angle: Radians,
|
||||||
|
): Point {
|
||||||
return point(
|
return point(
|
||||||
(x - cx) * Math.cos(angle) - (y - cy) * Math.sin(angle) + cx,
|
(x - cx) * Math.cos(angle) - (y - cy) * Math.sin(angle) + cx,
|
||||||
(x - cx) * Math.sin(angle) + (y - cy) * Math.cos(angle) + cy,
|
(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
|
* @param angle The degree to rotate the point by
|
||||||
* @returns The rotated point
|
* @returns The rotated point
|
||||||
*/
|
*/
|
||||||
export function pointRotateDegs<
|
export function pointRotateDegs<Point extends GenericPoint>(
|
||||||
Point extends GlobalPoint | LocalPoint | ViewportPoint,
|
point: Point,
|
||||||
>(point: Point, center: Point, angle: Degrees): Point {
|
center: Point,
|
||||||
|
angle: Degrees,
|
||||||
|
): Point {
|
||||||
return pointRotateRads(point, center, degreesToRadians(angle));
|
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
|
// TODO 99% of use is translating between global and local coords, which need to be formalized
|
||||||
export function pointTranslate<
|
export function pointTranslate<
|
||||||
From extends GlobalPoint | LocalPoint | ViewportPoint,
|
From extends GenericPoint,
|
||||||
To extends GlobalPoint | LocalPoint | ViewportPoint,
|
To extends GenericPoint,
|
||||||
>(p: From, v: Vector = [0, 0] as Vector): To {
|
>(p: From, v: Vector = [0, 0] as Vector): To {
|
||||||
return point(p[0] + v[0], p[1] + v[1]);
|
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
|
* @param b The other point to create the middle point for
|
||||||
* @returns The middle point
|
* @returns The middle point
|
||||||
*/
|
*/
|
||||||
export function pointCenter<P extends LocalPoint | GlobalPoint | ViewportPoint>(
|
export function pointCenter<P extends GenericPoint>(...p: P[]): P {
|
||||||
...p: P[]
|
|
||||||
): P {
|
|
||||||
return pointFromPair(
|
return pointFromPair(
|
||||||
p
|
p
|
||||||
.reduce((mid, x) => [mid[0] + x[0], mid[1] + x[1]], [0, 0])
|
.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
|
* @param b The other point to act like the vector to translate by
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export function pointAdd<
|
export function pointAdd<Point extends GenericPoint>(
|
||||||
Point extends LocalPoint | GlobalPoint | ViewportPoint,
|
a: Point,
|
||||||
>(a: Point, b: Point): Point {
|
b: Point,
|
||||||
|
): Point {
|
||||||
return point(a[0] + b[0], a[1] + b[1]);
|
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
|
* @param b The point which will act like a vector
|
||||||
* @returns The resulting point
|
* @returns The resulting point
|
||||||
*/
|
*/
|
||||||
export function pointSubtract<
|
export function pointSubtract<Point extends GenericPoint>(
|
||||||
Point extends LocalPoint | GlobalPoint | ViewportPoint,
|
a: Point,
|
||||||
>(a: Point, b: Point): Point {
|
b: Point,
|
||||||
|
): Point {
|
||||||
return point(a[0] - b[0], a[1] - b[1]);
|
return point(a[0] - b[0], a[1] - b[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -201,9 +190,7 @@ export function pointSubtract<
|
||||||
* @param b Second point
|
* @param b Second point
|
||||||
* @returns The euclidean distance between the two points.
|
* @returns The euclidean distance between the two points.
|
||||||
*/
|
*/
|
||||||
export function pointDistance<
|
export function pointDistance<P extends GenericPoint>(a: P, b: P): number {
|
||||||
P extends LocalPoint | GlobalPoint | ViewportPoint,
|
|
||||||
>(a: P, b: P): number {
|
|
||||||
return Math.hypot(b[0] - a[0], b[1] - a[1]);
|
return Math.hypot(b[0] - a[0], b[1] - a[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -216,9 +203,7 @@ export function pointDistance<
|
||||||
* @param b Second point
|
* @param b Second point
|
||||||
* @returns The euclidean distance between the two points.
|
* @returns The euclidean distance between the two points.
|
||||||
*/
|
*/
|
||||||
export function pointDistanceSq<
|
export function pointDistanceSq<P extends GenericPoint>(a: P, b: P): number {
|
||||||
P extends LocalPoint | GlobalPoint | ViewportPoint,
|
|
||||||
>(a: P, b: P): number {
|
|
||||||
return Math.hypot(b[0] - a[0], b[1] - a[1]);
|
return Math.hypot(b[0] - a[0], b[1] - a[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -230,9 +215,7 @@ export function pointDistanceSq<
|
||||||
* @param multiplier The scaling factor
|
* @param multiplier The scaling factor
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const pointScaleFromOrigin = <
|
export const pointScaleFromOrigin = <P extends GenericPoint>(
|
||||||
P extends GlobalPoint | LocalPoint | ViewportPoint,
|
|
||||||
>(
|
|
||||||
p: P,
|
p: P,
|
||||||
mid: P,
|
mid: P,
|
||||||
multiplier: number,
|
multiplier: number,
|
||||||
|
@ -247,9 +230,7 @@ export const pointScaleFromOrigin = <
|
||||||
* @param r The other point to compare against
|
* @param r The other point to compare against
|
||||||
* @returns TRUE if q is indeed between p and r
|
* @returns TRUE if q is indeed between p and r
|
||||||
*/
|
*/
|
||||||
export const isPointWithinBounds = <
|
export const isPointWithinBounds = <P extends GenericPoint>(
|
||||||
P extends GlobalPoint | LocalPoint | ViewportPoint,
|
|
||||||
>(
|
|
||||||
p: P,
|
p: P,
|
||||||
q: P,
|
q: P,
|
||||||
r: P,
|
r: P,
|
||||||
|
|
|
@ -1,23 +1,17 @@
|
||||||
import { pointsEqual } from "./point";
|
import { pointsEqual } from "./point";
|
||||||
import { lineSegment, pointOnLineSegment } from "./segment";
|
import { lineSegment, pointOnLineSegment } from "./segment";
|
||||||
import type { GlobalPoint, LocalPoint, Polygon, ViewportPoint } from "./types";
|
import type { GenericPoint, Polygon } from "./types";
|
||||||
import { PRECISION } from "./utils";
|
import { PRECISION } from "./utils";
|
||||||
|
|
||||||
export function polygon<Point extends GlobalPoint | LocalPoint>(
|
export function polygon<Point extends GenericPoint>(...points: Point[]) {
|
||||||
...points: Point[]
|
|
||||||
) {
|
|
||||||
return polygonClose(points) as Polygon<Point>;
|
return polygonClose(points) as Polygon<Point>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function polygonFromPoints<
|
export function polygonFromPoints<Point extends GenericPoint>(points: Point[]) {
|
||||||
Point extends GlobalPoint | LocalPoint | ViewportPoint,
|
|
||||||
>(points: Point[]) {
|
|
||||||
return polygonClose(points) as Polygon<Point>;
|
return polygonClose(points) as Polygon<Point>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const polygonIncludesPoint = <
|
export const polygonIncludesPoint = <Point extends GenericPoint>(
|
||||||
Point extends LocalPoint | GlobalPoint | ViewportPoint,
|
|
||||||
>(
|
|
||||||
point: Point,
|
point: Point,
|
||||||
polygon: Polygon<Point>,
|
polygon: Polygon<Point>,
|
||||||
) => {
|
) => {
|
||||||
|
@ -42,9 +36,7 @@ export const polygonIncludesPoint = <
|
||||||
return inside;
|
return inside;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const pointOnPolygon = <
|
export const pointOnPolygon = <Point extends GenericPoint>(
|
||||||
Point extends LocalPoint | GlobalPoint | ViewportPoint,
|
|
||||||
>(
|
|
||||||
p: Point,
|
p: Point,
|
||||||
poly: Polygon<Point>,
|
poly: Polygon<Point>,
|
||||||
threshold = PRECISION,
|
threshold = PRECISION,
|
||||||
|
@ -61,16 +53,12 @@ export const pointOnPolygon = <
|
||||||
return on;
|
return on;
|
||||||
};
|
};
|
||||||
|
|
||||||
function polygonClose<Point extends LocalPoint | GlobalPoint | ViewportPoint>(
|
function polygonClose<Point extends GenericPoint>(polygon: Point[]) {
|
||||||
polygon: Point[],
|
|
||||||
) {
|
|
||||||
return polygonIsClosed(polygon)
|
return polygonIsClosed(polygon)
|
||||||
? polygon
|
? polygon
|
||||||
: ([...polygon, polygon[0]] as Polygon<Point>);
|
: ([...polygon, polygon[0]] as Polygon<Point>);
|
||||||
}
|
}
|
||||||
|
|
||||||
function polygonIsClosed<
|
function polygonIsClosed<Point extends GenericPoint>(polygon: Point[]) {
|
||||||
Point extends LocalPoint | GlobalPoint | ViewportPoint,
|
|
||||||
>(polygon: Point[]) {
|
|
||||||
return pointsEqual(polygon[0], polygon[polygon.length - 1]);
|
return pointsEqual(polygon[0], polygon[polygon.length - 1]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,13 +4,7 @@ import {
|
||||||
pointFromVector,
|
pointFromVector,
|
||||||
pointRotateRads,
|
pointRotateRads,
|
||||||
} from "./point";
|
} from "./point";
|
||||||
import type {
|
import type { GenericPoint, LineSegment, Radians } from "./types";
|
||||||
GlobalPoint,
|
|
||||||
LineSegment,
|
|
||||||
LocalPoint,
|
|
||||||
Radians,
|
|
||||||
ViewportPoint,
|
|
||||||
} from "./types";
|
|
||||||
import { PRECISION } from "./utils";
|
import { PRECISION } from "./utils";
|
||||||
import {
|
import {
|
||||||
vectorAdd,
|
vectorAdd,
|
||||||
|
@ -26,14 +20,14 @@ import {
|
||||||
* @param points The two points delimiting the line segment on each end
|
* @param points The two points delimiting the line segment on each end
|
||||||
* @returns The line segment delineated by the points
|
* @returns The line segment delineated by the points
|
||||||
*/
|
*/
|
||||||
export function lineSegment<P extends GlobalPoint | LocalPoint | ViewportPoint>(
|
export function lineSegment<P extends GenericPoint>(
|
||||||
a: P,
|
a: P,
|
||||||
b: P,
|
b: P,
|
||||||
): LineSegment<P> {
|
): LineSegment<P> {
|
||||||
return [a, b] as LineSegment<P>;
|
return [a, b] as LineSegment<P>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function lineSegmentFromPointArray<P extends GlobalPoint | LocalPoint>(
|
export function lineSegmentFromPointArray<P extends GenericPoint>(
|
||||||
pointArray: P[],
|
pointArray: P[],
|
||||||
): LineSegment<P> | undefined {
|
): LineSegment<P> | undefined {
|
||||||
return pointArray.length === 2
|
return pointArray.length === 2
|
||||||
|
@ -46,7 +40,7 @@ export function lineSegmentFromPointArray<P extends GlobalPoint | LocalPoint>(
|
||||||
* @param segment
|
* @param segment
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const isLineSegment = <Point extends GlobalPoint | LocalPoint>(
|
export const isLineSegment = <Point extends GenericPoint>(
|
||||||
segment: unknown,
|
segment: unknown,
|
||||||
): segment is LineSegment<Point> =>
|
): segment is LineSegment<Point> =>
|
||||||
Array.isArray(segment) &&
|
Array.isArray(segment) &&
|
||||||
|
@ -63,9 +57,7 @@ export const isLineSegment = <Point extends GlobalPoint | LocalPoint>(
|
||||||
* @param origin
|
* @param origin
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const lineSegmentRotate = <
|
export const lineSegmentRotate = <Point extends GenericPoint>(
|
||||||
Point extends LocalPoint | GlobalPoint | ViewportPoint,
|
|
||||||
>(
|
|
||||||
l: LineSegment<Point>,
|
l: LineSegment<Point>,
|
||||||
angle: Radians,
|
angle: Radians,
|
||||||
origin?: Point,
|
origin?: Point,
|
||||||
|
@ -80,9 +72,7 @@ export const lineSegmentRotate = <
|
||||||
* Calculates the point two line segments with a definite start and end point
|
* Calculates the point two line segments with a definite start and end point
|
||||||
* intersect at.
|
* intersect at.
|
||||||
*/
|
*/
|
||||||
export const segmentsIntersectAt = <
|
export const segmentsIntersectAt = <Point extends GenericPoint>(
|
||||||
Point extends GlobalPoint | LocalPoint | ViewportPoint,
|
|
||||||
>(
|
|
||||||
a: Readonly<LineSegment<Point>>,
|
a: Readonly<LineSegment<Point>>,
|
||||||
b: Readonly<LineSegment<Point>>,
|
b: Readonly<LineSegment<Point>>,
|
||||||
): Point | null => {
|
): Point | null => {
|
||||||
|
@ -115,9 +105,7 @@ export const segmentsIntersectAt = <
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const pointOnLineSegment = <
|
export const pointOnLineSegment = <Point extends GenericPoint>(
|
||||||
Point extends LocalPoint | GlobalPoint | ViewportPoint,
|
|
||||||
>(
|
|
||||||
point: Point,
|
point: Point,
|
||||||
line: LineSegment<Point>,
|
line: LineSegment<Point>,
|
||||||
threshold = PRECISION,
|
threshold = PRECISION,
|
||||||
|
@ -131,9 +119,7 @@ export const pointOnLineSegment = <
|
||||||
return distance < threshold;
|
return distance < threshold;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const distanceToLineSegment = <
|
export const distanceToLineSegment = <Point extends GenericPoint>(
|
||||||
Point extends LocalPoint | GlobalPoint | ViewportPoint,
|
|
||||||
>(
|
|
||||||
point: Point,
|
point: Point,
|
||||||
line: LineSegment<Point>,
|
line: LineSegment<Point>,
|
||||||
) => {
|
) => {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import type { GlobalPoint, LocalPoint, Triangle } from "./types";
|
import type { GenericPoint, Triangle } from "./types";
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ import type { GlobalPoint, LocalPoint, Triangle } from "./types";
|
||||||
* @param p The point to test whether is in the triangle
|
* @param p The point to test whether is in the triangle
|
||||||
* @returns TRUE if the point is inside of the triangle
|
* @returns TRUE if the point is inside of the triangle
|
||||||
*/
|
*/
|
||||||
export function triangleIncludesPoint<P extends GlobalPoint | LocalPoint>(
|
export function triangleIncludesPoint<P extends GenericPoint>(
|
||||||
[a, b, c]: Triangle<P>,
|
[a, b, c]: Triangle<P>,
|
||||||
p: P,
|
p: P,
|
||||||
): boolean {
|
): boolean {
|
||||||
|
|
|
@ -58,7 +58,7 @@ export type GenericPoint = GlobalPoint | LocalPoint | ViewportPoint;
|
||||||
/**
|
/**
|
||||||
* A line is an infinitely long object with no width, depth, or curvature.
|
* A line is an infinitely long object with no width, depth, or curvature.
|
||||||
*/
|
*/
|
||||||
export type Line<P extends GlobalPoint | LocalPoint> = [p: P, q: P] & {
|
export type Line<P extends GenericPoint> = [p: P, q: P] & {
|
||||||
_brand: "excalimath_line";
|
_brand: "excalimath_line";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -67,10 +67,7 @@ export type Line<P extends GlobalPoint | LocalPoint> = [p: P, q: P] & {
|
||||||
* line that is bounded by two distinct end points, and
|
* line that is bounded by two distinct end points, and
|
||||||
* contains every point on the line that is between its endpoints.
|
* contains every point on the line that is between its endpoints.
|
||||||
*/
|
*/
|
||||||
export type LineSegment<P extends GlobalPoint | LocalPoint | ViewportPoint> = [
|
export type LineSegment<P extends GenericPoint> = [a: P, b: P] & {
|
||||||
a: P,
|
|
||||||
b: P,
|
|
||||||
] & {
|
|
||||||
_brand: "excalimath_linesegment";
|
_brand: "excalimath_linesegment";
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -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.
|
* 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
|
* @param origin The origin point in a given coordiante system
|
||||||
* @returns The created vector from the point and the origin
|
* @returns The created vector from the point and the origin
|
||||||
*/
|
*/
|
||||||
export function vectorFromPoint<
|
export function vectorFromPoint<Point extends GenericPoint>(
|
||||||
Point extends GlobalPoint | LocalPoint | ViewportPoint,
|
p: Point,
|
||||||
>(p: Point, origin: Point = [0, 0] as Point): Vector {
|
origin: Point = [0, 0] as Point,
|
||||||
|
): Vector {
|
||||||
return vector(p[0] - origin[0], p[1] - origin[1]);
|
return vector(p[0] - origin[0], p[1] - origin[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue