Public
Edited
Nov 26, 2022
Importers
8 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
gens = ([W, H], nb) => {
const rand = max => (max * Math.random()) | 0;
return array(nb, i => ({ x: rand(W), y: rand(H), value: i }));
}
Insert cell
pseudoGens = ([W, H], nb) => {
const d = Math.sqrt((W * H) / nb);
const [Nw, Nh] = [Math.floor(W / d), Math.floor(H / d)];
const [f1, f2] = [0.4, Math.min(W / Nw, H / Nh)];
const rlx = index => ((index + 0.5 + (Math.random() * 2 - 1) * f1) * f2) | 0;
const rand = max => (max * Math.random()) | 0;
const gen = (i, j) => ({ x: rlx(i), y: rlx(j), value: rand(nbGenerators) });
return array(Nw, i => array(Nh, j => gen(i, j))).flat();
}
Insert cell
generators = (pseudoRelaxed ? pseudoGens : gens)(dimensions, nbGenerators)
Insert cell
Insert cell
Insert cell
Insert cell
render(function*() {
const [W, H] = dimensions;
const V = array(H, j => Array(W));
const d2 = (dx, dy) => dx * dx + dy * dy;
const minPair = (pair1, pair2) => (pair1.d2 < pair2.d2 ? pair1 : pair2);
const pair = ({ x, y, value }, i, j) => ({ d2: d2(x - i, y - j), value });
const min = (i, j) => (current, gen) => minPair(current, pair(gen, i, j));
const closest = (i, j) => generators.reduce(min(i, j), { d2: +Infinity });
const rows = array(H, j => j);
for (let row of rows) {
V[row] = array(W, i => closest(i, row).value);
yield V;
}
})
Insert cell
Insert cell
render(function* () {
const [W, H] = dimensions;
const N = generators.length;
const res = Array(N).fill(0);
let nd = 0;

// initialization
const V = Array(H);
for (let j = 0; j < H; j++) V[j] = Array(W);
const setCell = function ({ x: x0, y: y0, value }, dx, dy) {
const [x, y] = [x0 + dx, y0 + dy];
if (x < 0 || x >= W || y < 0 || y >= H) return 0;
if (V[y][x] != undefined) return 0;
V[y][x] = value;
return 1;
};

let n = N;
let r = 0;
// As long as there are active generators
while (n > 0) {
r++;
let x = r - 1;
let y = 0;
let dx = 1;
let dy = 1;
let err = dx - (r << 1);

while (x >= y) {
for (let i = 0; i < N; i++) {
if (res[i] != -1) {
const gen = generators[i];
res[i] +=
setCell(gen, +x, +y) +
setCell(gen, +y, +x) +
setCell(gen, -y, +x) +
setCell(gen, -x, +y) +
setCell(gen, -x, -y) +
setCell(gen, -y, -x) +
setCell(gen, +y, -x) +
setCell(gen, +x, -y);
}
}

if (err <= 0) {
y++;
err += dy;
dy += 2;
}

if (err > 0) {
x--;
dx += 2;
err += dx - (r << 1);
}
}

// Count remaining active generators and reset them
n = 0;
for (let i = 0; i < N; i++) {
if (res[i] > 0) {
n++;
res[i] = 0;
} else {
res[i] = -1;
}
}

yield V;
}
})
Insert cell
Insert cell
render(function* () {
const [W, H] = dimensions;
const N = generators.length;
const res = Array(N).fill(0);
let nd = 0;

// initialization
const V = Array(H);
for (let j = 0; j < H; j++) V[j] = Array(W);
for (const { x, y, value } of generators) V[y][x] = value;

const setCell = function ({ x: x0, y: y0, value }, dx, dy) {
const [x, y] = [x0 + dx, y0 + dy];
if (x < 0 || x >= W || y < 0 || y >= H) return 0;
if (V[y][x] != undefined) return 0;
V[y][x] = value;
return 1;
};

let n = N;
let r = 0;
// As long as there are active generators
while (n > 0) {
r++;
let x = 0;
let y = r;
let d = r - 1;

while (y >= x) {
for (let i = 0; i < N; i++) {
if (res[i] != -1) {
const gen = generators[i];
res[i] +=
setCell(gen, +x, +y) +
setCell(gen, +y, +x) +
setCell(gen, -y, +x) +
setCell(gen, -x, +y) +
setCell(gen, -x, -y) +
setCell(gen, -y, -x) +
setCell(gen, +y, -x) +
setCell(gen, +x, -y);
}
}

if (d >= 2 * x) {
d = d - 2 * x - 1;
x++;
} else if (d < 2 * (r - y)) {
d = d + 2 * y - 1;
y--;
} else {
d = d + 2 * (y - x - 1);
y--;
x++;
}
}

// Count remaining active generators and reset them
n = 0;
for (let i = 0; i < N; i++) {
if (res[i] > 0) {
n++;
res[i] = 0;
} else {
res[i] = -1;
}
}

yield V;
}
})
Insert cell
Insert cell
render(function* () {
const [W, H] = dimensions;
const N = generators.length;
const D = Array(H);
const V = Array(H);
for (let j = 0; j < H; j++) {
D[j] = Array(W);
V[j] = Array(W);
for (let i = 0; i < W; i++) D[j][i] = +Infinity;
}
for (const { x, y, value } of generators) {
V[y][x] = value;
D[y][x] = 0;
}

const res = Array(N).fill(0);
const setCell = function ({ x: x0, y: y0, value }, dx, dy, d2) {
const [x, y] = [x0 + dx, y0 + dy];
if (x < 0 || x >= W || y < 0 || y >= H) return 0;
if (V[y][x] == undefined || d2 < D[y][x]) {
V[y][x] = value;
D[y][x] = d2;
return 1;
} else {
return 0;
}
};

let n = N;
let r = 0;
let nd = 0;

// As long as there are active generators
while (n > 0) {
r++;
let x = 0;
let y = r;

while (y >= x) {
const d2 = x * x + y * y;
nd++;
for (let i = 0; i < N; i++) {
if (res[i] != -1) {
const gen = generators[i];
res[i] +=
setCell(gen, x, y, d2) +
setCell(gen, x, -y, d2) +
setCell(gen, y, x, d2) +
setCell(gen, y, -x, d2) +
setCell(gen, -x, y, d2) +
setCell(gen, -x, -y, d2) +
setCell(gen, -y, -x, d2) +
setCell(gen, -y, x, d2);
}
}
x++;
y--;
}

// Count remaining active generators and reset them
n = 0;
for (let i = 0; i < N; i++) {
if (res[i] > 0) {
n++;
res[i] = 0;
} else {
res[i] = -1;
}
}

yield V;
}
})
Insert cell
Insert cell
render(function* () {
const [W, H] = dimensions;
const N = generators.length;
const D = Array(H);
const V = Array(H);
for (let j = 0; j < H; j++) {
D[j] = Array(W);
V[j] = Array(W);
for (let i = 0; i < W; i++) D[j][i] = +Infinity;
}
for (const { x, y, value } of generators) {
V[y][x] = value;
D[y][x] = 0;
}

const res = Array(N).fill(0);
const setCell = function ({ x: x0, y: y0, value }, dx, dy, d2) {
const [x, y] = [x0 + dx, y0 + dy];
if (x < 0 || x >= W || y < 0 || y >= H) return 0;
if (V[y][x] == undefined || d2 < D[y][x]) {
V[y][x] = value;
D[y][x] = d2;
return 1;
} else {
return 0;
}
};

let n = N;
let r = 0;
let nd = 0;

// As long as there are active generators
while (n > 0) {
r++;
let x = 0;
let y = r;

while (y >= x) {
nd++;
for (let i = 0; i < N; i++) {
if (res[i] != -1) {
const gen = generators[i];
res[i] +=
setCell(gen, x, y, r) +
setCell(gen, x, -y, r) +
setCell(gen, y, x, r) +
setCell(gen, y, -x, r) +
setCell(gen, -x, y, r) +
setCell(gen, -x, -y, r) +
setCell(gen, -y, -x, r) +
setCell(gen, -y, x, r);
}
}
x++;
y--;
}

// Count remaining active generators and reset them
n = 0;
for (let i = 0; i < N; i++) {
if (res[i] > 0) {
n++;
res[i] = 0;
} else {
res[i] = -1;
}
}

yield V;
}
})
Insert cell
Insert cell
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