-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
324 lines (294 loc) · 8.63 KB
/
index.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
'use strict';
const settings = {
sensor_distance: 2,
sensor_angle: 40/180*Math.PI, // radians
turning_speed: 40/180*Math.PI, // radians
speed: 1,
decay_factor: 0.95,
deposit_amount: 0.6,
num_agents: 5000,
start_in_circle: false, // otherwise start randomly
highlight_agents: false,
random_turning: false, // randomly turn within the limits of turning_speed
wrap_around: true,
show_debug: false,
};
const settings_to_text = {
sensor_angle: rad_to_deg,
turning_speed: rad_to_deg,
num_agents: v => ''+v,
};
// use a Gaussian kernel for diffusion
const weight = [
1/16, 1/8, 1/16,
1/8, 1/4, 1/8,
1/16, 1/8, 1/16,
];
let counts = [0,0,0,0];
let regenerate_next = true;
const default_settings = {};
for (const [key, value] of Object.entries(settings)) {
default_settings[key] = value;
}
// convert radians to degrees
function rad_to_deg(value) {
return Math.round(value * 180 / Math.PI);
}
// Update the state in place
function sim_step(agents, trail, width, height) {
function index(x, y) {
return x + y * width;
}
function step_sense_and_rotate() {
for (let agent of agents) {
function sense_relative_angle(theta) {
return trail[index(
Math.round(agent.x + Math.cos(agent.heading + theta) * settings.sensor_distance),
Math.round(agent.y + Math.sin(agent.heading + theta) * settings.sensor_distance)
)];
}
const sense_left = sense_relative_angle(settings.sensor_angle);
const sense_middle = sense_relative_angle(0);
const sense_right = sense_relative_angle(-settings.sensor_angle);
const modified_turning = (settings.random_turning ? (Math.random() * 0.5 + 0.5) : 1) * settings.turning_speed;
let option = -1;
if (sense_middle > sense_left && sense_middle > sense_right) {
// no change
option = 0;
} else if (sense_left > sense_right) {
option = 1;
agent.heading += modified_turning;
} else if (sense_right > sense_left) {
option = 2;
agent.heading -= modified_turning;
} else {
option = 3;
agent.heading += Math.round(Math.random() * 2 - 1) * settings.turning_speed;
}
counts[option] += 1
agent.last_option = option;
}
}
function step_move() {
for (let agent of agents) {
agent.x += settings.speed * Math.cos(agent.heading);
agent.y += settings.speed * Math.sin(agent.heading);
if (settings.wrap_around) {
agent.x = (agent.x + width) % width;
agent.y = (agent.y + height) % height;
}
}
}
function step_deposit() {
for (let agent of agents) {
const x = Math.round(agent.x);
const y = Math.round(agent.y);
if (x <= 0 || y <= 0 || x >= width-1 || y >= height-1)
continue;
trail[index(x, y)] += settings.deposit_amount;
}
}
function step_diffuse_and_decay() {
let old_trail = Float32Array.from(trail);
for (let y=1; y<height-1; ++y) {
for (let x=1; x<width-1; ++x) {
const diffused_value = (
old_trail[index(x-1, y-1)] * weight[0] +
old_trail[index(x , y-1)] * weight[1] +
old_trail[index(x+1, y-1)] * weight[2] +
old_trail[index(x-1, y )] * weight[3] +
old_trail[index(x , y )] * weight[4] +
old_trail[index(x+1, y )] * weight[5] +
old_trail[index(x-1, y+1)] * weight[6] +
old_trail[index(x , y+1)] * weight[7] +
old_trail[index(x+1, y+1)] * weight[8]
);
trail[index(x, y)] = Math.min(1.0, diffused_value * settings.decay_factor);
}
}
}
// Run steps shown in
// https://payload.cargocollective.com/1/18/598881/13800048/diagram_670.jpg
// Some steps are combined for speed and compactness
step_sense_and_rotate();
step_move();
step_deposit();
step_diffuse_and_decay();
return trail;
}
function render(trail, canvas, agents) {
const width = canvas.width;
const height = canvas.height;
const ctx = canvas.getContext('2d');
const trail_image = ctx.getImageData(0, 0, width, height);
const max_brightness = settings.highlight_agents ? 50 : 255;
let i = 0;
for (let y=0; y<height; ++y) {
for (let x=0; x<width; ++x) {
const value = trail[i];
const brightness = Math.floor(value * max_brightness);
trail_image.data[i*4+0] = brightness;
trail_image.data[i*4+1] = brightness;
trail_image.data[i*4+2] = brightness;
trail_image.data[i*4+3] = 255;
i++;
}
}
if (settings.highlight_agents) {
for (let agent of agents) {
let color = [0,0,0];
switch (agent.last_option) {
case 0: color = [150, 50, 50]; break; // straight
case 1: color = [ 50,150, 50]; break; // right
case 2: color = [ 50, 50,150]; break; // left
case 3: color = [255,255,255]; break; // indecisive
}
trail_image.data[(Math.floor(agent.x)+Math.floor(agent.y)*width)*4+0] = color[0];
trail_image.data[(Math.floor(agent.x)+Math.floor(agent.y)*width)*4+1] = color[1];
trail_image.data[(Math.floor(agent.x)+Math.floor(agent.y)*width)*4+2] = color[2];
}
}
ctx.putImageData(trail_image, 0, 0);
}
function update_settings_text() {
for (let name in settings) {
let node = document.getElementById('text_'+name);
if (!node) continue;
let converter = settings_to_text[name];
let value = settings[name];
let text = converter ? converter(value) : value;
if (typeof text == 'number') {
text = text.toFixed(2);
}
node.innerText = text;
}
}
// get settings from query string
function settings_from_query_string() {
for (const [name, value] of (new URL(document.location)).searchParams) {
if (name in settings) {
if (typeof(settings[name]) == 'number') {
settings[name] = parseFloat(value);
} else {
settings[name] = value == 'true';
}
}
}
}
// update query string with non-default settings
function settings_to_query_string() {
const params = (new URL(document.location)).searchParams;
const before = params.toString();
for (const [key, value] of Object.entries(settings)) {
if (value != default_settings[key]) {
params.set(key, value);
} else {
params.delete(key);
}
}
const url = new URL(document.location);
if (params.toString() !== before) {
url.search = '?'+params.toString();
window.history.replaceState({},'',url);
}
}
// store settings from settings object to DOM
function settings_to_dom() {
for (let name in settings) {
let node = document.getElementById(name);
if (typeof(settings[name]) == 'number') {
node.value = settings[name];
} else {
node.checked = settings[name];
}
}
update_settings_text();
}
// load settings from DOM to settings object
function settings_from_dom() {
for (let name in settings) {
let node = document.getElementById(name);
if (typeof(settings[name]) == 'number') {
settings[name] = parseFloat(node.value);
} else {
settings[name] = node.checked;
}
}
update_settings_text();
}
function reset_settings() {
for (name in settings) {
settings[name] = default_settings[name];
}
settings_to_dom();
}
function update_reset_button_enabled() {
let any_non_default = false;
for (let name in settings) {
if (settings[name] != default_settings[name]) {
any_non_default = true;
break;
}
}
document.getElementById('reset_button').disabled = !any_non_default;
}
onload = function() {
settings_from_query_string();
settings_to_dom();
const canvas = document.getElementById('simcanvas');
const width = canvas.width;
const height = canvas.height;
const agents = [];
function regenerate() {
agents.splice(0,agents.length); // empty list
if (settings.start_in_circle) {
const radius = Math.min(width, height) * 0.2;
for (let i=0; i<settings.num_agents; ++i) {
const t = 2 * Math.PI*i/settings.num_agents;
agents.push({
x: Math.cos(t) * radius + width / 2,
y: Math.sin(t) * radius + height / 2,
heading: t - Math.PI / 2,
});
}
} else {
for (let i=0; i<settings.num_agents; ++i) {
agents.push({
x: Math.random() * width,
y: Math.random() * height,
heading: Math.random() * 2 * Math.PI, // radians
});
}
}
regenerate_next = false;
}
let trail = new Float32Array(width * height);
function next_frame() {
settings_from_dom();
settings_to_query_string();
update_reset_button_enabled();
if (regenerate_next) {
regenerate();
}
trail = sim_step(agents, trail, width, height);
render(trail, canvas, agents);
window.requestAnimationFrame(next_frame);
document.getElementById('debug').style.display = settings.show_debug ? 'block' : 'none';
if (settings.show_debug) {
let sum = 0;
for (let c of counts) {
sum += c;
}
stats.innerText = 'Straight, right, left, random: ' +
counts.map(c => Math.round(c/sum*100)+'%').join(', ');
}
}
next_frame();
skip = function(num_frames) {
for (var i=0; i<num_frames; ++i) {
trail = sim_step(agents, trail, width, height);
}
}
}
// functions that will be overridden in onload
var skip = function() {}