Preliminaries
Start MATLAB (from Start Menu).
Arrange your windows so you can see MATLAB and this webpage.
Finish Your Homework
If you haven't finished finding the transformations for your
homework, you need to finish it now. You can use MATLAB to
help. (If you did it already, you can follow along to check
or jump on down to the next section).
Here's what you should have: 1 Big Triangle, 3-5 Small Triangles
You should know the vertices of all the triangles.
To find the transformation from the Big Triangle to a Small Triangle:
Let the Big Triangle have vertices ABC where A(x1,y1), B(x2,y2), C(x3,y3)
Let the Small Triangle have vertices A'B'C' where A'(x1',y1'), etc.
We want to find values a, b, c, d, e, f so that
x' = a x + b y + c and y' = d x + e y + f
transforms the Big Tri to the Small Tri, in particular A to A', etc.
We get the following equations for a-f:
a x1 + b y1 + c = x1' d x1 + e y1 + f = y1'
a x2 + b y2 + c = x2' d x2 + e y2 + f = y2'
a x3 + b y3 + c = x3' d x3 + e y3 + f = y3'
Note: you will have values for x1,x2,x3,y1,..
Using MATLAB to solve these systems (use the values not the variable names):
M = [x1 y1 1; x2 y2 1; x3 y3 1];
b1 = [x1';x2';x3'];
b2 = [y1';y2';y3'];
M\b1 % these are the values for a, b, c
M\b2 % these are the values for d, e, f
For the rest of the small triangles, you only need to change
the matrices b1 and b2 to find the new values for a-f.
Make a Cool Image
We are going to create a function that uses the transformations
you came up with to create a (hopefully) cool image.
The function will take the number of points to use (N) as
input and do the iterations and produce the graph. As
always you can write it yourself, use the hints I give, or copy
the program from below.
You will have to enter your own transformation matrices.
Once you have the program ready, try it out. Start with 1000 points
and then increase. You can also go in and change the probabilities
to see if that makes any difference.
Program Information
For efficiency, the program will create a 3xN matrix (X) which will
store all the points it produces. So (X(1,15),X(2,15)) is the 15th
point. We will use random numbers (produced by rand) to decide
which transformation to use.
Hints for the program:
Build your transformations:
T1 = [a b c; d e f; 0 0 1];
T2 = ...
Allocate space: X = zeros(3,N);
Starting value: X(:,1) = [0.5;0.5;1];
Do the loop N-1 times
Compute the random number r = rand; then
use a series if-elseif-elseif-else-end to decide which transformation
to use. Eg. if you had 3 possible transformations you might have
if (r < 0.3)
...
elseif (r < 0.7)
...
else
...
end
Note how the check values increase so the first has probability
0.3, the second 0.4 and the last 0.3 (1-0.3-0.4).
For the transformation multiplication store the result in the next
spot, i.e. X(:,i+1) = T*X(:,i);
When you are all done, plot the points:
plot(X(1,:),X(2,:),'.','MarkerSize',1)
This plots it using small dots. You can use other shapes and
sizes.
trifrac.m
function trifrac(N)
% uses the transformation defined below with random selection to
% produce N points in a hopefully pleasing pattern
T1 = [ FILL IN YOUR DATA ];
T2 = [
T3 = [
...
X = zeros(3,N); % allocate space
X(:,1) = [0.5;0.5;1]; % starting point
for i = 1:N-1
r = rand; % get a random value (in range [0,1))
% Change the number of cases and the probabilities for your data
if (r<0.3)
X(:,i+1) = T1*X(:,i);
elseif (r<0.7)
X(:,i+1) = T2*X(:,i);
else
X(:,i+1) = T3*X(:,i);
end
end
plot(X(1,:),X(2,:),'.','MarkerSize',1)
axis off
Exercise
Get your program working and play around with the probabilities
and the transformations until you get a nice picture. Print
it out and turn it in.
Play Time
Here are some other sets of transformations and probabilites, you
can try:
Fern
T1 = [0 0 0; 0.16 0 0; 0 0 1];
T2 = [0.85 0.04 -0.04; 0.85 0 1.6; 0 0 1];
T3 = [0.2 -0.26 0.23; 0.22 0 1.6; 0 0 1];
T4 = [-0.15 0.28 0.26; 0.24 0 0.44; 0 0 1];
probabilities 0.01, 0.85, 0.07, 0.07
Koch curve
T1 = [1/3 0 0; 0 1/3 0; 0 0 1];
T2 = [1/3*cos(pi/3) -1/3*sin(pi/3) 1/3; 1/3*sin(pi/3) 1/3*cos(pi/3) 0; 0 0 1];
T3 = [1/3*cos(pi/3) 1/3*sin(pi/3) 1/2; -1/3*sin(pi/3) 1/3*cos(pi/3) sqrt(3)/6; 0 0 1];
T4 = [1/3 0 2/3; 0 1/3 0; 0 0 1];
probabilities 1/4, 1/4, 1/4, 1/4
Sierpinski Triangle
T1 = [1/2 0 0;0 1/2 0;0 0 1];
T2 = [1/2 0 1/2;0 1/2 1/2; 0 0 1];
T3 = [1/2 0 1/2; 0 1/2 0; 0 0 1];
probabilites 1/3, 1/3, 1/3
Sierpinski Square
T1 = [1/3 0 0; 0 1/3 0; 0 0 1];
T2 = [1/3 0 2/3; 0 1/3 0; 0 0 1];
T3 = [1/3 0 1/3; 0 1/3 1/3; 0 0 1];
T4 = [1/3 0 0; 0 1/3 2/3; 0 0 1];
T5 = [1/3 0 2/3; 0 1/3 2/3; 0 0 1];
probabilities 1/5, 1/5, 1/5, 1/5, 1/5
Create More
I wrote a couple of small programs that let you draw triangles
and then creates the data for the transformation matrices.
Save these in the MATLAB work folder: drawit.m trimake.m
To run it just type trimake, follow the directions
and it will display the first 2 rows for each transformation of
each small triangle (the 3rd row is always 0 0 1). Put these
transformations into trifrac, assign some probabilities and see
how it looks.
Mail: ccollins@math.utk.edu