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;
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--;
}
n = 0;
for (let i = 0; i < N; i++) {
if (res[i] > 0) {
n++;
res[i] = 0;
} else {
res[i] = -1;
}
}
yield V;
}
})