mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
refactor: point()
-> pointFrom()
to fix compiler issue (#8578)
This commit is contained in:
parent
0c02972695
commit
b4d8b04d9e
50 changed files with 843 additions and 718 deletions
|
@ -6,31 +6,31 @@ import {
|
|||
arcSegmentInterceptPoints,
|
||||
} from "./arc";
|
||||
import { line } from "./line";
|
||||
import { point } from "./point";
|
||||
import { pointFrom } from "./point";
|
||||
import { segment } from "./segment";
|
||||
|
||||
describe("point on arc", () => {
|
||||
it("should detect point on simple arc", () => {
|
||||
expect(
|
||||
arcIncludesPoint(
|
||||
arc(point(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
|
||||
point(0.92291667, 0.385),
|
||||
arc(pointFrom(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
|
||||
pointFrom(0.92291667, 0.385),
|
||||
),
|
||||
).toBe(true);
|
||||
});
|
||||
it("should not detect point outside of a simple arc", () => {
|
||||
expect(
|
||||
arcIncludesPoint(
|
||||
arc(point(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
|
||||
point(-0.92291667, 0.385),
|
||||
arc(pointFrom(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
|
||||
pointFrom(-0.92291667, 0.385),
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
it("should not detect point with good angle but incorrect radius", () => {
|
||||
expect(
|
||||
arcIncludesPoint(
|
||||
arc(point(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
|
||||
point(-0.5, 0.5),
|
||||
arc(pointFrom(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
|
||||
pointFrom(-0.5, 0.5),
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
|
@ -40,42 +40,42 @@ describe("intersection", () => {
|
|||
it("should report correct interception point for segment", () => {
|
||||
expect(
|
||||
arcSegmentInterceptPoints(
|
||||
arc(point(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
|
||||
segment(point(2, 1), point(0, 0)),
|
||||
arc(pointFrom(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
|
||||
segment(pointFrom(2, 1), pointFrom(0, 0)),
|
||||
),
|
||||
).toEqual([point(0.894427190999916, 0.447213595499958)]);
|
||||
).toEqual([pointFrom(0.894427190999916, 0.447213595499958)]);
|
||||
});
|
||||
|
||||
it("should report both interception points when present for segment", () => {
|
||||
expect(
|
||||
arcSegmentInterceptPoints(
|
||||
arc(point(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
|
||||
segment(point(0.9, -2), point(0.9, 2)),
|
||||
arc(pointFrom(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
|
||||
segment(pointFrom(0.9, -2), pointFrom(0.9, 2)),
|
||||
),
|
||||
).toEqual([
|
||||
point(0.9, -0.4358898943540668),
|
||||
point(0.9, 0.4358898943540668),
|
||||
pointFrom(0.9, -0.4358898943540668),
|
||||
pointFrom(0.9, 0.4358898943540668),
|
||||
]);
|
||||
});
|
||||
|
||||
it("should report correct interception point for line", () => {
|
||||
expect(
|
||||
arcLineInterceptPoints(
|
||||
arc(point(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
|
||||
line(point(2, 1), point(0, 0)),
|
||||
arc(pointFrom(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
|
||||
line(pointFrom(2, 1), pointFrom(0, 0)),
|
||||
),
|
||||
).toEqual([point(0.894427190999916, 0.447213595499958)]);
|
||||
).toEqual([pointFrom(0.894427190999916, 0.447213595499958)]);
|
||||
});
|
||||
|
||||
it("should report both interception points when present for line", () => {
|
||||
expect(
|
||||
arcLineInterceptPoints(
|
||||
arc(point(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
|
||||
line(point(0.9, -2), point(0.9, 2)),
|
||||
arc(pointFrom(0, 0), 1, radians(-Math.PI / 4), radians(Math.PI / 4)),
|
||||
line(pointFrom(0.9, -2), pointFrom(0.9, 2)),
|
||||
),
|
||||
).toEqual([
|
||||
point(0.9, 0.4358898943540668),
|
||||
point(0.9, -0.4358898943540668),
|
||||
pointFrom(0.9, 0.4358898943540668),
|
||||
pointFrom(0.9, -0.4358898943540668),
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { point, pointRotateRads } from "./point";
|
||||
import { pointFrom, pointRotateRads } from "./point";
|
||||
import type { Curve, GenericPoint, Radians } from "./types";
|
||||
|
||||
/**
|
||||
|
@ -43,10 +43,10 @@ export function curveToBezier<Point extends GenericPoint>(
|
|||
const out: Point[] = [];
|
||||
if (len === 3) {
|
||||
out.push(
|
||||
point(pointsIn[0][0], pointsIn[0][1]), // Points need to be cloned
|
||||
point(pointsIn[1][0], pointsIn[1][1]), // Points need to be cloned
|
||||
point(pointsIn[2][0], pointsIn[2][1]), // Points need to be cloned
|
||||
point(pointsIn[2][0], pointsIn[2][1]), // Points need to be cloned
|
||||
pointFrom(pointsIn[0][0], pointsIn[0][1]), // Points need to be cloned
|
||||
pointFrom(pointsIn[1][0], pointsIn[1][1]), // Points need to be cloned
|
||||
pointFrom(pointsIn[2][0], pointsIn[2][1]), // Points need to be cloned
|
||||
pointFrom(pointsIn[2][0], pointsIn[2][1]), // Points need to be cloned
|
||||
);
|
||||
} else {
|
||||
const points: Point[] = [];
|
||||
|
@ -59,19 +59,19 @@ export function curveToBezier<Point extends GenericPoint>(
|
|||
}
|
||||
const b: Point[] = [];
|
||||
const s = 1 - curveTightness;
|
||||
out.push(point(points[0][0], points[0][1]));
|
||||
out.push(pointFrom(points[0][0], points[0][1]));
|
||||
for (let i = 1; i + 2 < points.length; i++) {
|
||||
const cachedVertArray = points[i];
|
||||
b[0] = point(cachedVertArray[0], cachedVertArray[1]);
|
||||
b[1] = point(
|
||||
b[0] = pointFrom(cachedVertArray[0], cachedVertArray[1]);
|
||||
b[1] = pointFrom(
|
||||
cachedVertArray[0] + (s * points[i + 1][0] - s * points[i - 1][0]) / 6,
|
||||
cachedVertArray[1] + (s * points[i + 1][1] - s * points[i - 1][1]) / 6,
|
||||
);
|
||||
b[2] = point(
|
||||
b[2] = pointFrom(
|
||||
points[i + 1][0] + (s * points[i][0] - s * points[i + 2][0]) / 6,
|
||||
points[i + 1][1] + (s * points[i][1] - s * points[i + 2][1]) / 6,
|
||||
);
|
||||
b[3] = point(points[i + 1][0], points[i + 1][1]);
|
||||
b[3] = pointFrom(points[i + 1][0], points[i + 1][1]);
|
||||
out.push(b[1], b[2], b[3]);
|
||||
}
|
||||
}
|
||||
|
@ -102,7 +102,7 @@ export const cubicBezierPoint = <Point extends GenericPoint>(
|
|||
3 * (1 - t) * Math.pow(t, 2) * p2[1] +
|
||||
Math.pow(t, 3) * p3[1];
|
||||
|
||||
return point(x, y);
|
||||
return pointFrom(x, y);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { point, pointRotateRads } from "./point";
|
||||
import { pointFrom, pointRotateRads } from "./point";
|
||||
import type { Radians } from "./types";
|
||||
|
||||
describe("rotate", () => {
|
||||
|
@ -9,14 +9,14 @@ describe("rotate", () => {
|
|||
const y2 = 30;
|
||||
const angle = (Math.PI / 2) as Radians;
|
||||
const [rotatedX, rotatedY] = pointRotateRads(
|
||||
point(x1, y1),
|
||||
point(x2, y2),
|
||||
pointFrom(x1, y1),
|
||||
pointFrom(x2, y2),
|
||||
angle,
|
||||
);
|
||||
expect([rotatedX, rotatedY]).toEqual([30, 20]);
|
||||
const res2 = pointRotateRads(
|
||||
point(rotatedX, rotatedY),
|
||||
point(x2, y2),
|
||||
pointFrom(rotatedX, rotatedY),
|
||||
pointFrom(x2, y2),
|
||||
-angle as Radians,
|
||||
);
|
||||
expect(res2).toEqual([x1, x2]);
|
||||
|
|
|
@ -10,7 +10,10 @@ import { vectorFromPoint, vectorScale } from "./vector";
|
|||
* @param y The Y coordinate
|
||||
* @returns The branded and created point
|
||||
*/
|
||||
export function point<Point extends GenericPoint>(x: number, y: number): Point {
|
||||
export function pointFrom<Point extends GenericPoint>(
|
||||
x: number,
|
||||
y: number,
|
||||
): Point {
|
||||
return [x, y] as Point;
|
||||
}
|
||||
|
||||
|
@ -24,7 +27,7 @@ export function pointFromArray<Point extends GenericPoint>(
|
|||
numberArray: number[],
|
||||
): Point | undefined {
|
||||
return numberArray.length === 2
|
||||
? point<Point>(numberArray[0], numberArray[1])
|
||||
? pointFrom<Point>(numberArray[0], numberArray[1])
|
||||
: undefined;
|
||||
}
|
||||
|
||||
|
@ -48,9 +51,9 @@ export function pointFromPair<Point extends GenericPoint>(
|
|||
*/
|
||||
export function pointFromVector<P extends GenericPoint>(
|
||||
v: Vector,
|
||||
offset: P = point(0, 0),
|
||||
offset: P = pointFrom(0, 0),
|
||||
): P {
|
||||
return point<P>(offset[0] + v[0], offset[1] + v[1]);
|
||||
return pointFrom<P>(offset[0] + v[0], offset[1] + v[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -102,7 +105,7 @@ export function pointRotateRads<Point extends GenericPoint>(
|
|||
const cos = Math.cos(angle);
|
||||
const sin = Math.sin(angle);
|
||||
|
||||
return point(
|
||||
return pointFrom(
|
||||
(x - cx) * cos - (y - cy) * sin + cx,
|
||||
(x - cx) * sin + (y - cy) * cos + cy,
|
||||
);
|
||||
|
@ -141,7 +144,7 @@ export function pointTranslate<
|
|||
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]);
|
||||
return pointFrom(p[0] + v[0], p[1] + v[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -171,7 +174,7 @@ export function pointAdd<Point extends GenericPoint>(
|
|||
a: Point,
|
||||
b: Point,
|
||||
): Point {
|
||||
return point(a[0] + b[0], a[1] + b[1]);
|
||||
return pointFrom(a[0] + b[0], a[1] + b[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -186,7 +189,7 @@ export function pointSubtract<Point extends GenericPoint>(
|
|||
a: Point,
|
||||
b: Point,
|
||||
): Point {
|
||||
return point(a[0] - b[0], a[1] - b[1]);
|
||||
return pointFrom(a[0] - b[0], a[1] - b[1]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue