curve.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import "@excalidraw/utils/test-utils";
  2. import {
  3. curve,
  4. curveClosestPoint,
  5. curveIntersectLineSegment,
  6. curvePointDistance,
  7. } from "../src/curve";
  8. import { pointFrom } from "../src/point";
  9. import { lineSegment } from "../src/segment";
  10. describe("Math curve", () => {
  11. describe("line segment intersection", () => {
  12. it("point is found when control points are the same", () => {
  13. const c = curve(
  14. pointFrom(100, 0),
  15. pointFrom(100, 100),
  16. pointFrom(100, 100),
  17. pointFrom(0, 100),
  18. );
  19. const l = lineSegment(pointFrom(0, 0), pointFrom(200, 200));
  20. expect(curveIntersectLineSegment(c, l)).toCloselyEqualPoints([
  21. [87.5, 87.5],
  22. ]);
  23. });
  24. it("point is found when control points aren't the same", () => {
  25. const c = curve(
  26. pointFrom(100, 0),
  27. pointFrom(100, 60),
  28. pointFrom(60, 100),
  29. pointFrom(0, 100),
  30. );
  31. const l = lineSegment(pointFrom(0, 0), pointFrom(200, 200));
  32. expect(curveIntersectLineSegment(c, l)).toCloselyEqualPoints([
  33. [72.5, 72.5],
  34. ]);
  35. });
  36. it("points are found when curve is sliced at 3 points", () => {
  37. const c = curve(
  38. pointFrom(-50, -50),
  39. pointFrom(10, -50),
  40. pointFrom(10, 50),
  41. pointFrom(50, 50),
  42. );
  43. const l = lineSegment(pointFrom(0, 112.5), pointFrom(90, 0));
  44. expect(curveIntersectLineSegment(c, l)).toCloselyEqualPoints([[50, 50]]);
  45. });
  46. it("can be detected where the determinant is overly precise", () => {
  47. const c = curve(
  48. pointFrom(41.028864759926016, 12.226249068355052),
  49. pointFrom(41.028864759926016, 33.55958240168839),
  50. pointFrom(30.362198093259348, 44.22624906835505),
  51. pointFrom(9.028864759926016, 44.22624906835505),
  52. );
  53. const l = lineSegment(
  54. pointFrom(-82.30963544324186, -41.19949363038283),
  55. pointFrom(188.2149592542487, 134.75505940984908),
  56. );
  57. expect(curveIntersectLineSegment(c, l)).toCloselyEqualPoints([
  58. [34.4, 34.71],
  59. ]);
  60. });
  61. });
  62. describe("point closest to other", () => {
  63. it("point can be found", () => {
  64. const c = curve(
  65. pointFrom(-50, -50),
  66. pointFrom(10, -50),
  67. pointFrom(10, 50),
  68. pointFrom(50, 50),
  69. );
  70. const p = pointFrom(0, 0);
  71. expect([curveClosestPoint(c, p)]).toCloselyEqualPoints([
  72. [5.965462100367372, -3.04104878946646],
  73. ]);
  74. });
  75. });
  76. describe("point shortest distance", () => {
  77. it("can be determined", () => {
  78. const c = curve(
  79. pointFrom(-50, -50),
  80. pointFrom(10, -50),
  81. pointFrom(10, 50),
  82. pointFrom(50, 50),
  83. );
  84. const p = pointFrom(0, 0);
  85. expect(curvePointDistance(c, p)).toBeCloseTo(6.695873043213627);
  86. });
  87. });
  88. });