In [41]:
%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.animation as ani
from numpy import *
In [2]:
rect = [[1,-1,-1,1,1],
[2,2,-2,-2,2]] #rectangle defined by given points in in-class exercises
plt.plot(rect[0],rect[1])
plt.axis([-4, 4, -4, 4])
Out[2]:
In [9]:
def rotMatrix2D(theta):
m = [[cos(theta), -sin(theta)],
[sin(theta), cos(theta)]]
return m
rot_30_cc = rotMatrix2D(pi/6)
resultant = dot(rot_30_cc,rect) #resultant matrix from rotating original rectangle counter-clockwise 30 degrees
plt.plot(rect[0], rect[1])
plt.plot(resultant[0],resultant[1])
plt.axis([-4, 4, -4, 4])
Out[9]:
In [12]:
#undo the rotation
rot_30_cw = rotMatrix2D(-pi/6)
resultant_undo = dot(rot_30_cw,resultant)
plt.plot(resultant[0],resultant[1])
plt.plot(resultant_undo[0], resultant_undo[1])
plt.axis([-4, 4, -4, 4])
Out[12]:
In [34]:
def animate_rotation(obj, theta_rad): #doesn't fully work - shows all at one time
f1 = plt.figure()
for i in range(0,10):
plt.figure(1)
resultant = dot(rotMatrix(theta_rad/10*i),obj)
plt.plot(resultant[0], resultant[1])
plt.axis([-4, 4, -4, 4])
plt.show()
plt.pause(.5)
animate_rotation(rect, pi/6)
In [26]:
#Working with Scaling Matrices
def scaleMatrix2D(scaleFactor):
return [[scaleFactor, 0],[0, scaleFactor]]
scaleMatrix3 = scaleMatrix2D(3)
big_rect = dot(scaleMatrix3, rect)
plt.plot(rect[0],rect[1])
plt.plot(big_rect[0],big_rect[1])
plt.axis([-12, 12, -12, 12])
Out[26]:
In [21]:
#Working with Translation Matrices
def translate2D(dx, dy, v):
transMatrix = [[1, 0, dx],[0, 1, dy],[0, 0, 1]]
bottom_row = []
for i in range(0,len(v[0])):
bottom_row.append(1)
new_v = [v[0],v[1],bottom_row]
print new_v
return dot(transMatrix, new_v)
translated_rect = translate2D(2, 3, rect)
plt.plot(translated_rect[0],translated_rect[1])
plt.axis([-12, 12, -12, 12])
Out[21]:
In [ ]:
In [45]:
#SHEARING
plt.figure(1)
plt.axis([-4, 4, -4, 4])
k = 1
square = [[0,1,1,0,0],[0,0,1,1,0]]
shear1 = [[1,1],[0,1]]
shear2 = [[1,0],[1,1]]
shear3 = [[1,2*k],[0,1]]
shear4 = [[1,0],[2*k, 1]]
sq_s1 = dot(shear1, square)
sq_s2 = dot(shear2, square)
sq_s3 = dot(shear3, square)
sq_s4 = dot(shear4, square)
plt.figure(1)
plt.axis([-4, 4, -4, 4])
plt.plot(sq_s1[0], sq_s1[1])
plt.plot(sq_s2[0], sq_s2[1])
plt.show()
plt.figure(2)
plt.axis([-4, 4, -4, 4])
plt.plot(sq_s3[0], sq_s3[1])
plt.plot(sq_s4[0], sq_s4[1])
plt.show()