
% MOUSE EATING CHEESE using Brownian motion
%
% Read "The Science of Fractal Images", Ed. Peitgen and Saupe, p. 37 (1988)
% no

% Author : Emilia Wakamatsu, University of California, Berkeley
% Date   : Never

clear
clf

nBites = 100;
maxX = 10;
maxY = 10;

nNewBitesPerFrame = 100;


A = ones(maxX, maxY);

% eaten hole where mouse begins
A(maxX/2,maxY/2) = 0;

xstart = maxX/2;
ystart = maxY/2;


x  = xstart;
y  = ystart;


pause(.1)


for i = 1:nBites
    
    xOld = x; %store current x and y values
    yOld = y;
    
    % Arbitrarily select direction to turn to
    
    r = rand();
    
    if (0<=r) && (r<=0.25)
        x= x-1;
    end
    
    if (0.25<r) && (r<=0.50)
        x= x+1;
    end
    
    if (0.50<r) && (r<=0.75)
        y= y-1;
    end
    
    if (0.75<r) && (r<=1.00)
        y=y+1;
    end
    
    
    
    
    if x > maxX
        x=x-maxX;
    end
    
    if x<1
        x=x+maxX;
    end
    if y > maxY
        y=y-maxY;
    end
    
    if y<1
        y=y+maxY;
    end
    
    if A(x,y) == 0  % ok, this site has been taken already, reject the move
        x=xOld;
        y=yOld;
        continue;
    end
    
    
    
    if  A(x,y)== 1
        A(x,y) = 0; % Eat it
        fprintf(' number of bites %d \n',i);
        
    end
    
    
   
    
    
    imagesc(A');
    set(gca,'YDir','normal');
    colormap(1-gray);
    axis equal;
    axis tight;
    drawnow
    
    pause(.1)
end
