-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVec3D.java
65 lines (56 loc) · 1.19 KB
/
Vec3D.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
public class Vec3D {
float x;
float y;
float z;
public Vec3D(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
public void normalize() {
float length = getLength();
if (length != 0) {
x /= length;
y /= length;
z /= length;
}
}
/**
* multiplies the vector by a scalar
*
* @param scalar
*/
public void multiply(float scalar) {
x *= scalar;
y *= scalar;
z *= scalar;
}
public Vec3D(float[] vec) {
this.x = vec[0];
this.y = vec[1];
this.z = vec[2];
}
public float get(int i) {
if (i == 0)
return x;
if (i == 1)
return y;
if (i == 2)
return z;
return 0;
}
public float[] toArray() {
return new float[] { x, y, z };
}
public float getLength() {
return (float) Math.sqrt(x * x + y * y + z * z);
}
/**
* toString method returns x, y, z string
*
* @return x, y, z string
*/
public String toString() {
return "x:" + x + ", y:" + y + ", z:" + z;
}
}