CSharp · Console
using System.Collections.Generic;
using System;
using System.Linq;
public class Program
{
private const int capacity = 625;
public static void Main()
{
int[] range = new int[capacity]; //Enumerable.Range(0, 100).ToArray();
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);
Console.WriteLine();
Console.WriteLine();
Print(range, (int)Math.Sqrt(capacity));
Console.WriteLine();
Console.WriteLine();
int i = 0;
for (int j = 0; j < capacity; ++j)
{
int num = range1[j];
if (num == 1)
{
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();
Console.WriteLine();
Console.WriteLine("Hash iterations: {0}", i);
Console.WriteLine();
Console.WriteLine();
// Memory exceded
/*int k = 0;
for (int j = 0; j < capacity; ++j)
{
int num = range2[j];
if (num == 1)
{
int x = j % width, y = j / width;
FloodFill(range1, x, y, width, height, 1, 2, out k);
}
}
Print(range2, (int)Math.Sqrt(capacity));
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Stack iterations: {0}", k);*/
// Only for test in Flood Fill was working
// Print(range.Select((x, ii) => range[ii] + range1[ii]).ToArray(), (int)Math.Sqrt(capacity));
//Console.WriteLine();
//Console.WriteLine();
}
private static int[] DrawCircle(int[] source, int x, int y, int width, int height, int r, bool inCenter = true)
{
int i = 0;
int rSquared = r * r;
if (inCenter)
{
x += width / 2;
y += height / 2;
}
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;
++i;
}
}
}
Console.WriteLine("Points in circle: {0}", i);
return source;
}
private static void Print<T>(T[] objs, int splitEvery = 10)
{
int i = 1;
foreach (T item in objs)
{
Console.Write(item);
if (i % splitEvery == 0)
Console.WriteLine();
++i;
}
}
}
public static class F
{
public static int P(int x, int y, int w, int h)
{
// (h - y - 1)
// y
return x + ((h - y - 1) * w);
}
public static int Pn(int x, int y, int w)
{
return x + (y * w);
}
public static void FloodFill(int[] source, int x, int y, int width, int height, int targetColor, int replacementColor, out int i)
{
i = 0;
Stack<int> pixels = new Stack<int>();
targetColor = source[P(x, y, width, height)];
pixels.Push(Pn(x, y, width));
while (pixels.Count > 0)
{
int _i = pixels.Pop(), _x = _i % width, _y = _i / width;
//if (a.x >= width || a.x < 0 || a.y >= height || a.y < 0)
// return;
if (source[Pn(_x, _y, width)] == targetColor)
{
source[Pn(_x, _y, width)] = replacementColor;
if (_x > 0)
pixels.Push(Pn(_x - 1, _y, width));
if (_x < width - 1)
pixels.Push(Pn(_x + 1, _y, width));
if (_y > 0)
pixels.Push(Pn(_x, _y - 1, width));
if (_y < height - 1)
pixels.Push(Pn(_x, _y + 1, width));
}
++i;
}
}
/// <summary>
/// Floods the fill.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="target">The target to replace.</param>
/// <param name="replacement">The replacement.</param>
public static void FloodFillHash<T>(this T[] source, int x, int y, int width, int height, T target, T replacement)
{
int i = 0;
FloodFillHash(source, x, y, width, height, target, replacement, out i);
}
/// <summary>
/// Floods the array following Flood Fill algorithm
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">The source.</param>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <param name="target">The target to replace.</param>
/// <param name="replacement">The replacement.</param>
/// <param name="i">The iterations made (if you want to debug).</param>
public static void FloodFillHash<T>(this T[] source, int x, int y, int width, int height, T target, T replacement, out int i)
{
i = 0;
// Queue of pixels to process. :silbar:
HashSet<int> queue = new HashSet<int>();
queue.Add(Pn(x, y, width));
while (queue.Count > 0)
{
int _i = queue.First(),
_x = _i % width,
_y = _i / width;
queue.Remove(_i);
if (source[_i].Equals(target))
source[_i] = replacement;
for (int offsetX = -1; offsetX < 2; offsetX++)
for (int offsetY = -1; offsetY < 2; offsetY++)
{
// do not check origin or diagonal neighbours
if (offsetX == 0 && offsetY == 0 || offsetX == offsetY || offsetX == -offsetY || -offsetX == offsetY)
continue;
int targetIndex = Pn(_x + offsetX, _y + offsetY, width);
int _tx = targetIndex % width,
_ty = targetIndex / width;
// skip out of bounds point
if (_tx < 0 || _ty < 0 || _tx >= width || _ty >= height)
continue;
if (!queue.Contains(targetIndex) && source[targetIndex].Equals(target))
{
queue.Add(targetIndex);
++i;
}
}
}
}
}