Skip to main content

Rotate Image by 90 degrees

Here given code implementation process.

import java.util.ArrayList;
/*
    Java Program for
    Rotate Image by 90 degrees
*/
public class Rotation
{
	// Display the elements of given record
	public void display(ArrayList < ArrayList < Integer >> record)
	{
		// Size of row element
		int n = record.size();
		// This loop are targeting to row element
		for (int i = 0; i < n; ++i)
		{
			// Size of column element
			int m = record.get(i).size();
			// This loop are targeting to column element
			for (int j = 0; j < m; ++j)
			{
				System.out.print("  " + record.get(i).get(j));
			}
			// Include new line
			System.out.print("\n");
		}
	}
	public ArrayList < ArrayList < Integer >> rotate90Degree(ArrayList < ArrayList < Integer >> record)
	{
		int n = record.size();
		// Auxiliary array which capture rotate view    
		ArrayList < ArrayList < Integer >> auxiliary = new ArrayList < ArrayList < Integer >> ();
		if (n == 0)
		{
			return auxiliary;
		}
		int m = record.get(0).size();
		for (int i = 0; i < m; ++i)
		{
			auxiliary.add(new ArrayList < Integer > ());
		}
		// Column element
		for (int i = 0; i < m; ++i)
		{
			// Row element
			for (int j = 0; j < n; ++j)
			{
				auxiliary.get(i).add(record.get(j).get(i));
			}
		}
		return auxiliary;
	}
	public static void main(String[] args)
	{
		Rotation task = new Rotation();
		ArrayList < ArrayList < Integer >> record = new ArrayList < ArrayList < Integer >> ();
		int data = 1;
		int row = 6;
		int col = 4;
		for (int i = 0; i < row; ++i)
		{
			record.add(new ArrayList < Integer > ());
		}
		for (int i = 0; i < row; ++i)
		{
			for (int j = 0; j < col; ++j)
			{
				record.get(i).add(data);
				data++;
			}
		}
		System.out.print("\n Before Rotate  \n");
		task.display(record);
		record = task.rotate90Degree(record);
		System.out.print("\n After Rotate \n");
		task.display(record);
	}
}

Output

 Before Rotate
  1  2  3  4
  5  6  7  8
  9  10  11  12
  13  14  15  16
  17  18  19  20
  21  22  23  24

 After Rotate
  1  5  9  13  17  21
  2  6  10  14  18  22
  3  7  11  15  19  23
  4  8  12  16  20  24
// Include header file
#include <iostream>
#include <vector>

using namespace std;
/*
    C++ Program for
    Rotate Image by 90 degrees
*/
class Rotation
{
	public:
		// Display the elements of given record
		void display(vector < vector < int > > record)
		{
			// Size of row element
			int n = record.size();
			// This loop are targeting to row element
			for (int i = 0; i < n; ++i)
			{
				// Size of column element
				int m = record.at(i).size();
				// This loop are targeting to column element
				for (int j = 0; j < m; ++j)
				{
					cout << "  " << record.at(i).at(j);
				}
				// Include new line
				cout << "\n";
			}
		}
	vector < vector < int > > rotate90Degree(vector < vector < int > > record)
	{
		int n = record.size();
		// Auxiliary array which capture rotate view    
		vector < vector < int > > auxiliary;
		if (n == 0)
		{
			return auxiliary;
		}
		int m = record[0].size();
		for (int i = 0; i < m; ++i)
		{
			auxiliary.push_back(vector < int > ());
		}
		// Column element
		for (int i = 0; i < m; ++i)
		{
			// Row element
			for (int j = 0; j < n; ++j)
			{
				auxiliary.at(i).push_back(record.at(j).at(i));
			}
		}
		return auxiliary;
	}
};
int main()
{
	Rotation *task = new Rotation();
	vector < vector < int > > record;
	int data = 1;
	int row = 6;
	int col = 4;
	for (int i = 0; i < row; ++i)
	{
		record.push_back(vector < int > ());
	}
	for (int i = 0; i < row; ++i)
	{
		for (int j = 0; j < col; ++j)
		{
			record.at(i).push_back(data);
			data++;
		}
	}
	cout << "\n Before Rotate  \n";
	task->display(record);
	record = task->rotate90Degree(record);
	cout << "\n After Rotate \n";
	task->display(record);
	return 0;
}

