質問

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. ^^

役に立ちましたか?

解決

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)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top