Input a 2D matrix, choose 90°/180°/270° CW or CCW rotation. See the visual index shift and get the C++ implementation.
Rotating a matrix 90 degrees swaps its dimensions, and that single fact breaks more implementations than the index arithmetic does. A 3 by 5 grid becomes 5 by 3, so the in-place transpose-and-reverse trick every tutorial teaches is only valid on a square. The Matrix Rotation Simulator handles rectangles correctly and emits C++ that does the same.
Cells are coloured by value rather than by position, which is the whole point of the visualisation. Track one colour from the input grid to the output and the index mapping stops being an abstraction you have to trust.
Six controls cover it: rows, columns, 90, 180 or 270 degrees, and clockwise or counter-clockwise. A custom-matrix box accepts your own values, validating that every row holds the same number of entries before it will use them.
The default grid is filled sequentially, so row one reads 1 2 3 and row three reads 7 8 9. Rotate 90 degrees clockwise and the first row becomes 7 4 1: the bottom-left corner has travelled to the top-left. Switch to counter-clockwise and the first row is 3 6 9 instead. The mapping written out is that a clockwise rotation sends the cell at row r and column c to row c and column rows minus 1 minus r.
Original (3×3)
After 90° CW (3×3)
Index Mapping: original[r][c] → rotated[r'][c']
| Value | Original [r][c] | Rotated [r'][c'] |
|---|---|---|
| 1 | [0][0] | [0][2] |
| 2 | [0][1] | [1][2] |
| 3 | [0][2] | [2][2] |
| 4 | [1][0] | [0][1] |
| 5 | [1][1] | [1][1] |
| 6 | [1][2] | [2][1] |
| 7 | [2][0] | [0][0] |
| 8 | [2][1] | [1][0] |
| 9 | [2][2] | [2][0] |
C++ Implementation
// Rotate 90° Clockwise
// Works for any rows x cols (not just square matrices).
vector<vector<int>> rotateMatrix(vector<vector<int>>& mat, int rows, int cols) {
// 90° CW: rotated[c][rows-1-r] = mat[r][c]
vector<vector<int>> rotated(cols, vector<int>(rows));
for (int r = 0; r < rows; r++)
for (int c = 0; c < cols; c++)
rotated[c][rows-1-r] = mat[r][c];
return rotated;
}