UtilityToolsLab

© 2026 UtilityToolsLab. Built and maintained by the UtilityToolsLab Team.

Free eBooks·About·Changelog·Privacy Policy·Terms of Service·Report a bug
HomeCompetitive ProgrammingMatrix Rotation

Related Tools

Code FormatterMatrix GeneratorComplexity CalcBit VisualizerBitmask PlannerPrime FactorsMEX CalcInterval MergerPBDS GeneratorSegment TreeGraph VisualizerStress TesterModulo CalcConvex HullPath FinderOffline JudgeBig-O AnalyzerCombinatoricsDP Table BuilderExtended GCDSieve VisualizerBinary SearchSorting VisualizerSparse Table RMQUnion-Find DSU

Matrix Rotation Simulator

Input a 2D matrix, choose 90°/180°/270° CW or CCW rotation. See the visual index shift and get the C++ implementation.

You Might Also Like

All Competitive Programming

Matrix Generator

Generate grid/matrix inputs for competitive programming. Random, zeros, identity, or sequential fill. Outputs in multiple formats.

Path Finder

Paint walls on an N×M grid, set start and end cells, then run BFS to visualize the shortest path. Copy the grid as a C++ 2D vector instantly.

Code Formatter

Format and beautify C++, Java, and Python code with consistent indentation. 100% client-side — no code leaves your browser.

Complexity Calc

Estimate Big-O operations and runtime for any N. Interactive reference table for all complexity classes from O(1) to O(N!).

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.

A Real Example: Rotating a 3 by 3

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.

The Algorithm Behind the Index Mapping

  • A 270 degree clockwise rotation is the same operation as 90 counter-clockwise, and the tool routes them to the same function rather than rotating three times.
  • 180 degrees is the cheap one. Reverse each row and reverse the order of the rows, and the dimensions are unchanged, so it is the only rotation that can be done in place on a rectangle.
  • The generated C++ allocates a fresh result matrix sized cols by rows. That is deliberate: the popular in-place trick reads out of bounds on any non-square input.
  • Sequential fill numbers cells row-major from 1, so on a 3 by 3 the value 9 is always the bottom-right of the original and its journey is the easiest to follow.
  • Custom input is split on spaces or commas per line. Rows of unequal length are rejected and the tool falls back to the generated grid rather than rotating a ragged array.

Original (3×3)

1
2
3
4
5
6
7
8
9

After 90° CW (3×3)

7
4
1
8
5
2
9
6
3

Index Mapping: original[r][c] → rotated[r'][c']

ValueOriginal [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;
}