I have a 1D array and I wish to access it like a 3D array.

If I know the values of the 3 dimensions

Width (x)
Height (y)
Depth (z)

then I can create the 1D array using array[width * height * depth].

How can I now access the indices of the 1D array correctly if I have 3D indices (x, y, z)?

I thought it would be something along the lines of

index1D = x * width + y * height + z;

but that doesn't work. I'm still thinking in 2D though.

Recommended Answers

All 8 Replies

x*width*height + y*height + z

x*width*height + y*height + z

Thank you, it would seem I'm still not grasping this correctly though.

If I imagine each index as a cube which is located at the x, y, z index position.

How would I check the value to the "right" of one of the cubes?

I have an 1D array of 4x3x3

int width = 4;
            int height = 3;
            int depth = 3;

            int[] array = new int[4 * 3 * 3];

I thought that the index to the "right" would be

int rightOfIndex = (x + 1) * width * height + y * width + z;

but if I have an original index of

int x = 2;
            int y = 0;
            int z = 0;

then rightOfIndex will be outside of the array.

How then can I correctly locate neighbouring indices?

Because of the way you created your equation, your 'z' axis is what we'd traditionally refer to as the 'x' axis. I believe that is causing your problem.

As for checking to the right, does the check wrap around the cube? For example, if I'm at the far right of the cube, does 'right' have any meaning?

Because of the way you created your equation, your 'z' axis is what we'd traditionally refer to as the 'x' axis. I believe that is causing your problem.

Could you please explain? Is it just a simple case of switching z and x around?

I would use the following if statement to prevent going beyond the array to the "right"

if (x + 1 < width)

What is it you are trying to acheive exactly? Why do you want to store 3D coordinates in a 1D array? Each item in a 1D array will store a singel value.
You could use a string to concatenate the coordinates and store them that way like:

string[] array = new string[3];
array[0] = "0,0,0";
array[1] = "1,0,0";
array[2] = "3,3,3";

I'd suggest you look at using a 3dimensional array, but it depends on your reasoning for needing it in a 1D array :/

I'm using a 1D array as it appears to work faster when saving/loading data. This is because only one loop is required for those processes.

If my width was 4 then would I access the array using

int rightOfIndex = x * width * height + y * width + (z + 1);

How does (x, y, z) as (z, y, x) change if the array is one dimensional?

int rightOfIndex = x * width * height + y * width + (z + 1);

How does (x, y, z) as (z, y, x) change if the array is one dimensional?

Two questions:
1) Why are you adding 1 to the z dimension? To get the one to the 'right'? This will result in wrapping and an error when you are referencing the last element of the array.
2) Consider an apartment building. The building has doors, hallways and floors. x is which door, y is which hallway and z is which floor. With the equation above, you've reversed the meaning of x and z (x is which floor, z is which door). This is really easy to show with pictures, btw :)

I've solved the problem now I finally understand where I was going wrong.

Thanks for taking the time to explain the nature of the arrays.

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.