-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvec3.js
40 lines (35 loc) · 994 Bytes
/
vec3.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
export default {
add(...args) {
return args.reduce((a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]])
},
sub(...args) {
return args.reduce((a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]])
},
mul(...args) {
return args.reduce((a, b) => {
if (b instanceof Array) return [a[0] * b[0], a[1] * b[1], a[2] * b[2]]
return [a[0] * b, a[1] * b, a[2] * b]
})
},
div(...args) {
return args.reduce((a, b) => {
if (b instanceof Array) return [a[0] / b[0], a[1] / b[1], a[2] / b[2]]
return [a[0] / b, a[1] / b, a[2] / b]
})
},
mag(a) {
return Math.sqrt(this.dot(a, a))
},
norm(a) {
return this.div(a, this.mag(a))
},
dis(a, b) {
return this.mag(this.sub(a, b))
},
dot(a, b) {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
},
lerp(a, b, t) {
return this.add(a, this.mul(this.sub(b, a), t))
}
}