-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
51 lines (42 loc) · 1.07 KB
/
util.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
// Useful utilities module
// Convert given radians to degrees
export function toDeg(rad) {
return rad * 180 / Math.PI;
}
// Convert given degrees to radians
export function toRad(deg) {
return deg * Math.PI / 180;
}
// Get distance between two points
export function distanceBetween(x1, y1, x2, y2) {
let a = x2 - x1;
let b = y2 - y1;
return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
}
// Get distance between two vectors
export function distanceBetweenVec(a, b) {
return distanceBetween(a.x, a.y, b.x, b.y);
}
// Get the difference between two vectors.
export function vectorDifference(a, b) {
return new Vector(b.x - a.x, b.y - a.y);
}
export function getRandomInt(max) { // range: [0, max)
return Math.floor(Math.random() * Math.floor(max));
}
/**
* 2D Vector with X and Y components.
*/
export class Vector {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
export function vectorFromSprite(sp) {
return new Vector(sp.x, sp.y);
}
// Cap a number at max
export function cap(n, max) {
return n > max ? max : n;
}