From: <sh...@us...> - 2006-04-25 14:12:45
|
Revision: 6074 Author: shk3 Date: 2006-04-25 07:12:36 -0700 (Tue, 25 Apr 2006) ViewCVS: http://svn.sourceforge.net/cdk/?rev=6074&view=rev Log Message: ----------- more fixes for bond highlighting Modified Paths: -------------- trunk/cdk/src/org/openscience/cdk/controller/AbstractController2D.java trunk/cdk/src/org/openscience/cdk/geometry/GeometryTools.java Modified: trunk/cdk/src/org/openscience/cdk/controller/AbstractController2D.java =================================================================== --- trunk/cdk/src/org/openscience/cdk/controller/AbstractController2D.java 2006-04-25 13:49:52 UTC (rev 6073) +++ trunk/cdk/src/org/openscience/cdk/controller/AbstractController2D.java 2006-04-25 14:12:36 UTC (rev 6074) @@ -1700,7 +1700,7 @@ double highlightRadius = r2dm.getHighlightRadius(); IAtomContainer atomCon = ChemModelManipulator.getAllInOneContainer(chemModel); if (atomCon.getBondCount() != 0) { - IBond closestBond = GeometryTools.getClosestBond(X, Y, atomCon); + IBond closestBond = GeometryTools.getClosestBond(X, Y, atomCon,r2dm.getRenderingCoordinates()); if (closestBond == null) { return null; Modified: trunk/cdk/src/org/openscience/cdk/geometry/GeometryTools.java =================================================================== --- trunk/cdk/src/org/openscience/cdk/geometry/GeometryTools.java 2006-04-25 13:49:52 UTC (rev 6073) +++ trunk/cdk/src/org/openscience/cdk/geometry/GeometryTools.java 2006-04-25 14:12:36 UTC (rev 6074) @@ -400,6 +400,27 @@ *@param atoms The vector of the given atoms *@return The center of the given atoms as Point2d */ + public static Point2d get2DCenter(IAtom[] atoms, HashMap renderingCoordinates) { + IAtom atom; + double x = 0; + double y = 0; + for (int f = 0; f < atoms.length; f++) { + atom = (IAtom) atoms[f]; + if (atom.getPoint2d() != null) { + x += ((Point2d)renderingCoordinates.get(atom)).x; + y += ((Point2d)renderingCoordinates.get(atom)).y; + } + } + return new Point2d(x / (double) atoms.length, y / (double) atoms.length); + } + + + /** + * Calculates the center of the given atoms and returns it as a Point2d + * + *@param atoms The vector of the given atoms + *@return The center of the given atoms as Point2d + */ public static Point2d get2DCenter(IAtom[] atoms) { IAtom atom; double x = 0; @@ -746,6 +767,35 @@ *@param atomCon The molecule that is searched for the closest bond *@return The bond that is closest to the given coordinates */ + public static IBond getClosestBond(int xPosition, int yPosition, IAtomContainer atomCon, HashMap renderingCoordinates) { + Point2d bondCenter; + IBond closestBond = null; + IBond currentBond; + double smallestMouseDistance = -1; + double mouseDistance; + IBond[] bonds = atomCon.getBonds(); + for (int i = 0; i < bonds.length; i++) { + currentBond = bonds[i]; + bondCenter = get2DCenter(currentBond.getAtoms(),renderingCoordinates); + mouseDistance = Math.sqrt(Math.pow(bondCenter.x - xPosition, 2) + Math.pow(bondCenter.y - yPosition, 2)); + if (mouseDistance < smallestMouseDistance || smallestMouseDistance == -1) { + smallestMouseDistance = mouseDistance; + closestBond = currentBond; + } + } + return closestBond; + } + + + /** + * Returns the bond of the given molecule that is closest to the given + * coordinates. + * + *@param xPosition The x coordinate + *@param yPosition The y coordinate + *@param atomCon The molecule that is searched for the closest bond + *@return The bond that is closest to the given coordinates + */ public static IBond getClosestBond(int xPosition, int yPosition, IAtomContainer atomCon) { Point2d bondCenter; IBond closestBond = null; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |