gapoints.ts 977 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import * as GA from "./ga";
  2. import * as GALine from "./galines";
  3. import type { Point, Line } from "./ga";
  4. import { join } from "./ga";
  5. export const from = ([x, y]: readonly [number, number]): Point => [
  6. 0,
  7. 0,
  8. 0,
  9. 0,
  10. y,
  11. x,
  12. 1,
  13. 0,
  14. ];
  15. export const toTuple = (point: Point): [number, number] => [point[5], point[4]];
  16. export const abs = (point: Point): Point => [
  17. 0,
  18. 0,
  19. 0,
  20. 0,
  21. Math.abs(point[4]),
  22. Math.abs(point[5]),
  23. 1,
  24. 0,
  25. ];
  26. export const intersect = (line1: Line, line2: Line): Point =>
  27. GA.normalized(GA.meet(line1, line2));
  28. // Projects `point` onto the `line`.
  29. // The returned point is the closest point on the `line` to the `point`.
  30. export const project = (point: Point, line: Line): Point =>
  31. intersect(GALine.orthogonal(line, point), line);
  32. export const distance = (point1: Point, point2: Point): number =>
  33. GA.norm(join(point1, point2));
  34. export const distanceToLine = (point: Point, line: Line): number =>
  35. GA.joinScalar(point, line);