Skip to content

Commit

Permalink
Improve performance: to find the closest atom, we do can simply use t…
Browse files Browse the repository at this point in the history
…he squared distances. The smaller than relation is equivalent in normal and squared distance space.

Signed-off-by: Rajarshi  Guha <rajarshi.guha@gmail.com>
  • Loading branch information
egonw authored and rajarshi committed Feb 21, 2010
1 parent 563fe28 commit 46b5f83
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/main/org/openscience/cdk/geometry/GeometryTools.java
Expand Up @@ -733,18 +733,21 @@ public static IAtom getClosestAtom(IAtomContainer atomCon, IAtom atom) {
public static IAtom getClosestAtom(double xPosition, double yPosition, IAtomContainer atomCon, IAtom toignore) {
IAtom closestAtom = null;
IAtom currentAtom;
double smallestMouseDistance = -1;
double mouseDistance;
// we compare squared distances, allowing us to do one sqrt()
// calculation less
double smallestSquaredMouseDistance = -1;
double mouseSquaredDistance;
double atomX;
double atomY;
for (int i = 0; i < atomCon.getAtomCount(); i++) {
currentAtom = atomCon.getAtom(i);
if(currentAtom!=toignore){
atomX = currentAtom.getPoint2d().x;
atomY = currentAtom.getPoint2d().y;
mouseDistance = Math.sqrt(Math.pow(atomX - xPosition, 2) + Math.pow(atomY - yPosition, 2));
if (mouseDistance < smallestMouseDistance || smallestMouseDistance == -1) {
smallestMouseDistance = mouseDistance;
mouseSquaredDistance = Math.pow(atomX - xPosition, 2) +
Math.pow(atomY - yPosition, 2);
if (mouseSquaredDistance < smallestSquaredMouseDistance || smallestSquaredMouseDistance == -1) {
smallestSquaredMouseDistance = mouseSquaredDistance;
closestAtom = currentAtom;
}
}
Expand Down

0 comments on commit 46b5f83

Please sign in to comment.