行次序索引 [英] Row-major order indices
问题描述
我正在处理将2D地形图保存到一维数组中的项目。地图中的每个块都用xy坐标进行索引。所以,为了将地图保存为一维数组,我使用行主要的顺序方法( http:// en。 wikipedia.org/wiki/Row-major_order )将xy坐标转换为单个索引值(这让我将块放入数组)。
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).
现在,我的问题是如何将其转换回来?我有一个唯一的数字,我必须转换回xy坐标。
任何帮助将不胜感激。 ^^
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. ^^
推荐答案
要计算索引,你应该使用这样的东西:
To calculate indices you should be using something like this:
index = X + Y * Width;
所以,为了扭转这个,你可以利用整数除法来获取Y,然后X是Y =(int)(index / 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)
这篇关于行次序索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!