forked from l0bster2/pocket-platformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathHelpers.js
34 lines (29 loc) · 988 Bytes
/
MathHelpers.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
class MathHelpers {
static getRandomNumberBetweenTwoNumbers(min, max, round = true) {
const randomNumber = Math.random() * (max - min) + min;
return round ? Math.floor(randomNumber) : randomNumber;
}
static getSometimesNegativeRandomNumber(min, max, round = true) {
let randomNumber = this.getRandomNumberBetweenTwoNumbers(min, max, round);
return randomNumber *= Math.round(Math.random()) ? 1 : -1;
}
static sortNumbers(numberArray) {
return numberArray.sort((a,b)=>a-b)
}
static getAngle(x1, y1, x2, y2) {
let result = Math.atan2(y2 - y1, x2 - x1) * (180 / Math.PI);
return result < 0 ? 360 + result : result; // range [0, 360)
}
static normalizeAngle(newAngle) {
if (newAngle > 360) {
return Math.abs(360 - newAngle);
}
else if (newAngle < 0) {
return 360 - Math.abs(newAngle);
}
return newAngle;
}
static getRadians(angle) {
return angle * Math.PI / 180;
}
}