Input a 2D matrix, choose 90°/180°/270° CW or CCW rotation. See the visual index shift and get the C++ implementation.
Yes, completely free. No sign-up, no credit card, no usage limits.
Never. Everything runs 100% in your browser. Nothing is uploaded to any server.
Yes. All tools are fully responsive and work on phones, tablets, and desktops.
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
void rotateMatrix(vector<vector<int>>& mat, int rows, int cols) {
// 90° CW: transpose then reverse each row
// Step 1: transpose
for (int i = 0; i < rows; i++)
for (int j = i+1; j < cols; j++)
swap(mat[i][j], mat[j][i]);
// Step 2: reverse each row
for (auto& row : mat) reverse(row.begin(), row.end());
}