C# 如何旋转二维整数数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/646468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Rotate a 2D Array of Integers
提问by Brock Woolf
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.
我正在编写一个俄罗斯方块克隆,在我的游戏中,我将我的 Tetromino 块存储为 4x4 块数组。我现在需要能够旋转数组中的整数位置,以便我得到一个旋转的俄罗斯方块。我不能简单地旋转纹理,因为我所有的碰撞检测等都被设计为与 2D 数组一起使用。该游戏是使用 XNA 用 C# 编写的。
How can i possibly rotate my 2D array of ints by 90 degrees clockwise/counter clockwise.
我怎么可能将我的二维整数数组顺时针/逆时针旋转 90 度。
Here is how my 'L' block is stored as an example.
以下是我的“L”块的存储方式作为示例。
0 1 0 0
0 1 0 0
0 1 1 0
0 0 0 0
Thanks for your help.
谢谢你的帮助。
采纳答案by Reed Copsey
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.
逆时针也是类似的。
回答by Toon Krijthe
If you want to rotate a 4 x 4 block, you just move the positions:
如果要旋转 4 x 4 块,只需移动位置:
A B C A
C D D B
B D D C
A C B A
Each A moves to the next A, and the same for B, C and D.
每个 A 移动到下一个 A,B、C 和 D 也是如此。
/-----\
| |
| V
A B C A
/->C D>D B--\
| B D D C |
| A C B A |
| | ^ |
| | | |
\----/ \----/
回答by Ryan Emerle
In classic tetris there are very few permutations of objects. I would simply have a constant array for each "tetromino," at each of the 4 positions, and simple logic to choose the appropriate one based on input.
在经典的俄罗斯方块中,对象的排列非常少。我只需为每个“tetromino”设置一个常量数组,在 4 个位置中的每个位置,并根据输入选择合适的简单逻辑。
Why waste CPU cycles trying to rotate it?
为什么要浪费 CPU 周期来尝试旋转它?
回答by Jon
Don't rotate the pieces with code. Just store an array of the different piece orientations and cycle through them when the piece is rotated. There's no need to dynamically rotate them in a Tetris game.
不要用代码旋转这些部分。只需存储一系列不同的工件方向,并在工件旋转时循环遍历它们。无需在俄罗斯方块游戏中动态旋转它们。
As the problem domain is Tetris, you will find that a rotation algorithm causes undesirable effects, such as the long thin Tetronimo not alternating between two positions (as it does in the real thing).
由于问题域是俄罗斯方块,您会发现旋转算法会导致不良影响,例如细长的 Tetronimo 不会在两个位置之间交替(就像在真实事物中那样)。
回答by Eugene Yokota
I'd store (x, y) coordinates of the "cells" and use rotation matrix to rotate them. See Drawing a Rotated Rectanglefor example. You probably have to round the result to closest 0.5 increment.
我会存储“单元格”的 (x, y) 坐标并使用旋转矩阵来旋转它们。例如,参见绘制旋转矩形。您可能必须将结果四舍五入到最接近的 0.5 增量。
回答by Sunny Sun
js code for both clockwise and counter-clockwise:
顺时针和逆时针的js代码:
function arrRotation90(arr, clockwise) {
var arr_rotated = [];
for (var i = 0; i < arr[0].length; i++) {
arr_rotated[i] = [];
}
if (clockwise) {
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
arr_rotated[arr[i].length-1-j][i] = arr[i][j];
}
}
} else {
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
arr_rotated[j][arr.length - 1 - i] = arr[i][j];
}
}
}
return arr_rotated;
}
回答by Baqer Naqvi
int[,] source = { { 1,2,3 }, {4,5,6 }, { 7,8,9} };
int[,] result = new int[3,3];
var rows = source.GetLength(0);
var cols = source.GetLength(1);
for (var r=0; r<rows; r++)
{
for (var c = 0; c < cols; c++)
{
result[r, c] = source[rows - 1 - c, r];
}
}