-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPointCP.java
169 lines (145 loc) · 4.19 KB
/
PointCP.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// This file contains material supporting section 2.9 of the textbook:
// "Object Oriented Software Engineering" and is issued under the open-source
// license found at www.lloseng.com
/**
* This class contains instances of coordinates in either polar or
* cartesian format. It also provides the utilities to convert
* them into the other type. It is not an optimal design, it is used
* only to illustrate some design issues.
*
* @author François Bélanger
* @author Dr Timothy C. Lethbridge
* @version July 2000
*/
public class PointCP
{
//Instance variables ************************************************
/**
* Contains C(artesian) or P(olar) to identify the type of
* coordinates that are being dealt with.
*/
private char typeCoord;
/**
* Contains the current value of X or RHO depending on the type
* of coordinates.
*/
private double xOrRho;
/**
* Contains the current value of Y or THETA value depending on the
* type of coordinates.
*/
private double yOrTheta;
//Constructors ******************************************************
/**
* Constructs a coordinate object, with a type identifier.
*/
public PointCP(char type, double xOrRho, double yOrTheta)
{
if(type != 'C' && type != 'P')
throw new IllegalArgumentException();
this.xOrRho = xOrRho;
this.yOrTheta = yOrTheta;
typeCoord = type;
}
//Instance methods **************************************************
public double getX()
{
if(typeCoord == 'C')
return xOrRho;
else
return (Math.cos(Math.toRadians(yOrTheta)) * xOrRho);
}
public double getY()
{
if(typeCoord == 'C')
return yOrTheta;
else
return (Math.sin(Math.toRadians(yOrTheta)) * xOrRho);
}
public double getRho()
{
if(typeCoord == 'P')
return xOrRho;
else
return (Math.sqrt(Math.pow(xOrRho, 2) + Math.pow(yOrTheta, 2)));
}
public double getTheta()
{
if(typeCoord == 'P')
return yOrTheta;
else
return Math.toDegrees(Math.atan2(yOrTheta, xOrRho));
}
/**
* Converts Cartesian coordinates to Polar coordinates.
*/
public void convertStorageToPolar()
{
if(typeCoord != 'P')
{
//Calculate RHO and THETA
double temp = getRho();
yOrTheta = getTheta();
xOrRho = temp;
typeCoord = 'P'; //Change coord type identifier
}
}
/**
* Converts Polar coordinates to Cartesian coordinates.
*/
public void convertStorageToCartesian()
{
if(typeCoord != 'C')
{
//Calculate X and Y
double temp = getX();
yOrTheta = getY();
xOrRho = temp;
typeCoord = 'C'; //Change coord type identifier
}
}
/**
* Calculates the distance in between two points using the Pythagorean
* theorem (C ^ 2 = A ^ 2 + B ^ 2). Not needed until E2.30.
*
* @param pointA The first point.
* @param pointB The second point.
* @return The distance between the two points.
*/
public double getDistance(PointCP pointB)
{
// Obtain differences in X and Y, sign is not important as these values
// will be squared later.
double deltaX = getX() - pointB.getX();
double deltaY = getY() - pointB.getY();
return Math.sqrt((Math.pow(deltaX, 2) + Math.pow(deltaY, 2)));
}
/**
* Rotates the specified point by the specified number of degrees.
* Not required until E2.30
*
* @param point The point to rotate
* @param rotation The number of degrees to rotate the point.
* @return The rotated image of the original point.
*/
public PointCP rotatePoint(double rotation)
{
double radRotation = Math.toRadians(rotation);
double X = getX();
double Y = getY();
return new PointCP('C',
(Math.cos(radRotation) * X) - (Math.sin(radRotation) * Y),
(Math.sin(radRotation) * X) + (Math.cos(radRotation) * Y));
}
/**
* Returns information about the coordinates.
*
* @return A String containing information about the coordinates.
*/
public String toString()
{
return "Stored as " + (typeCoord == 'C'
? "Cartesian (" + getX() + "," + getY() + ")"
: "Polar [" + getRho() + "," + getTheta() + "]") + "\n";
}
}