-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomwalks.js
More file actions
154 lines (137 loc) · 4.95 KB
/
randomwalks.js
File metadata and controls
154 lines (137 loc) · 4.95 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
1- Place N (e.g. 10) arbitrary start points with elevation 'height' (e.g. 500)
2- For each point P in the map, choose a random neighbor P'.
3- Calculate their difference in elevation minus one: diff = P.height - P'.height - 1
4- If diff > 0, move that amount of elevation to the neighbour P'. (i.e. P.height -= diff; P'.height += diff;).
5- Go back to step 2.
*/
var RandomWalks = function(canvas, initHeight, initPoints, callbacks) {
this.cnv = canvas;
this.ctx = this.cnv.getContext('2d');
this.initHeight = initHeight;
this.initPoints = initPoints;
this.world = new Array(this.cnv.width);
this.continuous = false;
this.cycle = 0;
this.currentMaxHeight = 0;
this.callbacks = callbacks;
//set canvas properties
this.ctx.strokeStyle = "black";
this.ctx.fillStyle = "black";
var that = this;
this.init = function () {
//Initialise the world
for (i = 0;i<that.cnv.width;i++) {
that.world[i] = new Array(that.cnv.height);
}
for (x = 0;x<that.cnv.width;x++) {
for (y = 0;y<that.cnv.width;y++) {
that.world[x][y] = 0;
}
}
//draw some random points
for (i = 0;i<initPoints;i++) {
var x = parseInt(Math.random()*that.cnv.width);
var y = parseInt(Math.random()*that.cnv.height);
that.world[x][y] = that.initHeight;
}
that.ctx.clearRect(0,0,that.cnv.width,that.cnv.height);
}
var getPoints = function() {
var Ps = [], x, y;
var maxHeight = 0;
//write down all the points here
for (x = 0;x<that.cnv.width;x++) {
for (y = 0;y<that.cnv.width;y++) {
if (that.world[x][y] > 0 ) {
maxHeight = Math.max(maxHeight, that.world[x][y]);
Ps.push([x,y]);
}
}
}
that.currentMaxHeight = maxHeight;
return Ps;
}
var findRandomNeighbour = function(x,y) {
//pick random neighbor
_i = parseInt(Math.random()*8);
if (_i == 1 || _i == 4 || _i == 6) {
_x = x-1;
}else if( _i == 2 || _i == 7) {
_x = x;
}else{
_x = x+1;
}
if (_i == 1 || _i == 2 || _i == 3) {
_y = y-1;
}else if( _i == 4 || _i == 5) {
_y = y;
}else{
_y = y+1;
}
return new Array(_x,_y);
}
var iterate = function() {
var neighbour, _i, _x, _y, diff;
Ps = getPoints();
//clone
newPs = new Array();//Ps.slice(0);
for (i in Ps) {
x = Ps[i][0];
y = Ps[i][1];
neighbour = findRandomNeighbour(x,y);
_x = neighbour[0];
_y = neighbour[1];
//skip borders
if (_y >= that.cnv.height || _x >= that.cnv.width || _y < 0 || _x < 0)
continue;
diff = that.world[x][y] - that.world[_x][_y] - 1;
if (diff > 0 ) {
that.world[_x][_y] += diff;
that.world[x][y] -= diff;
newPs.push([_x,_y]);
}
}
if (that.continuous == true) {
setTimeout(iterate,0);
}
drawMap(newPs);
that.cycle ++;
}
this.getPointValue = function(x,y) {
return that.world[x][y];
}
this.start = function() {
that.continuous = true;
iterate();
}
this.stop = function() {
that.continuous = false;
}
this.addPoint = function (x,y) {
that.world[x][y] = that.initHeight;
}
var drawMap = function(precalculatedPoints){
//No precalculated points, let's find them
if (typeof precalculatedPoints == "undefined") {
precalculatedPoints = [];
for (x = 0;x<that.cnv.width;x++) {
for (y = 0;y<that.cnv.width;y++) {
if (that.world[x][y] > 0 ) {
precalculatedPoints.push([x,y]);
}
}
}
}
//Draw all the found points
for (i in precalculatedPoints) {
x = precalculatedPoints[i][0];
y = precalculatedPoints[i][1];
that.ctx.fillRect(x,y,1,1);
}
that.callbacks.stats(that.currentMaxHeight, that.cycle);
}
this.save = function(){
return(that.cnv.toDataURL("image/jpg"));
}
};