Skip to content

Commit

Permalink
Add Precision Version of getDistanceSquared
Browse files Browse the repository at this point in the history
PrecisionPoint provides now its own getDistanceSquared which uses the
precise coordinates to reduce rounding errors in the distance
calculations.
  • Loading branch information
azoitl committed Jan 18, 2025
1 parent 3a971a5 commit 7a72888
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ public int getDistanceOrthogonal(Point p) {
return (int) (Math.abs(this.preciseX() - p.preciseX()) + Math.abs(this.preciseY() - p.preciseY()));
}

/**
* provide a precision version of
* org.eclipse.draw2d.geometry.Point#getDistanceSquared(org.eclipse.draw2d.geometry.Point)
*
* @since 3.19
*/
@Override
public int getDistanceSquared(Point p) {
double deltaX = p.preciseX() - this.preciseX();
double deltaY = p.preciseX() - this.preciseX();
long result = (long) (deltaX * deltaX + deltaY * deltaY);
if (result > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
return (int) result;
}

/**
* Returns a precise copy of this.
*
Expand Down

0 comments on commit 7a72888

Please sign in to comment.