10000 R113 follow up by rcj-siteimprove · Pull Request #1592 · Siteimprove/alfa · GitHub
[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R113 follow up #1592

Merged
merged 21 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
N 10000 ext Next commit
Apply suggestions from code review
Co-authored-by: Jean-Yves Moyen <jym@siteimprove.com>
  • Loading branch information
rcj-siteimprove and Jym77 authored Apr 5, 2024
commit 948fab600524526c992aa1dcabcd8f1ad8705736
54 changes: 29 additions & 25 deletions packages/alfa-rectangle/src/rectangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,32 +197,35 @@ export class Rectangle
}

/**
* @remarks
* Checks if the rectangle intersects a given circle.
*
* @remarks
* @see ../docs/circle-rectangle-intersection.png for a visual explanation of the case
* where the circle center lies in one of the corners of the padded rectangle.
*
* @privateRemarks
* To check intersection, we pad the rectangle by the radius of the circle and divide the problem into three cases:
*
* 1. The circle center is outside the padded rectangle.
* 2. The circle center is inside the padded rectangle, but not in one of the corners.
* 3. The circle center lies in one of the corners of the padded rectangle in which case we need to compute the distance to the corner
*
* r
* +-------+-------------------------+-------+
* | | | |
* 1 | | | |
* +-------+-------------------------+-------+
* | | | |
* | | | |
* | | | |
* | | | |
* | | | |
* +-------+-------------------------+-------+
* | | 2 | |
* | 3 | | |
* +-------+-------------------------+-------+
*/
public intersectsCircle(cx: number, cy: number, r: number): boolean {
// To check intersection, we pad the rectangle by the radius of the circle and divide the problem into three cases:
//
// 1. The circle center is outside the padded rectangle.
// 2. The circle center is inside the padded rectangle, but not in one of the corners.
// 3. The circle center lies in one of the corners of the padded rectangle in which case we need to compute the distance to the corner
//
// r
// +-------+-------------------------+-------+
// | | | |
// 1 | | | |
// +-------+-------------------------+-------+
// | | | |
// | | | |
// | | | |
// | | | |
// | | | |
// +-------+-------------------------+-------+
// | | 2 | |
// | 3 | | |
// +-------+-------------------------+-------+

const center = this.center;
const halfWidth = this.width / 2;
Expand All @@ -232,25 +235,26 @@ export class Rectangle
const dy = Math.abs(cy - center.y);

if (dx > halfWidth + r || dy > halfHeight + r) {
// The circle center is outside the padded rectangle
// 1. The circle center is outside the padded rectangle
return false;
}

// The circle center is inside the padded rectangle
if (dx <= halfWidth D682 || dy <= halfHeight) {
// The circle lies at most a radius away from the rectangle in the x or y directions
// 2. The circle lies at most a radius away from the rectangle in the x or y directions
return true;
}

// The circle center lies in one of the corners of the padded rectangle.
// 3. The circle center lies in one of the corners of the padded rectangle.
// If the distance from the circle center to the closest corner of the rectangle
// is less than the radius of the circle, the circle intersects the rectangle.
return (dx - halfWidth) ** 2 + (dy - halfHeight) ** 2 <= r ** 2;
}

/**
* @remarks
* Computes the squared distance between the centers of two rectangles.
*
* @remarks
* The squared distance is used to avoid the expensive square root operation.
* If the actual distance is needed, the square root of the squared distance can be taken.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class WithBoundingBox extends WithName {
return this._condition;
}

public get tooCloseNeighbors(): Sequence<Element> {
public get tooCloseNeighbors(): Iterable<Element> {
return this._tooCloseNeighbors;
}

Expand Down
Loading
0