1c292e4936
Co-authored-by: Mark Tolmacs <mark@lazycat.hu>
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
import { pointFrom } from "../src/point";
|
|
import {
|
|
lineSegment,
|
|
lineSegmentIntersectionPoints,
|
|
isLineSegment,
|
|
} from "../src/segment";
|
|
|
|
describe("line-segment intersections", () => {
|
|
it("should correctly detect intersection", () => {
|
|
expect(
|
|
lineSegmentIntersectionPoints(
|
|
lineSegment(pointFrom(0, 0), pointFrom(5, 0)),
|
|
lineSegment(pointFrom(2, -2), pointFrom(3, 2)),
|
|
),
|
|
).toEqual(pointFrom(2.5, 0));
|
|
});
|
|
it("should correctly detect non-intersection", () => {
|
|
expect(
|
|
lineSegmentIntersectionPoints(
|
|
lineSegment(pointFrom(0, 0), pointFrom(5, 0)),
|
|
lineSegment(pointFrom(3, 1), pointFrom(4, 4)),
|
|
),
|
|
).toEqual(null);
|
|
});
|
|
});
|
|
describe("isLineSegment validation", () => {
|
|
it("should return true for a valid segment", () => {
|
|
expect(
|
|
isLineSegment([
|
|
[0, 0],
|
|
[1, 1],
|
|
]),
|
|
).toBe(true);
|
|
});
|
|
|
|
it("should return false if second element is not a point", () => {
|
|
const invalidSegment = [[0, 0], "not-a-point"] as any;
|
|
|
|
expect(isLineSegment(invalidSegment)).toBe(false);
|
|
});
|
|
|
|
it("should return false for wrong length", () => {
|
|
expect(isLineSegment([[0, 0]])).toBe(false);
|
|
});
|
|
});
|