Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

3D array. How do I find the outermost indices?

This topic is 3084 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I create a 3D array of variable width, height and depth:
        public void CreateNewLevel(int width, int height, int depth)
        {
            array3D = new int[width, height, depth];

            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                {
                    for (int z = 0; z < depth; ++z)
                    {
                        
                    }
                }
            }
        }




How can I find the outermost indices? Would it be something along the line of checking if x - 1 < 0 || y - 1 < 0 || z - 1 < 0 x + 1 == width || y + 1 == height || z + 1 == depth? EDIT: Is this correct?
        public void CreateNewLevel(int width, int height, int depth)
        {
            map3D = new int[width, height, depth];

            for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                {
                    for (int z = 0; z < depth; ++z)
                    {
                        bool a = x == 0 || y == 0 || z == 0;
                        bool b = x + 1 == width || y + 1 == height || z + 1 == depth;
                        map3D[x, y, z] = a || b ? 1 : 0;
                    }
                }
            }
        }

or can anyone see any cases where it wouldn't work?

Share this post


Link to post
Share on other sites
Advertisement
The extremities of the level are in the slices where x, y, or z is 0 or (size - 1). Is that what you want? The corners are in the intersections of these slices (logical AND).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!