Output

 Before Rotate
  1  2  3  4
  5  6  7  8
  9  10  11  12
  13  14  15  16
  17  18  19  20
  21  22  23  24

 After Rotate
  1  5  9  13  17  21
  2  6  10  14  18  22
  3  7  11  15  19  23
  4  8  12  16  20  24
// Include namespace system
using System;
using System.Collections.Generic;
/*
    Csharp Program for
    Rotate Image by 90 degrees
*/
public class Rotation
{
	// Display the elements of given record
	public void display(List < List < int >> record)
	{
		// Size of row element
		int n = record.Count;
		// This loop are targeting to row element
		for (int i = 0; i < n; ++i)
		{
			// Size of column element
			int m = record[i].Count;
			// This loop are targeting to column element
			for (int j = 0; j < m; ++j)
			{
				Console.Write("  " + record[i][j]);
			}
			// Include new line
			Console.Write("\n");
		}
	}
	public List < List < int >> rotate90Degree(List < List < int >> record)
	{
		int n = record.Count;
		// Auxiliary array which capture rotate view    
		List < List < int >> auxiliary = new List < List < int >> ();
		if (n == 0)
		{
			return auxiliary;
		}
		int m = record[0].Count;
		for (int i = 0; i < m; ++i)
		{
			auxiliary.Add(new List < int > ());
		}
		// Column element
		for (int i = 0; i < m; ++i)
		{
			// Row element
			for (int j = 0; j < n; ++j)
			{
				auxiliary[i].Add(record[j][i]);
			}
		}
		return auxiliary;
	}
	public static void Main(String[] args)
	{
		Rotation task = new Rotation();
		List < List < int >> record = new List < List < int >> ();
		int data = 1;
		int row = 6;
		int col = 4;
		for (int i = 0; i < row; ++i)
		{
			record.Add(new List < int > ());
		}
		for (int i = 0; i < row; ++i)
		{
			for (int j = 0; j < col; ++j)
			{
				record[i].Add(data);
				data++;
			}
		}
		Console.Write("\n Before Rotate  \n");
		task.display(record);
		record = task.rotate90Degree(record);
		Console.Write("\n After Rotate \n");
		task.display(record);
	}
}

Output

 Before Rotate
  1  2  3  4
  5  6  7  8
  9  10  11  12
  13  14  15  16
  17  18  19  20
  21  22  23  24

 After Rotate
  1  5  9  13  17  21
  2  6  10  14  18  22
  3  7  11  15  19  23
  4  8  12  16  20  24
package main
import "fmt"
/*
    Go Program for
    Rotate Image by 90 degrees
*/
// Display the elements of given record
func display(record [][]int) {
	// Size of row element
	var n int = len(record)
	// This loop are targeting to row element
	for i := 0 ; i < n ; i++ {
		// Size of column element
		var m int = len(record[i])
		// This loop are targeting to column element
		for j := 0 ; j < m ; j++ {
			fmt.Print("  ", record[i][j])
		}
		// Include new line
		fmt.Print("\n")
	}
}
func rotate90Degree(record [][]int) [][]int {
	var n int = len(record)

	if n == 0 {
		return make([][]int,0)
	}
	var m int = len(record[0])
	// Auxiliary array which capture rotate view    
	var auxiliary = make([][]int,m)
	for i := 0 ; i < m ; i++ {
		auxiliary[i] = make([]int,n)
	}
	// Column element
	for i := 0 ; i < m ; i++ {
		// Row element
		for j := 0 ; j < n ; j++ {
			auxiliary[i][j] = record[j][i]
		}
	}
	return auxiliary
}
func main() {


	var data int = 1
	var row int = 6
	var col int = 4

	var record = make([][]int,row)
	for i := 0 ; i < row ; i++ {
		record[i] = make([]int,col)
	}
	for i := 0 ; i < row ; i++ {
		for j := 0 ; j < col ; j++ {
			record[i][j] = data
			data++
		}
	}
	fmt.Print("\n Before Rotate  \n")
	display(record)
	record = rotate90Degree(record)
	fmt.Print("\n After Rotate \n")
	display(record)
}

