View Javadoc
1   /*
2   * Copyright (c) 2003, The JUNG Authors 
3   *
4   * All rights reserved.
5   *
6   * This software is open-source under the BSD license; see either
7   * "license.txt" or
8   * https://github.com/jrtom/jung/blob/master/LICENSE for a description.
9   */
10  package edu.uci.ics.jung.algorithms.layout;
11  
12  import java.awt.geom.Point2D;
13  
14  /**
15   * Represents a point in polar coordinates: distance and angle from the origin.
16   * Includes conversions between polar and Cartesian
17   * coordinates (Point2D).
18   * 
19   * @author Tom Nelson - tomnelson@dev.java.net
20   */
21  public class PolarPoint 
22  {
23  	double theta;
24  	double radius;
25  	
26  	/**
27  	 * Creates a new instance with radius and angle each 0.
28  	 */
29  	public PolarPoint() {
30  		this(0,0);
31  	}
32  
33  	/**
34  	 * Creates a new instance with the specified radius and angle.
35  	 * @param theta the angle of the point to create
36  	 * @param radius the distance from the origin of the point to create
37  	 */
38  	public PolarPoint(double theta, double radius) {
39  		this.theta = theta;
40  		this.radius = radius;
41  	}
42  	
43  	/**
44  	 * @return the angle for this point
45  	 */
46  	public double getTheta() { return theta; }
47  
48  	/**
49  	 * @return the radius for this point
50  	 */
51  	public double getRadius() { return radius; }
52  	
53  	public void setTheta(double theta) { this.theta = theta; }
54  	
55  	public void setRadius(double radius) { this.radius = radius; }
56  
57  	/**
58  	 * @param polar the input location to convert
59  	 * @return the result of converting <code>polar</code> to Cartesian coordinates.
60  	 */
61  	public static Point2D polarToCartesian(PolarPoint polar) {
62  		return polarToCartesian(polar.getTheta(), polar.getRadius());
63  	}
64  
65  	/**
66  	 * @param theta the angle of the input location
67  	 * @param radius the distance from the origin of the input location
68  	 * @return the result of converting <code>(theta, radius)</code> to Cartesian coordinates.
69  	 */
70  	public static Point2D polarToCartesian(double theta, double radius) {
71  		return new Point2D.Double(radius*Math.cos(theta), radius*Math.sin(theta));
72  	}
73  
74  	/**
75  	 * @param point the input location
76  	 * @return the result of converting <code>point</code> to polar coordinates.
77  	 */
78  	public static PolarPoint cartesianToPolar(Point2D point) {
79  		return cartesianToPolar(point.getX(), point.getY());
80  	}
81  
82  	/**
83  	 * @param x the x coordinate of the input location
84  	 * @param y the y coordinate of the input location
85  	 * @return the result of converting <code>(x, y)</code> to polar coordinates.
86  	 */
87  	public static PolarPoint cartesianToPolar(double x, double y) {
88  		double theta = Math.atan2(y,x);
89  		double radius = Math.sqrt(x*x+y*y);
90  		return new PolarPoint(theta, radius);
91  	}
92  	
93  	@Override
94  	public String toString() {
95  	    return "PolarPoint[" + radius + "," + theta +"]";
96  	}
97  	
98  	/**
99  	 * Sets the angle and radius of this point to those of {@code p}.
100 	 * @param p the point whose location is copied into this instance
101 	 */
102 	public void setLocation(PolarPoint p) {
103 		this.theta = p.getTheta();
104 		this.radius = p.getRadius();
105 	}
106 }