eugeneng
asked on
Flood Fill question
I obtained a flood fill algorithm from the net. It is a recursive function :
void floodFill(int x, int y, int fill, int old)
{
if ((x < 0) || (x >= raster.width)) return;
if ((y < 0) || (y >= raster.height)) return;
if (raster.getPixel(x, y) == old) {
raster.setPixel(fill, x, y);
floodFill(x+1, y, fill, old);
floodFill(x, y+1, fill, old);
floodFill(x-1, y, fill, old);
floodFill(x, y-1, fill, old);
}
}
i would like to apply this function into program where can flood fill a map with a tile. But i encounter 1 problem, that is when the map size is huge (say 1000x1000), since this is recursive function, it might call itself 1000x1000 times, and thus it blown out the stack and crashes the program.
How am i going to solve this problem ?
void floodFill(int x, int y, int fill, int old)
{
if ((x < 0) || (x >= raster.width)) return;
if ((y < 0) || (y >= raster.height)) return;
if (raster.getPixel(x, y) == old) {
raster.setPixel(fill, x, y);
floodFill(x+1, y, fill, old);
floodFill(x, y+1, fill, old);
floodFill(x-1, y, fill, old);
floodFill(x, y-1, fill, old);
}
}
i would like to apply this function into program where can flood fill a map with a tile. But i encounter 1 problem, that is when the map size is huge (say 1000x1000), since this is recursive function, it might call itself 1000x1000 times, and thus it blown out the stack and crashes the program.
How am i going to solve this problem ?
write your own stack using file & push all local variable when u call the function & pop on return.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
u can enlarge the size of each pixel by drawing rects instead of lighting pixels.
say 4x4 of 8x8 or something similar it will save you time .
or you can save the old coordinates and draw a big rect, and then restore what you had before , which will do the same trick even better.
both - ways you save time and effort , if you like more details please notify.
say 4x4 of 8x8 or something similar it will save you time .
or you can save the old coordinates and draw a big rect, and then restore what you had before , which will do the same trick even better.
both - ways you save time and effort , if you like more details please notify.
ASKER
hi arbitrary, actually my map contains tiles instead of pixel, but basically they are the same. can you method still applicable to my program ? because i gotto check tile's data, not only color? if you method is still applicable, can you give me more detail of it.
your map into smaller pieces and floodFill them individually.