Click here to Skip to main content
15,886,026 members
Articles / Programming Languages / C#

Queue-Linear Flood Fill: A Fast Flood Fill Algorithm

Rate me:
Please Sign up or sign in to vote.
4.92/5 (44 votes)
15 Nov 20066 min read 236.6K   10K   59  
A super-fast flood fill algorithm and implementation, plus helpful optimization tips for image processing.
using System;
using System.Drawing;

namespace FloodFill2
{
    /// <summary>A queue of FloodFillRanges.</summary>
	public class FloodFillRangeQueue
	{
        FloodFillRange[] array;
        int size;
        int head;

        /// <summary>
        /// Returns the number of items currently in the queue.
        /// </summary>
        public int Count
        {
            get { return size; }
        }

		public FloodFillRangeQueue():this(10000)
		{

		}

        public FloodFillRangeQueue(int initialSize)
        {
            array = new FloodFillRange[initialSize];
            head = 0;
            size = 0;
        }

        /// <summary>Gets the <see cref="FloodFillRange"/> at the beginning of the queue.</summary>
        public FloodFillRange First 
		{
			get { return array[head]; }
		}

        /// <summary>Adds a <see cref="FloodFillRange"/> to the end of the queue.</summary>
        public void Enqueue(ref FloodFillRange r) 
		{
			if (size+head == array.Length) 
			{
                FloodFillRange[] newArray = new FloodFillRange[2 * array.Length];
                Array.Copy(array, head, newArray, 0, size);
				array = newArray;
                head = 0;
			}
            array[head+(size++)] = r;
		}

        /// <summary>Removes and returns the <see cref="FloodFillRange"/> at the beginning of the queue.</summary>
        public FloodFillRange Dequeue() 
		{
            FloodFillRange range = new FloodFillRange();
            if (size>0)
            {
                range = array[head];
                array[head] = new FloodFillRange();
                head++;//advance head position
                size--;//update size to exclude dequeued item
            }
            return range;
		}

        /// <summary>Remove all FloodFillRanges from the queue.</summary>
		/*public void Clear() 
		{
			if (size > 0)
				Array.Clear(array, 0, size);
			size = 0;
		}*/

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
My main goal as a developer is to improve the way software is designed, and how it interacts with the user. I like designing software best, but I also like coding and documentation. I especially like to work with user interfaces and graphics.

I have extensive knowledge of the .NET Framework, and like to delve into its internals. I specialize in working with VG.net and MyXaml. I also like to work with ASP.NET, AJAX, and DHTML.

Comments and Discussions