Advertisement
Kostiggig

Image reversing

May 4th, 2023
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. def rotate_270(matrix):
  2.     n = len(matrix)
  3.     new_matrix = [[matrix[i][j] for j in range(n)] for i in range(n)]
  4.  
  5.     # Transpose the matrix
  6.     for r in range(n):
  7.         for c in range(r, n):
  8.             temp = new_matrix[r][c]
  9.             new_matrix[r][c] = new_matrix[c][r]
  10.             new_matrix[c][r] = temp
  11.  
  12.     until = int(n / 2)
  13.  
  14.     # swap rows
  15.     for r in range(until):
  16.         temp = new_matrix[r]
  17.         new_matrix[r] = new_matrix[n - r - 1]
  18.         new_matrix[n - r - 1] = temp
  19.  
  20.     return new_matrix
  21.  
  22.  
  23. def rotate_180(matrix):
  24.     n = len(matrix)
  25.     until = int(n / 2)
  26.     new_matrix = [[matrix[i][j] for j in range(n)] for i in range(n)]
  27.  
  28.     # swap rows
  29.     for r in range(until):
  30.         temp = new_matrix[r]
  31.         new_matrix[r] = new_matrix[n - r - 1]
  32.         new_matrix[n - r - 1] = temp
  33.  
  34.     # reverse each row's array
  35.     for r in range(n):
  36.         new_matrix[r] = inverted_array(new_matrix[r])
  37.  
  38.     return new_matrix
  39.  
  40.  
  41. def rotate_90(matrix):
  42.     n = len(matrix)
  43.     new_matrix = [[0 for j in range(n)] for i in range(n)]
  44.  
  45.     # Transpose matrix
  46.     for r in range(n):
  47.         for c in range(n):
  48.             new_matrix[r][c] = matrix[c][r]
  49.  
  50.     for r in range(n):
  51.         new_matrix[r] = inverted_array(new_matrix[r])
  52.     return new_matrix
  53.  
  54.  
  55. def inverted_array(array):
  56.     length = len(array)
  57.     new_array = [array[i] for i in range(length)]
  58.     until = int(length / 2)
  59.  
  60.     for i in range(until):
  61.         temp = new_array[i]
  62.         new_array[i] = new_array[length - i - 1]
  63.         new_array[length - i - 1] = temp
  64.  
  65.     return new_array
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement