出处:
http://blog.china-pub.com/more.asp?name=reverie&id=5709
%============源程序============
function fern
% FERN 分形蕨
% 参考Michael Barnsley, Fractals Everywhere, Academic Press, 1993.
% 此版本不会自动停止,除非按停止
shg %显示图形窗口
clf reset %删除除位置和长度之外的所有图形属性,恢复到初始值
set(gcf,'color','white','menubar','none', ...
'numbertitle','off','name','Fractal Fern') %设置窗口为白色背景、无菜单
% 条、numbertitle关闭、标题为Fractal Fern
x = [.5; .5]; %初始点位置
h = plot(x(1),x(2),'.'); %在选定的位置画点,并令点的句柄为h
darkgreen = [0 2/3 0]; %点的颜色(RGB)
set(h,'markersize',1,'color',darkgreen,'erasemode','none'); %设置点的大
% 小为1,颜色为darkgreen,不可擦除
axis([-3 3 0 10]) %设置-3≤x1≤3,0≤x2≤10
axis off %不显示坐标轴
stop = uicontrol('style','toggle','string','stop', ...
'background','white'); %在窗口上画了一个按钮控件,标题为Stop,背景设为白色
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]; %A1~A4,b1~b3定义了四个仿射变换
cnt = 1; %计数
tic %计时
while ~get(stop,'value') %按下Stop键之后才停止绘画
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)); %改变点的位置
drawnow
cnt = cnt + 1;
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)') %按钮显示为Close,如果按下则调用关闭事件

