Jump to content
  • Advertisement
Sign in to follow this  

get XYZ values from one-dimensional array

This topic is 2541 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

I have a one dimensional array which represents size in 3D space, meaning like:

data[size * sizeY * size] ,where size is same for width/depth and sizeY is for height

When I want to change value in data[index], I do: index = (x * size) + (y * size * size) + z
and that works correctly

But I'm trying to get x, y and z value off the data[] with index value somewhere between 0 and (size*sizeY*size) but I'm not sure how I can get them.
This is what I've got so far for example:

x = 10
y = 20
z = 30
index = (x * size) + (y * size * size) + z
data[index] = some value

x = ? = 10
y = index / size / size = 20
z = ? = 30

How do I get the x and z from index?

Share this post

Link to post
Share on other sites
y = index / size / size
index -= y*size*size
x = index / size
index -= x*size
z = index

would work. as would:

z = index%size
index /= size
x = index%size
index /= size
y = index

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!