GS 2006/Math - Collins

Lab 11
Fractals from IFS and MRCM


Setup

1. First you must have your list of corners from your 3-5 quadrilaterals.

2. If you haven't formed your transformations yet, you need to figure them out. The easy way to do that is to form the matrix A as follows:

   A = [0 1 1; 0 0 1; 0 0 1]
which represents the 3 corners of the square (0,0), (1,0), (1,1). Then for each quadrilateral, form the matrix B as follows:
   B = [x1 x2 x3; y1 y2 y3; 0 0 1]
where you put the coordinates you found for the corners in for the values (x1,y1), (x2,y2), (x3,y3). Then to find the transformation you just solve XA = B for the transformation X, by X = B*inv(A);. Repeat this for each of your quadrilaterals and save the results in different matrices (X, Y, Z, W, U, etc)

3. Now you have your transformations, so create a M-file called mytrans.m which contains them. The format should be like this, with the names of transformations T{1}, T{2}, etc.

   T{1} = [1/2 0 0;0 1/2 0;0 0 1];
   T{2} = [1/2 0 1/2;0 1/2 0;0 0 1];
   T{3} = [1/2 0 1/4;0 1/2 sqrt(3)/4;0 0 1];
Next, copy these 2 functions into the M-file editor and save them:

mrcm.m

function mrcm(pts,N,T)
% routine for MRCM (multiple reduction copy machine)
% pauses between iterations, starts with any image
% an applies the various transformations in N cycles

k = length(T);   % number of transformations

np = [pts; ones(1,size(pts,1))];   % copy of points (homog)
plot(np(1,:),np(2,:))
axis equal, axis off
pause
for i = 1:N
    xp = [];
    for j = 1:k
        p = T{j}*np;   % apply transformation
        xp = [xp [NaN NaN 1]', p];    % add image to other copies
    end
    np = xp;
    plot(np(1,:),np(2,:))
    axis equal, axis off
    pause
end
ifs.m
function ifs(N,T)
% does N iterations of the IFS defined
% by the transformations passed in
                                                                                
K = length(T);     % number of transformations
X = zeros(3,N);    % allocate space
X(:,1) = [0.5; 0.5; 1]; % starting point

for i = 1:N-1
   r = floor(K*rand)+1;    % random number in 1..K
   X(:,i+1) = T{r}*X(:,i);
end

plot(X(1,:),X(2,:),'.','MarkerSize',1)

axis off

Running Fun

Both these functions use your transformations to make fractal images.
MRCM
The MRCM (Multiple-Reduction-Copy-Machine) applies each of your transformations to some original image and then applies the transformations again to the resulting image. This is repeated many times to make a multi-level fractal. To run MRCM, you first need to create an initial image. It can be anything simple, like a small box. Store it in an array called image. For example, a box would be entered as
   image = [0 0; 1 0; 1 1; 0 1; 0 0]';  
Next you need your transformations, so just type mytrans. Now, to run MRCM type:
   mrcm(image,4,T)
where 4 is the number of levels you want it to run (you shouldn't try a level more than 6 or 7 as MATLAB may crash. The function pauses after each iteration, so you need to hit the space bar to get it to continue. If you want it to stop, type ctrl-C.
IFS
To run the IFS function, you just need the transformations and then type
   ifs(1000,T)
where 1000 is the number of points you want it to plot. You should increase the number of points to about 100000 to see the fine details.

Exercises

1. Get both MRCM and IFS running with your transformations. Make a printout of the results of each showing some level of detail.

2. Pick on of the transformations below and try either MRCM or IFS with them. Again produce a printout.

Turn in your 3 pictures and the file containing the transformations that you created.

Other transformations

Copy each to a separate file or into the command window Fern
clear T
T{1} = [0 0 0; 0 0.16 0; 0 0 1];
T{2} = [0.85 0.04 0; -0.04 0.85 1.6; 0 0 1];
T{3} = [0.2 -0.26 0; 0.23 0.22 1.6; 0 0 1];
T{4} = [-0.15 0.28 0; 0.26 0.24 0.44; 0 0 1]; 
Fractal Tree
clear T
T{1} = [0 0 0; 0 0.5 0;0 0 1];
T{2} = [0.42 -0.42 0; 0.42 0.42 0.2;0 0 1];
T{3} = [0.42 0.42 0; -0.42 0.42 0.2; 0 0 1];
T{4} = [0.1 0 0; 0 0.1 0.2; 0 0 1];
Koch curve
clear T
T{1} = [1/3 0 0; 0 1/3 0; 0 0 1];
T{2} = [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];
T{3} = [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];
T{4} = [1/3 0 2/3; 0 1/3 0; 0 0 1];
Sierpinski Triangle
clear T
T{1} = [1/2 0 0;0 1/2 0;0 0 1];
T{2} = [1/2 0 1/2;0 1/2 1/2; 0 0 1];
T{3} = [1/2 0 1/2; 0 1/2 0; 0 0 1];
Sierpinski Square
clear T
T{1} = [1/3 0 0; 0 1/3 0; 0 0 1];
T{2} = [1/3 0 2/3; 0 1/3 0; 0 0 1];
T{3} = [1/3 0 1/3; 0 1/3 1/3; 0 0 1];
T{4} = [1/3 0 0; 0 1/3 2/3; 0 0 1];
T{5} = [1/3 0 2/3; 0 1/3 2/3; 0 0 1];
Sierpinski Carpet
clear T
T{1} = [1/3 0 0;0 1/3 0;0 0 1];
T{2} = [1/3 0 1/3;0 1/3 0; 0 0 1];
T{3} = [1/3 0 2/3;0 1/3 0; 0 0 1];
T{4} = [1/3 0 0;0 1/3 1/3; 0 0 1];
T{5} = [1/3 0 2/3;0 1/3 1/3; 0 0 1];
T{6} = [1/3 0 0;0 1/3 2/3;0 0 1];
T{7} = [1/3 0 1/3;0 1/3 2/3;0 0 1];
T{9} = [1/3 0 2/3;0 1/3 2/3;0 0 1];
Twin Christmas Tree
clear T
R = [0 -1 0;1 0 0; 0 0 1];   % 90 degree rot
T{1} = [1/2 0 1/2; 0 1/2 0;0 0 1]*R;
T{2} = [1/2 0 1/2;0 1/2 1/2;0 0 1]*inv(R);
T{3} = [1/2 0 1/4;0 1/2 sqrt(3)/4;0 0 1];
3-fold Dragon
clear T
R = [0 1 0;-1 0 0; 0 0 1];   % 270 degree rot
T{1} = [2/3 0 0;0 2/3 2/3;0 0 1]*R;
T{2} = [2/3 0 0;0 2/3 1;0 0 1]*R;
T{3} = [2/3 0 1/3;0 2/3 5/6;0 0 1]*R;
Cantor Maze
clear T
R = [0 -1 0;1 0 0;0 0 1];  % 90 degree rot
T{1} = [1/3 0 1/3;0 1 0;0 0 1]*R;
T{2} = [1/3 0 1/3;0 1/3 2/3;0 0 1];
T{3} = [1/3 0 2/3;0 1 0;0 0 1]*[-1 0 0;0 1 0;0 0 1]*R;
Mail: ccollins@math.utk.edu