-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
56 lines (51 loc) · 1.54 KB
/
util.js
File metadata and controls
56 lines (51 loc) · 1.54 KB
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
const Util = {
//inheritance function
inherits(childClass, parentClass) {
childClass.prototype = Object.create(parentClass.prototype);
childClass.prototype.constructor = childClass;
},
//random vector
randomVec(length) {
const deg = 2 * Math.PI * Math.random();
return Util.scale([Math.sin(deg), Math.cos(deg)], length);
},
// Scale the length of a vector by the given amount.
scale(vec, m) {
return [vec[0] * m, vec[1] * m];
},
//random Position in canvas space
randomPos(radius){
return [Math.floor(Math.random()*(1000- radius)), Math.floor(Math.random()*(600 - radius))];
},
//random color
randRGB(){
let r = Math.floor(Math.random()*255);
let g = Math.floor(Math.random()*255);
let b = Math.floor(Math.random()*255);
return `rgb(${r}, ${g}, ${b})`;
},
//wrap helper function
wrap(posPt, max){
if(posPt < 0){
return max - (Math.floor(posPt) % max);
}
else if (posPt > max){
return Math.floor(posPt) % max;
}
else{
return posPt;
}
},
//calculate distance between two points
distBetweenTwoPositions(pos1, pos2){
return Math.sqrt((pos1[0] - pos2[0])**2 + (pos1[1] - pos2[1])**2);
},
//calculate norm (magnitude or length)
norm(velocity){
return this.distBetweenTwoPositions([0,0], velocity);
},
fiftyPercentChance(){
return ((Math.random()*10) % 2) !== 0
}
};
module.exports = Util;