Coverage details for edu.uci.ics.jung.visualization.RadiusGraphElementAccessor

LineHitsSource
1 /*
2  * Copyright (c) 2005, the JUNG Project and the Regents of the University of
3  * California All rights reserved.
4  *
5  * This software is open-source under the BSD license; see either "license.txt"
6  * or http://jung.sourceforge.net/license.txt for a description.
7  *
8  *
9  * Created on Apr 12, 2005
10  */
11 package edu.uci.ics.jung.visualization;
12  
13 import java.awt.geom.Point2D;
14 import java.util.ConcurrentModificationException;
15 import java.util.Iterator;
16 import java.util.Set;
17  
18 import edu.uci.ics.jung.graph.Edge;
19 import edu.uci.ics.jung.graph.Vertex;
20  
21  
22 /**
23  * Simple implementation of PickSupport that returns the vertex or edge
24  * that is closest to the specified location. This implementation
25  * provides the same picking options that were available in
26  * previous versions of AbstractLayout.
27  *
28  * @author Tom Nelson
29  * @author Joshua O'Madadhain
30  */
31 public class RadiusGraphElementAccessor implements GraphElementAccessor {
32     
33     protected Layout layout;
34     protected double maxDistance;
35     
36     public RadiusGraphElementAccessor(Layout l) {
370        this(l, Math.sqrt(Double.MAX_VALUE - 1000));
380    }
39     
400    public RadiusGraphElementAccessor(Layout l, double maxDistance) {
410        this.maxDistance = maxDistance;
420        this.layout = l;
430    }
44     
45     /**
46      * Gets the vertex nearest to the location of the (x,y) location selected,
47      * within a distance of <tt>maxDistance</tt>. Iterates through all
48      * visible vertices and checks their distance from the click. Override this
49      * method to provde a more efficient implementation.
50      */
51     public Vertex getVertex(double x, double y) {
520        return getVertex(x, y, this.maxDistance);
53     }
54  
55     /**
56      * Gets the vertex nearest to the location of the (x,y) location selected,
57      * within a distance of <tt>maxDistance</tt>. Iterates through all
58      * visible vertices and checks their distance from the click. Override this
59      * method to provde a more efficient implementation.
60      * @param x
61      * @param y
62      * @param maxDistance temporarily overrides member maxDistance
63      */
64     public Vertex getVertex(double x, double y, double maxDistance) {
650        double minDistance = maxDistance * maxDistance;
660        Vertex closest = null;
67         while(true) {
68             try {
690                for (Iterator iter = layout.getGraph().getVertices().iterator();
700                iter.hasNext();
71                 ) {
720                    Vertex v = (Vertex) iter.next();
730                    Point2D p = layout.getLocation(v);
740                    double dx = p.getX() - x;
750                    double dy = p.getY() - y;
760                    double dist = dx * dx + dy * dy;
770                    if (dist < minDistance) {
780                        minDistance = dist;
790                        closest = v;
80                     }
81                 }
820                break;
830            } catch(ConcurrentModificationException cme) {}
84         }
850        return closest;
86     }
87     
88     /**
89      * Gets the edge nearest to the location of the (x,y) location selected.
90      * Calls the longer form of the call.
91      */
92     public Edge getEdge(double x, double y) {
930        return getEdge(x, y, this.maxDistance);
94     }
95  
96     /**
97      * Gets the edge nearest to the location of the (x,y) location selected,
98      * within a distance of <tt>maxDistance</tt>, Iterates through all
99      * visible edges and checks their distance from the click. Override this
100      * method to provide a more efficient implementation.
101      *
102      * @param x
103      * @param y
104      * @param maxDistance temporarily overrides member maxDistance
105      * @return Edge closest to the click.
106      */
107     public Edge getEdge(double x, double y, double maxDistance) {
1080        double minDistance = maxDistance * maxDistance;
1090        Edge closest = null;
110         while(true) {
111             try {
1120                for (Iterator iter = layout.getGraph().getEdges().iterator(); iter.hasNext();) {
1130                    Edge e = (Edge) iter.next();
114                     // if anyone uses a hyperedge, this is too complex.
1150                    if (e.numVertices() != 2)
1160                        continue;
117                     // Could replace all this set stuff with getFrom_internal() etc.
1180                    Set vertices = e.getIncidentVertices();
1190                    Iterator vertexIterator = vertices.iterator();
1200                    Vertex v1 = (Vertex) vertexIterator.next();
1210                    Vertex v2 = (Vertex) vertexIterator.next();
122                     // Get coords
1230                    Point2D p1 = layout.getLocation(v1);
1240                    Point2D p2 = layout.getLocation(v2);
1250                    double x1 = p1.getX();
1260                    double y1 = p1.getY();
1270                    double x2 = p2.getX();
1280                    double y2 = p2.getY();
129                     // Calculate location on line closest to (x,y)
130                     // First, check that v1 and v2 are not coincident.
1310                    if (x1 == x2 && y1 == y2)
1320                        continue;
1330                    double b =
134                         ((y - y1) * (y2 - y1) + (x - x1) * (x2 - x1))
135                         / ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
136                     //
137                     double distance2; // square of the distance
1380                    if (b <= 0)
1390                        distance2 = (x - x1) * (x - x1) + (y - y1) * (y - y1);
1400                    else if (b >= 1)
1410                        distance2 = (x - x2) * (x - x2) + (y - y2) * (y - y2);
142                     else {
1430                        double x3 = x1 + b * (x2 - x1);
1440                        double y3 = y1 + b * (y2 - y1);
1450                        distance2 = (x - x3) * (x - x3) + (y - y3) * (y - y3);
146                     }
147                     
1480                    if (distance2 < minDistance) {
1490                        minDistance = distance2;
1500                        closest = e;
151                     }
152                 }
1530                break;
1540            } catch(ConcurrentModificationException cme) {}
155         }
1560        return closest;
157     }
158  
159     public void setLayout(Layout l)
160     {
1610        this.layout = l;
1620    }
163 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.