Fractal
MATLAB code for generating some fractals recursively.
Recursive Square 1
function recursion2
%recursion2
global COLORMAP
n=7; %depth of recursion
COLORMAP=pink(n);
x=1;
y=1;
wide=1;
high=1;
rect(x,y,wide,high,n);
axis([0 2 0 2])
axis equal off
return
function rect(x,y,wide,high,n)
global COLORMAP
if n>0
xx=[x-wide/2 x+wide/2 x+wide/2 x-wide/2];
yy=[y-high/2 y-high/2 y+high/2 y+high/2];
rect(x-wide/2,y-high/2,wide/2,high/2,n-1);
rect(x-wide/2,y+high/2,wide/2,high/2,n-1);
rect(x+wide/2,y-high/2,wide/2,high/2,n-1);
rect(x+wide/2,y+high/2,wide/2,high/2,n-1);
patch(xx,yy,n*ones(size(yy))*0,'k',...
'facecolor',COLORMAP(n,:),'edgecolor','none');
end
return
Recursive Pentaflake
function recursion6
global COLORMAP
golden = 1.618033988749894848204586; %golden ratio
n=4; %depth of recursion
COLORMAP=copper(4);
h=1;
pentaflake(n,0,0,36/180*pi,h);
axis([-3 3 -3 3])
axis equal off
function pentaflake(n,x,y,theta,len)
global COLORMAP
if n>0
golden = 1.618033988749894848204586;
d=len/golden;
h=len;
t=linspace(0+theta,2*pi+theta,5+1);
%generate points around middle pentagon
[offx,offy]=pol2cart(t+18/180*pi,d*(1+golden));
for k=1:5
patch(h*sin(t)+offx(k)+x,h*cos(t)+offy(k)+y,0*cos(t)+4-n,'r',...
'facecolor','none','edgecolor',COLORMAP(n,:),'linewidth',n);
pentaflake(n-1,x+offx(k),y+offy(k),theta,len/(golden+1));
end
patch(h*sin(t)+offx(k)+x,h*cos(t)+offy(k)+y,0*cos(t)+4-n,'r',...
'facecolor','none','edgecolor',COLORMAP(n,:),'linewidth',n);
pentaflake(n-1,x,y,pi+theta,len/(golden+1));
end
return
Recursive Hexaflake 1
function recursion7
global COLORMAP
n=4; %levels of recursion
COLORMAP=1-winter(n) ;
hexaflake(n,0,0,1);
axis([-3.5 3.5 -3.5 3.5]);
axis equal off
function hexaflake(n,x,y,len)
global COLORMAP
if n>0
d=len;
t=linspace(0,2*pi,6+1);
[offx,offy]=pol2cart(t+30*pi/180,d*2); %generate points around middle hexagon
for k=1:6
patch(d*sin(t)+offx(k)+x,d*cos(t)+offy(k)+y,0*cos(t)-n,'r',...
'facecolor','none','edgecolor',COLORMAP(n,:),'linewidth',n);
hexaflake(n-1,x+offx(k),y+offy(k),d/3);
end
patch(d*sin(t)+x,d*cos(t)+y,0*cos(t)-n,'r',...
'facecolor','none','edgecolor',COLORMAP(n,:),'linewidth',n);
hexaflake(n-1,x,y,d/3);
end
return
Recursive Hexaflake 2
function recursion8
%hexaflake 2
global COLORMAP
n=4; %levels of recursion
COLORMAP=summer(n);
h=1;
t=linspace(0,2*pi,6+1);
hexaflake(n,0,0,h);
axis([-3 3 -3 3]);
axis equal off
function hexaflake(n,x,y,len)
global COLORMAP
if n>0
d=len;
t=linspace(0,2*pi,6+1);
[offx,offy]=pol2cart(t,d*2*0.866); %generate points around middle hexagon
for k=1:6
patch(d*sin(t)+offx(k)+x,d*cos(t)+offy(k)+y,0*cos(t)-n,'r'...
,'facecolor','none','edgecolor',COLORMAP(n,:),'linewidth',n);
hexaflake(n-1,x+offx(k),y+offy(k),d/3);
end
patch(d*sin(t)+x,d*cos(t)+y,0*cos(t)-n,'r',...
'facecolor','none','edgecolor',COLORMAP(n,:),'linewidth',n);
hexaflake(n-1,x,y,d/3);
end
return
Recursive Square 2
function recursion10
%Recursion of simple square
global COLORMAP
n=7; %level of recursion
COLORMAP=copper(n);
rect(0,0,1,1,n);
rect(1,0,1,1,n);
axis([-1.5 1.5 -1 2]);
axis equal off
function rect(x,y,wide,high,n)
global COLORMAP
if n>0
xx=[x-wide/2 x+wide/2 x+wide/2 x-wide/2];
yy=[y-high/2 y-high/2 y+high/2 y+high/2];
patch(xx,yy,'k',...
'facecolor',1-COLORMAP(n,:),...
'edgecolor',COLORMAP(n,:),'linewidth',1);
rect(x-wide/4,y-high/4+high,wide/2,high/2,n-1);
rect(x+wide/4,y-high/4+high,wide/2,high/2,n-1);
end
return
Recursive Square 3
function recursion11
%Recursion of simple square
global COLORMAP
n=8; %level of recursion
COLORMAP=copper(n);
rect(0,0,1,1,n);
axis([-0.75 0.75 -0.75 0.75]);
axis equal off
function rect(x,y,wide,high,n)
global COLORMAP
if n>0
xx=[x-wide/2 x+wide/2 x+wide/2 x-wide/2];
yy=[y-high/2 y-high/2 y+high/2 y+high/2];
patch(xx,yy,'k',...
'facecolor',1-COLORMAP(n,:),...
'edgecolor',COLORMAP(n,:),'linewidth',1);
rect(x-wide/4,y-high/4,wide/2,high/2,n-1);
rect(x+wide/4,y-high/4,wide/2,high/2,n-1);
end
return
Recursive Square 4
function recursion12
%Recursion of simple square
global COLORMAP
n=8; %level of recursion
COLORMAP=1-copper(n);
rect(0,0,1,1,n);
axis([-0.75 0.75 -0.75 0.75]);
axis equal off
function rect(x,y,wide,high,n)
global COLORMAP
if n>0
xx=[x-wide/2 x+wide/2 x+wide/2 x-wide/2];
yy=[y-high/2 y-high/2 y+high/2 y+high/2];
patch(xx,yy,'k',...
'facecolor',COLORMAP(n,:),...
'edgecolor','none','linewidth',1);
rect(x-wide/4,y-high/4,wide/2,high/2,n-1);
rect(x+wide/4,y+high/4,wide/2,high/2,n-1);
end
return
Recursive Square 5
function recursion13
%Recursion of simple square
global COLORMAP
n=8; %level of recursion
COLORMAP=1-copper(n);
rect(0,0,1,1,n);
axis([-0.75 0.75 -0.75 0.75]);
axis equal off
function rect(x,y,wide,high,n)
global COLORMAP
if n>0
xx=[x-wide/2 x+wide/2 x+wide/2 x-wide/2];
yy=[y-high/2 y-high/2 y+high/2 y+high/2];
patch(xx,yy,'k',...
'facecolor',COLORMAP(n,:),...
'edgecolor','none','linewidth',1);
rect(x-wide/4,y-high/4,wide/2,high/2,n-1);
rect(x-wide/4,y+high/4,wide/2,high/2,n-1);
rect(x+wide/4,y+high/4,wide/2,high/2,n-1);
end
return
Curlicue Fractal
s=sqrt(2)*0.0196;
theta=0;
phi=0;
total=10000;
curx=zeros(1,total);
cury=zeros(1,total);
xn=0;
yn=0;
for k=1:total
theta=mod(theta+2*pi*s,2*pi);
phi=mod(phi,2*pi)+theta;
[x,y]=pol2cart(phi,1);
xn=x+xn;
yn=y+yn;
curx(k)=xn;
cury(k)=yn;
end
line(curx,cury,'linewidth',1,'color','k')
s=log(2)*0.0185;
theta=0;
phi=0;
total=15000;
curx=zeros(1,total);
cury=zeros(1,total);
xn=0;
yn=0;
for k=1:total
theta=mod(theta+2*pi*s,2*pi);
phi=mod(phi,2*pi)+theta;
[x,y]=pol2cart(phi,1);
xn=x+xn;
yn=y+yn;
curx(k)=xn;
cury(k)=yn;
end
line(curx,cury,'linewidth',1,'color','k')
s=sqrt(2)*0.6813;
theta=0;
phi=0;
total=15000;
curx=zeros(1,total);
cury=zeros(1,total);
xn=0;
yn=0;
for k=1:total
theta=mod(theta+2*pi*s,2*pi);
phi=mod(phi,2*pi)+theta;
[x,y]=pol2cart(phi,1);
xn=x+xn;
yn=y+yn;
curx(k)=xn;
cury(k)=yn;
end
line(curx,cury,'linewidth',1,'color','k')