ga.test.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import * as GA from "./ga";
  2. import { point, toString, direction, offset } from "./ga";
  3. import * as GAPoint from "./gapoints";
  4. import * as GALine from "./galines";
  5. import * as GATransform from "./gatransforms";
  6. describe("geometric algebra", () => {
  7. describe("points", () => {
  8. it("distanceToLine", () => {
  9. const point = GA.point(3, 3);
  10. const line = GALine.equation(0, 1, -1);
  11. expect(GAPoint.distanceToLine(point, line)).toEqual(2);
  12. });
  13. it("distanceToLine neg", () => {
  14. const point = GA.point(-3, -3);
  15. const line = GALine.equation(0, 1, -1);
  16. expect(GAPoint.distanceToLine(point, line)).toEqual(-4);
  17. });
  18. });
  19. describe("lines", () => {
  20. it("through", () => {
  21. const a = GA.point(0, 0);
  22. const b = GA.point(2, 0);
  23. expect(toString(GALine.through(a, b))).toEqual(
  24. toString(GALine.equation(0, 2, 0)),
  25. );
  26. });
  27. it("parallel", () => {
  28. const point = GA.point(3, 3);
  29. const line = GALine.equation(0, 1, -1);
  30. const parallel = GALine.parallel(line, 2);
  31. expect(GAPoint.distanceToLine(point, parallel)).toEqual(0);
  32. });
  33. });
  34. describe("translation", () => {
  35. it("points", () => {
  36. const start = point(2, 2);
  37. const move = GATransform.translation(direction(0, 1));
  38. const end = GATransform.apply(move, start);
  39. expect(toString(end)).toEqual(toString(point(2, 3)));
  40. });
  41. it("points 2", () => {
  42. const start = point(2, 2);
  43. const move = GATransform.translation(offset(3, 4));
  44. const end = GATransform.apply(move, start);
  45. expect(toString(end)).toEqual(toString(point(5, 6)));
  46. });
  47. it("lines", () => {
  48. const original = GALine.through(point(2, 2), point(3, 4));
  49. const move = GATransform.translation(offset(3, 4));
  50. const parallel = GATransform.apply(move, original);
  51. expect(toString(parallel)).toEqual(
  52. toString(GALine.through(point(5, 6), point(6, 8))),
  53. );
  54. });
  55. });
  56. describe("rotation", () => {
  57. it("points", () => {
  58. const start = point(2, 2);
  59. const pivot = point(1, 1);
  60. const rotate = GATransform.rotation(pivot, Math.PI / 2);
  61. const end = GATransform.apply(rotate, start);
  62. expect(toString(end)).toEqual(toString(point(2, 0)));
  63. });
  64. });
  65. });