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

Expand convex hull bounds by half a pixel per side #529

Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/Drawable.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ class Drawable {
const transformedHullPoints = this._getTransformedHullPoints();
// Search through transformed points to generate box on axes.
result = result || new Rectangle();
result.initFromPointsAABB(transformedHullPoints);
result.initFromPointsAABB(transformedHullPoints, Math.max(this._scale[0], this._scale[1]) * 0.01);
return result;
}

Expand All @@ -545,7 +545,7 @@ class Drawable {
const filteredHullPoints = transformedHullPoints.filter(p => p[1] > maxY - slice);
// Search through filtered points to generate box on axes.
result = result || new Rectangle();
result.initFromPointsAABB(filteredHullPoints);
result.initFromPointsAABB(filteredHullPoints, Math.max(this._scale[0], this._scale[1]) * 0.01);
return result;
}

Expand Down
26 changes: 17 additions & 9 deletions src/Rectangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,35 @@ class Rectangle {
/**
* Initialize a Rectangle to the minimum AABB around a set of points.
* @param {Array<Array<number>>} points Array of [x, y] points.
* @param {number} scale The "Scratch-space" area under each point sample.
*/
initFromPointsAABB (points) {
initFromPointsAABB (points, scale = 0) {
this.left = Infinity;
this.right = -Infinity;
this.top = -Infinity;
this.bottom = Infinity;

// Each point is the center of a pixel. However, said pixels each have area.
// We can think of the `scale` parameter as the 'diameter' of each area sample centered around each point.
// The bounds must be extended by the 'radius' of each sample on every side.
// Since nearest-neighbor sampling gives pixels a square area, the radius is sqrt(2) / 2 = 0.707...
// at its longest (from the center of the pixel to one of its corners).
const halfPixel = 0.7071067811865476 * scale;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const halfPixel = 0.7071067811865476 * scale;
const halfPixel = Math.SQRT1_2 * scale;


for (let i = 0; i < points.length; i++) {
const x = points[i][0];
const y = points[i][1];
if (x < this.left) {
this.left = x;
if ((x - halfPixel) < this.left) {
this.left = x - halfPixel;
}
if (x > this.right) {
this.right = x;
if ((x + halfPixel) > this.right) {
this.right = x + halfPixel;
}
if (y > this.top) {
this.top = y;
if ((y + halfPixel) > this.top) {
this.top = y + halfPixel;
}
if (y < this.bottom) {
this.bottom = y;
if ((y - halfPixel) < this.bottom) {
this.bottom = y - halfPixel;
}
}
}
Expand Down