-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPoint2D.java
More file actions
304 lines (282 loc) · 8.31 KB
/
Copy pathPoint2D.java
File metadata and controls
304 lines (282 loc) · 8.31 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package sysmlinjava.valuetypes;
import static java.lang.Math.PI;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import java.io.Serializable;
import sysmlinjava.annotations.Operation;
import sysmlinjava.units.SysMLinJavaUnits;
/**
* SysMLinJava value type for the cartesian coordinate in two dimensional
* euclidean space in terms of an x and y coordinates expressed as Java
* {@code double} values<br>
* <p>
* <b>Note:</b> as an {@code ObservableValue} this value type can be "observed"
* by other objects. {@code ValueObserver}s call the {@code addValueObserver()}
* operation to be notified (called-back) by the {@code Point2D} object of any
* change in its value. This notification will only occur if and when the
* {@code Point2D} {@code value} is changed by a call to the {@code setValue()}
* operation. So, while the {@code Point2D.value} is publicly accessible and can
* be changed by direct assignment, the {@code setValue()} operation must be
* used if {@code ValueObserver}s are to be automatically notified of any
* change.
*
* @author ModelerOne
*
*/
public class Point2D extends SysMLValueType implements Serializable
{
/** Serializable ID */
private static final long serialVersionUID = 790350123208690812L;
/**
* x-value of the Point2D
*/
public double xValue;
/**
* y-value of the Point2D
*/
public double yValue;
/**
* Whether or not values are empty/not yet set
*/
public boolean isEmpty;
/**
* Constructor for empty values
*/
public Point2D()
{
super();
this.isEmpty = true;
this.createUnits();
}
/**
* Minimal constructor specifying the point's x and y values. Units are assumed
* to be simple Point units.
*
* @param xValue double value of the Point2D's x corrdinate
* @param yValue double value of the Point2D's y corrdinate
*/
public Point2D(double xValue, double yValue)
{
super();
this.xValue = xValue;
this.yValue = yValue;
this.isEmpty = false;
this.createUnits();
}
/**
* Copy constructor
*
* @param copied instance to be copied
*/
public Point2D(Point2D copied)
{
super();
this.xValue = copied.xValue;
this.yValue = copied.yValue;
this.isEmpty = false;
}
/**
* Returns whether or not values are empty/not yet set
*
* @return true if point's x and y values are missing/not yet set, false
* otherwise
*/
public boolean isEmpty()
{
return isEmpty;
}
/**
* Sets values to specified values and notifies all change observers
*
* @param xValue point's x value
* @param yValue point's y value
*/
public void setValue(double xValue, double yValue)
{
this.xValue = xValue;
this.yValue = yValue;
notifyValueChangeObservers();
}
/**
* Sets x and y values to values of specified point and notifies all change
* observers
*
* @param point point whose values are to be used
*/
public void setValue(Point2D point)
{
setValue(point.xValue, point.yValue);
}
/**
* Returns the value of X
*
* @return value of X
*/
public double getXValue()
{
return xValue;
}
/**
* Returns the value of Y
*
* @return value of Y
*/
public double getYValue()
{
return yValue;
}
/**
* Adds the specified points x and y values to the current points x and y values
*
* @param value values to be added
*/
public void add(Point2D value)
{
xValue += value.xValue;
yValue += value.yValue;
}
/**
* Scales (multiplies) this point by the specified value
*
* @param multipliedBy value to multiply this by
*/
public void scale(double multipliedBy)
{
xValue *= multipliedBy;
yValue *= multipliedBy;
}
/**
* Returns whether or not this point is within rectangle having specifed
* upper-right and lower-left corners where right/left is x and upper/lower is
* y.
*
* @param upperRightCorner point of rectangles upper right corner
* @param lowerLeftCorner point of rectangles lower left corner
* @param upperRightExclusive whether point is to be exclusively inside the
* upper right rectangle bounds, i.e. not on the
* bounds specified by the upper right corner
* @param lowerLeftExclusive whether point is to be exclusively inside the
* lower left rectangle bounds, i.e. not on the
* bounds specified by the lower left corner
*
* @return whether this point is in the rectangle
*/
public boolean isWithinRectangle(Point2D upperRightCorner, Point2D lowerLeftCorner, boolean upperRightExclusive, boolean lowerLeftExclusive)
{
if (upperRightExclusive)
if (lowerLeftExclusive)
return xValue < upperRightCorner.xValue && xValue > lowerLeftCorner.xValue && yValue < upperRightCorner.yValue && yValue > lowerLeftCorner.yValue;
else
return xValue < upperRightCorner.xValue && xValue >= lowerLeftCorner.xValue && yValue < upperRightCorner.yValue && yValue >= lowerLeftCorner.yValue;
else if (lowerLeftExclusive)
return xValue <= upperRightCorner.xValue && xValue > lowerLeftCorner.xValue && yValue <= upperRightCorner.yValue && yValue > lowerLeftCorner.yValue;
else
return xValue <= upperRightCorner.xValue && xValue >= lowerLeftCorner.xValue && yValue <= upperRightCorner.yValue && yValue >= lowerLeftCorner.yValue;
}
/**
* Constant value for 90 degrees in radians
*/
static final double rad90 = 0.5 * PI;
/**
* Constant value for 180 degrees in radians
*/
static final double rad180 = 1.0 * PI;
/**
* Constant value for 270 degrees in radians
*/
static final double rad270 = 1.5 * PI;
/**
* Constant value for 360 degrees in radians
*/
static final double rad360 = 2.0 * PI;
/**
* Returns a new point that is this point moved at the specified direction for
* the specified distance (length).
*
* @param lengthNumeric numeric (no units of) length of vector
* @param directionRadians radian direction of vector
*
* @return point for this point moved specified length and direction
*/
@Operation
public Point2D moved(double lengthNumeric, double directionRadians)
{
double xDelta = 0;
double yDelta = 0;
if (directionRadians < rad90)
{
xDelta = cos(rad90 - directionRadians) * lengthNumeric;
yDelta = sin(rad90 - directionRadians) * lengthNumeric;
}
else if (directionRadians < rad180)
{
xDelta = sin(rad180 - directionRadians) * lengthNumeric;
yDelta = -(cos(rad180 - directionRadians) * lengthNumeric);
}
else if (directionRadians < rad270)
{
xDelta = -(cos(rad270 - directionRadians) * lengthNumeric);
yDelta = -(sin(rad270 - directionRadians) * lengthNumeric);
}
else if (directionRadians < rad360)
{
xDelta = -(sin(rad360 - directionRadians) * lengthNumeric);
yDelta = cos(rad360 - directionRadians) * lengthNumeric;
}
return new Point2D(xValue + xDelta, yValue + yDelta);
}
/**
* Return an instance that is this multiplied by the specified value
*
* @param multipliedBy value to multiply this by
* @return instance that is this multiplied by the specified value
*/
public Point2D scaled(double multipliedBy)
{
return new Point2D(xValue * multipliedBy, yValue * multipliedBy);
}
/**
* Returns instance with specified values
*
* @param xValue initial x value
* @param yValue initial y value
* @return instance with initial x, y values
*/
public static Point2D of(double xValue, double yValue)
{
return new Point2D(xValue, yValue);
}
/**
* Returns instance with an empty point
*
* @return instance with an empty point
*/
public static Point2D empty()
{
return new Point2D();
}
@Override
protected void createUnits()
{
units = SysMLinJavaUnits.Point;
}
@Override
public String toString()
{
StringBuilder builder = new StringBuilder();
builder.append("Point2D [xValue=");
builder.append(xValue);
builder.append(", yValue=");
builder.append(yValue);
builder.append(", isEmpty=");
builder.append(isEmpty);
builder.append(", units=");
builder.append(units);
builder.append(", name=");
builder.append(name);
builder.append(", id=");
builder.append(id);
builder.append("]");
return builder.toString();
}
}