-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwalker.html
212 lines (206 loc) · 5.85 KB
/
walker.html
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Walker Bots Demo</title>
</head>
<body>
<input id="run" type="button" value="Add Bots">
<br>
<div style="display:table">
<div style="display:table-row">
<div id="coverage1" style="display:table-cell;border: dotted">
Coverage:
</div>
<div id="coverage2" style="display:table-cell;border: dotted;">
Coverage:
</div>
<div id="coverage3" style="display:table-cell;border: dotted;">
Coverage:
</div>
<div id="coverage4" style="display:table-cell;border: dotted;">
Coverage:
</div>
</div>
<div style="display:table-row">
<div style="display:table-cell;border: solid;width:250px;height:250px">
<canvas id="room1" width="250" height="250">
</canvas>
</div>
<div style="display:table-cell;border: solid;width:250px;height:250px">
<canvas id="room2" width="250" height="250">
</canvas>
</div>
<div style="display:table-cell;border: solid;width:250px;height:250px">
<canvas id="room3" width="250" height="250">
</canvas>
</div>
<div style="display:table-cell;border: solid;width:250px;height:250px">
<canvas id="room4" width="250" height="250">
</canvas>
</div>
</div>
<div style="display:table-row">
<div style="display:table-cell;border: dashed">
Rand Bot
</div>
<div style="display:table-cell;border: dashed;">
SA Bot
</div>
<div style="display:table-cell;border: dashed;">
Wings Bot
</div>
<div style="display:table-cell;border: dashed;">
Spiral Bot
</div>
</div>
</div>
<script type="application/javascript">
/**
* @constructor
*/
function Env(angle, x, y, context) {
this.angle = angle;
this.x = x;
this.y = y;
this.speed = 1;
this.context = context;
var ctx = this.context;
ctx.beginPath();
}
Env.prototype.rotate = function(angle) {
this.angle = this.angle + angle;
};
Env.prototype.move = function () {
return this.update(0.005);
};
Env.prototype.update = function (delta) {
var dx = this.speed * Math.cos(this.angle);
var dy = this.speed * Math.sin(this.angle);
var nx = this.x + dx * delta;
var ny = this.y + dy * delta;
if (nx >=0 && ny >=0 && nx <= 1 && ny <= 1) {
var ctx = this.context;
ctx.lineWidth = 10;
ctx.lineTo(nx * 250, ny * 250);
ctx.stroke();
this.x = nx;
this.y = ny;
return true;
} else {
return false;
}
};
/**
* @constructor
*/
function RandBot(env) {
this.env = env;
}
RandBot.prototype.update = function() {
var b = this.env.move();
if (!b) {
this.env.rotate(Math.PI + (Math.PI/4 * (2*Math.random() - 1)));
}
};
/**
* @constructor
*/
function SABot(env) {
this.env = env;
this.temp = Math.PI / 4;
this.steps = 0;
}
SABot.prototype.update = function() {
var b = this.env.move();
if (!b) {
this.steps++;
this.env.rotate((Math.PI + (this.temp * (2*Math.random() - 1)))/(1 + 0.1 * Math.log(this.steps)));
}
};
/**
* @constructor
*/
function WingsBot(env) {
this.env = env;
this.temp = Math.PI / 4;
this.steps = 0;
}
WingsBot.prototype.update = function() {
var b = this.env.move();
if (!b) {
this.steps++;
this.env.rotate(Math.PI/(1 + Math.random() * 0.5 * Math.log(this.steps)));
}
};
/**
* @constructor
*/
function SpiralBot(env) {
this.env = env;
this.temp = Math.PI / 4;
this.steps = 0;
this.square = 4;
}
SpiralBot.prototype.update = function() {
var b = this.env.move();
if (!b) {
this.env.rotate(Math.PI / 2);
this.square = 4 * Math.floor(Math.random() * 4);
} else {
this.steps++;
if (this.steps % this.square == 0) {
console.log(this.square);
this.steps = 0;
this.square = this.square + 4;
this.env.rotate(Math.PI / 2);
}
}
};
var bots = [
function(env) { return new RandBot(env) },
function(env) { return new SABot(env) },
function(env) { return new WingsBot(env) },
function(env) { return new SpiralBot(env) }
];
var runButton = document.getElementById('run');
var startSim = function(canvas, botId, cvg) {
var ctx = canvas.getContext("2d");
var env = new Env(Math.random() * Math.PI, Math.random(), Math.random(), ctx);
var bot = bots[botId](env);
var update = function() {
bot.update();
};
window.setInterval(update, 1);
var stats = function() {
var id = ctx.getImageData(0, 0, 250, 250);
var white = 0;
var black = 0;
for (var i = 0; i < id.data.length; i+=4) {
if (id.data[i + 3] == 0) {
black++;
} else {
white++;
}
}
cvg.innerText = 'Coverage : ' + 100 * white / (black + white);
};
window.setInterval(stats, 100);
};
runButton.addEventListener('click', function() {
var canvas1 = document.getElementById('room1');
var cvg1 = document.getElementById('coverage1');
startSim(canvas1, 0, cvg1);
var canvas2 = document.getElementById('room2');
var cvg2 = document.getElementById('coverage2');
startSim(canvas2, 1, cvg2);
var canvas3 = document.getElementById('room3');
var cvg3 = document.getElementById('coverage3');
startSim(canvas3, 2, cvg3);
var canvas4 = document.getElementById('room4');
var cvg4 = document.getElementById('coverage4');
startSim(canvas4, 3, cvg4);
}, false);
</script>
</body>
</html>