| Line | Hits | Source |
|---|---|---|
| 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) { | |
| 37 | 0 | this(l, Math.sqrt(Double.MAX_VALUE - 1000)); |
| 38 | 0 | } |
| 39 | ||
| 40 | 0 | public RadiusGraphElementAccessor(Layout l, double maxDistance) { |
| 41 | 0 | this.maxDistance = maxDistance; |
| 42 | 0 | this.layout = l; |
| 43 | 0 | } |
| 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) { | |
| 52 | 0 | 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) { | |
| 65 | 0 | double minDistance = maxDistance * maxDistance; |
| 66 | 0 | Vertex closest = null; |
| 67 | while(true) { | |
| 68 | try { | |
| 69 | 0 | for (Iterator iter = layout.getGraph().getVertices().iterator(); |
| 70 | 0 | iter.hasNext(); |
| 71 | ) { | |
| 72 | 0 | Vertex v = (Vertex) iter.next(); |
| 73 | 0 | Point2D p = layout.getLocation(v); |
| 74 | 0 | double dx = p.getX() - x; |
| 75 | 0 | double dy = p.getY() - y; |
| 76 | 0 | double dist = dx * dx + dy * dy; |
| 77 | 0 | if (dist < minDistance) { |
| 78 | 0 | minDistance = dist; |
| 79 | 0 | closest = v; |
| 80 | } | |
| 81 | } | |
| 82 | 0 | break; |
| 83 | 0 | } catch(ConcurrentModificationException cme) {} |
| 84 | } | |
| 85 | 0 | 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) { | |
| 93 | 0 | 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) { | |
| 108 | 0 | double minDistance = maxDistance * maxDistance; |
| 109 | 0 | Edge closest = null; |
| 110 | while(true) { | |
| 111 | try { | |
| 112 | 0 | for (Iterator iter = layout.getGraph().getEdges().iterator(); iter.hasNext();) { |
| 113 | 0 | Edge e = (Edge) iter.next(); |
| 114 | // if anyone uses a hyperedge, this is too complex. | |
| 115 | 0 | if (e.numVertices() != 2) |
| 116 | 0 | continue; |
| 117 | // Could replace all this set stuff with getFrom_internal() etc. | |
| 118 | 0 | Set vertices = e.getIncidentVertices(); |
| 119 | 0 | Iterator vertexIterator = vertices.iterator(); |
| 120 | 0 | Vertex v1 = (Vertex) vertexIterator.next(); |
| 121 | 0 | Vertex v2 = (Vertex) vertexIterator.next(); |
| 122 | // Get coords | |
| 123 | 0 | Point2D p1 = layout.getLocation(v1); |
| 124 | 0 | Point2D p2 = layout.getLocation(v2); |
| 125 | 0 | double x1 = p1.getX(); |
| 126 | 0 | double y1 = p1.getY(); |
| 127 | 0 | double x2 = p2.getX(); |
| 128 | 0 | double y2 = p2.getY(); |
| 129 | // Calculate location on line closest to (x,y) | |
| 130 | // First, check that v1 and v2 are not coincident. | |
| 131 | 0 | if (x1 == x2 && y1 == y2) |
| 132 | 0 | continue; |
| 133 | 0 | 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 | |
| 138 | 0 | if (b <= 0) |
| 139 | 0 | distance2 = (x - x1) * (x - x1) + (y - y1) * (y - y1); |
| 140 | 0 | else if (b >= 1) |
| 141 | 0 | distance2 = (x - x2) * (x - x2) + (y - y2) * (y - y2); |
| 142 | else { | |
| 143 | 0 | double x3 = x1 + b * (x2 - x1); |
| 144 | 0 | double y3 = y1 + b * (y2 - y1); |
| 145 | 0 | distance2 = (x - x3) * (x - x3) + (y - y3) * (y - y3); |
| 146 | } | |
| 147 | ||
| 148 | 0 | if (distance2 < minDistance) { |
| 149 | 0 | minDistance = distance2; |
| 150 | 0 | closest = e; |
| 151 | } | |
| 152 | } | |
| 153 | 0 | break; |
| 154 | 0 | } catch(ConcurrentModificationException cme) {} |
| 155 | } | |
| 156 | 0 | return closest; |
| 157 | } | |
| 158 | ||
| 159 | public void setLayout(Layout l) | |
| 160 | { | |
| 161 | 0 | this.layout = l; |
| 162 | 0 | } |
| 163 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |