add a tiny threshold for checks

This commit is contained in:
Ryan Di 2025-02-28 15:41:35 +11:00
parent 0d5091555c
commit ba7e3439f4
2 changed files with 9 additions and 4 deletions

View file

@ -166,7 +166,8 @@ const intersectionTest = (
return lassoSegments.some((lassoSegment) =>
elementSegments.some(
(elementSegment) =>
lineSegmentIntersectionPoints(lassoSegment, elementSegment) !== null,
// introduce a bit of tolerance to account for roughness and simplification of paths
lineSegmentIntersectionPoints(lassoSegment, elementSegment, 1) !== null,
),
);
};

View file

@ -159,13 +159,17 @@ export const distanceToLineSegment = <Point extends LocalPoint | GlobalPoint>(
*/
export function lineSegmentIntersectionPoints<
Point extends GlobalPoint | LocalPoint,
>(l: LineSegment<Point>, s: LineSegment<Point>): Point | null {
>(
l: LineSegment<Point>,
s: LineSegment<Point>,
threshold?: number,
): Point | null {
const candidate = linesIntersectAt(line(l[0], l[1]), line(s[0], s[1]));
if (
!candidate ||
!pointOnLineSegment(candidate, s) ||
!pointOnLineSegment(candidate, l)
!pointOnLineSegment(candidate, s, threshold) ||
!pointOnLineSegment(candidate, l, threshold)
) {
return null;
}