function maplep
% maplep klevo lapo fraktalas naudojantis optimalias tikimybes,
% pagal M.F. Barnsley, L.P. Hurd,
% fractal Image Compression, A.K. Peters Ltd., 1993
% Klevo lapas apibreziamas 24-iais skaiciais:
%49 1 25 0 62 -2; 27 52 0 -40 36 56;
%18 -73 88 50 26 8; 4 -1 52 50 0 32;

N=1024*2^6;
ll=[1024*2^0 1024*2^1 1024*2^2 1024*2^3 1024*2^4 1024*2^5 1024*2^6];
x=zeros(1,N); y=zeros(1,N); ii=randperm(N);
w=zeros(4,6); b=[0 1]; 
w(1,:)=[49 1 25 0 62 -2]/100; 
w(2,:)=[27 52 0 -40 36 56]/100; 
w(3,:)=[18 -73 88 50 26 8]/100; 
w(4,:)=[4 -1 52 50 0 32]/100; 
t=zeros(1,4);
for i=1:4, t(i)=abs(det([w(i,1:2); w(i,4:5)])); end;
mt=1000; t=t/sum(t); t=round(cumsum(t)*mt); nt=zeros(1,mt);
t
nt(1:t(1))=1; for i=2:4, nt(t(i-1)+1:t(i))=i; end;

p=-inv([w(1,1)-1 w(1,2); w(1,4)-1 w(1,5)])*[w(1,3);w(1,6)];
x(1)=p(1); y(1)=p(2);
l=1;
for k=2:N,
 i=nt(round(rand*(mt-1))+1);
 p=[w(i,1:2); w(i,4:5)]*[x(k-1);y(k-1)];
 x(k)=p(1)+w(i,3); y(k)=p(2)+w(i,6);
if k==ll(l),
  figure(l); clf; plot(x,-y,'k.','markersize',3); 
  title(['Klevo lapas, iteracija=' num2str(k)]); l=l+1;
end;
end;
