Skip to content

Instantly share code, notes, and snippets.

@andcarnivorous
Last active January 5, 2019 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andcarnivorous/06e5d8771e1ccf351ab62d221ca8eb5d to your computer and use it in GitHub Desktop.
Save andcarnivorous/06e5d8771e1ccf351ab62d221ca8eb5d to your computer and use it in GitHub Desktop.
Basic linear transformations
from math import cos,sin,radians
import matplotlib.pyplot as plt
import numpy as np
vec = np.array([-5,-8]) #YOUR VECTOR
k = 1 #decide K for shear
# Transformations
def custom_transf (vector, m1,m2,m3,m4):
result = np.dot(np.matrix([[m1,m2],[m3,m4]], vector))
return result
def nullvec(vector):
result = np.dot(np.matrix([[1,0],[0,1]]), vector)
return result
def mirrored_x (vector):
result = np.dot(np.matrix([[1,0],[0,-1]]), vector)
return result
def mirrored_y(vector):
result = np.dot(np.matrix([[-1,0],[0,1]]), vector)
return result
def shear_x(vector, k):
result = np.dot(np.matrix([[1,k],[0,1]]), vector)
return result
def shear_y(vector, k):
result = np.dot(np.matrix([[1,0],[k,1]]), vector)
return result
def rotation90(vector):
result = np.dot(np.matrix([[0,1],[-1,0]]), vector)
return result
def proj_on_y(vector):
result = np.dot(np.matrix([[0,0],[0,1]]), vector)
return result
def proj_on_x(vector):
result = np.dot(np.matrix([[1,0],[0,0]]), vector)
return result
def reflection(vector):
result = np.dot(np.matrix([[-1,0],[0,-1]]), vector)
return result
ax = plt.gca()
#Cartesian plane
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
plt.scatter(vec[0], vec[1])
labels = ["reflection","shear_x", "shear_y", "mirrored_Y", "mirrored_X", "proj_on_X", "proj_on_Y", "rotation90",
"custom_transf", "vector"]
custom = custom_transf(vec, cos(radians(-90)),sin(radians(-90)),-sin(radians(-90)),cos(radians(-90)))
values = [reflection(vec),shear_x(vec,k), shear_y(vec,k),mirrored_y(vec),mirrored_x(vec), proj_on_x(vec),proj_on_y(vec),rotation90(vec),
custom, nullvec(vec)] #ADD VALUES TO CUSTOM_TRANSF
# PLOT ALL THE POINTS
plt.scatter(reflection(vec)[0,0], reflection(vec)[0,1])
plt.scatter(shear_x(vec,k)[0,0], shear_x(vec,k)[0,1])
plt.scatter(shear_y(vec,k)[0,0], shear_y(vec,k)[0,1])
plt.scatter(mirrored_y(vec)[0,0], mirrored_y(vec)[0,1])
plt.scatter(mirrored_x(vec)[0,0], mirrored_x(vec)[0,1])
plt.scatter(proj_on_x(vec)[0,0], proj_on_x(vec)[0,1])
plt.scatter(proj_on_y(vec)[0,0], proj_on_y(vec)[0,1])
plt.scatter(rotation90(vec)[0,0], rotation90(vec)[0,1])
plt.scatter(custom[0,0], custom[0,1])
print(values[0][0,0])
# THESE ARE FOR LABELS
vals1 = [x[0,0] for x in values]
vals2 = [x[0,1] for x in values]
for x in range(0, len(values)):
#PLOT LINES FROM THE TRANSFORMATIONS
# TO ORIGINAL VECTOR
plt.plot((nullvec(vec)[0][0,0] , values[x][0,0]),
(nullvec(vec)[0][0,1] , values[x][0,1]),
linestyle="dashed", alpha=0.5)
# PLOT LINES FROM POINTS TO ORIGIN
plt.plot((0 , values[x][0,0]),
(0 , values[x][0,1]),
color="black", alpha=0.5)
for label, x, y in zip(labels, vals1, vals2):
#ADD LABELS
plt.annotate(
label,
xy=(x, y), xytext=(-20, 20),
textcoords='offset points', ha='right', va='bottom',
bbox=dict(boxstyle='round,pad=0.5', fc='yellow', alpha=0.5),
arrowprops=dict(arrowstyle = '-', connectionstyle='arc3,rad=0'))
# Keep ticks in the range of possible transformations
plt.xticks(range(-(abs(vec[0])+8),(abs(vec[0])+9)))
plt.grid()
plt.yticks(range(-(abs(vec[1])+8),(abs(vec[1])+9)))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment