Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] Flood Fill-Competition

Author
Message
Mr Kohlenstoff
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 6th Apr 2010 22:25 Edited at: 8th Apr 2010 00:38
Competition or not, I wrote a simple flood fill-algorithm. Anybody may read, use, alter, spread or curse it however he wants.




To go a step beyond, there's this "competition"-idea: You might write your own flood filling-function and try to make it faster than mine. My function is not that great or optimized, so that should be quite possible. The idea obviously is not to "win" but to find a fast function anybody can use.

By the way, my function does not check if it stays within the array. So, in some cases it might cause errors, but in the chosen example it does not. Thus, it is up to you if you want your function to work in every possible case or just in this example.

Basically, all you need is the Tile(x,y)-Array, which has a fixed size of 500x500. It stores a byte-variable, defining the type of the specific tile.


Edit: Added a second, non-recursive function. Seems to be a bit slower, but is probably more stable.



Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 7th Apr 2010 22:25
Last time I wrote a floodfill function using a recursive algorithm, DBP crashed on me once the area was too big.

Does DBP still have this problem?

Cheers!
Sven B

Mr Kohlenstoff
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 8th Apr 2010 00:19 Edited at: 8th Apr 2010 00:39
Good point, so I guess I'll write an iterative approach.

...

Here it is, as well as at the end of my first post.



Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 12th Apr 2010 18:59 Edited at: 12th Apr 2010 19:00
For a really fast technique, try scanning in different directions, like north then east then south then west. This way the fill will stretch out in one direction to the limit, then the next direction will fill a lot of space, because it has this long strip from the previous pass. I imagine it as a paint brush painting in a different direction each time, spreading out the fill.

You could also use a range box that expands with the flood fill. Like, store the maximum and minimum X and Y coordinates, and use them to start. So if your scanning north, you can start at the maximum Y location and avoid scanning areas that you know don't have any fillable pixels yet. The last time I tried this, the range box made a massive difference to the fill speed.


Health, Ammo, and bacon and eggs!
Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 12th Apr 2010 21:15 Edited at: 23rd Aug 2010 00:34

Login to post a reply

Server time is: 2024-04-20 17:04:17
Your offset time is: 2024-04-20 17:04:17