Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which flood-fill algorithm is better for performance?

i'm trying to implement an algorithm which is flood-fill alike. the problem is that i'm not sure in what way i should implement it e.g recursive - non-recursive.
i know that each has its defects but one of them must be faster than the other one. the recursive opens new function on the stack when the non-recursive allocates 4 new points each time.
example for the non-iterative :

Stack<Point> stack = new Stack<Point>();
    stack.Push(q);
    while (stack.Count > 0)
    {
        Point p = stack.Pop();
        int x = p.X;
        int y = p.Y;
        if (y < 0 || y > h - 1 || x < 0 || x > w - 1)
                continue;
        byte val = vals[y, x];
        if (val == SEED_COLOR)
        {
                vals[y, x] = COLOR;
                stack.Push(new Point(x + 1, y));
                stack.Push(new Point(x - 1, y));
                stack.Push(new Point(x, y + 1));
                stack.Push(new Point(x, y - 1));
        }
    }

edit : im going to apply the following algorithm on a 600X600 pixels map. though the floodfill isn't going to be applied on the entire map, it should cover about 30% - 80% of the map each iteration. my point is to discover edges in a height map and mark those edges for a further use.

like image 716
igal k Avatar asked Feb 16 '12 20:02

igal k


People also ask

Which is better flood fill or boundary-fill algorithm?

Boundary-fill algorithm is faster than the Flood-fill algorithm. In Flood-fill algorithm a random colour can be used to paint the interior portion then the old one is replaced with a new one. In Boundary-fill algorithm Interior points are painted by continuously searching for the boundary colour.

For what condition flood fill algorithm can be used?

Flood fill is an algorithm mainly used to determine a bounded area connected to a given node in a multi-dimensional array. It is a close resemblance to the bucket tool in paint programs.

What are the advantages of flood fill algorithm?

Advantages Flood FillIt is an easy way to fill color in the graphics. One just takes the shape and starts flood fill. The algorithm works in a manner so as to give all the pixels inside the boundary the same color leaving the boundary and the pixels outside.

Is flood fill DFS or BFS?

Floodfill can be implemented either with DFS or BFS, when all you care about is marking nodes with the same color. But when you also want to keep track of shortest distances, you'd better do a BFS rather than DFS.


1 Answers

Computers process XY loops and 2D arrays very efficiently.

check this video to see what happens if put the standard recursive routine in a loop: https://www.youtube.com/watch?v=LvacRISl99Y enter image description here

Instead of using complex logic to track previously verified neighbor spaces, you can have a 2D array that records all the verified spaces. Reading from the verified array is 2-3 instructions: IF pixel[23,23] is verified, THEN fill it and check it's neighbors.

like image 156
LifeInTheTrees Avatar answered Nov 16 '22 00:11

LifeInTheTrees