-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVector3.java
More file actions
67 lines (52 loc) · 1.75 KB
/
Vector3.java
File metadata and controls
67 lines (52 loc) · 1.75 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
class Vector3 {
public double x, y, z;
public Vector3(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public Vector3() {
this(0, 0, 0);
}
public Vector3 add(Vector3 other) {
return new Vector3(this.x + other.x, this.y + other.y, this.z + other.z);
}
public Vector3 subtract(Vector3 other) {
return new Vector3(this.x - other.x, this.y - other.y, this.z - other.z);
}
public double dot(Vector3 other) {
return this.x * other.x + this.y * other.y + this.z * other.z;
}
public Vector3 cross(Vector3 other) {
return new Vector3(
this.y * other.z - this.z * other.y,
this.z * other.x - this.x * other.z,
this.x * other.y - this.y * other.x
);
}
public Vector3 multiply(Vector3 other) {
return new Vector3(this.x * other.x, this.y * other.y, this.z * other.z);
}
public Vector3 multiply(double scalar) {
return new Vector3(this.x * scalar, this.y * scalar, this.z * scalar);
}
public Vector3 inverse() {
return this.multiply(-1);
}
public double norm() {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
public Vector3 normalize() {
double norm = this.norm();
return this.multiply(1 / norm);
}
public Vector3 clamp(double min, double max) {
double x = Math.min(Math.max(min, this.x), max);
double y = Math.min(Math.max(min, this.y), max);
double z = Math.min(Math.max(min, this.z), max);
return new Vector3(x, y, z);
}
public String toString() {
return String.format("(%3.2f, %3.2f, %3.2f)", this.x, this.y, this.z);
}
}