-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.js
54 lines (48 loc) · 1.22 KB
/
vector.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
46
47
48
49
50
51
52
53
54
class Vector {
constructor(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
}
// Get length of a Vector
function vLength(vector) { // -> int
return Math.sqrt(
vDotProduct(vector, vector)
)
}
// Add two Vectors
function vAdd(vector1, vector2) { // -> Vector
return new Vector(
vector1.x + vector2.x,
vector1.y + vector2.y,
vector1.z + vector2.z
);
}
// Subtract two Vectors
function vSub(vector1, vector2) { // -> Vector
return new Vector(
vector1.x - vector2.x,
vector1.y - vector2.y,
vector1.z - vector2.z
);
}
// Scale a Vector by a scalar
function vScale(vector, scalar) { // -> Vector
return new Vector(
scalar * vector.x,
scalar * vector.y,
scalar * vector.z
);
}
// Linearly interpolate two vectors given a percentage
// p E [0, 1] that indicates how far you are from vector1
function vLerp(vector1, vector2, p) { // -> Vector
return vAdd(vScale(vector1, 1 - p), vScale(vector2, p));
}
// Get the dot product of two vectors
function vDotProduct(vector1, vector2) { // -> int
return vector1.x * vector2.x +
vector1.y * vector2.y +
vector1.z * vector2.z;
}