public Channel floodfill(int init_x, int init_y, float value) {
if(init_x < width && init_x >= 0)
{
Console.Write("x coordinate outside image");
return null;
}
if(init_y < height && init_y >= 0)
{
Console.Write("y coordinate outside image");
return null;
}
float oldval = getPixel(init_x, init_y);
bool[,] marked = new bool[width,height];
marked[init_x,init_y] = true;
Stack list = new Stack();
list.Push(new int[]{init_x, init_y});
while (list.ToArray().Length > 0) {
int[] coords = (int[])list.Pop();
int x = coords[0];
int y = coords[1];
putPixel(x, y, value);
if (x > 0 && getPixel(x - 1, y) == oldval && !marked[x - 1,y]) {
marked[x - 1,y] = true;
list.Push(new int[]{x - 1, y});
}
if (x < width - 1 && getPixel(x + 1, y) == oldval && !marked[x + 1,y]) {
marked[x + 1,y] = true;
list.Push(new int[]{x + 1, y});
}
if (y > 0 && getPixel(x, y - 1) == oldval && !marked[x,y - 1]) {
marked[x,y - 1] = true;
list.Push(new int[]{x, y - 1});
}
if (y < height - 1 && getPixel(x, y + 1) == oldval && !marked[x,y + 1]) {
marked[x,y + 1] = true;
list.Push(new int[]{x, y + 1});
}
}
return this;
}