I am hoping to streamline this bit of code better. This snippet does not cause my any slow downs in relation to other parts of the algorithm(?). I would just like guidance on making it better.
Some variables not given by code:
regionDimensions = whatever you want. (256)
hexagonArray[,] = new Hexagon[regionDimensions, regionDimensions];
The Struct:
public struct Hexagon
{
private float height;
private bool[] hasSides;
public Hexagon(float height)
{
this.height = height;
hasSides = new bool[] { true, true, true, true, true, true };
}
public float Height
{
get { return height; }
}
public bool[] HasSides
{
get { return hasSides; }
set { hasSides = value; }
}
public bool this[int i]
{
get { return hasSides[i]; }
set { hasSides[i] = value; }
}
}
The Iterator:
// Evaluate heights of hexagons and cull sides
for (var x = 0; x < regionDimensions; x++)
for (var y = 0; y < regionDimensions; y++)
{
var neighborHexes = new Dictionary<Hexagon, int>();
// If it's not in bounds it doesn't need its side no matter what.
if (y % 2 == 0)
{
if (CheckBounds(x, y + 1)) // 0
neighborHexes.Add(hexagonArray[x, y + 1], 0);
else
hexagonArray[x, y].HasSides[0] = false;
if (CheckBounds(x + 1, y)) // 1
neighborHexes.Add(hexagonArray[x + 1, y], 1);
else
hexagonArray[x, y].HasSides[1] = false;
if (CheckBounds(x, y - 1)) // 2
neighborHexes.Add(hexagonArray[x, y - 1], 2);
else
hexagonArray[x, y].HasSides[2] = false;
if (CheckBounds(x - 1, y - 1)) // 3
neighborHexes.Add(hexagonArray[x - 1, y - 1], 3);
else
hexagonArray[x, y].HasSides[3] = false;
if (CheckBounds(x - 1, y)) // 4
neighborHexes.Add(hexagonArray[x - 1, y], 4);
else
hexagonArray[x, y].HasSides[4] = false;
if (CheckBounds(x - 1, y + 1)) // 5
neighborHexes.Add(hexagonArray[x - 1, y + 1], 5);
else
hexagonArray[x, y].HasSides[5] = false;
}
else
{
if (CheckBounds(x + 1, y + 1)) // 0
neighborHexes.Add(hexagonArray[x + 1, y + 1], 0);
else
hexagonArray[x, y].HasSides[0] = false;
if (CheckBounds(x + 1, y)) // 1
neighborHexes.Add(hexagonArray[x + 1, y], 1);
else
hexagonArray[x, y].HasSides[1] = false;
if (CheckBounds(x + 1, y - 1)) // 2
neighborHexes.Add(hexagonArray[x + 1, y - 1], 2);
else
hexagonArray[x, y].HasSides[2] = false;
if (CheckBounds(x, y - 1)) // 3
neighborHexes.Add(hexagonArray[x, y - 1], 3);
else
hexagonArray[x, y].HasSides[3] = false;
if (CheckBounds(x - 1, y)) // 4
neighborHexes.Add(hexagonArray[x - 1, y], 4);
else
hexagonArray[x, y].HasSides[4] = false;
if (CheckBounds(x, y + 1)) // 5
neighborHexes.Add(hexagonArray[x, y + 1], 5);
else
hexagonArray[x, y].HasSides[5] = false;
}
EvaluateNeighbors(neighborHexes, ref hexagonArray[x, y]);
}
Check Bounds:
private bool CheckBounds(int x, int y)
{
if ((x >= 0) && (x < regionDimensions) &&
(y >= 0) && (y < regionDimensions))
return true;
else
return false;
}
Evaluate Neighbors:
private void EvaluateNeighbors(Dictionary<Hexagon, int> neighbors, ref Hexagon currentHexagon)
{
foreach (var neighbor in neighbors)
{
if (currentHexagon.Height < neighbor.Key.Height)
currentHexagon.HasSides[neighbor.Value] = false;
else if (MathExtensions.NearlyEqual(currentHexagon.Height, neighbor.Key.Height))
currentHexagon.HasSides[neighbor.Value] = false;
}
}
Fix To Struct: Object is built once and is no longer mutable.
[Serializable]
public struct Hexagon
{
private readonly float height;
private readonly bool[] hasSides;
public Hexagon(float height, bool[] sides)
: this()
{
this.height = height;
this.hasSides = (bool[])sides.Clone();
}
public float Height
{
get { return height; }
}
public bool[] HasSides
{
get { return hasSides; }
}
public bool this[int i]
{
get { return hasSides[i]; }
}
}