Output

 Before Rotate
  1  2  3  4
  5  6  7  8
  9  10  11  12
  13  14  15  16
  17  18  19  20
  21  22  23  24

 After Rotate
  1  5  9  13  17  21
  2  6  10  14  18  22
  3  7  11  15  19  23
  4  8  12  16  20  24
<?php
/*
    Php Program for
    Rotate Image by 90 degrees
*/
class Rotation
{
	// Display the elements of given record
	public	function display($record)
	{
		// Size of row element
		$n = count($record);
		// This loop are targeting to row element
		for ($i = 0; $i < $n; ++$i)
		{
			// Size of column element
			$m = count($record[$i]);
			// This loop are targeting to column element
			for ($j = 0; $j < $m; ++$j)
			{
				echo("  ".$record[$i][$j]);
			}
			// Include new line
			echo("\n");
		}
	}
	public	function rotate90Degree($record)
	{
		$n = count($record);
		// Auxiliary array which capture rotate view    
		$auxiliary = array();
		if ($n == 0)
		{
			return $auxiliary;
		}
		$m = count($record[0]);
		for ($i = 0; $i < $m; ++$i)
		{
			$auxiliary[] = array();
		}
		// Column element
		for ($i = 0; $i < $m; ++$i)
		{
			// Row element
			for ($j = 0; $j < $n; ++$j)
			{
				$auxiliary[$i][] = $record[$j][$i];
			}
		}
		return $auxiliary;
	}
}

function main()
{
	$task = new Rotation();
	$record = array();
	$data = 1;
	$row = 6;
	$col = 4;
	for ($i = 0; $i < $row; ++$i)
	{
		$record[] = array();
	}
	for ($i = 0; $i < $row; ++$i)
	{
		for ($j = 0; $j < $col; ++$j)
		{
			$record[$i][] = $data;
			$data++;
		}
	}
	echo("\n Before Rotate  \n");
	$task->display($record);
	$record = $task->rotate90Degree($record);
	echo("\n After Rotate \n");
	$task->display($record);
}
main();

Output

 Before Rotate
  1  2  3  4
  5  6  7  8
  9  10  11  12
  13  14  15  16
  17  18  19  20
  21  22  23  24

 After Rotate
  1  5  9  13  17  21
  2  6  10  14  18  22
  3  7  11  15  19  23
  4  8  12  16  20  24
/*
    Node JS Program for
    Rotate Image by 90 degrees
*/
class Rotation
{
	// Display the elements of given record
	display(record)
	{
		// Size of row element
		var n = record.length;
		// This loop are targeting to row element
		for (var i = 0; i < n; ++i)
		{
			// Size of column element
			var m = record[i].length;
			// This loop are targeting to column element
			for (var j = 0; j < m; ++j)
			{
				process.stdout.write("  " + record[i][j]);
			}
			// Include new line
			process.stdout.write("\n");
		}
	}
	rotate90Degree(record)
	{
		var n = record.length;
		// Auxiliary array which capture rotate view    
		var auxiliary = [];
		if (n == 0)
		{
			return auxiliary;
		}
		var m = record[0].length;
		for (var i = 0; i < m; ++i)
		{
			auxiliary.push([]);
		}
		// Column element
		for (var i = 0; i < m; ++i)
		{
			// Row element
			for (var j = 0; j < n; ++j)
			{
				auxiliary[i].push(record[j][i]);
			}
		}
		return auxiliary;
	}
}

function main()
{
	var task = new Rotation();
	var record = [];
	var data = 1;
	var row = 6;
	var col = 4;
	for (var i = 0; i < row; ++i)
	{
		record.push([]);
	}
	for (var i = 0; i < row; ++i)
	{
		for (var j = 0; j < col; ++j)
		{
			record[i].push(data);
			data++;
		}
	}
	process.stdout.write("\n Before Rotate  \n");
	task.display(record);
	record = task.rotate90Degree(record);
	process.stdout.write("\n After Rotate \n");
	task.display(record);
}
main();

Output

 Before Rotate
  1  2  3  4
  5  6  7  8
  9  10  11  12
  13  14  15  16
  17  18  19  20
  21  22  23  24

 After Rotate
  1  5  9  13  17  21
  2  6  10  14  18  22
  3  7  11  15  19  23
  4  8  12  16  20  24
#    Python 3 Program for
#    Rotate Image by 90 degrees
class Rotation :
	#  Display the elements of given record
	def display(self, record) :
		#  Size of row element
		n = len(record)
		i = 0
		#  This loop are targeting to row element
		while (i < n) :
			#  Size of column element
			m = len(record[i])
			j = 0
			#  This loop are targeting to column element
			while (j < m) :
				print("  ", record[i][j], end = "")
				j += 1
			
			#  Include new line
			print(end = "\n")
			i += 1
		
	
	def rotate90Degree(self, record) :
		n = len(record)
		#  Auxiliary list which capture rotate view    
		auxiliary = []
		if (n == 0) :
			return auxiliary
		
		m = len(record[0])
		i = 0
		while (i < m) :
			auxiliary.append([])
			i += 1
		
		i = 0
		#  Column element
		while (i < m) :
			j = 0
			#  Row element
			while (j < n) :
				auxiliary[i].append(record[j][i])
				j += 1
			
			i += 1
		
		return auxiliary
	

def main() :
	task = Rotation()
	record = []
	data = 1
	row = 6
	col = 4
	i = 0
	while (i < row) :
		record.append([])
		i += 1
	
	i = 0
	while (i < row) :
		j = 0
		while (j < col) :
			record[i].append(data)
			data += 1
			j += 1
		
		i += 1
	
	print("\n Before Rotate  ")
	task.display(record)
	record = task.rotate90Degree(record)
	print("\n After Rotate ")
	task.display(record)

if __name__ == "__main__": main()

Output

 Before Rotate
   1   2   3   4
   5   6   7   8
   9   10   11   12
   13   14   15   16
   17   18   19   20
   21   22   23   24

 After Rotate
   1   5   9   13   17   21
   2   6   10   14   18   22
   3   7   11   15   19   23
   4   8   12   16   20   24
#    Ruby Program for
#    Rotate Image by 90 degrees
class Rotation 
	#  Display the elements of given record
	def display(record) 
		#  Size of row element
		n = record.length
		i = 0
		#  This loop are targeting to row element
		while (i < n) 
			#  Size of column element
			m = record[i].length
			j = 0
			#  This loop are targeting to column element
			while (j < m) 
				print("  ", record[i][j])
				j += 1
			end

			#  Include new line
			print("\n")
			i += 1
		end

	end

	def rotate90Degree(record) 
		n = record.length
		#  Auxiliary array which capture rotate view    
		auxiliary = []
		if (n == 0) 
			return auxiliary
		end

		m = record[0].length
		i = 0
		while (i < m) 
			auxiliary.push([])
			i += 1
		end

		i = 0
		#  Column element
		while (i < m) 
			j = 0
			#  Row element
			while (j < n) 
				auxiliary[i].push(record[j][i])
				j += 1
			end

			i += 1
		end

		return auxiliary
	end

end

def main() 
	task = Rotation.new()
	record = []
	data = 1
	row = 6
	col = 4
	i = 0
	while (i < row) 
		record.push([])
		i += 1
	end

	i = 0
	while (i < row) 
		j = 0
		while (j < col) 
			record[i].push(data)
			data += 1
			j += 1
		end

		i += 1
	end

	print("\n Before Rotate  \n")
	task.display(record)
	record = task.rotate90Degree(record)
	print("\n After Rotate \n")
	task.display(record)
end

main()

Output

 Before Rotate  
  1  2  3  4
  5  6  7  8
  9  10  11  12
  13  14  15  16
  17  18  19  20
  21  22  23  24

 After Rotate 
  1  5  9  13  17  21
  2  6  10  14  18  22
  3  7  11  15  19  23
  4  8  12  16  20  24
import scala.collection.mutable._;
/*
    Scala Program for
    Rotate Image by 90 degrees
*/
class Rotation()
{
	// Display the elements of given record
	def display(record: ArrayBuffer[ArrayBuffer[Int]]): Unit = {
		// Size of row element
		var n: Int = record.size;
		var i: Int = 0;
		// This loop are targeting to row element
		while (i < n)
		{
			// Size of column element
			var m: Int = record(i).size;
			var j: Int = 0;
			// This loop are targeting to column element
			while (j < m)
			{
				print("  " + record(i)(j));
				j += 1;
			}
			// Include new line
			print("\n");
			i += 1;
		}
	}
	def rotate90Degree(record: ArrayBuffer[ArrayBuffer[Int]]): 
	ArrayBuffer[ArrayBuffer[Int]] = {
		var n: Int = record.size;
		// Auxiliary array which capture rotate view    
		var auxiliary: ArrayBuffer[ArrayBuffer[Int]] = 
				   new ArrayBuffer[ArrayBuffer[Int]]();
		if (n == 0)
		{
			return auxiliary;
		}
		var m: Int = record(0).size;
		var i: Int = 0;
		while (i < m)
		{
			auxiliary += new ArrayBuffer[Int]();
			i += 1;
		}
		i = 0;
		// Column element
		while (i < m)
		{
			var j: Int = 0;
			// Row element
			while (j < n)
			{
				auxiliary(i) += record(j)(i);
				j += 1;
			}
			i += 1;
		}
		return auxiliary;
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Rotation = new Rotation();
		var record: ArrayBuffer[ArrayBuffer[Int]] = 
          new ArrayBuffer[ArrayBuffer[Int]]();
		var data: Int = 1;
		var row: Int = 6;
		var col: Int = 4;
		var i: Int = 0;
		while (i < row)
		{
			record += new ArrayBuffer[Int]();
			i += 1;
		}
		i = 0;
		while (i < row)
		{
			var j: Int = 0;
			while (j < col)
			{
				record(i) += data;
				data += 1;
				j += 1;
			}
			i += 1;
		}
		print("\n Before Rotate  \n");
		task.display(record);
		record = task.rotate90Degree(record);
		print("\n After Rotate \n");
		task.display(record);
	}
}

Output

 Before Rotate
  1  2  3  4
  5  6  7  8
  9  10  11  12
  13  14  15  16
  17  18  19  20
  21  22  23  24

 After Rotate
  1  5  9  13  17  21
  2  6  10  14  18  22
  3  7  11  15  19  23
  4  8  12  16  20  24
