Turn an image by 90 degree
Given an image, how will you turn it by 90 degrees? A vague question. Minimize the browser and try your solution before going further. An image can be treated as 2D matrix which can be stored in a buffer. We are provided with matrix dimensions and it’s base address. How can we turn it? For example see the below picture,
* * * ^ * * * * * * | * * * * * * | * * * * * * | * * *
After rotating right, it appears (observe arrow direction)
* * * * * * * * * * * * — — — > * * * * * * * * * * * *
The idea is simple. Transform each row of source matrix into required column of final image. We will use an auxiliary buffer to transform the image. From the above picture, we can observe that
first row of source ------> last column of destination second row of source ------> last but-one column of destination so ... on last row of source ------> first column of destination
In pictorial form, we can represent the above transformations of an (m x n) matrix into (n x m) matrix,

If you have not attempted, atleast try your pseudo code now. It will be easy to write our pseudo code. In C/C++ we will usually traverse matrix on row major order. Each row is transformed into different column of final image. We need to construct columns of final image. See the following algorithm (transformation)
for (r = 0; r < m; r++) { for (c = 0; c < n; c++) { // Hint: Map each source element indices into // indices of destination matrix element. dest_buffer [ c ] [ m - r - 1 ] = source_buffer [ r ] [ c ]; } }
Note that there are various ways to implement the algorithm based on traversal of matrix, row major or column major order. We have two matrices and two ways (row and column major) to traverse each matrix. Hence, there can atleast be 4 different ways of transformation of source matrix into final matrix.
C++
// C++ program to turn an // image by 90 Degree #include <bits/stdc++.h> using namespace std; void displayMatrix(unsigned int const *p, unsigned int row, unsigned int col); void rotate(unsigned int *pS, unsigned int *pD, unsigned int row, unsigned int col); void displayMatrix(unsigned int const *p, unsigned int r, unsigned int c) { unsigned int row, col; cout << "\n\n" ; for (row = 0; row < r; row++) { for (col = 0; col < c; col++) cout << * (p + row * c + col) << "\t" ; cout << "\n" ; } cout << "\n\n" ; } void rotate(unsigned int *pS, unsigned int *pD, unsigned int row, unsigned int col) { unsigned int r, c; for (r = 0; r < row; r++) { for (c = 0; c < col; c++) { *(pD + c * row + (row - r - 1)) = *(pS + r * col + c); } } } // Driver Code int main() { // declarations unsigned int image[][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; unsigned int *pSource; unsigned int *pDestination; unsigned int m, n; // setting initial values // and memory allocation m = 3, n = 4, pSource = (unsigned int *)image; pDestination = (unsigned int *) malloc ( sizeof ( int ) * m * n); // process each buffer displayMatrix(pSource, m, n); rotate(pSource, pDestination, m, n); displayMatrix(pDestination, n, m); free (pDestination); return 0; } // This code is contributed by rathbhupendra |
C
// C program to turn an // image by 90 Degree #include <stdio.h> #include <stdlib.h> void displayMatrix(unsigned int const *p, unsigned int row, unsigned int col); void rotate(unsigned int *pS, unsigned int *pD, unsigned int row, unsigned int col); void displayMatrix(unsigned int const *p, unsigned int r, unsigned int c) { unsigned int row, col; printf ( "\n\n" ); for (row = 0; row < r; row++) { for (col = 0; col < c; col++) printf ( "%d\t" , * (p + row * c + col)); printf ( "\n" ); } printf ( "\n\n" ); } void rotate(unsigned int *pS, unsigned int *pD, unsigned int row, unsigned int col) { unsigned int r, c; for (r = 0; r < row; r++) { for (c = 0; c < col; c++) { *(pD + c * row + (row - r - 1)) = *(pS + r * col + c); } } } // Driver Code int main() { // declarations unsigned int image[][4] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}}; unsigned int *pSource; unsigned int *pDestination; unsigned int m, n; // setting initial values // and memory allocation m = 3, n = 4, pSource = (unsigned int *)image; pDestination = (unsigned int *) malloc ( sizeof ( int ) * m * n); // process each buffer displayMatrix(pSource, m, n); rotate(pSource, pDestination, m, n); displayMatrix(pDestination, n, m); free (pDestination); getchar (); return 0; } |
Java
// Java program to turn an // image by 90 Degree import java.util.*; class GFG { static void displayMatrix( int [] p, int r, int c) { int row, col; System.out.println( "\n\n" ); for (row = 0 ; row < r; row++) { for (col = 0 ; col < c; col++) System.out.print(p[row * c + col] + "\t" ); System.out.println(); } System.out.println( "\n" ); } static void rotate( int [] pS, int [] pD, int row, int col) { int r, c; for (r = 0 ; r < row; r++) { for (c = 0 ; c < col; c++) { pD = pS[r * col + c]; } } } // Driver Code public static void main(String[] args) { // declarations int [] image = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 }; // setting initial values // and memory allocation int m = 3 , n = 4 ; int [] pSource = new int [image.length]; for ( int i = 0 ; i < image.length; i++) pSource[i] = image[i]; int [] pDestination = new int [m * n]; // process each buffer displayMatrix(pSource, m, n); rotate(pSource, pDestination, m, n); displayMatrix(pDestination, n, m); } } // This code is contributed by phasing17 |
Python3
# Python3 program to turn an # image by 90 Degree def displayMatrix(p, r, c): print ( "\n\n" ); for row in range (r): for col in range (c): print (p[row * c + col], end = "\t" ); print () print ( "\n" ) def rotate(pS, pD, row, col): for r in range (row): for c in range (col): pD = pS[r * col + c]; # Driver Code # declarations image = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ]; # setting initial values # and memory allocation m = 3 n = 4 ; pSource = image.copy() pDestination = [ None ] * (m * n); # process each buffer displayMatrix(pSource, m, n); rotate(pSource, pDestination, m, n); displayMatrix(pDestination, n, m); # This code is contributed by phasing17 |
C#
// C# program to turn an // image by 90 Degree using System; using System.Collections.Generic; class GFG { static void displayMatrix( int [] p, int r, int c) { int row, col; Console.WriteLine( "\n\n" ); for (row = 0; row < r; row++) { for (col = 0; col < c; col++) Console.Write(p[row * c + col] + "\t" ); Console.WriteLine(); } Console.WriteLine( "\n" ); } static void rotate( int [] pS, int [] pD, int row, int col) { int r, c; for (r = 0; r < row; r++) { for (c = 0; c < col; c++) { pD = pS[r * col + c]; } } } // Driver Code public static void Main( string [] args) { // declarations int [] image = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; // setting initial values // and memory allocation int m = 3, n = 4; int [] pSource = new int [image.Length]; for ( int i = 0; i < image.Length; i++) pSource[i] = image[i]; int [] pDestination = new int [m * n]; // process each buffer displayMatrix(pSource, m, n); rotate(pSource, pDestination, m, n); displayMatrix(pDestination, n, m); } } // This code is contributed by phasing17 |
Javascript
// JS program to turn an // image by 90 Degree function displayMatrix(p, r, c) { let row, col; console.log( "\n\n" ); for (row = 0; row < r; row++) { for (col = 0; col < c; col++) process.stdout.write(p[row * c + col] + "\t" ); console.log() } console.log( "\n" ) } function rotate(pS, pD, row, col) { let r, c; for (r = 0; r < row; r++) { for (c = 0; c < col; c++) { pD = pS[r * col + c]; } } } // Driver Code // declarations let image = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ]; // setting initial values // and memory allocation let m = 3, n = 4; let pSource = [... image] let pDestination = new Array(m * n); // process each buffer displayMatrix(pSource, m, n); rotate(pSource, pDestination, m, n); displayMatrix(pDestination, n, m); // This code is contributed by phasing17 |
Output :
1 2 3 4 5 6 7 8 9 10 11 12 9 5 1 10 6 2 11 7 3 12 8 4
Time Complexity: O(N*M), as we are using nested loops for traversing the matrix.
Auxiliary Space: O(N*M), as we are using extra space for matrix.
Another Approach:
- The input image is defined as a 2D list of pixels. In this example, the input image has 3 rows and 4 columns.
- The transposed_image matrix is created by swapping the rows and columns of the input image. This is achieved using a nested list comprehension that iterates over the rows and columns of the input image and creates a new matrix where the rows and columns are swapped.
- The rotated_image matrix is created by reversing the order of rows in the transposed_image matrix. This step effectively rotates the image by 90 degrees counterclockwise.
- Finally, the rotated image is printed by iterating over its rows and printing them.
Python3
# define input image as a 2D list of pixels image = [[ 1 , 2 , 3 , 4 ], [ 5 , 6 , 7 , 8 ], [ 9 , 10 , 11 , 12 ]] # transpose image matrix transposed_image = [[image[j][i] for j in range ( len (image))] for i in range ( len (image[ 0 ]))] # reverse the order of rows to rotate the image by 90 degrees counterclockwise rotated_image = [ list ( reversed (row)) for row in transposed_image] # print rotated image for row in rotated_image: print (row) |
Javascript
// define input image as a 2D array of pixels const image = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]; // transpose image matrix const transposedImage = image[0].map((_, i) => image.map(row => row[i])); // reverse the order of rows to rotate the image by 90 degrees counterclockwise const rotatedImage = transposedImage.map(row => row.reverse()); // print rotated image for (let row of rotatedImage) { console.log(row); } |
C#
using System; class Program { static void Main( string [] args) { // Define input image as a 2D array of pixels int [, ] image = new int [, ] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; // Transpose image matrix int [, ] transposed_image = new int [image.GetLength(1), image.GetLength(0)]; for ( int i = 0; i < image.GetLength(0); i++) { for ( int j = 0; j < image.GetLength(1); j++) { transposed_image[j, i] = image[i, j]; } } // Reverse the order of columns to rotate the image // by 90 degrees counterclockwise int [, ] rotated_image = new int [transposed_image.GetLength(0), transposed_image.GetLength(1)]; for ( int i = 0; i < transposed_image.GetLength(0); i++) { for ( int j = 0; j < transposed_image.GetLength(1); j++) { rotated_image[i, j] = transposed_image [i, transposed_image.GetLength(1) - 1 - j]; } } // Print rotated image for ( int i = 0; i < rotated_image.GetLength(0); i++) { Console.Write( "[" ); for ( int j = 0; j < rotated_image.GetLength(1); j++) { Console.Write(rotated_image[i, j] + ", " ); } Console.WriteLine( "]" ); } } } |
[9, 5, 1] [10, 6, 2] [11, 7, 3] [12, 8, 4]
Time Complexity: O(N*M)
Auxiliary Space: O(N*M)
Inplace rotate square matrix by 90 degrees Compiled by Venki. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...