blob: 5e69447ba982f43f52a35f6f61b3495aaf0af905 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/**
* @param {number} value Value
* @param {number} min Lower bound
* @param {number} max Upper bound
* @returns {number} Value clamped between min and max
*/
export const clamp = function(value, min, max) {
return Math.max(Math.min(value, max), min);
};
/**
* @param {number} x Value
* @param {number} m Modulus
* @returns {number} Result of y where y = x modulo m and y > 0
*/
export const positiveModulo = function(x, m) {
console.assert(m > 0);
let result = x % m;
while (result < 0) {
result += m;
}
return result;
};
|