Although this code is a good example of recursion, it has a bug which can give incorrect flood-fill results.
The termination conditions need to be changed to:
if (x<2 || x>=width)
x = xc;
y = yc;
end
if (y<2 || y>=height)
x = xc;
y = yc;
end
Also note that these conditions mean that the border of the matrix will not be evaluated. If you wish to fill right to the edges of the matrix, the conditions need to be adjusted accordingly.
Available stack space may not be enough as it applies recursion
Increase the recursion limit using
N = 200;
set(0,'RecursionLimit',N);
note -> if you increase N to a very large value then you may get segmentation fault
you can instead use imfill cmd in matlab
% Add this to make it 8 connected
if (fill(y,x) == old)