-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.java
More file actions
153 lines (138 loc) · 2.61 KB
/
Vector.java
File metadata and controls
153 lines (138 loc) · 2.61 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
/**
* Entity class for a 3D vector.
*/
public class Vector {
private double x;
private double y;
private double z;
/**
* Constructs a default vector with all 0 components.
*/
public Vector() {
x = 0;
y = 0;
z = 0;
}
/**
* Constructs a vector from vertex a to vertex b.
*
* @param a
* The start vertex.
* @param b
* The end vertex.
*/
public Vector(Vertex a, Vertex b) {
this.x = b.getX() - a.getX();
this.y = b.getY() - a.getY();
this.z = b.getZ() - a.getZ();
}
/**
* Creates a vector with the the component values given.
*
* @param x
* The x component.
* @param y
* The y component.
* @param z
* The z component.
*/
public Vector(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
/**
* Sets the x component of the vector.
*
* @param x
* The x component of the vector
*/
public void setX(double x) {
this.x = x;
}
/**
* Sets the y component of the vector.
*
* @param y
* The y component of the vector
*/
public void setY(double y) {
this.y = y;
}
/**
* Sets the z component of the vector.
*
* @param z
* The z component of the vector
*/
public void setZ(double z) {
this.z = z;
}
/**
* Gets the x component of the vector.
*
* @return The x component.
*/
public double getX() {
return x;
}
/**
* Gets the y component of the vector.
*
* @return The y component.
*/
public double getY() {
return y;
}
/**
* Gets the z component of the vector.
*
* @return The z component.
*/
public double getZ() {
return z;
}
/**
* Sets the vector with the the component values given.
*
* @param x
* The x component.
* @param y
* The y component.
* @param z
* The z component.
*/
public void setXYZ(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
/**
* Make the vector a unit vector so it still points the same direction but has a
* magnitude of 1.
*/
public void makeUnitVector() {
double magnitude = getMagnitude();
if (magnitude != 0) {
setX(x / magnitude);
setY(y / magnitude);
setZ(z / magnitude);
}
}
/**
* Calculates the magnitude of the vector.
*
* @return The magnitude of the vector.
*/
public double getMagnitude() {
return Math.sqrt(Math.pow(getX(), 2) + Math.pow(getY(), 2) + Math.pow(getZ(), 2));
}
/**
* ToString to help with debugging.
*
* @return A string representing the vector
*/
public String toString() {
return ("(" + x + ", " + y + ", " + z + ")");
}
}