-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector.js
More file actions
55 lines (48 loc) · 744 Bytes
/
vector.js
File metadata and controls
55 lines (48 loc) · 744 Bytes
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
export let zero = {
x : 0,
y : 0
};
export function dist(a, b){
let dx = b.x - a.x, dy = b.y - a.y;
return Math.sqrt(dx * dx + dy * dy);
}
export function distSquared(a, b){
let dx = b.x - a.x, dy = b.y - a.y;
return dx * dx + dy * dy;
}
export function add(...vectors){
let x = 0, y = 0;
for(let i = 0; i < vectors.length; i++){
x += vectors[i].x;
y += vectors[i].y;
}
return {
x : x,
y : y
};
}
export function sub(a, b){
return {
x : a.x - b.x,
y : a.y - b.y
};
}
export function mul(a, b){
return {
x : a.x * b.x,
y : a.y * b.y
};
}
export function div(a, b){
return {
x : a.x / b.x,
y : a.y / b.y
};
}
export function unit(a){
let d = dist(a, zero);
return {
x : a.x / d,
y : a.y / d
};
}