mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
Line and segment intersection
This commit is contained in:
parent
f347281c21
commit
b7f504796b
3 changed files with 157 additions and 29 deletions
51
packages/math/line.test.ts
Normal file
51
packages/math/line.test.ts
Normal file
|
@ -0,0 +1,51 @@
|
|||
import { line, lineIntersectsLine, lineIntersectsSegment } from "./line";
|
||||
import { point } from "./point";
|
||||
import { segment } from "./segment";
|
||||
|
||||
describe("line-line intersections", () => {
|
||||
it("should correctly detect intersection at origin", () => {
|
||||
expect(
|
||||
lineIntersectsLine(
|
||||
line(point(-5, -5), point(5, 5)),
|
||||
line(point(5, -5), point(-5, 5)),
|
||||
),
|
||||
).toEqual(point(0, 0));
|
||||
});
|
||||
|
||||
it("should correctly detect intersection at non-origin", () => {
|
||||
expect(
|
||||
lineIntersectsLine(
|
||||
line(point(0, 0), point(10, 10)),
|
||||
line(point(10, 0), point(0, 10)),
|
||||
),
|
||||
).toEqual(point(5, 5));
|
||||
});
|
||||
|
||||
it("should correctly detect parallel lines", () => {
|
||||
expect(
|
||||
lineIntersectsLine(
|
||||
line(point(0, 0), point(0, 10)),
|
||||
line(point(10, 0), point(10, 10)),
|
||||
),
|
||||
).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("line-segment intersections", () => {
|
||||
it("should correctly detect intersection", () => {
|
||||
expect(
|
||||
lineIntersectsSegment(
|
||||
line(point(0, 0), point(5, 0)),
|
||||
segment(point(2, -2), point(3, 2)),
|
||||
),
|
||||
).toEqual(point(2.5, -0));
|
||||
});
|
||||
it("should correctly detect non-intersection", () => {
|
||||
expect(
|
||||
lineIntersectsSegment(
|
||||
line(point(0, 0), point(5, 0)),
|
||||
segment(point(3, 1), point(4, 4)),
|
||||
),
|
||||
).toEqual(null);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue