You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
success/packages/math/line.test.ts

52 lines
1.3 KiB
TypeScript

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);
});
});