I've created an algorithm that draws a 2D grid in the most efficient way I could think of. Suggestions to improve it are encouraged though :)
public void DrawWireGrid(int xDivisions, int yDivisions, int cellSize, Color color)
{
Vector3 a = new Vector3();
Vector3 b = new Vector3();
for (int x = 0; x <= xDivisions; ++x)
{
a.X = x * cellSize;
b.X = a.X;
b.Y = yDivisions * cellSize;
DrawLine(a, b, color);
}
a.X = b.X = a.Y = b.Y = 0.0f;
for (int y = 0; y <= yDivisions; ++y)
{
a.Y = y * cellSize;
b.X = xDivisions * cellSize;
b.Y = a.Y;
DrawLine(a, b, color);
}
}
Using my original algorithm I've altered it to draw a 3 dimensional grid.
public void DrawWireGrid(int xDivisions, int yDivisions, int zDivisions, int cellSize, Color color)
{
Vector3 a = new Vector3();
Vector3 b = new Vector3();
for (int z = 0; z <= zDivisions; ++z)
{
for (int x = 0; x <= xDivisions; ++x)
{
a.X = x * cellSize;
a.Z = z * cellSize;
b.X = a.X;
b.Y = yDivisions * cellSize;
b.Z = z * cellSize;
DrawLine(a, b, color);
}
a.X = b.X = a.Y = b.Y = a.Z = b.Z = 0.0f;
for (int y = 0; y <= yDivisions; ++y)
{
a.Y = y * cellSize;
a.Z = z * cellSize;
b.X = xDivisions * cellSize;
b.Y = a.Y;
b.Z = z * cellSize;
DrawLine(a, b, color);
}
}
}
If I comment out either the yDivisions for loop, or the xDivisions for loop, the grid lines will draw correctly for that dimension.
When both loops inside the zDivisions loop are run, the grid refuses to draw the xDivisions correctly and no longer moves them out by the z value.
Can anyone spot where this algorithm is going wrong?