% Draw DLA, but keep tract of the vertices of the convex 
% hull at each stage. Draw DLA with two colors, depending 
% on whether a new disk is on the convex hull boundary when 
% it is added.

function   dla2(n)
   tic
   clear centers;
   clear lograd;
   tic;
   centers=[0,2];
   hullx=[0,2];hully=[0,0];
   pointer=[0,0;1,2];
   counter=2;
   attempts=2;
 %  centers=zeros(1,10^7);
   for k=1:n
      maxradius=max(abs(centers))+2;
      z=(maxradius+1)* 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 % while
      if  (step > 4999)  % use Poissonkernel to retart 
      end % if 
      if (step<.02)
          [m,index]=min(abs(z-centers));
          pointer=[pointer;index,z];
          attempts=attempts+1;
          counter=counter+1;
          centers(counter)=z;
          newhullx=[hullx,real(z)];
          newhully=[hully,imag(z)];
          K=convhull(newhullx,newhully);
          hullx=newhullx(K);
          hully=newhully(K);
          saveK(counter)=length(K);
          if max(K)==length(newhullx)
             hull(counter)=1;
          else 
             hull(counter)=0;
          end % if
          hull;          
      end % if 
      if mod(k,10000)==0
         [k/10000,toc]
      end % if
   end % for k
   
  figure;  hold on; axis equal;
  title('Red disks on convex hull when added');
  list=find(hull==0);
  L1=length(list);
  scatter(real(centers(list)),imag(centers(list)),'.','k');
  list=find(hull==1);
  L2=length(list);
  percent_red=L2/(L1+L2)
  scatter(real(centers(list)),imag(centers(list)),'.','r');
  % plot dla 
  plotcirc(centers,hull);
  figure; hold on; grid on;
  title('Number of points on convex hull at time t');
  plot(saveK)
  %figure
  %smooth=conv(saveK,ones(1,10))/10;
  %smooth=smooth(10:10:length(saveK-10));
  %plot(smooth);
  toc

return



