46 lines
1.2 KiB
C#
46 lines
1.2 KiB
C#
// 数组旋转
|
||
//
|
||
|
||
namespace EventCubeMelt.Utils
|
||
{
|
||
public static class MatrixRotation<T>
|
||
{
|
||
// 二维数组(矩阵)顺时针旋转90度
|
||
public static T[,] Rotate90Clockwise(T[,] matrix)
|
||
{
|
||
int rows = matrix.GetLength(0);
|
||
int cols = matrix.GetLength(1);
|
||
|
||
T[,] result = new T[cols, rows]; // 旋转后行列互换
|
||
|
||
for (int i = 0; i < rows; i++)
|
||
{
|
||
for (int j = 0; j < cols; j++)
|
||
{
|
||
result[j, rows - 1 - i] = matrix[i, j];
|
||
}
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
// 二维数组逆时针旋转90度
|
||
public static T[,] Rotate90CounterClockwise(T[,] matrix)
|
||
{
|
||
int rows = matrix.GetLength(0);
|
||
int cols = matrix.GetLength(1);
|
||
|
||
T[,] result = new T[cols, rows];
|
||
|
||
for (int i = 0; i < rows; i++)
|
||
{
|
||
for (int j = 0; j < cols; j++)
|
||
{
|
||
result[cols - 1 - j, i] = matrix[i, j];
|
||
}
|
||
}
|
||
|
||
return result;
|
||
}
|
||
}
|
||
} |