/
math.js
61 lines (51 loc) Β· 1.11 KB
/
math.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
58
59
60
61
function heading([x, y]) {
return Math.atan2(y, x);
}
function calcVec(x, y) {
return [y - x, -x - y];
}
function normalize(n, start1, stop1, start2, stop2, withinBounds) {
const v = ((n - start1) / (stop1 - start1)) * (stop2 - start2) + start2;
if (!withinBounds) {
return v;
}
if (start2 < stop2) {
return constrain(v, start2, stop2);
} else {
return constrain(v, stop2, start2);
}
}
function constrain(n, low, high) {
return Math.max(Math.min(n, high), low);
}
function noiseGrid(simplex, gridSize) {
return (x, y, time, offset = 0) =>
simplex.noise3D(
x / (gridSize * 2) + offset,
y / (gridSize * 2) + offset,
time,
);
}
function lerp(v0, v1, t) {
return v0 * (1 - t) + v1 * t;
}
function randomNumber(start, end) {
return Math.floor(Math.random() * end) + start;
}
function range(count) {
return Array.from(Array(count).keys());
}
function degreesToRadians(angleInDegrees) {
return (Math.PI * angleInDegrees) / 180;
}
module.exports = {
heading,
calcVec,
normalize,
constrain,
noiseGrid,
lerp,
randomNumber,
range,
degreesToRadians,
};