diff --git a/packages/math/rectangle.test.ts b/packages/math/rectangle.test.ts new file mode 100644 index 0000000000..632ae4562d --- /dev/null +++ b/packages/math/rectangle.test.ts @@ -0,0 +1,39 @@ +import { point } from "./point"; +import { rectangle, rectangleDistanceFromPoint } from "./rectangle"; + +describe("rectangle distance", () => { + it("finds the shortest distance", () => { + expect( + rectangleDistanceFromPoint( + rectangle(point(-1, -1), point(1, 1)), + point(2, 0), + ), + ).toBe(1); + expect( + rectangleDistanceFromPoint( + rectangle(point(-1, -1), point(1, 1)), + point(0, 2), + ), + ).toBe(1); + expect( + rectangleDistanceFromPoint( + rectangle(point(-1, -1), point(1, 1)), + point(-2, 0), + ), + ).toBe(1); + expect( + rectangleDistanceFromPoint( + rectangle(point(-1, -1), point(1, 1)), + point(0, -2), + ), + ).toBe(1); + }); + it("finds the corner as closest point", () => { + expect( + rectangleDistanceFromPoint( + rectangle(point(-1, -1), point(1, 1)), + point(2, 2), + ), + ).toBe(Math.sqrt(2)); + }); +}); diff --git a/packages/math/rectangle.ts b/packages/math/rectangle.ts index 96c00fbedc..f0c76a1c21 100644 --- a/packages/math/rectangle.ts +++ b/packages/math/rectangle.ts @@ -1,19 +1,19 @@ import { invariant } from "../excalidraw/utils"; +import { point } from "./point"; +import { segment, segmentDistanceToPoint } from "./segment"; import type { GenericPoint, Rectangle } from "./types"; export function rectangle
( - a: P, - b: P, - c: P, - d: P, + topLeft: P, + bottomRight: P, ): Rectangle
{ - return [a, b, c, d] as Rectangle
; + return [topLeft, bottomRight] as Rectangle
; } -export function rectangleFromQuad
( - quad: [a: P, b: P, c: P, d: P], +export function rectangleFromPair
( + pair: [a: P, b: P], ): Rectangle
{ - return quad as Rectangle
; + return pair as Rectangle
; } export function rectangleFromArray
( @@ -26,3 +26,17 @@ export function rectangleFromArray
( return pointArray as Rectangle
;
}
+
+export function rectangleDistanceFromPoint (a: P, b: P): Segment {
- invariant(
- !pointsEqual(a, b),
- "The start and end points of the segment cannot match",
- );
+ if (pointsEqual(a, b)) {
+ console.warn("The start and end points of the segment cannot match");
+ }
return [a, b] as Segment ;
}
diff --git a/packages/math/types.ts b/packages/math/types.ts
index 742d6724b7..4a7c73fa02 100644
--- a/packages/math/types.ts
+++ b/packages/math/types.ts
@@ -88,7 +88,7 @@ export type Triangle = [a: P, b: P, c: P] & {
/**
* A rectangular shape represented by 4 points at its corners
*/
-export type Rectangle = [a: P, b: P, c: P, d: P] & {
+export type Rectangle = [a: P, b: P] & {
_brand: "excalimath__rectangle";
};
diff --git a/packages/utils/geometry/shape.ts b/packages/utils/geometry/shape.ts
index ef005a3dc3..2d7c2bf9e1 100644
--- a/packages/utils/geometry/shape.ts
+++ b/packages/utils/geometry/shape.ts
@@ -253,15 +253,13 @@ const polylineFromPoints = <
return polyline;
};
-export const getFreedrawShape = <
- Point extends GlobalPoint | LocalPoint | ViewportPoint,
->(
+export const getFreedrawShape = (
element: ExcalidrawFreeDrawElement,
- center: Point,
+ center: GlobalPoint,
isClosed: boolean = false,
-): GeometricShape