-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib.js
49 lines (38 loc) · 1.08 KB
/
lib.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
// From https://redblobgames.github.io/circular-obstacle-pathfinding/
// Copyright 2017 Red Blob Games <[email protected]>
// License: Apache v2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>
function vec_polar(r, a) {
return {x: r * Math.cos(a), y: r * Math.sin(a)};
}
function vec_add(p, q) {
return {x: p.x + q.x, y: p.y + q.y};
}
function vec_sub(p, q) {
return {x: p.x - q.x, y: p.y - q.y};
}
function vec_dot(p, q) {
return p.x * q.x + p.y * q.y;
}
function vec_cross(p, q) {
return p.x * q.y - p.y * q.x;
}
function vec_interpolate(p, q, t) {
return {x: p.x + (q.x - p.x) * t, y: p.y + (q.y - p.y) * t};
}
function vec_facing(p, q) {
const dx = q.x - p.x, dy = q.y - p.y;
return Math.atan2(dy, dx);
}
function vec_length(p) {
return Math.sqrt(p.x*p.x + p.y*p.y);
}
function vec_distance(p, q) {
return vec_length(vec_sub(p, q));
}
function vec_normalize(p) {
let d = vec_length(p) || 1e-6; // avoid divide by 0
return {x: p.x / d, y: p.y / d};
}
function angle_difference(a, b) {
return Math.abs(b - a) % (2 * Math.PI);
}