%CLASSICALFRACTALS.M: Plots the following 3 Classical Fractals:- % [1] Koch Curve % [2] Sierpinski Gasket % [3] Sierpinski Carpet % % "n" is the Number of Iterations. % The typical range for n (for screen resolution 800*600) is as follows:- % (i) For Koch Curve n<7. % (ii) For Sierpinski Gasket n<8. % (iii)For Sierpinski Carpet n<5. % To properly display results for higher values of n, use higher screen % resolutions. Be patient for higher values of n. % % Usage: Type "classicalfractals" at Matlab command prompt to get started. % % Copyright (c) 2000 by Salman Durrani (dsalman@wol.net.pk). function ClassicalFractals s=get(0,'ScreenSize'); set(gcf,'Position',[0 0 s(3) s(4)-70],'Color',[0 0 0]) K=[]; K = menu('This program plots Classical Fractals','Cantor Set','Koch Curve','Sierpinski Triangle','Sierpinski Isosceles Triangle','Sierpinski Carpet','Carpet2','Carpet 3','Maple Leaf','Leaf','Exotic Tree','Plant','Menger sponge','Cross 1','Cross 2','Pyramide','Tree'); choice1=isempty(K); if choice1==0 if K==1 clear; clf; n=selection; for i=1:n cfcantor(i) pause(3) end ClassicalFractalsMenu elseif K==2 clear; clf; n=selection; for i=1:n cfkoch(i) pause(3) end ClassicalFractalsMenu elseif K==3 clear; clf; n=selection; for i=1:n cfsierp(i) pause(3) end ClassicalFractalsMenu elseif K==4 clear; clf; n=selection; for i=1:n cfsierpiso(i) pause(3) end ClassicalFractalsMenu elseif K==5 clear; clf; n=selection; for i=1:n cfcarpet(i) pause(3) end ClassicalFractalsMenu elseif K==6 clear; clf; n=selection; for i=1:n cfcarpet2(i) pause(3) end ClassicalFractalsMenu elseif K==7 clear; clf; n=selection; for i=1:n cfcarpet3(i) pause(3) end ClassicalFractalsMenu elseif K==8 clear; clf; n=selection; for i=1:n cfmaple(i) pause(3) end ClassicalFractalsMenu elseif K==9 clear; clf; n=selection; for i=1:n cfleaf(i) pause(3) end ClassicalFractalsMenu elseif K==10 clear; clf; n=selection; for i=1:n cfexotic(i) pause(3) end ClassicalFractalsMenu elseif K==11 clear; clf; n=selection; for i=1:n cfplant(i) pause(3) end ClassicalFractalsMenu elseif K==12 clear; clf; for i=1:4 cfmengersponge(i) pause(3) end ClassicalFractalsMenu elseif K==13 clear; clf; for i=1:5 cfcross1(i) pause(3) end ClassicalFractalsMenu elseif K==14 clear; clf; for i=1:5 cfcross2(i) pause(3) end ClassicalFractalsMenu elseif K==15 clear; clf; for i=1:7 cfpyramide(i) pause(3) end ClassicalFractalsMenu elseif K==16 clear; clf; for i=1:4 cftree(i) pause(3) end ClassicalFractalsMenu end end %----------------------------------------------------- % Function to make a choise function n=selection prompt={'Enter Number of iterations n'}; def={'3'}; dlgTitle='Input for Classical Fractals function'; lineNo=1; ans=inputdlg(prompt,dlgTitle,lineNo,def); choice=isempty(ans); if (choice==0) n=ceil(abs(str2num(char(ans(1))))); end %----------------------------------------------------- % Function to generate Cantor Set function cfcantor(n) W=[ 1/3 0 0 0 0 0; 1/3 0 0 0 2/3 0]; A0=[0 0]; Cantor=MYDIA_R2(A0,1,W,2,n); X=Cantor(:,1); Y=Cantor(:,2); plot(X, Y , 'w.' , 'MarkerSize' , 6); good_axis N=num2str(n); NN=strcat('Cantor Set',' n=',N); set(gcf,'Name',NN) %----------------------------------------------------- % Function to generate Koch Curve function cfkoch(n) hold on W=[ 1/3 0 0 1/3 0 0; 1/6 -sqrt(3)/6 sqrt(3)/6 1/6 1/3 0; 1/6 sqrt(3)/6 -sqrt(3)/6 1/6 1/2 sqrt(3)/6; 1/3 0 0 1/3 2/3 0]; A0=[0 0;1 0]; koch=MYDIA_R2(A0,2,W,4,n); X=koch(:,1); Y=koch(:,2); plot(X, Y, 'w.' , 'MarkerSize' , 6); good_axis N=num2str(n); NN=strcat('Koch Curve',' n=',N); set(gcf,'Name',NN) %----------------------------------------------- % Function to generate Sierpinski Gasket function cfsierp(n) W=[ 0.5 0.0 0.0 0.5 0.0 0; 0.5 0.0 0.0 0.5 0.5 0; 0.5 0.0 0.0 0.5 0.0 0.5]; A0=[0 1]; Sierpinski=MYDIA_R2(A0,1,W,3,n); X=Sierpinski(:,1); Y=Sierpinski(:,2); plot(X, Y , 'r.' , 'MarkerSize' , 6); good_axis N=num2str(n); NN=strcat('Sierpinski Gasket ',' n=',N); set(gcf,'Name',NN) %----------------------------------------------------- % Function to generate Sierpinski Isosceles Gasket function cfsierpiso(n) W=[ 0.5 0.0 0.0 0.5 0 0; 0.5 0.0 0.0 0.5 0.5 0; 0.5 0.0 0.0 0.5 0.25 sqrt(3)/4]; A0=[0 100]; Sierpinski=MYDIA_R2(A0,1,W,3,n); X=Sierpinski(:,1); Y=Sierpinski(:,2); plot(X, Y , 'r.' , 'MarkerSize' , 6); good_axis N=num2str(n); NN=strcat('Sierpinski Isosceles Gasket ',' n=',N); set(gcf,'Name',NN) %------------------------------------------------ % Function to generate Sierpinski Carpet function cfcarpet(n) W=[ 1/3 0 0 1/3 0 0; 1/3 0 0 1/3 1/3 0; 1/3 0 0 1/3 2/3 0; 1/3 0 0 1/3 0 1/3; 1/3 0 0 1/3 2/3 1/3; 1/3 0 0 1/3 0 2/3; 1/3 0 0 1/3 1/3 2/3 1/3 0 0 1/3 2/3 2/3]; A0=[0 0]; Sierpinski=MYDIA_R2(A0,1,W,8,n); X=Sierpinski(:,1); Y=Sierpinski(:,2); plot(X, Y , 'm.' , 'MarkerSize' , 5); good_axis N=num2str(n); NN=strcat('Sierpinski Carpet',' n=',N); set(gcf,'Name',NN) %------------------------------------------------ % Function to generate CarpetX function cfcarpet2(n) W=[ 1/3 0 0 1/3 0 0; 1/3 0 0 1/3 2/3 0; 1/3 0 0 1/3 1/3 1/3; 1/3 0 0 1/3 0 2/3; 1/3 0 0 1/3 2/3 2/3]; A0=[0 0]; Sierpinski=MYDIA_R2(A0,1,W,5,n); X=Sierpinski(:,1); Y=Sierpinski(:,2); plot(X, Y , 'm.' , 'MarkerSize' , 5); good_axis N=num2str(n); NN=strcat('Carpet X',' n=',N); set(gcf,'Name',NN) %------------------------------------------------ % Function to generate Carpet+ function cfcarpet3(n) W=[ 1/3 0 0 1/3 1/3 0; 1/3 0 0 1/3 0 1/3; 1/3 0 0 1/3 1/3 1/3; 1/3 0 0 1/3 2/3 1/3; 1/3 0 0 1/3 1/3 2/3]; A0=[0 0]; Sierpinski=MYDIA_R2(A0,1,W,5,n); X=Sierpinski(:,1); Y=Sierpinski(:,2); plot(X, Y , 'm.' , 'MarkerSize' , 5); good_axis N=num2str(n); NN=strcat('Carpet +',' n=',N); set(gcf,'Name',NN) %------------------------------------------------ % Function to generate Maple Leaf function cfmaple(n) W=[ 0.49 0.01 0 0.62 25 -2; 0.27 0.52 -0.40 0.36 0 56; 0.18 -0.73 0.50 0.26 88 8; 0.04 -0.01 0.50 0 52 32] A0=[0 0]; Maple=MYDIA_R2(A0,1,W,4,n); X=Maple(:,1); Y=Maple(:,2); plot(X, Y , 'b.' , 'MarkerSize' , 2); good_axis N=num2str(n); NN=strcat('Maple Leaf',' n=',N); set(gcf,'Name',NN) %------------------------------------------------ % Function to generate Leaf function cfleaf(n) W=[ 1/3 0 0 1/3 0 0; 1/3 0 0 1/3 1/3 0; 1/3 0 0 1/3 2/3 0; 0.129 -0.4825 0.4825 0.129 2/10 0; 0.129 0.4825 -0.4825 0.129 2/10 0; 1/6 -sqrt(3)/6 sqrt(3)/6 1/6 4/10 0; 1/6 sqrt(3)/6 -sqrt(3)/6 1/6 4/10 0; sqrt(2)/8 -sqrt(2)/8 sqrt(2)/8 sqrt(2)/8 0.55 0; sqrt(2)/8 sqrt(2)/8 -sqrt(2)/8 sqrt(2)/8 0.55 0; sqrt(3)/10 -1/10 1/10 sqrt(3)/10 7/10 0; sqrt(3)/10 1/10 -1/10 sqrt(3)/10 7/10 0]; A0=[0 0]; leaf=MYDIA_R2(A0,1,W,11,n); X=leaf(:,1); Y=leaf(:,2); plot(X, Y , 'b.' , 'MarkerSize' , 2); good_axis N=num2str(n); NN=strcat('Leaf',' n=',N); set(gcf,'Name',NN) %----------------------------------------------------- % Function to generate Exotic Tree function cfexotic(n) W=[ 0.5 0.0 0.0 0.5 1/4 0.5; 0.0 -0.5 0.5 0.0 1/2 0; 0.0 0.5 -0.5 0.0 1/2 1/2]; A0=[1/2 1]; exotic=MYDIA_R2(A0,1,W,3,n); X=exotic(:,1); Y=exotic(:,2); plot(X, Y , 'bo' , 'MarkerSize' , 2); good_axis N=num2str(n); NN=strcat('Exotic Tree',' n=',N); set(gcf,'Name',NN) %----------------------------------------------------- % Function to generate Plant function cfplant(n) W=[ 0.5 0.0 0.0 0.5 0 0.5; 0.0 -0.5 0.5 0.0 1/2 0; -0.5 0.0 0.0 0.5 1 0]; A0=[0 0]; plant=MYDIA_R2(A0,1,W,3,n); X=plant(:,1); Y=plant(:,2); plot(X, Y , 'bo' , 'MarkerSize' , 2); good_axis N=num2str(n); NN=strcat('Plant',' n=',N); set(gcf,'Name',NN) %--------------------------------------------------------- % Function to generate Menger Sponge function cfmengersponge(n) W=[ 1/3 0 0 0 1/3 0 0 0 1/3 0 0 0; 1/3 0 0 0 1/3 0 0 0 1/3 1/3 0 0; 1/3 0 0 0 1/3 0 0 0 1/3 2/3 0 0; 1/3 0 0 0 1/3 0 0 0 1/3 0 1/3 0; 1/3 0 0 0 1/3 0 0 0 1/3 0 2/3 0; 1/3 0 0 0 1/3 0 0 0 1/3 1/3 2/3 0; 1/3 0 0 0 1/3 0 0 0 1/3 2/3 1/3 0; 1/3 0 0 0 1/3 0 0 0 1/3 2/3 2/3 0; 1/3 0 0 0 1/3 0 0 0 1/3 0 0 1/3; 1/3 0 0 0 1/3 0 0 0 1/3 2/3 0 1/3; 1/3 0 0 0 1/3 0 0 0 1/3 0 2/3 1/3; 1/3 0 0 0 1/3 0 0 0 1/3 2/3 2/3 1/3; 1/3 0 0 0 1/3 0 0 0 1/3 0 0 2/3; 1/3 0 0 0 1/3 0 0 0 1/3 1/3 0 2/3; 1/3 0 0 0 1/3 0 0 0 1/3 2/3 0 2/3; 1/3 0 0 0 1/3 0 0 0 1/3 0 1/3 2/3 1/3 0 0 0 1/3 0 0 0 1/3 0 2/3 2/3; 1/3 0 0 0 1/3 0 0 0 1/3 1/3 2/3 2/3; 1/3 0 0 0 1/3 0 0 0 1/3 2/3 1/3 2/3; 1/3 0 0 0 1/3 0 0 0 1/3 2/3 2/3 2/3]; A0=[0 0 0]; menger=MYDIA_R3(A0,1,W,20,n); X=menger(:,1); Y=menger(:,2); Z=menger(:,3); plot3(X, Y, Z, 'c.' , 'MarkerSize' , 4); good_axis N=num2str(n); NN=strcat('Menger Sponge',' n=',N); set(gcf,'Name',NN) %--------------------------------------------------------- % Function to generate 3D X function cfcross1(n) W=[ 1/3 0 0 0 1/3 0 0 0 1/3 0 0 0; 1/3 0 0 0 1/3 0 0 0 1/3 2/3 0 0; 1/3 0 0 0 1/3 0 0 0 1/3 0 2/3 0; 1/3 0 0 0 1/3 0 0 0 1/3 2/3 2/3 0; 1/3 0 0 0 1/3 0 0 0 1/3 1/3 1/3 1/3; 1/3 0 0 0 1/3 0 0 0 1/3 0 0 2/3; 1/3 0 0 0 1/3 0 0 0 1/3 2/3 0 2/3; 1/3 0 0 0 1/3 0 0 0 1/3 0 2/3 2/3; 1/3 0 0 0 1/3 0 0 0 1/3 2/3 2/3 2/3]; A0=[0 0 0]; cross1=MYDIA_R3(A0,1,W,9,n); X=cross1(:,1); Y=cross1(:,2); Z=cross1(:,3); plot3(X, Y, Z, 'c.' , 'MarkerSize' , 4); good_axis N=num2str(n); NN=strcat('Cross 1',' n=',N); set(gcf,'Name',NN) %--------------------------------------------------------- % Function to generate 3D + function cfcross2(n) W=[ 1/3 0 0 0 1/3 0 0 0 1/3 1/3 1/3 0; 1/3 0 0 0 1/3 0 0 0 1/3 1/3 0 1/3; 1/3 0 0 0 1/3 0 0 0 1/3 0 1/3 1/3; 1/3 0 0 0 1/3 0 0 0 1/3 1/3 1/3 1/3; 1/3 0 0 0 1/3 0 0 0 1/3 2/3 1/3 1/3; 1/3 0 0 0 1/3 0 0 0 1/3 1/3 2/3 1/3; 1/3 0 0 0 1/3 0 0 0 1/3 1/3 1/3 2/3]; A0=[0 0 0]; cross2=MYDIA_R3(A0,1,W,7,n); X=cross2(:,1); Y=cross2(:,2); Z=cross2(:,3); plot3(X, Y, Z, 'c.' , 'MarkerSize' , 4); good_axis N=num2str(n); NN=strcat('Cross 2',' n=',N); set(gcf,'Name',NN) %----------------------------------------------- % Function to generate Pyramide function cfpyramide(n) W=[ 0.5 0.0 0.0 0.0 0.5 0.0 0.0 0.0 1/2 0.0 0.0 0; 0.5 0.0 0.0 0.0 0.5 0.0 0.0 0.0 1/2 1/2 0.0 0; 0.5 0.0 0.0 0.0 0.5 0.0 0.0 0.0 1/2 1/4 sqrt(3)/4 0; 0.5 0.0 0.0 0.0 0.5 0.0 0.0 0.0 1/2 1/4 1/4 1/2]; A0=[0 0 0]; Pyramide=MYDIA_R3(A0,1,W,4,n); X=Pyramide(:,1); Y=Pyramide(:,2); Z=Pyramide(:,3); plot3(X, Y, Z, 'y.' , 'MarkerSize' , 4); good_axis N=num2str(n); NN=strcat('Pyramide',' n=',N); set(gcf,'Name',NN) %------------------------------------------------ % Function to generate Tree function cftree(n) W=[ 1/3 0 0 0 1/3 0 0 0 1/3 0 0 0; 1/3 0 0 0 1/3 0 0 0 1/3 0 0 1/3; 1/3 0 0 0 1/3 0 0 0 1/3 0 0 2/3; 1/2 0 0 0 0.129 -0.4825 0 0.4825 0.129 0 0 1/3; 1/2 0 0 0 0.129 0.4825 0 -0.4825 0.129 0 0 1/3; 0 0.129 -0.4825 1/2 0 0 0 0.4825 0.129 0 0 1/3; 0 0.129 0.4825 1/2 0 0 0 -0.4825 0.129 0 0 1/3; 1/3 0 0 0 0.086 -0.321 0 0.321 0.086 0 0 0.47; 1/3 0 0 0 0.086 0.321 0 -0.321 0.086 0 0 0.47; 0 0.086 -0.321 1/3 0 0 0 0.321 0.086 0 0 0.47; 0 0.086 0.321 1/3 0 0 0 -0.321 0.086 0 0 0.47; 1/5 0 0 0 0.1 -0.173 0 0.173 0.1 0 0 0.6; 1/5 0 0 0 0.1 0.173 0 -0.173 0.1 0 0 0.6; 0 0.1 -0.173 1/5 0 0 0 0.173 0.1 0 0 0.6; 0 0.1 0.173 1/5 0 0 0 -0.173 0.1 0 0 0.6; 1/6 0 0 0 sqrt(2)/12 -sqrt(2)/12 0 sqrt(2)/12 sqrt(2)/12 0 0 0.73; 1/6 0 0 0 sqrt(2)/12 sqrt(2)/12 0 -sqrt(2)/12 sqrt(2)/12 0 0 0.73; 0 sqrt(2)/12 -sqrt(2)/12 1/6 0 0 0 sqrt(2)/12 sqrt(2)/12 0 0 0.73; 0 sqrt(2)/12 sqrt(2)/12 1/6 0 0 0 -sqrt(2)/12 sqrt(2)/12 0 0 0.73; 1/8 0 0 0 0.108 -0.0625 0 0.0625 0.108 0 0 5/6; 1/8 0 0 0 0.108 0.0625 0 -0.0625 0.108 0 0 5/6; 0 0.108 -0.0625 1/8 0 0 0 0.0625 0.108 0 0 5/6; 0 0.108 0.0625 1/8 0 0 0 -0.0625 0.108 0 0 5/6]; A0=[0 0 0]; tree=MYDIA_R3(A0,1,W,23,n); X=tree(:,1); Y=tree(:,2); Z=tree(:,3); plot3(X, Y, Z, 'g.' , 'MarkerSize' , 4); good_axis N=num2str(n); NN=strcat('Tree',' n=',N); set(gcf,'Name',NN) %--------------------------------------------------------- %--------------------------------------------------------- %--------------------------------------------------------- %--------------------------------------------------------- %---------------------------------------------------------- function B=MYDIA_R2(A0,P,W,N,K) %A0 is the initial set representing a 1x2 matrix %N is the number of the mappings %K is the number of iterations %W is the matrix Nx6 containing the parameters of the mappings %Each row (i) contains the 6 coefficients of the map w_i. a,b,c,s,d,e % / \ / \ % |a b| | d | %w=| | + | | % |c s| | e | % \ / \ / %The function returns the matrix FP N^K x 2!!! A=A0; points=P; for (i=1:K) for j=1:points %for every point x=A(j,1); y=A(j,2); for r=1:N %map the point through each map B(points*(r-1)+j,1)=W(r,1)*x+W(r,2)*y+W(r,5); B(points*(r-1)+j,2)=W(r,3)*x+W(r,4)*y+W(r,6); end; end; points=N^i; A=B; end; %---------------------------------------------------------- function B=MYDIA_R3(A0,P,W,N,K) %A0 is the initial set representing a 1x3 matrix %N is the number of the mappings %K is the number of iterations %W is the matrix Nx12 containing the parameters of the mappings %Each row (i) contains the 12 coefficients of the map w_i. %a1,b1,c1,a2,b2,c2,a3,b3,c3,d1,d2,d3 % / \ / \ % |a1 b1 c1| | d1 | %w=|a2 b2 c2| + | d2 | % |a3 b3 d3| | d3 | % \ / \ / %The function returns the matrix FP N^K x 3!!! A=A0; points=P; for (i=1:K) for j=1:points %for every point x=A(j,1); y=A(j,2); z=A(j,3); for r=1:N %map the point through each map B(points*(r-1)+j,1)=W(r,1)*x+W(r,2)*y+W(r,3)*z+W(r,10); B(points*(r-1)+j,2)=W(r,4)*x+W(r,5)*y+W(r,6)*z+W(r,11); B(points*(r-1)+j,3)=W(r,7)*x+W(r,8)*y+W(r,9)*z+W(r,12); end; end; points=N^i; A=B; end; %---------------------------------------------------------- function good_axis axis equal set(gca,'Visible','off')