-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.js
More file actions
36 lines (35 loc) · 1.03 KB
/
Utils.js
File metadata and controls
36 lines (35 loc) · 1.03 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
var Utils = {
distance:function(o1, o2){
var difx = o2.x - o1.x;
var dify = o2.y - o1.y;
return Math.sqrt( (difx*difx) + (dify*dify) );
},
getRandomInt:function(min, max){
if (min === undefined){
throw new Error("getRandomInt must have at least one parameter: max");
}
// If one parameter is given, use it as max and default min to 0
if (max === undefined) {
max = min;
min = 0;
}
return Math.floor(Math.random() * (max - min + 1)) + min;
},
getDelta:function(start,end,speed){
var dy = end.y - start.y;
var dx = end.x - start.x;
var distance=Math.sqrt((dx*dx)+(dy*dy));
dx/=distance;
dy/=distance;
dx*=speed;
dy*=speed;
return {dX:dx,dY:dy}
},
inArray:function(needle, haystack) {
var length = haystack.length;
for(var i = 0; i < length; i++) {
if(haystack[i] == needle) return true;
}
return false;
}
};