-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv.js
53 lines (44 loc) · 1.51 KB
/
v.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
// veca: Simple 2D and 3D vectors for JS.
function v (x, y, z) {
return new (function () {
this.x = x
this.y = y
this.z = z
// Prints contents as a string.
this.str = this.toString = () => this.z ?
`x: ${this.x}, y: ${this.y}, z: ${this.z}` :
`x: ${this.x}, y: ${this.y}`
// Returns number of dimensions in vector.
this.dimens = () => this.z ? 3 : 2
// Generates an iterator over this vector and another.
this.iter = (cb) => (b) => v(
cb(this.x, b.x || b),
cb(this.y, b.y || b),
this.z ? cb(this.z, b.z || b) : undefined
)
// Makes an array of this vector's values.
this.arr = () => this.z ?
[ this.x, this.y, this.z ] :
[ this.x, this.y ]
// Makes a basic x,y,z object of this vector.
this.obj = () => this.z ?
{ x: this.x, y: this.y, z: this.z } :
{ x: this.x, y: this.y }
// Basic operations with another vector.
this.mult = this.iter((a, b) => a * b)
this.div = this.iter((a, b) => a / b)
this.add = this.iter((a, b) => a + b)
this.sub = this.iter((a, b) => a - b)
this.pow = this.iter((a, b) => a ** b)
this.mod = this.iter((a, b) => a % b)
// Dot product with another vector.
this.dot = (b) =>
this.x * b.x + this.y * b.y + (this.z ? this.z * b.z : 0)
// Cross product with another vector.
this.cross = (b) =>
v(this.y * b.z - this.z * b.y,
this.z * b.x - this.x * b.z,
this.x * b.y - this.y * b.x)
})
}
module.exports = v;