Вот вам пример поворота двумерного массива на 90 градусов по и против часовой стрелке
static void Main(string[] args)
{
int[,] array = new int[6, 5] {
{ 1,2,3,4 ,5},
{ 5,6,7,8,6 },
{ 9,0,1,2 ,7},
{ 3,4,5,6,8 },
{ 3,4,5,6,8 },
{ 3,4,5,6,8 }
};
for (int i = 0; i < array.GetLength(0); i++)
{
for (int k= 0; k < array.GetLength(1); k++)
{
Console.Write(array[i,k]+ " ");
}
Console.WriteLine();
}
Console.WriteLine("Rotate Matrix Clockwise");
int[,] rotated = RotateMatrixClockwise(array);
for (int i = 0; i < rotated.GetLength(0); i++)
{
for (int k = 0; k < rotated.GetLength(1); k++)
{
Console.Write(rotated[i, k]+ " ");
}
Console.WriteLine();
}
Console.WriteLine("Rotate Matrix AntiClockwise");
int[,] rotated1 = RotateMatrixAntiClockwise(array);
for (int i = 0; i < rotated1.GetLength(0); i++)
{
for (int k = 0; k < rotated1.GetLength(1); k++)
{
Console.Write(rotated1[i, k]+ " ");
}
Console.WriteLine();
}
}
public static int[,] RotateMatrixClockwise(int[,] oldMatrix)
{
int[,] newMatrix = new int[oldMatrix.GetLength(1), oldMatrix.GetLength(0)];
int newColumn, newRow = 0;
for (int oldColumn = oldMatrix.GetLength(1) - 1; oldColumn >= 0; oldColumn--)
{
newColumn = 0;
for (int oldRow = 0; oldRow < oldMatrix.GetLength(0); oldRow++)
{
newMatrix[newRow, newColumn] = oldMatrix[oldRow, oldColumn];
newColumn++;
}
newRow++;
}
return newMatrix;
}
public static int[,] RotateMatrixAntiClockwise(int[,] oldMatrix)
{
int[,] newMatrix = new int[oldMatrix.GetLength(1), oldMatrix.GetLength(0)];
int newColumn, newRow = 0;
for (int oldColumn = 0; oldColumn < oldMatrix.GetLength(0) - 1; oldColumn++)
{
newColumn = 0;
for (int oldRow = oldMatrix.GetLength(1) ; oldRow >=0 ; oldRow--)
{
newMatrix[newRow, newColumn] = oldMatrix[oldRow, oldColumn];
newColumn++;
}
newRow++;
}
return newMatrix;
}
newMatrix
не видно. Покажите как создаетеoldMatrix