affine transformations
.
The most frequently used transformation has
A
=
0
.
85
0
.
04

0
.
04
0
.
85
¶
, b
=
0
1
.
6
¶
.
This transformation shortens and rotates
x
a little bit, then adds 1
.
6 to its second
component. Repeated application of this transformation moves the point up and to
the right, heading toward the upper tip of the fern. Every once in a while, one of
the other three transformations is picked at random. These transformations move
the point into the lower subfern on the right, the lower subfern on the left, or the
stem.
Here is the complete fractal fern program.
function fern
%FERN
MATLAB implementation of the Fractal Fern
%Michael Barnsley, Fractals Everywhere, Academic Press,1993
%This version runs forever, or until stop is toggled.
%See also: FINITEFERN.
shg
clf reset
set(gcf,’color’,’white’,’menubar’,’none’, ...
’numbertitle’,’off’,’name’,’Fractal Fern’)
x = [.5; .5];
h = plot(x(1),x(2),’.’);
darkgreen = [0 2/3 0];
set(h,’markersize’,1,’color’,darkgreen,’erasemode’,’none’);
axis([3 3 0 10])
axis off
stop = uicontrol(’style’,’toggle’,’string’,’stop’, ...
’background’,’white’);
drawnow
p = [ .85 .92 .99 1.00];
A1 = [ .85 .04; .04 .85]; b1 = [0; 1.6];
A2 = [ .20 .26; .23 .22]; b2 = [0; 1.6];
A3 = [.15 .28; .26 .24]; b3 = [0; .44];
A4 = [ 0
0 ;
0
.16];
cnt = 1;
16
Chapter 1.
Introduction to MATLAB
tic
while ~get(stop,’value’)
r = rand;
if r < p(1)
x = A1*x + b1;
elseif r < p(2)
x = A2*x + b2;
elseif r < p(3)
x = A3*x + b3;
else
x = A4*x;
end
set(h,’xdata’,x(1),’ydata’,x(2));
cnt = cnt + 1;
drawnow
end
t = toc;
s = sprintf(’%8.0f points in %6.3f seconds’,cnt,t);
text(1.5,0.5,s,’fontweight’,’bold’);
set(stop,’style’,’pushbutton’,’string’,’close’, ...
’callback’,’close(gcf)’)
Let’s examine this program a few statements at a time.
shg
stands for “show graph window.” It brings an existing graphics window forward,
or creates a new one if necessary.
clf reset
resets most of the figure properties to their default values.
set(gcf,’color’,’white’,’menubar’,’none’, ...
’numbertitle’,’off’,’name’,’Fractal Fern’)
changes the background color of the figure window from the default gray to white
and provides a customized title for the window.
x = [.5; .5];
provides the initial coordinates of the point.
h = plot(x(1),x(2),’.’);
plots a single dot in the plane and saves a
handle
,
h
, so that we can later modify
the properties of the plot.
darkgreen = [0 2/3 0];
1.3.
Fractal Fern
17
defines a color where the red and blue components are zero and the green component
is twothirds of its full intensity.
set(h,’markersize’,1,’color’,darkgreen,’erasemode’,’none’);
makes the dot referenced by
h
smaller, changes its color, and specifies that the image
of the dot on the screen should not be erased when its coordinates are changed. A
record of these old points is kept by the computer’s graphics hardware (until the
figure is reset), but
Matlab
itself does not remember them.
You've reached the end of your free preview.
Want to read all 55 pages?