Initial opendaylight infrastructure commit!!
[controller.git] / third-party / net.sf.jung2 / src / main / java / edu / uci / ics / jung / algorithms / layout / PolarPoint.java
1 /*
2 * Copyright (c) 2003, the JUNG Project and the Regents of the University 
3 * of California
4 * All rights reserved.
5 *
6 * This software is open-source under the BSD license; see either
7 * "license.txt" or
8 * http://jung.sourceforge.net/license.txt 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 radius {@code radius} and angle {@code theta}.
35          */
36         public PolarPoint(double theta, double radius) {
37                 this.theta = theta;
38                 this.radius = radius;
39         }
40         
41         /**
42          * Returns the angle for this point.
43          */
44         public double getTheta() { return theta; }
45
46         /**
47          * Returns the radius for this point.
48          */
49         public double getRadius() { return radius; }
50         
51         /**
52          * Sets the angle for this point to {@code theta}.
53          */
54         public void setTheta(double theta) { this.theta = theta; }
55         
56         /**
57          * Sets the radius for this point to {@code theta}.
58          */
59         public void setRadius(double radius) { this.radius = radius; }
60
61         /**
62          * Returns the result of converting <code>polar</code> to Cartesian coordinates.
63          */
64         public static Point2D polarToCartesian(PolarPoint polar) {
65                 return polarToCartesian(polar.getTheta(), polar.getRadius());
66         }
67
68         /**
69          * Returns the result of converting <code>(theta, radius)</code> to Cartesian coordinates.
70          */
71         public static Point2D polarToCartesian(double theta, double radius) {
72                 return new Point2D.Double(radius*Math.cos(theta), radius*Math.sin(theta));
73         }
74
75         /**
76          * Returns 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          * Returns the result of converting <code>(x, y)</code> to polar coordinates.
84          */
85         public static PolarPoint cartesianToPolar(double x, double y) {
86                 double theta = Math.atan2(y,x);
87                 double radius = Math.sqrt(x*x+y*y);
88                 return new PolarPoint(theta, radius);
89         }
90         
91         @Override
92         public String toString() {
93             return "PolarPoint[" + radius + "," + theta +"]";
94         }
95         
96         /**
97          * Sets the angle and radius of this point to those of {@code p}.
98          */
99         public void setLocation(PolarPoint p) {
100                 this.theta = p.getTheta();
101                 this.radius = p.getRadius();
102         }
103 }