import Foundation;
/*
    Swift 4 Program for
    Rotate Image by 90 degrees
*/
class Rotation
{
	// Display the elements of given record
	func display(_ record: [[Int]])
	{
		// Size of row element
		let n: Int = record.count;
		var i: Int = 0;
		// This loop are targeting to row element
		while (i < n)
		{
			// Size of column element
			let m: Int = record[i].count;
			var j: Int = 0;
			// This loop are targeting to column element
			while (j < m)
			{
				print("  ", record[i][j], terminator: "");
				j += 1;
			}
			// Include new line
			print(terminator: "\n");
			i += 1;
		}
	}
	func rotate90Degree(_ record: [[Int]]) -> [[Int]]
	{
		let n: Int = record.count;
		// Auxiliary array which capture rotate view    
		var auxiliary: [[Int]] = [[Int]]();
		if (n == 0)
		{
			return auxiliary;
		}
		let m: Int = record[0].count;
		var i: Int = 0;
		while (i < m)
		{
			auxiliary.append([Int]());
			i += 1;
		}
		i = 0;
		// Column element
		while (i < m)
		{
			var j: Int = 0;
			// Row element
			while (j < n)
			{
				auxiliary[i].append(record[j][i]);
				j += 1;
			}
			i += 1;
		}
		return auxiliary;
	}
}
func main()
{
	let task: Rotation = Rotation();
	var record: [[Int]] = [[Int]]();
	var data: Int = 1;
	let row: Int = 6;
	let col: Int = 4;
	var i: Int = 0;
	while (i < row)
	{
		record.append([Int]());
		i += 1;
	}
	i = 0;
	while (i < row)
	{
		var j: Int = 0;
		while (j < col)
		{
			record[i].append(data);
			data += 1;
			j += 1;
		}
		i += 1;
	}
	print("\n Before Rotate  ");
	task.display(record);
	record = task.rotate90Degree(record);
	print("\n After Rotate ");
	task.display(record);
}
main();

Output

 Before Rotate
   1   2   3   4
   5   6   7   8
   9   10   11   12
   13   14   15   16
   17   18   19   20
   21   22   23   24

 After Rotate
   1   5   9   13   17   21
   2   6   10   14   18   22
   3   7   11   15   19   23
   4   8   12   16   20   24
/*
    Kotlin Program for
    Rotate Image by 90 degrees
*/
class Rotation
{
    // Display the elements of given record
    fun display(record: MutableList < MutableList < Int >>  ): Unit
    {
        // Size of row element
        val n: Int = record.size;
        var i: Int = 0;
        // This loop are targeting to row element
        while (i < n)
        {
            // Size of column element
            val m: Int = record[i].size;
            var j: Int = 0;
            // This loop are targeting to column element
            while (j < m)
            {
                print("  " + record[i][j]);
                j += 1;
            }
            // Include new line
            print("\n");
            i += 1;
        }
    }
    fun rotate90Degree(record: MutableList < MutableList < Int >> ): 
    MutableList < MutableList < Int >> 
    {
        val n: Int = record.size;
        // Auxiliary array which capture rotate view    
        var auxiliary: MutableList<MutableList<Int>> = mutableListOf();
        if (n == 0)
        {
            return auxiliary;
        }
        val m: Int = record[0].size;
        var i: Int = 0;
        while (i < m)
        {
            auxiliary.add(mutableListOf < Int > ());
            i += 1;
        }
        i = 0;
        // Column element
        while (i < m)
        {
            var j: Int = 0;
            // Row element
            while (j < n)
            {
                auxiliary[i].add(record[j][i]);
                j += 1;
            }
            i += 1;
        }
        return auxiliary;
    }
}
fun main(args: Array < String > ): Unit
{
    val task: Rotation = Rotation();
    var record:  MutableList<MutableList<Int>>  = mutableListOf();
    var data: Int = 1;
    val row: Int = 6;
    val col: Int = 4;
    var i: Int = 0;
    while (i < row)
    {
        record.add(mutableListOf < Int > ());
        i += 1;
    }
    i = 0;
    while (i < row)
    {
        var j: Int = 0;
        while (j < col)
        {
            record[i].add(data);
            data += 1;
            j += 1;
        }
        i += 1;
    }
    print("\n Before Rotate  \n");
    task.display(record);
    record = task.rotate90Degree(record);
    print("\n After Rotate \n");
    task.display(record);
}

Output

 Before Rotate
  1  2  3  4
  5  6  7  8
  9  10  11  12
  13  14  15  16
  17  18  19  20
  21  22  23  24

 After Rotate
  1  5  9  13  17  21
  2  6  10  14  18  22
  3  7  11  15  19  23
  4  8  12  16  20  24

Observe after transform matrix in 90 degree its dimensions can be change. In below image are describe overview of this problem and solution.

90 degree rotation example of matrix




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment