-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVector2D.java
More file actions
206 lines (192 loc) · 5.28 KB
/
Copy pathVector2D.java
File metadata and controls
206 lines (192 loc) · 5.28 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
package sysmlinjava.valuetypes;
import static java.lang.Math.PI;
import static java.lang.Math.atan;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import static java.lang.Math.hypot;
import static java.lang.Math.toRadians;
import static java.lang.Math.pow;
import static java.lang.Math.sqrt;
import java.util.List;
import sysmlinjava.annotations.Attribute;
import sysmlinjava.units.SysMLinJavaUnits;
/**
* SysMLinJava value type for a vector in terms of a length of any numeric units
* and a direction in radians
*
* @author ModelerOne
*
*/
public class Vector2D extends RReal
{
/** Serializable ID*/private static final long serialVersionUID = -1566489011777283872L;
/**
* Attribute for vector direction
*/
@Attribute
public DirectionRadians direction;
/**
* Constructor
*
* @param length double value for initial vector length
* @param directionRadians double value for vector direction
*/
public Vector2D(double length, double directionRadians)
{
super(length);
this.direction = new DirectionRadians(directionRadians);
}
/**
* Constructor
*
* @param length RReal value for initial vector length
* @param directionRadians RReal value for vector direction
*/
public Vector2D(RReal length, RReal directionRadians)
{
super(length);
this.direction = new DirectionRadians(directionRadians);
}
/**
* Constructor
*
* @param length double value for initial vector length
* @param directionRadians double value for vector direction
* @param id unique ID for this instance, e.g. index into array of
* vectors
*/
public Vector2D(double length, double directionRadians, long id)
{
this(length, directionRadians);
this.id = id;
}
public Vector2D(Point2D fromPoint, Point2D toPoint)
{
super(0);
double xDelta = 0;
double yDelta = 0;
if(fromPoint.xValue >= toPoint.xValue)
{
xDelta = fromPoint.xValue - toPoint.xValue;
if(fromPoint.yValue >= toPoint.yValue)
{
yDelta = fromPoint.yValue - toPoint.yValue;
direction = new DirectionRadians(1.0 * PI + atan(xDelta/yDelta));
}
else
{
yDelta = toPoint.yValue - fromPoint.yValue;
direction = new DirectionRadians(2.0 * PI - atan(xDelta/yDelta));
}
}
else
{
xDelta = toPoint.xValue - fromPoint.xValue;
if(fromPoint.yValue >= toPoint.yValue)
{
yDelta = fromPoint.yValue - toPoint.yValue;
direction = new DirectionRadians(1.0 * PI - atan(xDelta/yDelta));
}
else
{
yDelta = toPoint.yValue - fromPoint.yValue;
direction = new DirectionRadians(0.0 * PI + atan(xDelta/yDelta));
}
}
value = sqrt(pow(xDelta, 2.0) + pow(yDelta, 2.0));
}
/**
* Constructor - copy
*
* @param copied instance whose magnitude and direction values are to used as
* initial values of this copy
*/
public Vector2D(Vector2D copied)
{
super(copied);
this.direction = new DirectionRadians(copied.direction);
}
/**
* Returns the horizontal component of this force
*
* @return vector value for the horizontal component of this vector
*/
public Vector2D horizontalComponent()
{
double magnitude = value * sin(direction.value);
double direction = magnitude < 0 ? toRadians(270) : toRadians(90);
return new Vector2D(Math.abs(magnitude), direction);
}
/**
* Returns the vertical component of this force
*
* @return vector value for the vertical component of this vector
*/
public Vector2D verticalComponent()
{
double magnitude = value * cos(direction.value);
double direction = magnitude < 0 ? toRadians(180) : toRadians(0);
return new Vector2D(Math.abs(magnitude), direction);
}
/**
* Returns instance that is the vector sum of the specified set of vectors
*
* @param vectors list of vector values to be summed
* @return vector sum of the specified vectors
*/
public static Vector2D sum(List<Vector2D> vectors)
{
Vector2D result = new Vector2D(0, 0);
double horizontalComponents = 0;
double verticalComponents = 0;
for (Vector2D force : vectors)
{
horizontalComponents += force.value * sin(force.direction.value);
verticalComponents += force.value * cos(force.direction.value);
}
result.value = hypot(horizontalComponents, verticalComponents);
double direction = atan(horizontalComponents / verticalComponents);
if (direction < 0)
direction += 2 * PI;
if (verticalComponents < 0)
if (horizontalComponents < 0)
direction += PI;
else
direction -= PI;
result.direction.value = direction;
return result;
}
/**
* Sets this vector's values to the values of the specified vector
*
* @param vector vector from which this vector's values are to be set
*/
public void setValue(Vector2D vector)
{
value = vector.value;
direction.value = vector.direction.value;
notifyValueChangeObservers();
}
@Override
protected void createUnits()
{
units = SysMLinJavaUnits.Numeric;
}
@Override
public String toString()
{
StringBuilder builder = new StringBuilder();
builder.append("Vector2D [value=");
builder.append(value);
builder.append(", units=");
builder.append(units);
builder.append(", name=");
builder.append(name);
builder.append(", id=");
builder.append(id);
builder.append(", direction=");
builder.append(direction);
builder.append("]");
return builder.toString();
}
}