2 * Copyright (c) 2003, the JUNG Project and the Regents of the University
6 * This software is open-source under the BSD license; see either
8 * http://jung.sourceforge.net/license.txt for a description.
10 package edu.uci.ics.jung.algorithms.layout;
12 import java.awt.geom.Point2D;
15 * Represents a point in polar coordinates: distance and angle from the origin.
16 * Includes conversions between polar and Cartesian
17 * coordinates (Point2D).
19 * @author Tom Nelson - tomnelson@dev.java.net
21 public class PolarPoint
27 * Creates a new instance with radius and angle each 0.
34 * Creates a new instance with radius {@code radius} and angle {@code theta}.
36 public PolarPoint(double theta, double radius) {
42 * Returns the angle for this point.
44 public double getTheta() { return theta; }
47 * Returns the radius for this point.
49 public double getRadius() { return radius; }
52 * Sets the angle for this point to {@code theta}.
54 public void setTheta(double theta) { this.theta = theta; }
57 * Sets the radius for this point to {@code theta}.
59 public void setRadius(double radius) { this.radius = radius; }
62 * Returns the result of converting <code>polar</code> to Cartesian coordinates.
64 public static Point2D polarToCartesian(PolarPoint polar) {
65 return polarToCartesian(polar.getTheta(), polar.getRadius());
69 * Returns the result of converting <code>(theta, radius)</code> to Cartesian coordinates.
71 public static Point2D polarToCartesian(double theta, double radius) {
72 return new Point2D.Double(radius*Math.cos(theta), radius*Math.sin(theta));
76 * Returns the result of converting <code>point</code> to polar coordinates.
78 public static PolarPoint cartesianToPolar(Point2D point) {
79 return cartesianToPolar(point.getX(), point.getY());
83 * Returns the result of converting <code>(x, y)</code> to polar coordinates.
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);
92 public String toString() {
93 return "PolarPoint[" + radius + "," + theta +"]";
97 * Sets the angle and radius of this point to those of {@code p}.
99 public void setLocation(PolarPoint p) {
100 this.theta = p.getTheta();
101 this.radius = p.getRadius();