Move path related function into the math package

This commit is contained in:
Mark Tolmacs 2024-09-23 18:08:17 +02:00
parent 1b56cf90fb
commit 9eb08df3ea
No known key found for this signature in database
9 changed files with 72 additions and 46 deletions

View file

@ -2,9 +2,11 @@ export * from "./arc";
export * from "./angle";
export * from "./curve";
export * from "./line";
export * from "./path";
export * from "./point";
export * from "./polygon";
export * from "./range";
export * from "./rectangle";
export * from "./segment";
export * from "./triangle";
export * from "./types";

27
packages/math/path.ts Normal file
View file

@ -0,0 +1,27 @@
import { pointDistance } from "./point";
import type { LocalPoint } from "./types";
/**
* Checks if the first and last point are close enough to be considered a loop
*
* @param points
* @param threshold
* @returns
*/
export const pathIsALoop = (
points: readonly LocalPoint[],
/** supply if you want the loop detection to account for current zoom */
threshold: number,
//zoomValue: Zoom["value"] = 1 as NormalizedZoomValue,
): boolean => {
if (points.length >= 3) {
const [first, last] = [points[0], points[points.length - 1]];
const distance = pointDistance(first, last);
// Adjusting LINE_CONFIRM_THRESHOLD to current zoom so that when zoomed in
// really close we make the threshold smaller, and vice versa.
return distance <= threshold; // LINE_CONFIRM_THRESHOLD / zoomValue;
}
return false;
};