public static void Main()
var matrix = new int[,]{ {1,2,3,4},{5,6,7,8}, {9,10,11,12}};
var res = MatrixRotation.ClockwiseRotate90Degree(matrix);
public class MatrixRotation
public static T[,] HorizontalFlip<T>(T[,] source)
for (var i = 0; i < source.GetLength(1); i++)
int t = 0, b = source.GetLength(0) - 1;
source[t, i] = source[b, i];
public static T[,] VerticalFlip<T>(T[,] source)
for (var i = 0; i < source.GetLength(0); i++)
int l = 0, r = source.GetLength(1) - 1;
source[i, l] = source[i, r];
public static T[,] SquareDiagnoalFlip<T>(T[,] source)
int rows = source.GetLength(0);
int cols = source.GetLength(1);
if (rows != cols) throw new InvalidOperationException("Cannot use this algorithm to flip rectangle, must be square!");
for (var i = 0; i < rows; i++)
for (var j = 0; j <= i; j++)
source[i, j] = source[j, i];
public static T[,] DiagnoalFlip<T>(T[,] source)
var res = new T[source.GetLength(1), source.GetLength(0)];
for (var i = 0; i < source.GetLength(0); i++)
for (var j = 0; j < source.GetLength(1); j++)
res[j, i] = source[i, j];
public static T[,] AntiDiagnoalFlip<T>(T[,] source)
int rows = source.GetLength(0);
int cols = source.GetLength(1);
var res = new T[cols, rows];
for (var i = 0; i < rows; i++)
for (var j = 0; j < cols; j++)
res[cols - j, rows - i] = source[i, j];
public static T[,] ClockwiseRotate90Degree<T>(T[,] source)
var res = HorizontalFlip(source);
public static T[,] CounterClockwiseRotate90Degree<T>(T[,] source)
var res = VerticalFlip(source);
res = DiagnoalFlip(source);