Flood fill

Monkey Forums/Monkey Programming/Flood fill

Sub_Zero(Posted 2013) [#1]
Does anyone have a gradient fill function?

Edit: Should be flood fill! (sorry) :)


Goodlookinguy(Posted 2013) [#2]
I ripped these from my code library and changed them to work with default mojo. I was going to make one that could do a gradient fill at any angle but lost interest when I realized that I would never use it. Well, anyways, here you go.




Sub_Zero(Posted 2013) [#3]
Thanks for that... Does anyone have a simple fill function? Single color will do fine. It needs to work similar to that of bucket fill in mspaint.


Nobuyuki(Posted 2013) [#4]
That's doubtful. Why don't you take a look at this?
http://en.wikipedia.org/wiki/Flood_fill

Disclaimer: I used to use recursive floodfill back in high school and managed to overflow the call stack in VB on anything larger than a small sprite's worth of fill. My suggestion is to look into the alternative method. These sorts of things have very limited application, so you won't usually see them around in a zillion easy-to-digest forms.


Goodlookinguy(Posted 2013) [#5]
I think I misunderstood the original question...

Anyways, if we're talking about flood filling, I've also done recursive flood filling and caused a stack overflow. I strongly recommend the queue version talked about in the Wikipedia page. It has a much lower chance of this problem and is a lot faster.


Sub_Zero(Posted 2013) [#6]
Thanks guys :) Will have a look :)


Sub_Zero(Posted 2013) [#7]
Ended up with these two:

4-way flood fill function using stacks:


Flood fill Scanline function using stacks (faster):



Gerry Quinn(Posted 2013) [#8]
If you need a fill that always works properly, it might be better to find out how to edit the stack size on your target(s).

EDIT: Never mind, I didn't read properly and thought people were talking about an old memory-saving technique using a cyclic buffer, which used relatively little memory at the cost of sometimes doing an incomplete fill.

The various techniques to replace stack by heap allocation are fine.


maltic(Posted 2013) [#9]
Or just use your own stack data-structure, like Sub_Zero's example code. Using the call stack or using your own stack is (in this case) functionally equivalent. A stack based flood-fill is optimal, I think. You consider every pixel only once--which you have to do to draw them anyway, and use at most O(L) memory on the stack (where L is the length in pixels from the start point to the furthest away pixel to be filled) as opposed to O(W) using a queue (where W is the maximum size in pixels of the perimeter of your flood fill area).