Repository navigation
Expand file tree
/
Copy pathVector3F.java
More file actions
186 lines (111 loc) · 4.07 KB
/
Copy pathVector3F.java
File metadata and controls
186 lines (111 loc) · 4.07 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
import java.util.Objects;
public class Vector3F {
private float x;
private float y;
private float z;
public Vector3F(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
public float length() {
return (float) Math.sqrt((x * x) + (y * y) + (z * z));
}
public float dotProd(Vector3F other) {
return (this.x * other.getX()) + (this.y * other.getY()) + (this.z * other.getZ());
}
@Override
public String toString() {
return "(" + x + ", " + y + ", " + z + ")";
}
public Vector3F crossProd(Vector3F other){
float new_x = y * other.getZ() - z * other.getY();
float new_y = z * other.getX() - x * other.getZ();
float new_z = x * other.getY() - y * other.getX();
return new Vector3F(new_x, new_y, new_z);
}
public Vector3F normalized() {
float len = this.length();
return new Vector3F(x /len, y/len, z/len); // this;
}
public Vector3F rotate(float angle, Vector3F axisToRotateAround) {
float sinHalfAngle = (float)Math.sin(Math.toRadians(angle / 2));
float cosHalfAngle = (float)Math.cos(Math.toRadians(angle / 2));
float rX = axisToRotateAround.getX() * sinHalfAngle;
float rY = axisToRotateAround.getY() * sinHalfAngle;
float rZ = axisToRotateAround.getZ() * sinHalfAngle;
float rW = cosHalfAngle;
MyQuaternion rotation = new MyQuaternion(rX, rY, rZ, rW);
MyQuaternion conjugate = rotation.conjugate();
MyQuaternion w = rotation.mul(this).mul(conjugate);
return new Vector3F(w.getX(), w.getY(), w.getZ());
}
public Vector3F add(Vector3F other) {
return new Vector3F(this.x + other.getX(), this.y + other.getY(), this.z + other.getZ());
}
public Vector3F add(float other) {
return new Vector3F(this.x + other, this.y + other, this.z + other);
}
public Vector3F sub(Vector3F other) {
return new Vector3F(this.x - other.getX(), this.y - other.getY(), this.z - other.getZ());
}
public Vector3F sub(float other) {
return new Vector3F(this.x - other, this.y - other, this.z - other);
}
public Vector3F mult(Vector3F other) {
return new Vector3F(this.x * other.getX(), this.y * other.getY(), this.z * other.getZ());
}
public Vector3F mult(float other) {
return new Vector3F(this.x * other, this.y * other, this.z * other);
}
public Vector3F div(Vector3F other) {
return new Vector3F(this.x / other.getX(), this.y / other.getY(), this.z / other.getZ());
}
public Vector3F div(float other) {
return new Vector3F(this.x / other, this.y / other, this.z / other);
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public float getZ() {
return z;
}
public void setZ(float z) {
this.z = z;
}
public Vector3F abs(){ return new Vector3F(Math.abs(x), Math.abs(y), Math.abs(z)); };
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Vector3F vector3F = (Vector3F) o;
return Float.compare(vector3F.x, x) == 0 && Float.compare(vector3F.y, y) == 0 && Float.compare(vector3F.z, z) == 0;
}
public Vector2F getXY() {
return new Vector2F(x, y);
}
public Vector2F getYX() {
return new Vector2F(y, x);
}
public Vector2F getXZ() {
return new Vector2F(x, z);
}
public Vector2F getZX() {
return new Vector2F(z, x);
}
public Vector2F getYZ() {
return new Vector2F(y, z);
}
public Vector2F getZY() {
return new Vector2F(z, y);
}
}