line.test.ts 867 B

12345678910111213141516171819202122232425262728293031
  1. import { line, linesIntersectAt } from "../src/line";
  2. import { pointFrom } from "../src/point";
  3. describe("line-line intersections", () => {
  4. it("should correctly detect intersection at origin", () => {
  5. expect(
  6. linesIntersectAt(
  7. line(pointFrom(-5, -5), pointFrom(5, 5)),
  8. line(pointFrom(5, -5), pointFrom(-5, 5)),
  9. ),
  10. ).toEqual(pointFrom(0, 0));
  11. });
  12. it("should correctly detect intersection at non-origin", () => {
  13. expect(
  14. linesIntersectAt(
  15. line(pointFrom(0, 0), pointFrom(10, 10)),
  16. line(pointFrom(10, 0), pointFrom(0, 10)),
  17. ),
  18. ).toEqual(pointFrom(5, 5));
  19. });
  20. it("should correctly detect parallel lines", () => {
  21. expect(
  22. linesIntersectAt(
  23. line(pointFrom(0, 0), pointFrom(0, 10)),
  24. line(pointFrom(10, 0), pointFrom(10, 10)),
  25. ),
  26. ).toBe(null);
  27. });
  28. });