1
2
3
4
5
6
7
8
9
10
11 package edu.uci.ics.jung.algorithms.layout;
12 import java.awt.Dimension;
13 import java.awt.geom.Point2D;
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import edu.uci.ics.jung.graph.Forest;
18
19
20
21
22
23
24
25 public class RadialTreeLayout<V,E> extends TreeLayout<V,E> {
26
27 protected Map<V,PolarPoint> polarLocations;
28
29 public RadialTreeLayout(Forest<V,E> g) {
30 this(g, DEFAULT_DISTX, DEFAULT_DISTY);
31 }
32
33 public RadialTreeLayout(Forest<V,E> g, int distx) {
34 this(g, distx, DEFAULT_DISTY);
35 }
36
37 public RadialTreeLayout(Forest<V,E> g, int distx, int disty) {
38 super(g, distx, disty);
39 }
40
41 @Override
42 protected void buildTree() {
43 super.buildTree();
44 this.polarLocations = new HashMap<V, PolarPoint>();
45 setRadialLocations();
46 }
47
48 @Override
49 public void setSize(Dimension size) {
50 this.size = size;
51 buildTree();
52 }
53
54 @Override
55 protected void setCurrentPositionFor(V vertex) {
56 locations.getUnchecked(vertex).setLocation(m_currentPoint);
57 }
58
59 @Override
60 public void setLocation(V v, Point2D location)
61 {
62 Point2D c = getCenter();
63 Point2D pv = new Point2D.Double(location.getX() - c.getX(),
64 location.getY() - c.getY());
65 PolarPoint newLocation = PolarPoint.cartesianToPolar(pv);
66 PolarPoint currentLocation = polarLocations.get(v);
67 if (currentLocation == null)
68 polarLocations.put(v, newLocation);
69 else
70 currentLocation.setLocation(newLocation);
71 }
72
73
74
75
76 public Map<V,PolarPoint> getPolarLocations() {
77 return polarLocations;
78 }
79
80 @Override
81 public Point2D apply(V v) {
82 PolarPoint pp = polarLocations.get(v);
83 double centerX = getSize().getWidth()/2;
84 double centerY = getSize().getHeight()/2;
85 Point2D cartesian = PolarPoint.polarToCartesian(pp);
86 cartesian.setLocation(cartesian.getX()+centerX,cartesian.getY()+centerY);
87 return cartesian;
88 }
89
90 private Point2D getMaxXY() {
91 double maxx = 0;
92 double maxy = 0;
93 for(Point2D p : locations.asMap().values()) {
94 maxx = Math.max(maxx, p.getX());
95 maxy = Math.max(maxy, p.getY());
96 }
97 return new Point2D.Double(maxx,maxy);
98 }
99
100 private void setRadialLocations() {
101 Point2D max = getMaxXY();
102 double maxx = max.getX();
103 double maxy = max.getY();
104 maxx = Math.max(maxx, size.width);
105 double theta = 2*Math.PI/maxx;
106
107 double deltaRadius = size.width/2/maxy;
108 for(Map.Entry<V, Point2D> entry : locations.asMap().entrySet()) {
109 V v = entry.getKey();
110 Point2D p = entry.getValue();
111 PolarPoint polarPoint =
112 new PolarPoint(p.getX()*theta, (p.getY() - this.distY)*deltaRadius);
113 polarLocations.put(v, polarPoint);
114 }
115 }
116 }