Row-major order indices

让人想犯罪 __ 提交于 2019-11-26 21:18:24

问题


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 into a one-dimensional array, I used the row-major order method (http://en.wikipedia.org/wiki/Row-major_order) to convert the xy coordinates into a single index value (Which let me put the block into an array).

Now, my problem is how do I convert it back? I have a unique number which I have to convert back into xy coordinates. Any help would be appreciated. ^^


回答1:


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)


来源:https://stackoverflow.com/questions/5991837/row-major-order-indices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!