Recursive algo to make a "fill" tool like almost every graphic app has - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Recursive algo to make a "fill" tool like almost every graphic app has

Posts: 2Member
well... I'm currently fighting with a procedure to make the good fill tool one can find in Photoshop... No worry here about tolerance, I just need it to fill the adjacents pixels with the same color of the one the user clicked... So here, RECURSIVE algorithm... mine works fine and completely fills the area it s supposed to HOWEVER... if this area is too big I get a StackOverFlow Exception cause I m calling too much the recursive function... couldn't solve this one (and I m pretty sad to have to waste such a well working algo just cause...)
so if anyone knows of an algo recursive or not, It would help a lot... Just in case someone would be interested, here is the code of my solution:

(well, more like making a tool to fill surfaces with pixels like in Photoshop, I'm doing it to fill tiles for a 2D game map editor... well, the algo itself shouldn't be that much different)

(I know the algo is written in basic but that's the language I was using and I didn't want to translate it... besides, the code is fairly easy to understand...)

Sub Fill(TileNum As Integer, i As Integer, j As Integer)

..Dim a,b As Integer

..for a=i-1 to i+1
....for b=j-1 to j+1
......if a>=0 and a<=NbTilesY and b>=0 and b<=NbTilesX and (a=i or b=j) then
........if Map(b,a)=nil and TileNum=-1 then
..........Map(b,a)=New TileClass
..........Map(b,a).Layer=CurrentLayer
..........Map(b,a).PicNum=CurrentTileIndex
..........MapPic.Graphics.DrawPicture Tiles(CurrentTileIndex),b*TileSizeX,a*TileSizeY

..........Fill(TileNum,a,b)
........end if
........if Map(b,a)<>nil then
..........if Map(b,a).PicNum=TileNum and Map(b,a).PicNum<>CurrentTileIndex and Map(b,a).Layer=CurrentLayer then
............Map(b,a).PicNum=CurrentTileIndex
............MapPic.Graphics.DrawPicture Tiles(CurrentTileIndex),b*TileSizeX,a*TileSizeY

............Fill(TileNum,a,b)
..........end if
........end if
......end if
....next
..next

Basically, I fill the current tile with the selected color and test the 4 tiles surrounding it (the above,under, left and right one), calling fill on each if it s the same color as the first selected one (this way, a tile "calls" the fill function only if himself has to be filled, so a tile won't call Fill twice... but still, I get a stackoverflow for large area)