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 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<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>;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
* @returns The created line
|
||||
*/
|
||||
export function lineFromPointPair<P extends GlobalPoint | LocalPoint>([a, b]: [
|
||||
export function lineFromPointPair<P extends GenericPoint>([a, b]: [
|
||||
P,
|
||||
P,
|
||||
]): Line<P> {
|
||||
|
@ -30,7 +30,7 @@ export function lineFromPointPair<P extends GlobalPoint | LocalPoint>([a, b]: [
|
|||
* @param pointArray
|
||||
* @returns
|
||||
*/
|
||||
export function lineFromPointArray<P extends GlobalPoint | LocalPoint>(
|
||||
export function lineFromPointArray<P extends GenericPoint>(
|
||||
pointArray: P[],
|
||||
): Line<P> | undefined {
|
||||
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
|
||||
// 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>,
|
||||
angle: Radians,
|
||||
origin?: Point,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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<Point extends GlobalPoint | LocalPoint>(
|
||||
...points: Point[]
|
||||
) {
|
||||
export function polygon<Point extends GenericPoint>(...points: Point[]) {
|
||||
return polygonClose(points) as Polygon<Point>;
|
||||
}
|
||||
|
||||
export function polygonFromPoints<
|
||||
Point extends GlobalPoint | LocalPoint | ViewportPoint,
|
||||
>(points: Point[]) {
|
||||
export function polygonFromPoints<Point extends GenericPoint>(points: Point[]) {
|
||||
return polygonClose(points) as Polygon<Point>;
|
||||
}
|
||||
|
||||
export const polygonIncludesPoint = <
|
||||
Point extends LocalPoint | GlobalPoint | ViewportPoint,
|
||||
>(
|
||||
export const polygonIncludesPoint = <Point extends GenericPoint>(
|
||||
point: Point,
|
||||
polygon: Polygon<Point>,
|
||||
) => {
|
||||
|
@ -42,9 +36,7 @@ export const polygonIncludesPoint = <
|
|||
return inside;
|
||||
};
|
||||
|
||||
export const pointOnPolygon = <
|
||||
Point extends LocalPoint | GlobalPoint | ViewportPoint,
|
||||
>(
|
||||
export const pointOnPolygon = <Point extends GenericPoint>(
|
||||
p: Point,
|
||||
poly: Polygon<Point>,
|
||||
threshold = PRECISION,
|
||||
|
@ -61,16 +53,12 @@ export const pointOnPolygon = <
|
|||
return on;
|
||||
};
|
||||
|
||||
function polygonClose<Point extends LocalPoint | GlobalPoint | ViewportPoint>(
|
||||
polygon: Point[],
|
||||
) {
|
||||
function polygonClose<Point extends GenericPoint>(polygon: Point[]) {
|
||||
return polygonIsClosed(polygon)
|
||||
? polygon
|
||||
: ([...polygon, polygon[0]] as Polygon<Point>);
|
||||
}
|
||||
|
||||
function polygonIsClosed<
|
||||
Point extends LocalPoint | GlobalPoint | ViewportPoint,
|
||||
>(polygon: Point[]) {
|
||||
function polygonIsClosed<Point extends GenericPoint>(polygon: Point[]) {
|
||||
return pointsEqual(polygon[0], polygon[polygon.length - 1]);
|
||||
}
|
||||
|
|
|
@ -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<P extends GlobalPoint | LocalPoint | ViewportPoint>(
|
||||
export function lineSegment<P extends GenericPoint>(
|
||||
a: P,
|
||||
b: P,
|
||||
): LineSegment<P> {
|
||||
return [a, b] as LineSegment<P>;
|
||||
}
|
||||
|
||||
export function lineSegmentFromPointArray<P extends GlobalPoint | LocalPoint>(
|
||||
export function lineSegmentFromPointArray<P extends GenericPoint>(
|
||||
pointArray: P[],
|
||||
): LineSegment<P> | undefined {
|
||||
return pointArray.length === 2
|
||||
|
@ -46,7 +40,7 @@ export function lineSegmentFromPointArray<P extends GlobalPoint | LocalPoint>(
|
|||
* @param segment
|
||||
* @returns
|
||||
*/
|
||||
export const isLineSegment = <Point extends GlobalPoint | LocalPoint>(
|
||||
export const isLineSegment = <Point extends GenericPoint>(
|
||||
segment: unknown,
|
||||
): segment is LineSegment<Point> =>
|
||||
Array.isArray(segment) &&
|
||||
|
@ -63,9 +57,7 @@ export const isLineSegment = <Point extends GlobalPoint | LocalPoint>(
|
|||
* @param origin
|
||||
* @returns
|
||||
*/
|
||||
export const lineSegmentRotate = <
|
||||
Point extends LocalPoint | GlobalPoint | ViewportPoint,
|
||||
>(
|
||||
export const lineSegmentRotate = <Point extends GenericPoint>(
|
||||
l: LineSegment<Point>,
|
||||
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 = <Point extends GenericPoint>(
|
||||
a: Readonly<LineSegment<Point>>,
|
||||
b: Readonly<LineSegment<Point>>,
|
||||
): Point | null => {
|
||||
|
@ -115,9 +105,7 @@ export const segmentsIntersectAt = <
|
|||
return null;
|
||||
};
|
||||
|
||||
export const pointOnLineSegment = <
|
||||
Point extends LocalPoint | GlobalPoint | ViewportPoint,
|
||||
>(
|
||||
export const pointOnLineSegment = <Point extends GenericPoint>(
|
||||
point: Point,
|
||||
line: LineSegment<Point>,
|
||||
threshold = PRECISION,
|
||||
|
@ -131,9 +119,7 @@ export const pointOnLineSegment = <
|
|||
return distance < threshold;
|
||||
};
|
||||
|
||||
export const distanceToLineSegment = <
|
||||
Point extends LocalPoint | GlobalPoint | ViewportPoint,
|
||||
>(
|
||||
export const distanceToLineSegment = <Point extends GenericPoint>(
|
||||
point: Point,
|
||||
line: LineSegment<Point>,
|
||||
) => {
|
||||
|
|
|
@ -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<P extends GlobalPoint | LocalPoint>(
|
||||
export function triangleIncludesPoint<P extends GenericPoint>(
|
||||
[a, b, c]: Triangle<P>,
|
||||
p: P,
|
||||
): boolean {
|
||||
|
|
|
@ -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 extends GlobalPoint | LocalPoint> = [p: P, q: P] & {
|
||||
export type Line<P extends GenericPoint> = [p: P, q: P] & {
|
||||
_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
|
||||
* contains every point on the line that is between its endpoints.
|
||||
*/
|
||||
export type LineSegment<P extends GlobalPoint | LocalPoint | ViewportPoint> = [
|
||||
a: P,
|
||||
b: P,
|
||||
] & {
|
||||
export type LineSegment<P extends GenericPoint> = [a: P, b: P] & {
|
||||
_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.
|
||||
|
@ -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<Point extends GenericPoint>(
|
||||
p: Point,
|
||||
origin: Point = [0, 0] as Point,
|
||||
): Vector {
|
||||
return vector(p[0] - origin[0], p[1] - origin[1]);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue