Rotate Image by 90 Degree using JavaScript
Last Updated :
26 Mar, 2024
In this article, we are going to learn how we can rotate images by 90 degrees clockwise using JavaScript. In simple words, we are given a matrix, and we have to rotate it by 90 degrees clockwise. To understand the question clearly, below is the image illustration explaining how the matrix looks when it is rotated 90 degrees clockwise:

Brute force Approach
In this approach we are going to discuss how we can rotate image by 90 degree in clockwise direction by taking another matrix of same size. We will create another temporary matrix of same size of original matrix i.e. n*n and then we take the first row of original matrix and put it in the last column of the temporary matrix , take second row of original matrix and put it in the second last column of matrix , take third row of original matrix and put it in the third last column of matrix and so.
Example: To demonstrate rotation of image by 90 degree in clockwise direction by taking another matrix of same size by traversing the matrix using nested loop.
JavaScript
function rotate(matrix) {
let n = matrix.length;
let temp = new Array(n).fill().map(() => new Array(n).fill(0));
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
temp[j][n - i - 1] = matrix[i][j];
}
}
return temp;
}
function main() {
let arr = [
[1, 2, 3 ],
[4, 5, 6 ],
[7 , 8, 9]
];
let answer = rotate(arr);
console.log("Rotated image by 90 degree in clockwise direction ");
for (let i = 0; i < answer.length; i++) {
for (let j = 0; j < answer[0].length; j++) {
process.stdout.write(answer[i][j] + " ");
}
console.log();
}
}
main();
OutputRotated image by 90 degree in clockwise direction
7 4 1
8 5 2
9 6 3
Time Complexity: O(N*N) , we traverse original matrix.
Space Complexity: O(N*N) , created matrix of same size.
Using Optimal Approach (reduce Time Complexity)
In this approach we will reduce time complexity of problem from O(N*N) to O(1). If we observe the matrix , we can rotate it by 90 degree clockwise by two simple steps. First we will take transpose of matrix (change rows to columns and rows to columns) and then reverse each row of matrix. The matrix obtained is rotated matrix (by 90 degrees).
Example : To demonstrate rotation of image by 90 degree in clockwise direction by taking transpose of matrix and reversing its each row.
JavaScript
function rotateBy90Degree(matrix) {
const n = matrix.length;
for (let i = 0; i < n; i++) {
for (let j = 0; j < i; j++) {
[matrix[i][j],
matrix[j][i]] = [matrix[j][i],
matrix[i][j]];
}
}
for (let i = 0; i < n; i++) {
matrix[i].reverse();
}
}
function main() {
let arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
rotateBy90Degree(arr);
console.log("Rotated Image by 90 degree in clockwise direction ");
for (let i = 0; i < arr.length; i++) {
console.log(arr[i].join(" "));
}
}
main();
OutputRotated Image by 90 degree in clockwise direction
7 4 1
8 5 2
9 6 3
Time Complexity: O(N*N) , transposing the matrix + O(N*N) , reversing rows = O(N*N)
Space Complexity: O(1), constant space
Similar Reads
JavaScript Program to Convert Radians to Degree Radians and degrees are units of angular measurement used extensively in mathematics, physics, and engineering. Converting radians to degrees is a common operation in trigonometry and other mathematical calculations. Example:// Formula to convert radian into degree: degrees = radians à (180/Ï)Where:
2 min read
JavaScript Program for array rotation Array rotation in JavaScript involves shifting elements within an array to the left or right by a specified number of positions. There are several approaches to rotate an array in JavaScript which are as follows: Table of Content Using Array Slice and Concatenation MethodUsing Array Splice and Push
3 min read
JavaScript Program for Left Rotate by One in an Array In this article, we are going to learn about left Rotation by One in an Array by using JavaScript, Left rotation by one in an array means shifting all elements from one position to the left, with the last element moving to the first position. The order of elements is cyclically rotated while maintai
5 min read
JavaScript Program to Search a Target Value in a Rotated Array In this article, we are going to implement a JavaScript program to Search for a target value in a rotated array. Table of Content Using Binary SearchUsing Linear SearchUsing Recursive FunctionUsing Modified Binary Search with Pivot Detection Using Binary SearchIn this approach, we begin by locating
5 min read
JavaScript Program to Find Circular Rotation of an Array by K Positions Given an array of integers and an integer K, rotate the array to the right by K positions in a circular manner. Examples: Input: nums = [1, 2, 3, 4, 5], k=2Output: [4, 5, 1, 2, 3]Input: nums = [6, 7, 8, 9, 10], k=1Output: [10, 9, 6, 7, 8]Below are the approaches to finding the Circular rotation of a
2 min read
Javascript Program to Print all possible rotations of a given Array Given an integer array arr[] of size N, the task is to print all possible rotations of the array.Examples: Input: arr[] = {1, 2, 3, 4} Output: {1, 2, 3, 4}, {4, 1, 2, 3}, {3, 4, 1, 2}, {2, 3, 4, 1} Explanation: Initial arr[] = {1, 2, 3, 4} After first rotation arr[] = {4, 1, 2, 3} After second rotat
3 min read