mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
refactor: separate elements logic into a standalone package (#9285)
Some checks failed
Auto release excalidraw next / Auto-release-excalidraw-next (push) Failing after 2m36s
Build Docker image / build-docker (push) Failing after 6s
Cancel previous runs / cancel (push) Failing after 1s
Publish Docker / publish-docker (push) Failing after 31s
New Sentry production release / sentry (push) Failing after 2m3s
Some checks failed
Auto release excalidraw next / Auto-release-excalidraw-next (push) Failing after 2m36s
Build Docker image / build-docker (push) Failing after 6s
Cancel previous runs / cancel (push) Failing after 1s
Publish Docker / publish-docker (push) Failing after 31s
New Sentry production release / sentry (push) Failing after 2m3s
This commit is contained in:
parent
a18f059188
commit
432a46ef9e
372 changed files with 3466 additions and 2466 deletions
102
packages/math/tests/curve.test.ts
Normal file
102
packages/math/tests/curve.test.ts
Normal file
|
@ -0,0 +1,102 @@
|
|||
import "@excalidraw/utils/test-utils";
|
||||
|
||||
import {
|
||||
curve,
|
||||
curveClosestPoint,
|
||||
curveIntersectLineSegment,
|
||||
curvePointDistance,
|
||||
} from "../src/curve";
|
||||
import { pointFrom } from "../src/point";
|
||||
import { lineSegment } from "../src/segment";
|
||||
|
||||
describe("Math curve", () => {
|
||||
describe("line segment intersection", () => {
|
||||
it("point is found when control points are the same", () => {
|
||||
const c = curve(
|
||||
pointFrom(100, 0),
|
||||
pointFrom(100, 100),
|
||||
pointFrom(100, 100),
|
||||
pointFrom(0, 100),
|
||||
);
|
||||
const l = lineSegment(pointFrom(0, 0), pointFrom(200, 200));
|
||||
|
||||
expect(curveIntersectLineSegment(c, l)).toCloselyEqualPoints([
|
||||
[87.5, 87.5],
|
||||
]);
|
||||
});
|
||||
|
||||
it("point is found when control points aren't the same", () => {
|
||||
const c = curve(
|
||||
pointFrom(100, 0),
|
||||
pointFrom(100, 60),
|
||||
pointFrom(60, 100),
|
||||
pointFrom(0, 100),
|
||||
);
|
||||
const l = lineSegment(pointFrom(0, 0), pointFrom(200, 200));
|
||||
|
||||
expect(curveIntersectLineSegment(c, l)).toCloselyEqualPoints([
|
||||
[72.5, 72.5],
|
||||
]);
|
||||
});
|
||||
|
||||
it("points are found when curve is sliced at 3 points", () => {
|
||||
const c = curve(
|
||||
pointFrom(-50, -50),
|
||||
pointFrom(10, -50),
|
||||
pointFrom(10, 50),
|
||||
pointFrom(50, 50),
|
||||
);
|
||||
const l = lineSegment(pointFrom(0, 112.5), pointFrom(90, 0));
|
||||
|
||||
expect(curveIntersectLineSegment(c, l)).toCloselyEqualPoints([[50, 50]]);
|
||||
});
|
||||
|
||||
it("can be detected where the determinant is overly precise", () => {
|
||||
const c = curve(
|
||||
pointFrom(41.028864759926016, 12.226249068355052),
|
||||
pointFrom(41.028864759926016, 33.55958240168839),
|
||||
pointFrom(30.362198093259348, 44.22624906835505),
|
||||
pointFrom(9.028864759926016, 44.22624906835505),
|
||||
);
|
||||
const l = lineSegment(
|
||||
pointFrom(-82.30963544324186, -41.19949363038283),
|
||||
|
||||
pointFrom(188.2149592542487, 134.75505940984908),
|
||||
);
|
||||
|
||||
expect(curveIntersectLineSegment(c, l)).toCloselyEqualPoints([
|
||||
[34.4, 34.71],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("point closest to other", () => {
|
||||
it("point can be found", () => {
|
||||
const c = curve(
|
||||
pointFrom(-50, -50),
|
||||
pointFrom(10, -50),
|
||||
pointFrom(10, 50),
|
||||
pointFrom(50, 50),
|
||||
);
|
||||
const p = pointFrom(0, 0);
|
||||
|
||||
expect([curveClosestPoint(c, p)]).toCloselyEqualPoints([
|
||||
[5.965462100367372, -3.04104878946646],
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("point shortest distance", () => {
|
||||
it("can be determined", () => {
|
||||
const c = curve(
|
||||
pointFrom(-50, -50),
|
||||
pointFrom(10, -50),
|
||||
pointFrom(10, 50),
|
||||
pointFrom(50, 50),
|
||||
);
|
||||
const p = pointFrom(0, 0);
|
||||
|
||||
expect(curvePointDistance(c, p)).toBeCloseTo(6.695873043213627);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue