Mail Archives: djgpp/1999/08/26/19:35:07
Carl Marg writes:
> The funny thing is it will seem to work right for a fraction of a
> second, but a bit into the fill, it will crash.
[...]
> void boundfill(int x, int y, int fill, int bound)
> {
> int cur;
> cur=getpixel(screen,x,y);
> if ((cur!=bound) && (cur!=fill))
> {
> putpixel(screen,x,y,fill);
> boundfill(x+1,y,fill,bound);
> boundfill(x-1,y,fill,bound);
> boundfill(x,y+1,fill,bound);
> boundfill(x,y-1,fill,bound);
> } //if
> }
You are probably running out of stack space: this single pixel recursive
algorithm will use an insane amount of stack for anything other than
very tiny areas. You can implement this far more efficiently if you do
the filling in terms of horizontal scanline segments rather than as
individual pixels, but even after you do that, it would probably be
better to use dynamically allocated memory rather than recursion
(because even with a very smart algorithm, there are still some
situations that will recurse too deeply and overflow your stack).
Foley & Van Damme, "Principles and Practice of Computer Graphics" has
a decent section about floodfill algorithms, or you could look at
the Allegro source file flood.c for a working implementation.
Shawn Hargreaves.
- Raw text -