Create a Figure
1. Draw a figure made of straight lines and determine the coordinates
of the points. It is best if the figure lives near the origin.
2. Create an M-file in MATLAB (call it myfig.m) which creates
a list of the points you used. It should start like
pts = [
and each line after that should be the coordinates of the points
you found from Step 1. Since MATLAB will connect these points one
to the next, if you want a break enter NaN NaN
as a point where you want a break. End the list with
];
and save the file (as myfig.m)
An example of a boring square:
pts = [
0 0
1 0
1 1
0 1
0 0
];
Note if you want a closed figure you have to connect back
to the first point.
3. Test it. Go to the command window and type
myfig
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. 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:
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
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.
1. Applying a transformation T to our figure. 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. (I've supplied code below).
After you have the function (call it transform.m) written, 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
hold off/on tells MATLAB to erase or not erase a drawing before
doing the next plot command.
Here's my take for the function:
function newpts = transform(pts,T)
% applies the (homogeneous) transformation T to the points in pts
N = size(pts,1); % number of points
pts = [pts,ones(N,1)]; % add 1s as 3rd coordinate
newpts = T*pts'; % apply T (turn pts around first)
newpts = newpts(1:2,:)'; % strip off 3rd coordinate and turn around
2. 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];
(Note: th must be in radians: radians = degrees*pi/180)
Scaling: T = [sx 0 0;0 sy 0; 0 0 1];
Reflection: T = [1 0 0;0 -1 0;0 0 1];
Shear: T = [1 s 0; 0 1 0; 0 0 1];
3. 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];
Assignment
1. Get all the functions you need to do the work working.
2. Construct a non-basic transformation T
(Explain what you think it does)
Apply it to your figure and plot the results. It helps
if you plot the original figure and the transformed
figure on the same graph. Print out the result.
3. Using the same (or a new) transformation, apply it
over and over again to your figure (use a for
loop), plotting all the results. Print out the result.
4. (Challenge) Find a non-basic transformation that is
periodic, i.e. after applying it M times you get
your original image back.
5. (Challenge) Given a non-basic transformation construct
a figure such that the image of the figure under the transformation
gives back the original figure.
5. (Challenge) Create two (or more transformations) and
apply them in order to your image, plotting the
intermediate results. Can you cover the entire
plane with some set of transformations?
Fun
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