如何旋转的整数的二维数组 [英] How to Rotate a 2D Array of Integers
问题描述
我编程俄罗斯方块的克隆和我的比赛我储存我的四格骨牌块作为块4×4阵列。我现在需要能够让我得到一个旋转的俄罗斯方块块旋转阵列的整数位置。我只是不能旋转纹理,因为我所有的碰撞检测等的设计与二维数组工作。该游戏采用XNA C#编写的。
I am programming a Tetris clone and in my game I store my tetromino blocks as 4x4 arrays of blocks. I now need to be able to rotate the integer positions in the arrays so that I get a rotated tetris block. I cannot simply rotate the texture because all my collision detection, etc has been designed to work with the 2D array. The game is written in C# using XNA.
我怎么可能通过顺时针/逆时针旋转90度我int的二维数组。
How can i possibly rotate my 2D array of ints by 90 degrees clockwise/counter clockwise.
下面是我的'L'块存储方式,例如,
Here is how my 'L' block is stored as an example.
0 1 0 0
0 1 0 0
0 1 1 0
0 0 0 0
感谢您的帮助。
推荐答案
如果他们是一个二维数组,您可以通过不同的数组访问命令复制旋转落实
If they're a 2D array, you can implement rotation by copying with different array access orders.
即,为顺时针旋转,请尝试:
i.e., for a clockwise rotation, try:
int [,] newArray = new int[4,4];
for (int i=3;i>=0;--i)
{
for (int j=0;j<4;++j)
{
newArray[j,3-i] = array[i,j];
}
}
逆时针是相似的。
Counter-clockwise is similar.
这篇关于如何旋转的整数的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!