using System.Collections.Generic;
private const int capacity = 625;
public static void Main()
int[] range = new int[capacity];
int[] range1 = new int[capacity];
int[] range2 = new int[capacity];
int width = (int)Math.Sqrt(capacity), height = (int)Math.Sqrt(capacity);
range = DrawCircle(range, 0, 0, width, height, 5);
Array.Copy(range, range1, capacity);
Array.Copy(range, range2, capacity);
Print(range, (int)Math.Sqrt(capacity));
for (int j = 0; j < capacity; ++j)
int x = j % width, y = j / width;
F.FloodFillHash(range1, x, y, width, height, 1, 2, out i);
Print(range1, (int)Math.Sqrt(capacity));
Console.WriteLine("Hash iterations: {0}", i);
private static int[] DrawCircle(int[] source, int x, int y, int width, int height, int r, bool inCenter = true)
for (int u = 0; u < width; ++u)
for (int v = 0; v < height; ++v)
if ((x - u) * (x - u) + (y - v) * (y - v) < rSquared)
source[F.Pn(u, v, width)] = 1;
Console.WriteLine("Points in circle: {0}", i);
private static void Print<T>(T[] objs, int splitEvery = 10)
public static int P(int x, int y, int w, int h)
return x + ((h - y - 1) * w);
public static int Pn(int x, int y, int w)
public static void FloodFill(int[] source, int x, int y, int width, int height, int targetColor, int replacementColor, out int i)
Stack<int> pixels = new Stack<int>();
targetColor = source[P(x, y, width, height)];
pixels.Push(Pn(x, y, width));
int _i = pixels.Pop(), _x = _i % width, _y = _i / width;
if (source[Pn(_x, _y, width)] == targetColor)
source[Pn(_x, _y, width)] = replacementColor;
pixels.Push(Pn(_x - 1, _y, width));
pixels.Push(Pn(_x + 1, _y, width));
pixels.Push(Pn(_x, _y - 1, width));
pixels.Push(Pn(_x, _y + 1, width));
public static void FloodFillHash<T>(this T[] source, int x, int y, int width, int height, T target, T replacement)
FloodFillHash(source, x, y, width, height, target, replacement, out i);
public static void FloodFillHash<T>(this T[] source, int x, int y, int width, int height, T target, T replacement, out int i)
HashSet<int> queue = new HashSet<int>();
queue.Add(Pn(x, y, width));
if (source[_i].Equals(target))
source[_i] = replacement;
for (int offsetX = -1; offsetX < 2; offsetX++)
for (int offsetY = -1; offsetY < 2; offsetY++)
if (offsetX == 0 && offsetY == 0 || offsetX == offsetY || offsetX == -offsetY || -offsetX == offsetY)
int targetIndex = Pn(_x + offsetX, _y + offsetY, width);
int _tx = targetIndex % width,
_ty = targetIndex / width;
if (_tx < 0 || _ty < 0 || _tx >= width || _ty >= height)
if (!queue.Contains(targetIndex) && source[targetIndex].Equals(target))