-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.js
57 lines (48 loc) · 1.31 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
55
56
57
class Vec2 {
constructor(pos) {
if (typeof (pos) === "number")
this.val = [pos, pos]
else // list
this.val = pos
}
size() {
return Math.sqrt(this.val[0] * this.val[0] + this.val[1] * this.val[1])
}
addVec2(v) {
this.val[0] += v.val[0]
this.val[1] += v.val[1]
}
}
function subVec2(v1, v2) {
return new Vec2([v1.val[0] - v2.val[0], v1.val[1] - v2.val[1]])
}
function addVec2(v1, v2) {
return new Vec2([v1.val[0] + v2.val[0], v1.val[1] + v2.val[1]])
}
function normalizeVec2(v) {
return new Vec2([v.val[0] / v.size(), v.val[1] / v.size()])
}
function modVec2(v1, v2) {
let v3 = subVec2(v1, v2)
return v3.size()
}
function mulSc(v, sc) {
return new Vec2([v.val[0] * sc, v.val[1] * sc])
}
function dotVec2(v1, v2) {
return v1.val[0] * v2.val[0] + v1.val[1] * v2.val[1]
}
function mulVec2(v1, v2) {
return new Vec2([v1.val[0] * v2.val[0], v1.val[1] * v2.val[1]])
}
function crossVec2(v1, v2) {
return new Vec2([v1.val[0] * v2.val[1] - v1.val[1] * v2.val[0]])
}
function normalVec2(v1, v2) {
let dx = v2.val[0] - v1.val[0]
let dy = v2.val[1] - v1.val[1]
return normalizeVec2(new Vec2([-dy, dx]))
}
function angleBetweenVec2(v1, v2) {
return Math.acos(dotVec2(v1, v2) / (v1.size() * v2.size()))
}