1 /*
2 * Copyright (c) 2003-2008 by Cosylab d. d.
3 *
4 * This file is part of CosyBeans-Common.
5 *
6 * CosyBeans-Common is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CosyBeans-Common is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CosyBeans-Common. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package com.cosylab.gui.components.gauger;
21
22 import java.awt.Point;
23 import java.awt.geom.Point2D;
24
25
26 /**
27 * Class representing polar point defined by radius and angle.
28 *
29 * @author <a href="mailto:ales.pucelj@cosylab.com">Ales Pucelj</a>
30 * @version $id$
31 */
32 public class PolarPoint
33 {
34 private static final double DEG_TO_RAD = Math.PI / 180.0;
35 private static final double RAD_TO_DEG = 180.0 / Math.PI;
36
37 private double r;
38
39 private double phi;
40
41 /**
42 * Creates a new PolarPoint object.
43 */
44 public PolarPoint()
45 {
46 }
47
48 /**
49 * Creates a new PolarPoint object.
50 *
51 * @param r Radius parameter.
52 * @param phi Angle parameter in radians.
53 */
54 public PolarPoint(double r, double phi)
55 {
56 this.r = r;
57 this.phi = phi;
58 }
59
60 /**
61 * Converts degrees to radians.
62 *
63 * @param angle angle in degrees.
64 *
65 * @return Angle in radians.
66 */
67 public static final double degToRad(double angle)
68 {
69 return angle * DEG_TO_RAD;
70 }
71
72 /**
73 * Converts radians to degrees.
74 *
75 * @param angle in radians.
76 *
77 * @return angle in degrees.
78 */
79 public static final double radToDeg(double angle)
80 {
81 return angle * RAD_TO_DEG;
82 }
83
84 /**
85 * Converts polar point to cartesian coordinates.
86 *
87 * @param r Radius parameter.
88 * @param phi Angle parameter.
89 *
90 * @return Point in cartesian space.
91 */
92 public static final Point2D polarToCartesian(double r, double phi)
93 {
94 double phiInRadians = degToRad(phi);
95 double x = (r * Math.cos(phiInRadians));
96 double y = -(r * Math.sin(phiInRadians));
97
98 return new Point2D.Double(x, y);
99 }
100
101 /**
102 * Converts polar point to cartesian coordinates. Result will be placed in
103 * point specified. This method will not create new instance of point to
104 * hold the result, but the provided instance will be returned.
105 *
106 * @param p Point which will hold the result.
107 * @param r Radius parameter.
108 * @param phi Angle parameter.
109 *
110 * @return p parameter.
111 */
112 public static final Point2D polarToCartesian(Point2D p, double r, double phi)
113 {
114 double rPhi = degToRad(phi);
115 p.setLocation((r * Math.cos(rPhi)), -(r * Math.sin(rPhi)));
116
117 return p;
118 }
119
120 /**
121 * Converts polar point to cartesian coordinates.
122 *
123 * @param pp Point in polar coordinates.
124 * @param p Point in cartesian coordinates.
125 */
126 public static final void polarToCartesian(PolarPoint pp, Point2D p)
127 {
128 double rPhi = degToRad(pp.phi);
129
130 p.setLocation(pp.r * Math.cos(rPhi), pp.r * Math.sin(rPhi));
131 }
132
133 /**
134 * Converts this point to cartesian coordinate representation.
135 *
136 * @return Point in cartesian coordinates.
137 */
138 public Point2D toPoint()
139 {
140 return polarToCartesian(r, phi);
141 }
142
143 /**
144 * Converts this point to cartesian coordinates and puts result in provided
145 * instance of point.
146 *
147 * @param p Object that will hold the result.
148 *
149 * @return Resulting point, which is the same object as one passed as
150 * parameter.
151 */
152 public Point2D toPoint(Point p)
153 {
154 return polarToCartesian(p, r, phi);
155 }
156 }
157
158 /* __oOo__ */