mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
Rectangle distance and tests
This commit is contained in:
parent
9a8aabbeca
commit
47cc842415
5 changed files with 72 additions and 25 deletions
|
@ -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<P extends GenericPoint>(
|
||||
a: P,
|
||||
b: P,
|
||||
c: P,
|
||||
d: P,
|
||||
topLeft: P,
|
||||
bottomRight: P,
|
||||
): Rectangle<P> {
|
||||
return [a, b, c, d] as Rectangle<P>;
|
||||
return [topLeft, bottomRight] as Rectangle<P>;
|
||||
}
|
||||
|
||||
export function rectangleFromQuad<P extends GenericPoint>(
|
||||
quad: [a: P, b: P, c: P, d: P],
|
||||
export function rectangleFromPair<P extends GenericPoint>(
|
||||
pair: [a: P, b: P],
|
||||
): Rectangle<P> {
|
||||
return quad as Rectangle<P>;
|
||||
return pair as Rectangle<P>;
|
||||
}
|
||||
|
||||
export function rectangleFromArray<P extends GenericPoint>(
|
||||
|
@ -26,3 +26,17 @@ export function rectangleFromArray<P extends GenericPoint>(
|
|||
|
||||
return pointArray as Rectangle<P>;
|
||||
}
|
||||
|
||||
export function rectangleDistanceFromPoint<Point extends GenericPoint>(
|
||||
r: Rectangle<Point>,
|
||||
p: Point,
|
||||
): number {
|
||||
const sides = [
|
||||
segment(point(r[0][0], r[0][1]), point(r[1][0], r[0][1])),
|
||||
segment(point(r[1][0], r[0][1]), point(r[1][0], r[1][1])),
|
||||
segment(point(r[1][0], r[1][1]), point(r[0][0], r[1][1])),
|
||||
segment(point(r[0][0], r[1][1]), point(r[0][0], r[0][1])),
|
||||
];
|
||||
|
||||
return Math.min(...sides.map((side) => segmentDistanceToPoint(p, side)));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue