k=logical([1 0 1;0 1 0;1 0 1]);
for n=1:4
b=logical(zeros(3^n));
k=[k b k;b k b;k b k];
end
[mm,nn]=size(k);
[m,n]=find(k==1);
h=scatter(m,n,10,(m+n));
set(h,'marker','x')
axis equal off,
colormap hsv(256)
|
|
MATLAB code to generate Sierpinski Carpet.The generator matrix were taken from MathWorld.
b0=logical([1 1 1;1 0 1;1 1 1]);
for n=1:5 %don't exceed 6 because of expanding array inside loop
x=logical(zeros(3^n));
b0=[b0 b0 b0;b0 x b0;b0 b0 b0];
end
imagesc(b0),colormap(gray(2));
imwrite(bn,'sierpinski1.png','png','bitdepth',1);
|
|
MATLAB code to generate Haferman Carpet.The generator matrix were taken from MathWorld.
a0=logical([1 1 1;1 1 1;1 1 1]);
b0=logical([0 1 0;1 0 1;0 1 0]);
for n=1:5 %don't exceed 6 because of expanding array inside loop
an=[b0 b0 b0;b0 b0 b0;b0 b0 b0];
b0=[a0 b0 a0;b0 a0 b0;a0 b0 a0];
a0=an;
end
imagesc(b0),colormap(gray(2));
imwrite(b0,'haferman1.png','bitdepth',1);
|
|
MATLAB code for Cesaro fractal.I wrote this in 2009.The code given below is inefficient because of expanding array inside loop.But it is effective.This can also be generated using l system code.
phi=[0 pi/2 pi/2 pi/2];
theta=[85*pi/180 -170*pi/180 85*pi/180];
%theta=[-85*pi/180 170*pi/180 -85*pi/180]; %for inside out
%theta=[-80*pi/180 160*pi/180 -80*pi/180]; %
%theta=[80*pi/180 -160*pi/180 80*pi/180]; %
iter=5; %don't exceed 6
angle_new=[];
for n=1:iter
angle_new=[angle_new theta(1) angle_new theta(2) angle_new theta(3) angle_new];
end
angle_new=[phi(1) angle_new ...
phi(2) angle_new ...
phi(3) angle_new ...
phi(4) angle_new ...
0];
len_angle=length(angle_new);
x_arr=zeros(len_angle,1);
y_arr=zeros(len_angle,1);
x=0;y=0;x1=0;y1=0;
angle_2=0;
for n=1:len_angle
[x1,y1]=pol2cart(angle_2,1);
angle_2=angle_new(n)+angle_2;
x=x+x1;
y=y+y1;
x_arr(n)=x;
y_arr(n)=y;
end
%plot(x_arr,y_arr,'linewidth',1);
axis off square
x_arr=[x_arr;nan];
y_arr=[y_arr;nan];
patch(x_arr,y_arr,sqrt(x_arr.^2+y_arr.^2),...
'edgecolor','flat','facecolor','none','facevertexcdata',hsv(length(x_arr)));
|
 |
MATLAB code for pentaflake is in the table given below with heading Recursive Pentaflake .
MATLAB code to generate image given on right side.A much better code is available here.
%dragon curve
dvec=['1'];
rough=[];
len_dvec=1;
iters=10; %dont exceed 12
%loop to make sequence
% 110
% 1101100
%110110011100100
for loop_count=1:iters
len_dvec=length(dvec);
rough=dvec;
rough(len_dvec*0.5+0.5)='0';
dvec=[dvec '1' rough];
end
x=0;y=0;
len_dvec=length(dvec);
theta=pi/2;
x1=0;y1=0;x2=0;y2=0;
x_arr=zeros(len_dvec*2,1);
y_arr=zeros(len_dvec*2,1);
for k=1:len_dvec
if dvec(k)=='1'
[x1,y1]=pol2cart(theta,0.75);
x=x+x1;y=y+y1;
x_arr(2*k-1)=x;
y_arr(2*k-1)=y;
theta=theta+pi/4;
[x2,y2]=pol2cart(theta,sqrt(2)*0.25);
x=x+x2;y=y+y2;
x_arr(2*k)=x;
y_arr(2*k)=y;
theta=theta+pi/4;
else
[x1,y1]=pol2cart(theta,0.75);
x=x+x1;y=y+y1;
x_arr(2*k-1)=x;
y_arr(2*k-1)=y;
theta=theta-pi/4;
[x2,y2]=pol2cart(theta,sqrt(2)*0.25);
x=x+x2;y=y+y2;
x_arr(2*k)=x;
y_arr(2*k)=y;
theta=theta-pi/4;
end
end
x_arr=[x_arr ; nan];
y_arr=[y_arr ; nan];
patch(x_arr,y_arr,sqrt(x_arr.^2+y_arr.^2),'edgecolor','interp','facecolor','none');
axis equal off,colormap hsv(64)
|

