-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
85 lines (73 loc) · 1.52 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* From http://www.karlbunyan.co.uk/2008/12/repeatable-random-numbers-in-javascript.aspx
* As despite seeding Math.random(), it doesn't return the same sequence each time!
*/
var Random =
{
seed : 12345,
//Returns a random number between 0 and 1
next : function(lower,upper)
{
var maxi = Math.pow(2,32);
this.seed = (134775813 * (this.seed + 1))
% maxi;
var num = (this.seed) / maxi;
if(typeof lower!='undefined')
{
var range = upper - lower;
num *= range;
num += lower;
}
return num;
}
}
function irand(x) {
return Math.floor( Math.random() * x );
}
function rand(x) {
return Math.random() * x;
}
// pass in a fraction like .8
function odds(x) {
return x >= rand(1.0);
}
function vary(x, variance) {
return x + variance - 2 * rand(variance);
}
function varyUp(x, variance) {
return x + rand(variance);
}
function either(a,b) {
return Math.random() < 0.5? a : b;
}
function randomPick(array) {
return array[irand(array.length)];
}
///
function deepCopy(obj) {
if (Object.prototype.toString.call(obj) == '[object Array]') {
var out = [], i = 0, len = obj.length;
for ( ; i < len ; ++i) {
out[i] = arguments.callee(obj[i]);
}
return out;
}
if (typeof obj == 'object') {
var out = [], i;
for (i in obj) {
out[i] = arguments.callee(obj[i]);
}
return out;
}
return obj;
}
function setObjectFromMap(obj, map) {
for (i in map) {
obj[i] = map[i];
}
}
function distance(x,y,x1,y1) {
var distX = x1 - x;
var distY = y1 - y;
return Math.sqrt(distX * distX + distY * distY);
}