Well, I'm getting the outline (path) of a building. And there is a problem. When I iterate it's pixels I check if the current pixel is white to start detecting it's walls.
What happens with this? When the detection is finished it starts again and again, because the next iterated pixel is also white. That's why I need to use a flood fill algorithm (to flag that the current build is completed).
Well, I have already implemented it, but I have problems.
public static void FloodFill(ref Color[] source, Point pt, int width, int height, Color targetColor, Color replacementColor)
{
Stack<Point> pixels = new Stack<Point>();
targetColor = source[P(pt.x, pt.y, width, height)];
pixels.Push(pt);
while (pixels.Count > 0)
{
Point a = pixels.Pop();
if (source[P(a.x, a.y, width, height)] == targetColor)
{
source[P(a.x, a.y, width, height)] = replacementColor;
if (a.x > 0)
pixels.Push(new Point(a.x - 1, a.y));
if (a.x < width)
pixels.Push(new Point(a.x + 1, a.y));
if (a.y > 0)
pixels.Push(new Point(a.x, a.y - 1));
if (a.y < height)
pixels.Push(new Point(a.x, a.y + 1));
}
}
}
Extracted from: https://simpledevcode.wordpress.com/2015/12/29/flood-fill-algorithm-using-c-net/
The problem here is that by some reason, ref keyword isn't taking effect.
My implementation:
private IEnumerator MainGenerationDebug(Color[] source, Color32[] target, int width, int height)
{
// (1)
for (int i = 0; i < source.Length; Interlocked.Increment(ref i))
{
// (2)
Color c = source[i];
// (3)
int x = i % width,
y = i / width;
// (4) & (5)
yield return IntMapIteration(source, target, width, c, i, exceptions, (s, t) =>
{
source = s;
target = t;
});
// (6)
if (c == Color.white)
{
F.FloodFill(ref source, new Point(x, y), width, height, Color.white, Color.red);
}
}
}
The process is the following:
- I start looping all pixels, because I'm in another thread I use Interlocked.Increment.
- I get the current color
- I get the current x, y (2D) from the index (1D)
- Due to I'm in a Coroutine (I'm debugging so I need to see what's happening) I use yielding
- When the IEnumerator finishes I call an Action. (I use IEnumerators because I'm calling an Coroutine in Unity3D)
- After everything finishes, if the current pixel is white, I start flood-filling the array.
I think everything is correct. By maybe I'm missing something.
What could you say about this?