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?

Recommended Answers

All 4 Replies

Some optimizations for your loops could be done, take your xDivisions loop:

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);
}

Lines 3, 6 and 7 never change values inside the loop, so move them outside the loops. Also 3 and 7 are the same equations, don't compute it twice:

a.Z = z * cellSize;
b.Y = yDivisions * cellSize;
b.Z = a.Z;
for (int x = 0; x <= xDivisions; x++) {
    a.X = x * cellSize
    b.X = a.X;

    DrawLine(a, b, color);
}

Also (most of the time) multiplication is more 'expensive' in terms of CPU time than addition is, and you currently perform x multiplications in the loop. Change this to 1 multiplication and x additions:

float limit = xDivisions * cellSize;
a.Z = z * cellSize;
b.Y = yDivisions * cellSize;
b.Z = a.Z;
for (float x = 0.0f; x <= limit; x += cellSize) {
    a.X = x;
    b.X = x;

    DrawLine(a, b, color);
}

This also saves you a call to the a.X property.

commented: kudos. Some very neat improvements :) +3
commented: Nice! +7
commented: Good improvements, thank you +1

Thank you Momerath. A great improvement on what I had come up with originally.

I'm having a hard time with the 3D variation and still scratching my noodle over how to do it properly.

Vector3 a = new Vector3();
            Vector3 b = new Vector3();

            int endZ = zDivisions * cellSize;

            for (int z = 0; z <= endZ; z += cellSize)
            {
                int end = xDivisions * cellSize;
                b.Y = yDivisions * cellSize;

                for (int x = 0; x <= end; x += cellSize)
                {
                    a.X = x;
                    a.Z = z;
                    b.X = x;
                    b.Z = z;

                    DrawLine(a, b, color);
                }

                end = yDivisions * cellSize;
                a.X = 0.0f;
                b.X = xDivisions * cellSize;

                for (int y = 0; y <= end; y += cellSize)
                {
                    a.Z = z;
                    a.Y = y;
                    b.Y = y;
                    b.Z = z;

                    DrawLine(a, b, color);
                }
            }

The z values will only draw 2D grids along a third axis with the above equation and will not create a cube like grid.

Any suggestions on where to start?

After some hard thinking I've got an algorithm to draw a 3D grid in fastest way I can think of.

Vector3 a = new Vector3();
            Vector3 b = new Vector3();

            int eX = xDivisions * cellSize;
            int eY = yDivisions * cellSize;
            int eZ = zDivisions * cellSize;

            for (int z = 0; z <= eZ; z += cellSize)
            {
                // X Axis
                a.X = 0.0f;
                b.X = eX;

                for (int y = 0; y <= eY; y += cellSize)
                {
                    a.Y = y;
                    a.Z = z;

                    b.Y = y;
                    b.Z = z;

                    DrawLine(a, b, color);
                }

                // Y Axis
                a.Y = 0.0f;
                b.Y = eY;

                for (int x = 0; x <= eX; x += cellSize)
                {
                    a.X = x;               
                    a.Z = z;

                    b.X = x;
                    b.Z = z;

                    DrawLine(a, b, color);
                }
            }

            // Z axis
            for (int y = 0; y <= eY; y += cellSize)
            {
                for (int x = 0; x <= eX; x += cellSize)
                {
                    a.X = x;
                    a.Y = y;
                    a.Z = 0.0f;

                    b.X = x;
                    b.Y = y;
                    b.Z = eZ;

                    DrawLine(a, b, color);
                }
            }

If anyone reading has any advice to improve my method, or perhaps has an alternate approach, please let it be known. :)

a.Z = 0.0f can be set once before the Z axis loop. I've just changed that.

EDIT:

Or how about

Vector3 a = new Vector3();
            Vector3 b = new Vector3();

            int eX = xDivisions * cellSize;
            int eY = yDivisions * cellSize;
            int eZ = zDivisions * cellSize;

            for (int z = 0; z <= eZ; z += cellSize)
            {
                for (int y = 0; y <= eY; y += cellSize)
                {
                    // X axis
                    a.X = 0.0f;
                    a.Y = y;
                    a.Z = z;

                    b.X = eX;
                    b.Y = y;
                    b.Z = z;

                    DrawLine(a, b, color);

                    for (int x = 0; x <= eX; x += cellSize)
                    {
                        // Y axis
                        a.X = x;
                        a.Y = 0.0f;
                        a.Z = z;

                        b.X = x;
                        b.Y = eY;
                        b.Z = z;

                        DrawLine(a, b, color);

                        // Z axis
                        a.X = x;
                        a.Y = y;
                        a.Z = 0.0f;

                        b.X = x;
                        b.Y = y;
                        b.Z = eZ;

                        DrawLine(a, b, color);
                    }
                }
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.