Skip to content

Instantly share code, notes, and snippets.

@dubpirate
Created April 11, 2019 02:07
Show Gist options
  • Save dubpirate/a5dd11784b940b32865f28977c3689b2 to your computer and use it in GitHub Desktop.
Save dubpirate/a5dd11784b940b32865f28977c3689b2 to your computer and use it in GitHub Desktop.
MatrixFlipper created by dubpirate - https://repl.it/@dubpirate/MatrixFlipper
import numpy as np
lmin = -3 # (0 - (layers/2 round down))
lmax = 3 # layers/2 round down
# Creates a 4x4 Matrix where every object is a tuple: (x, y) <- Tuple
matrix = np.zeros((4, 4), dtype=tuple)
# Fill the matrix with the tuples corrosponding to the x, y, values.
for x in range(0, 4):
for y in range(0, 4):
matrix[x][y] = (x, y)
# layer(matrix) draws the matrix in layers.
def layer(matrix):
for layer in range(0, 7):
vis = []
#print("\n layer {0}".format(layer))
x = min(layer, lmax)
y = max(lmin + layer, 0)
xto = y
while x >= xto:
vis.append(matrix[x][y])
#print("Coords: {0}".format(matrix[x, y]))
x -= 1
y += 1
print("Layer {0}: {1}".format(layer, vis))
print(" North:")
print("In Matrix form: \n{0}".format(matrix))
print("In layers: ")
layer(matrix)
print("\n East:")
matrix = np.rot90(matrix)
print("In Matrix form: \n{0}".format(matrix))
print("In layers: ")
layer(matrix)
print("\n South")
matrix = np.rot90(matrix)
print("In Matrix form: \n{0}".format(matrix))
print("In layers: ")
layer(matrix)
print("\n West")
matrix = np.rot90(matrix)
print("In Matrix form: \n{0}".format(matrix))
print("In layers: ")
layer(matrix)
@dubpirate
Copy link
Author

Hello there! This is a gist of a program I wrote to rotate matrices that represent a game board for a project last year for my Computer Science Degree. In the end we didn't use python, but it was helpful to use numpy to visualise what was happening to the matrix as its rotated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment