# Thread: HELP! this easy code locks my computer!

1. ## 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);

}``` 2. A note: You're painting each pixel several times. Work it out for yourself with a small case. 3. 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] 4. 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);
}``` 5. 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. 6. I made a non-recursive version that works just fine, though. 7. 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. 8. You're right, although that had already been pointed out by confuted. Popular pages Recent additions 