chore: Refactor doBoundsIntersect (#9657)

This commit is contained in:
Márk Tolmács
2025-06-16 12:30:42 +02:00
committed by GitHub
parent 058918f8e5
commit 958597dfaa
8 changed files with 172 additions and 105 deletions

View File

@@ -10,6 +10,12 @@ export function rectangle<P extends GlobalPoint | LocalPoint>(
return [topLeft, bottomRight] as Rectangle<P>;
}
export function rectangleFromNumberSequence<
Point extends LocalPoint | GlobalPoint,
>(minX: number, minY: number, maxX: number, maxY: number) {
return rectangle(pointFrom<Point>(minX, minY), pointFrom<Point>(maxX, maxY));
}
export function rectangleIntersectLineSegment<
Point extends LocalPoint | GlobalPoint,
>(r: Rectangle<Point>, l: LineSegment<Point>): Point[] {
@@ -22,3 +28,12 @@ export function rectangleIntersectLineSegment<
.map((s) => lineSegmentIntersectionPoints(l, s))
.filter((i): i is Point => !!i);
}
export function rectangleIntersectRectangle<
Point extends LocalPoint | GlobalPoint,
>(rectangle1: Rectangle<Point>, rectangle2: Rectangle<Point>): boolean {
const [[minX1, minY1], [maxX1, maxY1]] = rectangle1;
const [[minX2, minY2], [maxX2, maxY2]] = rectangle2;
return minX1 < maxX2 && maxX1 > minX2 && minY1 < maxY2 && maxY1 > minY2;
}