# HELP! this easy code locks my computer!

• 07-11-2005
porsuk
HELP! this easy code locks my computer!
hi,
i have a boundaryFill() function and I want it to paint pixel by pixel
the area limited x and y

x is limited 10 to 100
y is limited 10 to 100

and I call function boundaryFill(50,50);
what is wrong with this code it locks my computer.
Code:

```void boundaryFill(int x, int y)     {         if ((x < 10) || (x >= 100)) return;         if ((y < 10) || (y >= 100)) return;         Form1->Canvas->Pixels[x][y]=clRed;             boundaryFill(x+1,y);             boundaryFill(x,y+1);             boundaryFill(x-1,y);             boundaryFill(x,y-1);             }```
• 07-11-2005
confuted
A note: You're painting each pixel several times. Work it out for yourself with a small case.
• 07-11-2005
dwks
Code:

```void boundaryFill(int x, int y) {         if(Form1->Canvas->Pixels[x][y] == clRed) return;  /* it's already red! */         if ((x < 10) || (x >= 100)) return;         if ((y < 10) || (y >= 100)) return;         Form1->Canvas->Pixels[x][y]=clRed;         boundaryFill(x+1,y);         boundaryFill(x,y+1);         boundaryFill(x-1,y);         boundaryFill(x,y-1);        }```
Beware, I did a similar function on my computer, and if you let if fill in larger than 200x200, you run out of stack space.

The above code does a flood fill.
[/edit]
• 07-11-2005
dwks
If you don't want a flood fill, start in the upper left corner and work your way downwards:

Code:

```void boundaryFill(int x, int y) {         if ((x < 10) || (x >= 100)) return;         if ((y < 10) || (y >= 100)) return;         Form1->Canvas->Pixels[x][y]=clRed;         boundaryFill(x+1,y);         boundaryFill(x,y+1); }```
• 07-11-2005
Salem
Well I did say in your previous post that this algorithm sucked.
And now you know why - it's an effective stack smasher on most real machines.
• 07-12-2005
dwks
I made a non-recursive version that works just fine, though.
• 07-12-2005
Rashakil Fol
I'd like to point out to the original poster that the original code is an infinite loop (that, while looping, overflows the stack). It will tunnel to a corner and then alternate back and forth between two pixels.
• 07-13-2005
dwks
You're right, although that had already been pointed out by confuted.