% MATLAB code used to draw some slides from Chris Bishop's lecture
% "Harmonic measures, true trees and quasiconformal folding"

% to run this file, save it as 'Brazil.m' and open MATLAB  in 
% the same directory. Then type 'Brazil'. Enter 1 or 2 when the 
% program prompts you. A 1 will cause another random path to be 
% drawn. A 2 will supress drawing of the paths and will eventually 
% plot the ending points of 100 random walks (including any that 
% you drew before entering 2. The total number is set as limit=100
% below and can be changed, as can the starting point ('start').

% This gives coordinates of map of Brazil (x = odds, y = evens)
% this is derived by tracing an online image of a map in XFIG
% and saving the result
 map=[9180 10605 9420 10620 9420 10365 10770 9360 10755 8895 11415 8550 12195 8355 12480 8415 12885 8220 12930 7950 13320 7650 13260 7590 13335 7530 13695 7020 13740 6795 13785 6285 14130 6105 14295 5745 14985 5220 15000 4350 14415 4245 13905 3855 12840 3780 11835 3270 11520 3285 11370 3435 11325 3570 11400 3255 10995 3090 10740 3225 10725 3105 11010 2985 11025 2850 10770 2595 10785 2235 10530 2160 10275 2655 9975 2730 9540 2550 9225 2535 9240 2730 9105 2745 8790 2775 8670 2865 8490 2820 8325 2700 8355 2115 7860 1995 7950 2160 7455 2250 7425 2400 7335 2385 7245 2205 6855 2250 7185 2565 6630 3000 6330 2955 6165 2700 5820 2775 5310 2685 5355 2910 5625 2940 5610 3030 5325 3045 5520 3435 5340 4095 4500 4140 4470 4575 4245 4695 4215 4920 4815 5415 5190 5280 5160 5550 6135 5445 6600 5265 6510 5715 7140 6030 7470 6045 7845 6210 7965 6750 8430 6810 8715 7125 8490 7470 8580 8055 8925 8010 9105 8145 9075 8415 9420 8400 9435 8535 9285 8745 9495 8880 9465 9090 9060 9270 8460 9750 8775 10005 9255 10290 9210 10515 9180 10605];

   x=map(1:2:196);
   y=1.2 *map(2:2:196);
   brazilmap=x-i*y;
   list = brazilmap;
   L=length(list);
   diff1=max(abs(list(1:L-1)-list(2:L)));

   % ---------------------------
   % these are main 2 parameters 
   % a user may wish to change
   % --------------------------
   %starting location
   start=11000-7000*i;
   %start=8000-5000*i;
   % total number of random walks to draw
   limit=100;

   % put a bounding box around the whole image
   % helps stabilze sucessive figures in slides
   a=min(x)-150;
   b=max(x)+150;
   c=min(-y)-150;
   d=max(-y)+150;
   boxx=[a;a;b;b;a];
   boxy=[c;d;d;c;c];

   % add point to lists. We do this to make it easier 
   % to check when the random walk crosses the boundary
   % It would be more elegant to check when we cross the 
   % edge between to points, but this was faster to program.
   num=100;
   newlist=list(1);
   for k=1:length(list)-1
   newlist=[newlist,list(k)+(list(k+1)-list(k))*[1:num]/num];
   end % for k
   list=newlist;


   
   % these were only needed to make lecture slides 
   if 0==1
      % Draw the outline of the country in black
      figure; hold on; axis off; axis equal;
      plot(real(list)',imag(list)','k','LineWidth',5)
      plot(boxx,boxy,'w');

      % Draw the country and add a blue edge along part 
      % of boundary 
      figure; hold on; axis off; axis equal;
      plot(real(list)',imag(list)','k','LineWidth',5)
      plot(real(brazilmap(2:35))',imag(brazilmap(2:35))','b','LineWidth',6)
      plot(boxx,boxy,'w');

      %  Same as above and add Green starting point
      figure; hold on; axis off; axis equal;
      plot(real(list)',imag(list)','k','LineWidth',5)
      plot(real(brazilmap(2:35))',imag(brazilmap(2:35))','b','LineWidth',6)
      plot(boxx,boxy,'w');
      dot=start+ 50*exp(2*pi*i*[0:.05:1]);
      plot(real(dot)',imag(dot)','g','LineWidth',6);
      dot=start+ 100*exp(2*pi*i*[0:.05:1]);
      plot(real(dot)',imag(dot)','k','LineWidth',3);
   end % if 0==1



  % stop walk if we have not yet hit boundary
  maxwalk=100000;
  % count number of hits on whole boundary 
  counter=1;
  % count number of hits on subset 
  counter2=0;
  % save the locations of hit points
  clear save;
  tic
  % set flag to determine if pictures should be drawn
  flag=0;
  while counter <= limit+1
     counter
     toc
     clear walk;
     % set step size of random walk
     delta=20;
     % set tolerance for hitting boundary
     epsilon=20;
     % set initial position of walk
     walk(1)=start;
     flag2=0;
     for k=2:maxwalk
       walk(k)=walk(k-1)+delta*exp(2*pi*i*rand);
       if min(abs(walk(k)-list))< epsilon
          save(counter)=walk(k);
          flag2=1;
          break;
       end % if
     end % for k

     % what to if we have entered 3 on keyboard and 
     % no longer want to plot the random walks, only 
     % the accumulated endpoints 
     if (flag  > 0)&(flag2==1) 
        save(counter)=walk(end);
        counter=counter+1;
        % this is quick check of hitting subset; everything 
        % right of a certain vertical line.  More careful code
        % would check which boundary vertex we are closest to.
        if walk(k) > brazilmap(2)
           counter2=counter2+1;
        end  % if
        if counter== limit
           figure; hold on; axis off; axis equal;
           plot(boxx,boxy,'w');
           plot(boxx,boxy,'w');
           plot(real(list)',imag(list)','k','LineWidth',5)
           plot(real(brazilmap(2:35))',imag(brazilmap(2:35))','b','LineWidth',6)
           for k=1:length(save)
              dot=save(k)+ 60*exp(2*pi*i*[0:.05:1]);
              plot(real(dot)',imag(dot)','r','LineWidth',6);
              dot=save(k)+ 100*exp(2*pi*i*[0:.05:1]);
              plot(real(dot)',imag(dot)','k','LineWidth',4);
           end % for k 
           dot=walk(1)+ 60*exp(2*pi*i*[0:.05:1]);
           plot(real(dot)',imag(dot)','g','LineWidth',6);
           dot=walk(1)+ 100*exp(2*pi*i*[0:.05:1]);
           plot(real(dot)',imag(dot)','k','LineWidth',3);
           drawnow;
           break;
         end % if counter
      end % if 
   
      %What to do if we want to plot the next walk 
      if (flag==0)&(flag2==1)
         figure; hold on; axis off; axis equal;
         plot(boxx,boxy,'w');
         % plot brazil
         plot(real(list)',imag(list)','k','LineWidth',5)
         % plot coast
         plot(real(brazilmap(2:35))',imag(brazilmap(2:35))','b','LineWidth',6)
         % plot random walk
         plot(real(walk)',imag(walk)','r','LineWidth',2)
         % plot ending dots 
         for k=1:length(save)
            dot=save(k)+ 60*exp(2*pi*i*[0:.05:1]);
            plot(real(dot)',imag(dot)','r','LineWidth',6);
            dot=save(k)+ 100*exp(2*pi*i*[0:.05:1]);
            plot(real(dot)',imag(dot)','k','LineWidth',3);
         end % for k 
         % plot starting point
         dot=walk(1)+ 60*exp(2*pi*i*[0:.05:1]);
         plot(real(dot)',imag(dot)','g','LineWidth',5);
         dot=walk(1)+ 100*exp(2*pi*i*[0:.05:1]);
         plot(real(dot)',imag(dot)','k','LineWidth',3);
         drawnow;
         % after drawing each path the program asks for 
         % input. Giving a 1 draws another path. Giving a 
         % 2 stops plotting individual paths and proceeds 
         % to finding and drawing endpoints of all paths
         x=input('keep plotting?  1= yes, 2= no and continue to end.');
         if x==1 
            counter=counter+1;
            if walk(k) > brazilmap(2)
               counter2=counter2+1;
            end  % if walk
         elseif  x==2
            flag=1;
         end % if x 
      end % if flag

   end %  while 

    counter
    counter2
    counter2/counter

return



