public static T[,] Rotate <T>(this T[,] matrix, MatrixRotationType rotationType = MatrixRotationType.Clockwise) { T[,] rotation = null; switch (rotationType) { case MatrixRotationType.Clockwise: { rotation = matrix.RotateClockwise(); break; } case MatrixRotationType.Clockwise180: { rotation = matrix.RotateClockwise180(); break; } case MatrixRotationType.CounterClockwise: { rotation = matrix.RotateCounterClockwise(); break; } } return(rotation); }
public static bool RotateInline <T>(this T[,] matrix, MatrixRotationType rotationType = MatrixRotationType.Clockwise) { if (matrix == null || matrix.Length < 1) { return(false); } int rows = matrix.GetLength(0); int columns = matrix.GetLength(1); int round = rows / 2 + 1; if (rows != columns) { return(false); } if (round == 1) { return(true); } for (int x = 0; x <= round; x++) { int range = round - x; for (int y = x; y <= range; y++) { int r = rows - x - 1; // opposite in row int c = columns - y - 1; // opposite in column var t = matrix[x, y]; // keep start point switch (rotationType) { case MatrixRotationType.Clockwise: { matrix[x, y] = matrix[c, x]; matrix[c, x] = matrix[r, c]; matrix[r, c] = matrix[y, r]; matrix[y, r] = t; break; } case MatrixRotationType.Clockwise180: { matrix[x, y] = matrix[r, c]; // switch matrix[r, c] = t; var s = matrix[c, x]; // keep switching point matrix[c, x] = matrix[y, r]; matrix[y, r] = s; break; } case MatrixRotationType.CounterClockwise: { matrix[x, y] = matrix[y, r]; matrix[y, r] = matrix[r, c]; matrix[r, c] = matrix[c, x]; matrix[c, x] = t; break; } } } } return(true); }