gapoints.ts 949 B

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