2 * Copyright (c) 2005, the JUNG Project and the Regents of the University of
3 * California All rights reserved.
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.
8 * Created on Jul 9, 2005
11 package edu.uci.ics.jung.algorithms.layout;
12 import java.awt.Dimension;
13 import java.awt.geom.Point2D;
14 import java.util.HashMap;
17 import edu.uci.ics.jung.graph.Forest;
20 * A radial layout for Tree or Forest graphs.
25 public class RadialTreeLayout<V,E> extends TreeLayout<V,E> {
27 protected Map<V,PolarPoint> polarLocations;
30 * Creates an instance for the specified graph with default X and Y distances.
32 public RadialTreeLayout(Forest<V,E> g) {
33 this(g, DEFAULT_DISTX, DEFAULT_DISTY);
37 * Creates an instance for the specified graph and X distance with
40 public RadialTreeLayout(Forest<V,E> g, int distx) {
41 this(g, distx, DEFAULT_DISTY);
45 * Creates an instance for the specified graph, X distance, and Y distance.
47 public RadialTreeLayout(Forest<V,E> g, int distx, int disty) {
48 super(g, distx, disty);
52 protected void buildTree() {
54 this.polarLocations = new HashMap<V, PolarPoint>();
59 public void setSize(Dimension size) {
65 protected void setCurrentPositionFor(V vertex) {
66 locations.get(vertex).setLocation(m_currentPoint);
70 public void setLocation(V v, Point2D location)
72 Point2D c = getCenter();
73 Point2D pv = new Point2D.Double(location.getX() - c.getX(),
74 location.getY() - c.getY());
75 PolarPoint newLocation = PolarPoint.cartesianToPolar(pv);
76 PolarPoint currentLocation = polarLocations.get(v);
77 if (currentLocation == null)
78 polarLocations.put(v, newLocation);
80 currentLocation.setLocation(newLocation);
84 * Returns the map from vertices to their locations in polar coordinates.
86 public Map<V,PolarPoint> getPolarLocations() {
87 return polarLocations;
91 public Point2D transform(V v) {
92 PolarPoint pp = polarLocations.get(v);
93 double centerX = getSize().getWidth()/2;
94 double centerY = getSize().getHeight()/2;
95 Point2D cartesian = PolarPoint.polarToCartesian(pp);
96 cartesian.setLocation(cartesian.getX()+centerX,cartesian.getY()+centerY);
100 private Point2D getMaxXY() {
103 for(Point2D p : locations.values()) {
104 maxx = Math.max(maxx, p.getX());
105 maxy = Math.max(maxy, p.getY());
107 return new Point2D.Double(maxx,maxy);
110 private void setRadialLocations() {
111 Point2D max = getMaxXY();
112 double maxx = max.getX();
113 double maxy = max.getY();
114 maxx = Math.max(maxx, size.width);
115 double theta = 2*Math.PI/maxx;
117 double deltaRadius = size.width/2/maxy;
118 for(Map.Entry<V, Point2D> entry : locations.entrySet()) {
119 V v = entry.getKey();
120 Point2D p = entry.getValue();
121 PolarPoint polarPoint = new PolarPoint(p.getX()*theta, (p.getY() - this.distY)*deltaRadius);
122 polarLocations.put(v, polarPoint);