行优先顺序索引 [英] 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用完"后剩下的:
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屋!