Published
Edited
Feb 14, 2019
3 forks
11 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
field = {
reset;
const field = new Uint8Array(width * height);
const fcenterX = Math.floor(width / 2);
const fcenterY = Math.floor(height / 2);
field[fcenterX + fcenterY * width] = 1;
return field;
}
Insert cell
particles = {
reset;
const particles = {
x: new Uint16Array(count),
y: new Uint16Array(count),
stuck: new Uint8Array(count)
};
for (let i = 0; i < count; i++) {
resetSingleParticle(particles, i);
}
return particles;
}
Insert cell
resetSingleParticle = (particles, i) => {
let x = Math.floor(Math.random() * width);
let y = Math.floor(Math.random() * height);

while (field[y * width + x]) {
x = Math.floor(Math.random() * width);
y = Math.floor(Math.random() * height);
}

particles.x[i] = x;
particles.y[i] = y;
}
Insert cell
function alone(i) {
let cx = particles.x[i];
let cy = particles.y[i];

let lx = cx - 1;
let rx = cx + 1;
let ty = cy - 1;
let by = cy + 1;

if (
cx <= 0 ||
cx >= width ||
lx <= 0 ||
lx >= width ||
rx <= 0 ||
rx >= width ||
cy <= 0 ||
cy >= height ||
ty <= 0 ||
ty >= height ||
by <= 0 ||
by >= height
) {
return true;
}

cy *= width;
by *= width;
ty *= width;

if (field[cx + ty] || field[lx + cy] || field[rx + cy] || field[cx + by]) {
return false;
}

if (field[lx + ty] || field[lx + by] || field[rx + ty] || field[rx + by]) {
return false;
}

return true;
}
Insert cell
updateSingleParticle = i => {
let x = particles.x[i];
let y = particles.y[i];

x += Math.random() > 0.5 ? 1 : -1;
y += Math.random() > 0.5 ? 1 : -1;

if (x < 0 || y < 0 || x >= width || y >= height) {
return resetSingleParticle(particles, i);
}

particles.x[i] = x;
particles.y[i] = y;

if (!alone(i)) {
particles.stuck[i] = 1;
field[y * width + x] = 1;
}
}
Insert cell
count = 40000
Insert cell
height = width * 0.75
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more