|
MATLAB code given is only generates the 45 degree Pythagoras tree. The code is not generalized for any angle.
-
Pythagoras Tree 2D MATLAB function
function pytree
x=[-1 1 1 -1];
y=[-1 -1 1 1];
level=8; %level of recursion
tree(x,y,0,level);
axis equal
return
function tree(x,y,theta,n)
if n>0
[xx,yy]=trans2d(x,y,0,0,pi/4);
tree(xx*0.707-1,yy*0.707+2,theta+pi/4,n-1);
[xx,yy]=trans2d(x,y,0,0,-pi/4);
tree(xx*0.707+1,yy*0.707+2,theta+pi/4,n-1);
phi=mod(atan2(y(1),x(1))+pi,pi)/pi;
colors=hsv2rgb([phi 1 1]);
patch(x,y,'y','facecolor',colors);
end
return
function [xx,yy]=trans2d(x,y,tx,ty,phi)
[theta,rad]=cart2pol(x,y);
xx=rad.*cos(theta+phi)+tx;
yy=rad.*sin(theta+phi)+ty;
return
|

|
-
Pythagoras Tree 3D MATLAB function
pytree3d
x=[-1 1 1 -1];
y=[-1 -1 1 1];
vert=[1 1 1
1 1 -1
1 -1 1
1 -1 -1
-1 1 1
-1 1 -1
-1 -1 1
-1 -1 -1];
face=[1 4 3 2
1 2 6 5
5 8 4 1
2 3 7 6
3 4 8 7
6 7 8 5];
level=8; %level of recursion
tree(x,y,face,0,level);
axis equal tight
view(3)
return
function tree(x,y,face,theta,n)
if n>0
[xx,yy]=trans2d(x,y,0,0,pi/4);
tree(xx*0.707-1,yy*0.707+2,face,theta+pi/4,n-1);
[xx,yy]=trans2d(x,y,0,0,-pi/4);
tree(xx*0.707+1,yy*0.707+2,face,theta+pi/4,n-1);
phi=mod(atan2(y(1),x(1))+pi,pi)/pi;
colors=hsv2rgb([phi 1 1]);
len=sqrt((x(2)-x(1))^2+(y(2)-y(1))^2);
vert=[x' zeros(4,1)-len/2 y';x' len*ones(4,1)-len/2 y'];
patch('faces',face,'vertices',vert,'facecolor',colors);
end
return
function [xx,yy]=trans2d(x,y,tx,ty,phi)
[theta,rad]=cart2pol(x,y);
xx=rad.*cos(theta+phi)+tx;
yy=rad.*sin(theta+phi)+ty;
return
|
|
-
High Resolution Mandelbrot Set
|
Image with high resolution cannot be generated directly.With MATLAB 7.1 and 512MB RAM I got maximum resloution of 2300x2300.
There
is another way to generate image with higher resolution.In this method I
have used file handling and BMP file format.BMP file has two parts
header and data.First make a BMP file header which contain all the
infromation of BMP file including resolution,width and height.Then start
calculating the data and write it to BMP file continuously to data part
using file handling.By doing so data is not stored in RAM it is
directly converted to image.
By this method I have generated
image of resolution 6500x6500(42 megapixel)in 46 seconds,10000x10000
(100 megapixel ) in 74 seconds,15000x15000(225 megapixel)in 165
seconds.All images with 100 iterations.
The image generated is in BMP file format so it is very large.It is then converted to PNG file.
The first file mandellogical.m generates RGB BMP file and second file mandellogical2.m generates indexed BMP file.
The third file bigjulia.m generates julia set.
|
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);
[offx,offy]=pol2cart(t+18/180*pi,d*(1+golden));%generate points around middle pentagon
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 2
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 3
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 4
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
|
|
|
|