Matrix Rotation is the concept of rotating a clockwise or counterclockwise matrix by different angles.
Firstly in this code, we'll have one user-defined function named rotate_matrix with arguments matrix and angle which returns the rotated matrix.
Before that, we'll have the main function where the user gets the size of the matrix and then gets the input matrix and store it as a nested list.
n=int(input("enter size of matrix:")) matrix=[] print("enter matrix:") for i in range(n): a=[] for j in range(n): a.append(int(input())) matrix.append(a) print("\n original matrix:") for i in range(n): for j in range(n): print(matrix[i][j], end = " ") print()
In the above-given code, we get the size of the matrix and store it in n. Then we declare an empty list matrix and get the input matrix from the user. We have another empty list 'a', which is used to store the elements row-wise and then append it to the matrix list as a nested lists
For example, if we give the size of the matrice as 2, then it is a 2x2 matrix, so in we'll have the matrix as
[
[1,2],
[3,4]
]
And then, we iterate through two loops and print the original matrix given by the user.
angles=[90,180,270] for ang in angles: new=rotate_matrix(matrix,ang) print("\n rotated by",ang ,"degrees:\n") for r in new: for ele in r: print(ele,end=' ') print()
In this part of the code, we declare the angles in the list and iterate through each angle to rotate the given matrix and print the resultant rotated matrix
Now, we declare the user-defined function rotate_matrix
def rotate_matrix(matrix,angle): n=len(matrix) r_matrix=[[0]*n for _ in range(n)] #90-deg if angle==90: for i in range(n): for j in range(n): r_matrix[j][n-i-1]= matrix[i][j]
After defining a function we get the length of the matrix and initialize the elements of the resultant rotated matrix to 0. Then for 90-degree rotation, we use nested loops to iterate through each element of the matrix and swap it with the corresponding element in the rotated matrix whose elements we initialized to zero.
For Example, if the size of the matrix is 3,
then for 'i' in range(3) -> i=0,1,2
for 'j' in range(3) ->j=0,1,2
first loop: i=0,j=0
r_matrix[0][(3-1-0)]=matrix[0][0]
where i indicates the row and j indicates the column
so r_matrix[0][2] = matrix[0][0]
the element of '0'th row and '2'nd column is replaced with the element of the matrix from '0'th row and '0'th column
Similarly second loop has i=0 and j=1 and the loop goes on until i=2 and j=2
Now, we'll see the rotation of the matrix for 180 degree
#180-deg elif angle==180: for i in range(n): for j in range(n): r_matrix[n-i-1][n-j-1] =matrix[i][j]
This code also works similarly to the above rotation except for one change. For 180 degrees rotation, we'll have r_matrix with [n-i-1] and [n-j-1] values for row and column respectively and swap it with the given matrix.
For Example, we'll have the n=3
first loop: i=0,j=0
r_matrix[(3-0-1)][(3-0-1)] =matrix[0][0] => r_matrix[2][2] =matrix[0][0]
The element of '2'nd row and '2'nd column is
replaced with the element of the matrix from '0'th row and '0'th column
Similarly second loop has i=0 and j=1 and the loop goes on until i=2 and j=2
Now, we'll see the rotation of the matrix for 270 degree
#270-deg elif angle==270: for i in range(n): for j in range(n): r_matrix[n - j - 1 ][i ] = matrix[i][j] return (r_matrix)
This code also works similarly but for 270 degrees and the r_matrix will be [n-j-1] and [i] values for row and columns respectively and swaps them with the corresponding given matrix.
For the same size, in the first loop, we have i=0,j=0
so r_matrix[2][0] = matrix [0][0]
lastly, we'll return the r_matrix
Output:
enter size of matrix:3
enter matrix:
1
2
3
4
5
6
7
8
9
original matrix:
1 2 3
4 5 6
7 8 9
rotated by 90 degrees:
7 4 1
8 5 2
9 6 3
rotated by 180 degrees:
9 8 7
6 5 4
3 2 1
rotated by 270 degrees:
3 6 9
2 5 8
1 4 7
Submitted by Rakshini Rajkumar (RakshiniRajkumar11)
Download packets of source code on Coders Packet
Comments