# This one shows specific matrix transformations in 2D...skew, rotation, etc.
N=2
pretty_print(html('Notice what happens to the ideas of contraction, expansion, left and right when the scalar is less than 1'))
@interact
def _(transform=('Select:',list(['Identity', 'Reflect x-axis', 'Reflect y-axis', 'Rotate Origin', 'Skew Right','Skew Left', 'Skew Up', 'Skew Down','Horizontal Contraction','Horizontal Expansion','Vertical Contraction','Vertical Expansion','Rotation'])),scalar=slider(1/10,47/10,1/10,2), move_box =checkbox(default=False) ):
A = identity_matrix(N)
if transform=='Reflect x-axis':
A = matrix([[1,0],[0,-1]])
show(A)
if transform=='Reflect y-axis':
A = matrix([[-1,0],[0,1]])
show(A)
if transform=='Rotate Origin':
A = matrix([[-1,0],[0,-1]])
show(A)
if transform=='Skew Right':
A = matrix([[1,scalar],[0,1]])
show(A)
if transform=='Skew Left':
A = matrix([[1,-scalar],[0,1]])
show(A)
if transform=='Skew Up':
A = matrix([[1,0],[scalar,1]])
show(A)
if transform=='Skew Down':
A = matrix([[1,0],[-scalar,1]])
show(A)
if transform=='Horizontal Contraction':
A = matrix([[1/scalar,0],[0,1]])
show(A)
if transform=='Horizontal Expansion':
A = matrix([[scalar,0],[0,1]])
show(A)
if transform=='Vertical Contraction':
A = matrix([[1,0],[0,1/scalar]])
show(A)
if transform=='Vertical Expansion':
A = matrix([[1,0],[0,scalar]])
show(A)
if transform=='Rotation':
angle = scalar # radians
c = cos(angle)
s = sin(angle)
A = matrix([[c,-s],[s,c]])
show(A)
StartBox = line2d([(0,0),(0,1)],thickness=6)+line2d([(0,1),(1,2)],thickness=6)+line2d([(1,2),(2,1)],thickness=6)+line2d([(2,1),(2,0)],thickness=6)+line2d([(2,0),(0,0)],thickness=6)
BoxCorners = matrix([[0, 0, 1, 2, 2],[0, 1, 2, 1, 0]])
if move_box:
StartBox = line2d([(1,1),(1,2)],thickness=6)+line2d([(1,2),(2,3)],thickness=6)+line2d([(2,3),(3,2)],thickness=6)+line2d([(3,2),(3,1)],thickness=6)+line2d([(3,1),(1,1)],thickness=6)
BoxCorners = matrix([[1, 1, 2, 3, 3],[1, 2, 3, 2, 1]])
NewCorners = (A*BoxCorners).transpose() # switch result matrix to rows for easier extraction of A*B columns.
EndBox = Graphics()
for col in range(5):
c1 = NewCorners[Mod(col,5)]
c2 = NewCorners[Mod(col+1,5)]
EndBox += line2d([c1,c2],thickness=5, color="red")
# EndBox += line2d([c1,c2],thickness=5, rgbcolor=(0.5,1,0.5))
show(StartBox+EndBox,aspect_ratio=True)
|
Notice what happens to the ideas of contraction, expansion, left and right when the scalar is less than 1
Click to the left again to hide and once more to show the dynamic interactive window
|