gatransforms.ts 1.1 KB

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