Task 6 of 6
Jump flooding is an approximation. It is not "exact but for rounding": there are seed layouts for which a cell's true nearest seed never reaches it. The route of halving jumps the ladder counted on always exists — but a cell part-way along it only forwards the seed it is holding at that moment, and it may be holding a different one that looked nearer when the pass ran. The chain breaks in the middle. Every honest description of this algorithm says so, and the way to believe it is to count.
Careful about what "wrong" means, though. Two seeds can be exactly the same distance away, and then both answers are right — comparing the ids the two methods chose would report roughly twice as many failures as there are. A cell is wrong only when the seed it holds is strictly farther than the true nearest one.
The layout here is 24 seeds chosen to make the flaw visible. It is not typical: across 200 random 24-seed layouts on this grid, 95 came out perfect and the average was 1.4 wrong cells out of 16,384 — 0.009%. This one manages 67. The standard patch, one extra pass at stride 1 ("JFA+1"), takes it to 55 — better, and still not exact. If you need exact, you need a different algorithm; what you get here is a fast answer with a bounded, measurable error, and for a glow or an outline or a shatter pattern that is the right trade.
worse(jfa, truth) — 1 where the flooded seed
is strictly farther than the true nearest one, 0 otherwise — and countOnes to
total it.-1 counts as wrongcountOnes sums the field in plain JavaScriptUnpack jfa[y][x] and truth[y][x] the usual way, measure
both seeds from this pixel, and compare the squared distances. Strictly greater:
let bad = 0;
if (dJfa > dTruth) bad = 1;
return bad;An id of -1 unpacks to nonsense, so give it a distance larger than
anything on the grid before the comparison — the same
n * n * 2 the flood pass starts from.
The field is 0s and 1s, so the count is the sum:
let n = 0;
for (let y = 0; y < 128; y++) {
for (let x = 0; x < 128; x++) n += grid[y][x];
}
return n;This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.