
% Draw diffusion limited aggregation

function   out=dla(n)
   clear centers;
   clear lograd;
   tic;
   centers=[0];
   counter=1;
   attempts=1;
   %centers=zeros(1,n);
   hold on;
   while counter < n
      maxradius=max(abs(centers));
      z=(maxradius+2)* exp(2 * pi * i * random('unif',0,1));
      step=min(abs(z-centers))-2;
      while((step>.01)&&(step<5000))
         z=z+ step * exp( 2 * pi * i * random('unif',0,1));
         step=min(abs(z-centers))-2;
      end
      if (step<.2)
          attempts=attempts+1;
              counter=counter+1;
              centers(counter)=z;
      end % if
      if counter==1000
         figure;
         axis equal;
         hold on;
      end
      if mod(counter,1000)==0
        title(['DLA, n = ',num2str(counter)])
       %  title('DLA');
        scatter(real(centers),imag(centers) ,'k','.');
        drawnow;
        [counter/1000,toc]
      end % if
   end % for k
   if  n< 1000
       figure;
       hold on; grid on; axis equal;
       plotcirc(centers,zeros(1,length(centers)));
   else
      figure
       hold on;
       axis equal;
       title(['DLA, n = ',num2str(length(centers))])
       colormap jet;
       scatter(real(centers),imag(centers),[],[1:length(centers)],'.');
   end
   
   return 
   figure;   hold on;   grid on;
   title('Cummulative maximum of DLA centers')
   cm=cummax(abs(centers));
   plot(cm);
   figure;   hold on;   grid on;
   title('Log of cummax of DLA centers')
   x=[1:length(cm)];
   plot( log(cm(100:end))./log(x(100:end)) );
  toc;
  
  return

