forked from bostongfx/cs460student_2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.js
42 lines (34 loc) · 953 Bytes
/
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
export const math = (function() {
return {
rand_range: function(a, b) {
return Math.random() * (b - a) + a;
},
rand_normalish: function() {
const r = Math.random() + Math.random() + Math.random() + Math.random();
return (r / 4.0) * 2.0 - 1;
},
rand_int: function(a, b) {
return Math.round(Math.random() * (b - a) + a);
},
lerp: function(x, a, b) {
return x * (b - a) + a;
},
smoothstep: function(x, a, b) {
x = x * x * (3.0 - 2.0 * x);
return x * (b - a) + a;
},
smootherstep: function(x, a, b) {
x = x * x * x * (x * (x * 6 - 15) + 10);
return x * (b - a) + a;
},
clamp: function(x, a, b) {
return Math.min(Math.max(x, a), b);
},
sat: function(x) {
return Math.min(Math.max(x, 0.0), 1.0);
},
in_range: (x, a, b) => {
return x >= a && x <= b;
},
};
})();