Pattern visualizer
Rotate Image
Rotating clockwise looks like it needs a second grid to avoid overwriting cells mid-rotation, but it decomposes into two simple in-place passes: transposing (flip across the main diagonal) turns rows into columns, and reversing each row afterward flips them left-right — together that's exactly a 90-degree clockwise turn, using zero extra memory. Animated on: matrix = [[1,2,3],[4,5,6],[7,8,9]] — rotate 90 degrees clockwise in place (shown flattened row-major)..
Transpose, then reverse each row — no extra grid needed
3x3 matrix, flattened row-major (idx = row*3+col). Rotate 90 deg clockwise with no extra grid: transpose, then reverse each row.
1FUNCTION rotate(matrix):2 n = the number of rows in matrix3 FOR i from 0 to n, j from i+1 to n:4 swap matrix[i][j] with matrix[j][i] (transpose)5 FOR each row:6 reverse the row7 RETURN matrix
← / → step · space play · Home restart