public class MatrixRotation
static (int, int) RotateCoordinateClockwise(int x, int y, int sizeX, int sizeY)
var (newSizeX, newSizeY) = RotateSize(sizeX, sizeY, 1);
return (y, newSizeX - 1 - x);
public static (int, int) RotateSize(int sizeX, int sizeY, int rotations)
rotations = rotations % 4;
if (rotations % 2 == 0) return (sizeX, sizeY);
public static (int, int) Get2DPosition(int index, int sizeX)
public static void Main()
var (newSizeX, newSizeY) = RotateSize(sizeX, sizeY, rotations);
Console.WriteLine($"Rotations: {rotations}");
Console.WriteLine($"Old size: [{sizeX}, {sizeY}]");
Console.WriteLine($"New size: [{newSizeX}, {newSizeY}]");
Console.WriteLine("\nMapping of coordinates after rotation:");
for (int y = 0; y < newSizeY; ++y)
for (int x = 0; x < newSizeX; ++x)
var (newX, newY) = RotateCoordinateClockwise(x, y, sizeX, sizeY);
var index = newY * newSizeX + newX;
Console.WriteLine($"Element: {flatMatrix[index]}, Old Position: [{x},{y}] -> New Position: [{newX},{newY}]");