iconEuler Examples

Sierpinski Gasket

by Alain Busser

The Sierpinski Gasket can be generated by iterating a random choice of
three functions. In the limit, the points tend to an attracting set of
fixed points. I.e., the limit set is the union of images of itself
under the three mappings.
>function fill (p,n) ...
global A,B,C,D
t=floor(4*random(1,n));
M=random(1,3);
for i=1 to n;
  if t[i]==0 then; 
    M=(M+A)/2;
  elseif t[i]==1;
    M=(M+B)/2;
  elseif t[i]==2;
    M=(M+C)/2;
  else;
    M=(M+D)/2;
 endif;
 p[1,i]=M[1];
 p[2,i]=M[2];
 p[3,i]=M[3];
end;
endfunction
>A=[1,1,1];
>B=[-1,1,-1];
>C=[-1,-1,1];
>D=[1,-1,-1];
>nb=10000;
>p=ones(4,nb);
>fill(p,nb);
The plot can be turned, but will turn only very slowly due to the
thousands of points. 

You need red/cyan glasses to appreciate the view.
>markerstyle("."); plot3d(p[1],p[2],p[3],points=1,user=1, ...
>  anaglyph=1,frame=0,angle=190°,zoom=3);
It is better to turn antialias off in this case.
>insimg(antialias=false);

Sierpinski Gasket

The Fern

This is a similar example in the plane.
>function fern (n) ...
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];
x=[0.5;0.5];
X=zeros(n,2);
r=random(1,n);
loop 1 to n;
  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;
  endif
  X[#]=x';
end;
return X;
endfunction
>X=fern(100000)'; ...
>plot2d(X[1],X[2],a=-3,b=3,c=0,d=10, ...
>  >points,<frame,<grid,style=".",color=green,>insimg);

Sierpinski Gasket

Examples Homepage