MA 310 Lab Worksheet Manipulating Graphs with Matrices in 2-Space There are many ways in which graphs can be manipulated: a graph can be reduced or enlarged in size, reflected about a line, sheared, or rotated. All of these operations can be performed by applying a matrix to the coordinates of a graph. These matrices are called transformation matrices. In this lab, you will write some m-files in Matlab to transform a graph and animate an image with a movie. 1. First, we need a graph. We'll take a simple one like the letter A. As you read through this, you should start writing m-files to carry out the plots. In what follows, lines starting with >> are lines for you to type on the command line, but don't type the >>. The letter A can be drawn by connecting a set of vertices. Enter the following in Matlab: >>x=[0,2,3,5,4.25,3.5,1.4,1.75,3.25,2.5,1.75,1.4,.75,0] ; >>y=[0,3,3,0,0,1,1,1.5,1.5,2.5,1.5,1,0,0] ; Create the matrix M=[x;y], a matrix of 2 rows, 14 columns. In what follows, the x coordinates of the letter A are in row 1 of M. You can see these by typing M(1,:), which is row 1, and every column. The y-coordinates are M(2,:). To plot the letter in Matlab, you can use the command >>plot(M(1,:),M(:,2)); Or, to draw the letter in Matlab and color it red, use the command >> fill(M(1,:), M(2,:),'r'); >> hold on; % Keep the graph so more plots can be applied If you like, you can change the axes by adding something like >> axis([-1 6 -1 5]); % this is the window [minx maxx miny maxy] Dilation and Contraction Transformations To reduce the size of letter A, suppose we want to reduce the x direction by 0.5, and the y direction by 0.8. Then the 2x2 transformation matrix T1=[0.5,0; 0,0.8] will provide the reduction. Check some entries of W1 here to see how this matrix changes the x and y values in M. >> W1=T1*M; >> fill(W1(1,:),W1(2,:),'g'); % Draw the reduced A in green. Note here that W1(1,:) gives the x-coordinates in row1 of W1, and W1(2,:) gives the y-coordinates in W1. Shear Transformations The letter A can also be sheared, something that is used to express fonts in italics. A shearing transformation can be applied to obtain many different results. In this example, we'll shear the letter A by 30 ˚to the right. We'll start a new figure. Enter the following in Matlab. >>figure; >>fill(M(1,:), M(2,:),'r'); >> hold on; >> theta=pi/6; >> T2=[1,cos(theta); 0,1]; >> W2=T2*M; >> fill(W2(1,:),W2(2,:),'b'); To see what is happening, manually multiply matrix T2 by the first few columns in M to see how T2 changes the x-coordinates, but does not change the y-coordinates. You can change T2 to see how to perform other shears. Translation Transformations To translate an image horizontally or vertically, we only need to add a constant to the x-coordinates and/or y-coordinates. For example, to move the letter A one unit to the right and up 2 units, we need a matrix of 14 columns of 1's and 14 columns of 2's. Enter the following in Matlab: >>figure; >>fill(M(1,:), M(2,:),'r'); >>axis([-2 6 -2 6]); >> hold on; >>L=[ones(1,14);2*ones(1,14)]; >> W3=M+L; % shift M 1 unit to right, 2 units up. >> fill(W3(1,:),W(2,:),'g'); Rotation Transformations An image like letter A can be rotated in many ways; for example about a point or line. See your calculus textbook for a derivation of a rotation matrix. For simplicity, we'll apply a rotation about the origin. >>figure; >>fill(M(1,:), M(2,:),'r'); >> hold on; >> theta=pi/6; % The rotation matrix is >>T3=[cos(theta), -sin(theta); sin(theta), cos(theta)]; >>W3=T3*M; >>fill(W3(1,:),W3(2,:),'c'); % Let's make one more rotation >>theta=pi; >>T3=[cos(theta), -sin(theta); sin(theta), cos(theta)]; >>W3=T3*M; >>fill(W3(1,:),W3(2,:),'c'); We can continue with a series of rotations to create a rotation movie. Open a new m-file in Matlab and copy the following code. function []=movie2; % Make a movie of rotating the letter A about the origin. % Make up the x,y coordinates of the letter A, or any other figure x=[0,2,3,5,4.25,3.5,1.4,1.75,3.25,2.5,1.75,1.4,.75,0]; y=[0,3,3,0,0,1,1,1.5,1.5,2.5,1.5,1,0,0]; M=[x;y]; fig=figure; axis([-8 8 -8 8]); set(gca,'nextplot','replacechildren'); mov=avifile('myname.avi','fps',10,'compression','None'); mov.quality=100; % Loop through and create 24 frames to make the movie for k=1:24 theta=k*pi/6; T=[cos(theta), -sin(theta); sin(theta), cos(theta)]; W=T*M; fill(W(1,:),W(2,:),'b'); axis([-8 8 -8 8]); F(k)=getframe(fig); mov=addframe(mov,F(k)); end; mov=close(mov); % save your movie myname.avi to a folder disp('movie created'); clf; disp('playing movie..Press Ctrl C to stop'); %mov=aviread('myname.avi'); movie(F,5,10) % play the movie 5 time at 10 FPS