-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec.js
More file actions
61 lines (60 loc) · 2.24 KB
/
vec.js
File metadata and controls
61 lines (60 loc) · 2.24 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
const vec2 = (() => {
function add(other) { return vec2(this.x + other.x, this.y + other.y) }
function sub(other) { return vec2(this.x - other.x, this.y - other.y) }
function mul(val) { return vec2(this.x * val, this.y * val) }
function div(val) { return vec2(this.x / val, this.y / val) }
function length() { return Math.hypot(this.x, this.y) }
function angle() { return Math.atan2(this.y, this.x) }
function turn(a) { return vec2(
this.x*Math.cos(a) - this.y*Math.sin(a),
this.x*Math.sin(a) + this.y*Math.cos(a)
) }
function dot(other) { return this.x*other.x + this.y*other.y }
function normalize() {
const len = this.length();
return vec2(this.x / len, this.y / len);
}
function map(fn) { return vec2(fn(this.x, 'x'), fn(this.y, 'y')); }
return (x, y) => ({
x, y,
0: x, 1: y,
*[Symbol.iterator]() { yield* [x, y]; },
add, sub,
mul, div,
length, normalize,
angle, turn,
dot,
map,
xy: { x, y },
})
})();
const vec3 = (() => {
function add(other) { return vec3(this.x + other.x, this.y + other.y, this.z + other.z) }
function sub(other) { return vec3(this.x - other.x, this.y - other.y, this.z - other.z) }
function mul(val) { return vec3(this.x * val, this.y * val, this.z * val) }
function div(val) { return vec3(this.x / val, this.y / val, this.z / val) }
function length() { return Math.hypot(this.x, this.y, this.z) }
function dot(other) { return this.x * other.x + this.y * other.y + this.z * other.z }
function cross(other) {
return vec3(
this.y * other.z - this.z * other.y,
this.z * other.x - this.x * other.z,
this.x * other.y - this.y * other.x
)
}
function normalize() {
const len = this.length();
return vec3(this.x / len, this.y / len, this.z / len);
}
function map(fn) { return vec2(fn(this.x, 'x'), fn(this.y, 'y')); }
return (x, y, z) => ({
x, y, z,
add, sub,
mul, div,
length,
dot, cross,
normalize,
map,
xyz: { x, y, z },
})
})();