gatransforms.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import * as GA from "./ga";
  2. import { Line, Direction, Point, Transform } from "./ga";
  3. import * as GADirection from "./gadirections";
  4. /**
  5. * TODO: docs
  6. */
  7. export const rotation = (pivot: Point, angle: number): Transform =>
  8. GA.add(GA.mul(pivot, Math.sin(angle / 2)), Math.cos(angle / 2));
  9. export const translation = (direction: Direction): Transform => [
  10. 1,
  11. 0,
  12. 0,
  13. 0,
  14. -(0.5 * direction[5]),
  15. 0.5 * direction[4],
  16. 0,
  17. 0,
  18. ];
  19. export const translationOrthogonal = (
  20. direction: Direction,
  21. distance: number,
  22. ): Transform => {
  23. const scale = 0.5 * distance;
  24. return [1, 0, 0, 0, scale * direction[4], scale * direction[5], 0, 0];
  25. };
  26. export const translationAlong = (line: Line, distance: number): Transform =>
  27. GA.add(GA.mul(GADirection.orthogonalToLine(line), 0.5 * distance), 1);
  28. export const compose = (motor1: Transform, motor2: Transform): Transform =>
  29. GA.mul(motor2, motor1);
  30. export const apply = (
  31. motor: Transform,
  32. nvector: Point | Direction | Line,
  33. ): Point | Direction | Line =>
  34. GA.normalized(GA.mul(GA.mul(motor, nvector), GA.reverse(motor)));