In this section, we are going to explore different types of transformation matrices. The following code is designed to demonstrate the properties of some different transformation matrices.
Do This
Review the following code.
#Some python packages we will be using
%matplotlib inline
import numpy as np
import matplotlib.pylab as plt
from mpl_toolkits.mplot3d import Axes3D #Lets us make 3D plots
import numpy as np
import sympy as sym
sym.init_printing(use_unicode=True) # Trick to make matrixes look nice in jupyter
#Example Scaling Matrix
#Define Matrix
scale = 0.5 #The amount that coordinates are scaled.
S = np.matrix([[scale,0,0], [0,scale,0], [0,0,1]])
#Apply matrix
mp = p.copy()
mp = S*mp
#Plot points after transform
plt.plot(mp[0,:].tolist()[0],mp[1,:].tolist()[0], color='green')
plt.axis('scaled')
plt.axis([-10,20,-15,15])
plt.title('After Scaling')
#Uncomment the next line if you want to see the original.
# plt.plot(p[0,:].tolist()[0],p[1,:].tolist()[0], color='blue',alpha=0.3);
sym.Matrix(S)
Example Translation Matrix
#Example Translation Matrix
#Define Matrix
dx = 1 #The amount shifted in the x-direction
dy = 1 #The amount shifted in the y-direction
T = np.matrix([[1,0,dx], [0,1,dy], [0,0,1]])
#Apply matrix
mp = p.copy()
mp = T*mp
#Plot points after transform
plt.plot(mp[0,:].tolist()[0],mp[1,:].tolist()[0], color='green')
plt.axis('scaled')
plt.axis([-10,20,-15,15])
plt.title('After Translation')
#Uncomment the next line if you want to see the original.
# plt.plot(p[0,:].tolist()[0],p[1,:].tolist()[0], color='blue',alpha=0.3);
sym.Matrix(T)
Example Reflection Matrix
#Example Reflection Matrix
#Define Matrix
Re = np.matrix([[1,0,0],[0,-1,0],[0,0,1]]) ## Makes all y-values opposite so it reflects over the x-axis.
#Apply matrix
mp = p.copy()
mp = Re*mp
#Plot points after transform
plt.plot(mp[0,:].tolist()[0],mp[1,:].tolist()[0], color='green')
plt.axis('scaled')
plt.axis([-10,20,-15,15])
#Uncomment the next line if you want to see the original.
# plt.plot(p[0,:].tolist()[0],p[1,:].tolist()[0], color='blue',alpha=0.3);
sym.Matrix(Re)
Example Rotation Matrix
#Example Rotation Matrix
#Define Matrix
degrees = 30
theta = degrees * np.pi / 180 ##Make sure to always convert from degrees to radians.
# Rotates the points 30 degrees counterclockwise.
R = np.matrix([[np.cos(theta),-np.sin(theta),0],[np.sin(theta), np.cos(theta),0],[0,0,1]])
#Apply matrix
mp = p.copy()
mp = R*mp
#Plot points after transform
plt.plot(mp[0,:].tolist()[0],mp[1,:].tolist()[0], color='green')
plt.axis('scaled')
plt.axis([-10,20,-15,15])
#Uncomment the next line if you want to see the original.
# plt.plot(p[0,:].tolist()[0],p[1,:].tolist()[0], color='blue',alpha=0.3);
sym.Matrix(R)
Example Shear Matrix
Combine Transforms
We have five transforms \(R\), \(S\), \(T\), \(Re\), and \(SH\)
Do This
Construct a (\(3 \times 3\)) transformation Matrix (called \(M\)) which combines these five transforms into a single matrix. You can choose different orders for these five matrix, then compare your result with other students.