From f8772eab6dc48728da5991cdaba6dacd32adcf0e Mon Sep 17 00:00:00 2001 From: Alois Zoitl Date: Sat, 28 Sep 2024 23:12:16 +0200 Subject: [PATCH] Introduce getDistanceSquared for deprecated getDistance2 #21 Having the squared distance between two points can be used often for simple sorting of points. It is more efficient then calculating the real distance. However the old getDistanc2's name was not very intuitive. With this change we have an appropriate replacement. Addresses https://github.com/eclipse/gef-classic/issues/21 --- .../src/org/eclipse/draw2d/test/PointTests.java | 6 +++--- org.eclipse.draw2d/META-INF/MANIFEST.MF | 2 +- .../src/org/eclipse/draw2d/geometry/Point.java | 15 ++++++++++++++- .../logicdesigner/figures/NodeFigure.java | 8 ++++---- .../gef/editpolicies/BendpointEditPolicy.java | 4 ++-- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/PointTests.java b/org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/PointTests.java index 4205d4b03..fb305b190 100644 --- a/org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/PointTests.java +++ b/org.eclipse.draw2d.tests/src/org/eclipse/draw2d/test/PointTests.java @@ -186,9 +186,9 @@ public void testGetDifferencePoint() { @SuppressWarnings("static-method") @Test - public void testGetDistance2Point() { - assertEquals(25, new Point(4, 7).getDistance2(new Point(1, 3))); - assertEquals(25, new Point(-1, -2).getDistance2(new Point(-5, 1))); + public void testGetDistanceSquaredPoint() { + assertEquals(25, new Point(4, 7).getDistanceSquared(new Point(1, 3))); + assertEquals(25, new Point(-1, -2).getDistanceSquared(new Point(-5, 1))); } @SuppressWarnings("static-method") diff --git a/org.eclipse.draw2d/META-INF/MANIFEST.MF b/org.eclipse.draw2d/META-INF/MANIFEST.MF index dc8ccb3a4..795c0c621 100644 --- a/org.eclipse.draw2d/META-INF/MANIFEST.MF +++ b/org.eclipse.draw2d/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %Plugin.name Bundle-SymbolicName: org.eclipse.draw2d;singleton:=true -Bundle-Version: 3.17.100.qualifier +Bundle-Version: 3.18.0.qualifier Bundle-Vendor: %Plugin.providerName Bundle-Localization: plugin Export-Package: org.eclipse.draw2d, diff --git a/org.eclipse.draw2d/src/org/eclipse/draw2d/geometry/Point.java b/org.eclipse.draw2d/src/org/eclipse/draw2d/geometry/Point.java index 2a1559ad0..403d161f2 100644 --- a/org.eclipse.draw2d/src/org/eclipse/draw2d/geometry/Point.java +++ b/org.eclipse.draw2d/src/org/eclipse/draw2d/geometry/Point.java @@ -198,10 +198,23 @@ public double getDistance(Point p) { * @return distance2 * @since 2.0 * @noreference This method is not intended to be referenced by clients. - * @deprecated Use {@link #getDistance(Point)} and square the result instead. + * @deprecated Use {@link #getDistanceSquared(Point)}. */ @Deprecated(since = "3.7", forRemoval = true) public int getDistance2(Point p) { + return getDistanceSquared(p); + } + + /** + * Calculates the distance squared between this Point and the one specified. If + * the distance squared is larger than the maximum integer value, then + * Integer.MAX_VALUE will be returned. + * + * @param p The reference Point + * @return distance2 + * @since 3.18 + */ + public int getDistanceSquared(Point p) { long i = p.x() - x; long j = p.y() - y; long result = i * i + j * j; diff --git a/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/figures/NodeFigure.java b/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/figures/NodeFigure.java index 941bb1c60..33b88a515 100644 --- a/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/figures/NodeFigure.java +++ b/org.eclipse.gef.examples.logic/src/org/eclipse/gef/examples/logicdesigner/figures/NodeFigure.java @@ -34,7 +34,7 @@ public ConnectionAnchor connectionAnchorAt(Point p) { for (ConnectionAnchor c : getSourceConnectionAnchors()) { Point p2 = c.getLocation(null); - long d = p.getDistance2(p2); + long d = p.getDistanceSquared(p2); if (d < min) { min = d; closest = c; @@ -42,7 +42,7 @@ public ConnectionAnchor connectionAnchorAt(Point p) { } for (ConnectionAnchor c : getTargetConnectionAnchors()) { Point p2 = c.getLocation(null); - long d = p.getDistance2(p2); + long d = p.getDistanceSquared(p2); if (d < min) { min = d; closest = c; @@ -70,7 +70,7 @@ public ConnectionAnchor getSourceConnectionAnchorAt(Point p) { for (ConnectionAnchor c : getSourceConnectionAnchors()) { Point p2 = c.getLocation(null); - long d = p.getDistance2(p2); + long d = p.getDistanceSquared(p2); if (d < min) { min = d; closest = c; @@ -89,7 +89,7 @@ public ConnectionAnchor getTargetConnectionAnchorAt(Point p) { for (ConnectionAnchor c : getTargetConnectionAnchors()) { Point p2 = c.getLocation(null); - long d = p.getDistance2(p2); + long d = p.getDistanceSquared(p2); if (d < min) { min = d; closest = c; diff --git a/org.eclipse.gef/src/org/eclipse/gef/editpolicies/BendpointEditPolicy.java b/org.eclipse.gef/src/org/eclipse/gef/editpolicies/BendpointEditPolicy.java index 87d7a5b56..3e3f8e0b4 100644 --- a/org.eclipse.gef/src/org/eclipse/gef/editpolicies/BendpointEditPolicy.java +++ b/org.eclipse.gef/src/org/eclipse/gef/editpolicies/BendpointEditPolicy.java @@ -315,9 +315,9 @@ private void setReferencePoints(BendpointRequest request) { // points include the bend points as well as start and end, which we may // leave out when searching for the bend point index for (int i = 1; i < points.size() - 1; i++) { - if (smallestDistance == -1 || points.getPoint(i).getDistance2(bp) < smallestDistance) { + if (smallestDistance == -1 || points.getPoint(i).getDistanceSquared(bp) < smallestDistance) { bpIndex = i; - smallestDistance = points.getPoint(i).getDistance2(bp); + smallestDistance = points.getPoint(i).getDistanceSquared(bp); if (smallestDistance == 0) { break; }