Advertisement
Guest User

FloodFill fix 3

a guest
Feb 20th, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1.         public static void FloodFill(PixelManipulator manipulator, Point position, Pixel targetColor, Pixel replaceColor, byte threshold)
  2.         {
  3.             unchecked
  4.             {
  5.                 manipulator.Position = position;
  6.  
  7.                 if (!manipulator.Current.Similar(targetColor, threshold))
  8.                     return;
  9.  
  10.                 Queue<Point> q = new Queue<Point>((manipulator.Width * manipulator.Height));
  11.                 q.Enqueue(manipulator.Position);
  12.                 HashSet<Point> visitedNodes = new HashSet<Point>();
  13.  
  14.                 while (q.Count > 0)
  15.                 {
  16.                     Point n = q.Dequeue();
  17.                     if (n.X < 0 || n.Y < 0 || n.X >= manipulator.Width || n.Y >= manipulator.Height || visitedNodes.Contains(n))
  18.                     {
  19.                         continue;
  20.                     }
  21.  
  22.                     visitedNodes.Add(n);
  23.                     manipulator.Position = n;
  24.                     if (!manipulator.Current.Similar(targetColor, threshold))
  25.                     {
  26.                         continue;
  27.                     }
  28.  
  29.                     manipulator.Current = replaceColor;
  30.                     q.Enqueue(new Point(n.X - 1, n.Y)); //Left
  31.                     q.Enqueue(new Point(n.X + 1, n.Y)); //Right
  32.                     q.Enqueue(new Point(n.X, n.Y - 1)); //Top
  33.                     q.Enqueue(new Point(n.X, n.Y + 1)); //Bottom
  34.                 }
  35.             }
  36.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement