curve.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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(10, -60), pointFrom(10, 60));
  44. expect(curveIntersectLineSegment(c, l)).toCloselyEqualPoints([
  45. [9.99, 5.05],
  46. ]);
  47. });
  48. it("can be detected where the determinant is overly precise", () => {
  49. const c = curve(
  50. pointFrom(41.028864759926016, 12.226249068355052),
  51. pointFrom(41.028864759926016, 33.55958240168839),
  52. pointFrom(30.362198093259348, 44.22624906835505),
  53. pointFrom(9.028864759926016, 44.22624906835505),
  54. );
  55. const l = lineSegment(
  56. pointFrom(-82.30963544324186, -41.19949363038283),
  57. pointFrom(188.2149592542487, 134.75505940984908),
  58. );
  59. expect(curveIntersectLineSegment(c, l)).toCloselyEqualPoints([
  60. [34.4, 34.71],
  61. ]);
  62. });
  63. });
  64. describe("point closest to other", () => {
  65. it("point can be found", () => {
  66. const c = curve(
  67. pointFrom(-50, -50),
  68. pointFrom(10, -50),
  69. pointFrom(10, 50),
  70. pointFrom(50, 50),
  71. );
  72. const p = pointFrom(0, 0);
  73. expect([curveClosestPoint(c, p)]).toCloselyEqualPoints([
  74. [5.965462100367372, -3.04104878946646],
  75. ]);
  76. });
  77. });
  78. describe("point shortest distance", () => {
  79. it("can be determined", () => {
  80. const c = curve(
  81. pointFrom(-50, -50),
  82. pointFrom(10, -50),
  83. pointFrom(10, 50),
  84. pointFrom(50, 50),
  85. );
  86. const p = pointFrom(0, 0);
  87. expect(curvePointDistance(c, p)).toBeCloseTo(6.695873043213627);
  88. });
  89. });
  90. });