Row-major order indices

前端 未结 1 1173
忘了有多久
忘了有多久 2020-12-06 07:01

I\'m currently working on project of where 2d terrain maps are saved into a one-dimensional array. Each block in the map is indexed by xy coordinates. So, to save the map in

相关标签:
1条回答
  • 2020-12-06 07:26

    To calculate indices you should be using something like this:

    index = X + Y * Width;
    

    So, to reverse this you can take advantage of integer division truncation to get Y, and then X is just what's left over after what Y "used up":

    Y = (int)(index / Width)
    X = index - (Y * Width)
    
    0 讨论(0)
提交回复
热议问题