-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec3d.js
47 lines (36 loc) · 992 Bytes
/
vec3d.js
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
export class Vec3D {
constructor(x, y, z) {
[this.x, this.y, this.z] = [x, y, z];
}
static ZERO = new Vec3D(0, 0, 0);
static UP = new Vec3D(0, 1, 0);
static dot(a, b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
static cross(a, b) {
return new Vec3D(
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x
);
}
scale(m) {
return new Vec3D(this.x * m, this.y * m, this.z * m);
}
norm() {
let length = Math.sqrt(Vec3D.dot(this, this));
return this.scale(1 / length);
}
static add(a, b) {
return new Vec3D(a.x + b.x, a.y + b.y, a.z + b.z);
}
plus(b) {
return new Vec3D(this.x + b.x, this.y + b.y, this.z + b.z);
}
static subtract(a, b) {
return new Vec3D(a.x - b.x, a.y - b.y, a.z - b.z);
}
minus(b) {
return new Vec3D(this.x - b.x, this.y - b.y, this.z - b.z);
}
}