## Lab 8 Graphical Transformations I

#### Make and Draw Your Name

In MATLAB, open a New M-File. This should open a new window in the M-File editor. In this file we are going to create a 2 by M matrix of all the points you found for your name. Since when MATLAB draws your name, it will connect consecutive points, you can enter NaN, NaN to separate the different segments. For example if my name was Iv my file would look like this:
```pts = [
0 0
0 1
NaN NaN
0.5 1
1 0
1.5 1]';
```
Note that we end it with a transpose (') to make it 2 by M and with a semicolon so that it won't be printed out.

Once you have all the points typed in, save it as myfig.m.

Now to test it. Go to the command window and type myfig. This should load your data into MATLAB. If you have any problems, it may be that either you didn't save it or that you saved it where MATLAB can't find it. If you didn't save it, go back and save it. If MATLAB can't find it, you'll have to change your workspace (use the ... button at the top of the command window).

Now we can plot your name using

```   plot(pts(1,:),pts(2,:))
```
You should see your figure. If you don't like what you see, go back and change your myfig.m file. If you want to get fancy, you can change the color and line width around using
```
plot(pts(1,:),pts(2,:),'Color',[0.3 0.8 0.2],'LineWidth',2)
```
where the numbers after the keyword Color are the RGB values (0<=v<1) and the number after LineWidth is the width. If you repeatedly use a certain plot command, you might want to make a function out of it, like this:
```   function myplot(pts)
% my hairy plotter
plot(pts(1,:),pts(2,:),'Color',[0.3 0.8 0.2],'LineWidth',2)
hold on
plot(pts(1,:),pts(2,:),'k*')
hold off
```
Copy this to a new M-file and save as myplot.m, you run it by typing myplot(pts) in the Command Window.

Once you have myfig.m the way you want it, you should email the file to yourself or save it by some other means as we will be using it again tomorrow.

#### Create Transformations

We are using 3x3 matrices as our transformations (we are using the homogeneous coordinate system). Below are instructions/templates for various transformations as well as code on how to apply them.
##### Applying a transformation
Suppose we have a 3x3 matrix T we want to apply to our name. Since we are doing this over and over again, it is best to write it as an M-file function. The input should be our list of points pts and the transformation matrix T and the output should be the transformed list of points. Since we are using homogeneous coordinates, the function will need to add ones to our list of points (see ones) and then strip off the extra data before returning the points. Copy this code into transform.m
```function newpts = transform(pts,T)
% applies the (homogeneous) transformation T to the points in pts

M = size(pts,2);         % number of points
pts = [pts;ones(1,M)];   % add 1s as 3rd coordinate
newpts = T*pts;          % apply T
newpts = newpts(1:2,:);  % strip off 3rd coordinate
```
After you have saved the function, try this
```
T = [2 0 0; 0 2 0; 0 0 1];
myfig
np = transform(pts,T);
plot(pts(1,:),pts(2,:))
hold on
plot(np(1,:),np(2,:),'r')
hold off
```
You should see your name twice, once in blue and once twice the original size in red. The commands hold off/on tells MATLAB to erase or not erase a drawing before doing the next plot command.
##### Basic Transformations
Below are templates for the transformations we discussed in class; you'll need to either put in values for the variables, or set the values of the variables before you use the template.

Translation: T = [1 0 tx; 0 1 ty; 0 0 1];
Rotations: T = [cos(th) -sin(th) 0; sin(th) cos(th) 0; 0 0 1];
Scaling: T = [sx 0 0;0 sy 0; 0 0 1];
Reflection: T = [1 0 0;0 -1 0;0 0 1];

##### Complex Transformations
You can build any type of transformation you want by simply combining Basic Transformations. We combine them simply by multiplying them together. So as you build Basic Transformations, give them different names and then you can build any sort of complex transformation. All these transformations are invertible, so you can use inv(T) to get the opposite transformation. Here's an example:
```   T1 =[1 0 1; 0 1 -1; 0 0 1];
T2 = [cos(pi/3) -sin(pi/3) 0; sin(pi/3) cos(pi/3) 0; 0 0 1];
T3 = [2 0 0;0 0.5 0; 0 0 1];
CT = T3*inv(T1)*T2*T1;
```
This translates (x+1,y-1), rotates 60 degrees, translates back (x-1,y+1) and then scales (x*2, y*0.5)

If you are too lazy to make a transformation this way you can create random transformations by Tr = [randn(2,3); 0 0 1];

#### Exercises

1. Plot your name and copies of your name rotated 45, 90, 135, 180, 225, 270 and 315 degrees (pi/4, pi/2, 3*pi/4, pi, 5*pi/4, 3*pi/2, 7*pi/4). Plot them all on the same graph and print out the result.

2. Create a transformation representing a translation and a rotation (you can choose the values and the order). Plot your name and copies of your name after the transformation has been applied 1, 2, 3 and 4 times.

3. Construct 3 different basic transformations of 3 different types. Construct the 6 different complex transformations formed by applying the 3 basics in all the possible different orders, for example T123 = T1*T2*T3, T132 = T1*T3*T2, etc. Plot your name and copies of your name under each of these 6 transformations.

#### Fun

##### Saving Images
If you want to save an image to use on a webpage or wherever, just select Export under the file menu and select the name and file type you want to save it as. A jpeg is usually a good choice.
##### Movies
MATLAB also has the ability to record pictures you draw and then play them back, i.e. make a movie.
After you figure out what pictures you want in your movie, you need to figure out what screen (or axes) will hold all your pictures. Figure out the x-range and the y-range that can contain every picture you draw. Create a vector of these values:
myaxis = [xleft xright ybot ytop];
Now you are ready to make a movie. The steps are pretty basic, first you create the space for the movie, then you create images frame by frame and save them. It looks like this:
```   n = 24;   % number of frames (you can use more or less)
axis(myaxis)
M = moviein(n);
for j=1:n
% put your plot commands here, before the axis command
axis(myaxis)
M(:,j) = getframe;    % captures whatever is on the graph
end
```
The command axis off removes the axis markings.

To play your movie, you just type movie(M) or movie(M,10) or movie(M,-10). The first version plays it once, the second 10 times, the third plays it forward and backward.

Mail: ccollins@math.utk.edu