-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vec3.java
136 lines (126 loc) · 2.51 KB
/
Vec3.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
public class Vec3 {
public static final int DIMENSIONS = 3;
public static final int X = 0;
public static final int Y = 1;
public static final int Z = 2;
private double[] v;
public Vec3() {
v = new double[DIMENSIONS];
}
public Vec3(double x, double y, double z) {
v = new double[] {x, y, z};
}
public Vec3(double[] v) {
if(v.length == DIMENSIONS) {
this.v = v.clone();
} else {
this.v = new double[DIMENSIONS];
}
}
public double x() {
return v[X];
}
public double y() {
return v[Y];
}
public double z() {
return v[Z];
}
public double get(int dimension) {
return v[dimension];
}
public void setX(double x) {
v[X] = x;
}
public void setY(double y) {
v[Y] = y;
}
public void setZ(double z) {
v[Z] = z;
}
public void set(double value, int dimension) {
v[dimension] = value;
}
public double theta() {
return Math.atan2(y(), x());
}
public double phi() {
return Math.atan2(Math.sqrt(y()*y() + x()*x()), z());
}
public double lengthSquared() {
double r = 0;
for(int i = 0; i < DIMENSIONS; i++) {
r += v[i]*v[i];
}
return r;
}
public double length() {
return Math.sqrt(lengthSquared());
}
public Vec3 mult(double d) {
for(int i = 0; i < DIMENSIONS; i++) {
v[i] *= d;
}
return this;
}
public Vec3 mult(Mat3 mat) {
v = mult(this, mat).v;
return this;
}
public Vec3 add(Vec3 vec) {
for(int i = 0; i < DIMENSIONS; i++) {
v[i] += vec.v[i];
}
return this;
}
public Vec3 sub(Vec3 vec) {
for(int i = 0; i < DIMENSIONS; i++) {
v[i] -= vec.v[i];
}
return this;
}
public Vec3 invert() {
for(int i = 0; i < DIMENSIONS; i++) {
v[i] = -v[i];
}
return this;
}
public Vec3 normalize() {
mult(1/length());
return this;
}
public static double dotProduct(Vec3 a, Vec3 b) {
double prod = 0;
for(int i = 0; i < DIMENSIONS; i++) {
prod += a.v[i]*b.v[i];
}
return prod;
}
public static Vec3 mult(Vec3 vec, double d) {
return vec.clone().mult(d);
}
public static Vec3 mult(Vec3 vec, Mat3 mat) {
Vec3 ans = new Vec3();
for(int i = 0; i < DIMENSIONS; i++) {
for(int j = 0; j < DIMENSIONS; j++) {
ans.v[i] += vec.v[j]*mat.get(i, j);
}
}
return ans;
}
public static Vec3 add(Vec3 a, Vec3 b) {
return a.clone().add(b);
}
public static Vec3 sub(Vec3 a, Vec3 b) {
return a.clone().sub(b);
}
public static Vec3 inverse(Vec3 vec) {
return vec.clone().invert();
}
public static Vec3 normalized(Vec3 vec) {
return vec.clone().normalize();
}
public Vec3 clone() {
return new Vec3(x(), y(), z());
}
}