% Script PaintBall
% Draws a unit black square with lower left corner at (0,0).
% Repeatedly draws disks whose centers are randomly placed in the square.
% Each disks has radius .03 and if it is entirely located in the square,
%    it is colored magenta, yellow, or cyan with equal probability.
% Otherwise, it is drawn white.
% The process continues until 50 white disks are drawn.

  close all
  figure
  axis equal off
  hold on
%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
% Initializations...
  r = .03;
  EdgeHits = 0;
% Draw the square...
  DrawRect(0,0,1,1,'k')
% Draw random disks...
  while EdgeHits < 50
%    Locate a random disk and see if it is inside the square
     x = rand(1); y = rand(1);
     if 0<x-r && x+r<1 && 0<y-r && y+r<1
%       a has the value 1, 2, or 3 with equal probability...
        a = ceil(3*rand(1));
        if a==1
            DrawDisk(x,y,r,'m')  % Magenta disk
        elseif a==2
            DrawDisk(x,y,r,'y')  % Yellow disk
        elseif a==3
            DrawDisk(x,y,r,'c')  % Cyan disk 
        end 
     else
%       The disk intersects the perimeter of the square...
        DrawDisk(x,y,r,'w')
        EdgeHits = EdgeHits +1;
     end
     if EdgeHits<=2
        pause(.2)
     else
         pause(.01)
     end
  end
%!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

hold off
shg