Iterative Flood Fill Demo

Published 2023-06-12


I wanted to see if I could implement a generic flood fill that's fast enough for live use. This probably isn't it; it's fast enough for the demo but maybe not for a quick-moving game, and misses some edge cases. Plus it eats a lot of memory. It was fun to write, though! And it doesn't recurse, it just builds a queue in memory and iterates through it.

How to play with the demo:

There are two fills happening here; the dark red from the bright red point, and the dark blue from (0, 0). (There's also a white rectangle behind the whole thing, which you'll notice if you move the shape around in a way that keeps the fills from reaching everything.)

I learned how to do the memory stuff from krajzeg's excruciatingly cool lighting engine, which is written up here if you haven't had the pleasure: https://hackernoon.com/pico-8-lighting-part-1-thin-dark-line-8ea15d21fed7

I'm making myself knock it off and go to bed before I solve those last couple of edge cases, but if this is useful to you as-is, please use it! Note that it helps itself to all of the user-defined memory space by default; you can limit it by changing the constants queue_start and queue_size (immediately above the floodfill function), but could in theory run into trouble if you set it very small and try to fill a very weird shape. (I haven't actually done the math on what it would require.)