## Lab 9 Graphical Transformations II

#### Data and Transformations

Make sure that you have myfig.m on your computer somewhere and that MATLAB knows where it is. You can check by typing myfig in the Command Window. You will also need transform.m from the last lab as well.

From this data we are going to construct some transformations. The idea is this: instead of thinking of each segment as two points, we're going to think of it as a thin rectangle and then construct the matrix transformation that takes the large rectangle containing your name into this thin rectangle. So there will be a different transformation for each segment in your name. Copy this program into a new M-file and call it maketrans.m:

function T = maketrans(pts,scale)
% creates a (cell) array of transformations, one
% for each segment.  scale (default = 1/10) is
% the ratio of the height of the rectangle used for the
% segment compared to the height of the name

if (nargin==1), scale = 1/10; end   % set default scale if not entered

M = size(pts,2);   % number of points
L = max(pts(1,:)); % maximum x value (length of rectangle holding name)
H = max(pts(2,:)); % maximum y value (height of rectangle holding name)

% for each segment (x1,y1)-(x2,y2) we build a transformation of the form:
%  [A  B  E]  which takes the rectangle at the origin with length L, height H
%  [C  D  F]    to the rectangle along the segment from (x1,y1)-(x2,y2) with
%  [0  0  1]    height H*scale

numtrans = 0;   % keep track of the number of transformations
for i = 1:M-1
if ~any(isnan(pts(1:2,i:i+1)))    % check if there's a break, if not make the trans
x1 = pts(1,i); y1 = pts(2,i); x2 = pts(1,i+1); y2 = pts(2,i+1);
E = x1; F = y1;
A = (x2-x1)/L;   C = (y2-y1)/L;
lambda = scale/sqrt((x2-x1)^2+(y2-y1)^2);   % used to compute B & D
B = lambda*(y2-y1);
D = -lambda*(x2-x1);
if (A*D-B*C<0)     % check for a reflection
B = -B;
D = -D;
end
numtrans = numtrans + 1;
T{numtrans} = [A, B, E; C, D, F;0 0 1];   % store the transformation
end
end
To run this type T = maketrans(pts); in the Command Window. If after you do the next part you want to change the height of the rectangles, try T = maketrans(pts,0.2); or use another scale factor;

Here's the big idea for the fractal version of your name: Instead of drawing each of the segments in your name, when we go to draw a segment we instead use the transformation for that segment to write your whole name there instead.
This is what is call a substitution rule type of fractal generator.

The program to do this is pretty simple as we just have to loop through all the transformations apply each of them to your name and then plot it. Here's my take:

function fracname(pts,T)
% 'fractalizes' your name in pts with the list of transformations in T

N = length(T);   % number of transformations
for i = 1:N
y = transform(pts,T{i});   % apply the ith transform
plot(y(1,:),y(2,:))        % plot your transformed name
if (i==1), hold on, end    % make all plots on same graph
end
hold of
Copy this into a new M-file and save it as fracname.m

If you have all the files and values you need, you should be able to run this by typing fracname(pts,T)

If you get an error message make sure you have loaded pts from myfig, you have created the transformations T from maketrans and that you have the routine transform.m loaded.

#### Exercises

1. Get all this working and 'fractalize' your name. Print out the result.

2. (Extra) Modify fracname so that it (a) applies some basic or complex transformation (see Lab 8) to your fractalized name or (b) instead of using straight lines to draw a segment it uses some shape or pattern in some color.

3. (Extra Plus) Modify fracname so that it applies the substitution rule twice, i.e. each segment of your name is written using the fractal version of your name. (Not recommended for those we lots of segments in their name)