function myPi = approxPi(nDarts)
% Approximate Pi using Monte Carlo simulations
% Usage:  myPi = approxPi(nDarts)
%   NDARTS = number of "darts" thrown
%   myPi = Monte Carlo approximation of Pi

L = 10;  % length of square

% Throw darts in L-by-L area, centered at 0,0
throws = L*rand(nDarts,2) - L/2;
x = throws(:,1);  % x-coordinates of darts
y = throws(:,2);  % y-coordinates of darts

% Location of darts relative to center
  dist = sqrt(x.^2+y.^2);  % distance from center
  nIn = sum(dist <= L/2);  % #darts inside circle
  
myPi = 4*nIn/nDarts;

% Plot darts in domain
% YOU ARE NOT RESPONSIBLE FOR LEARNING AXIS FORMATS
  % Circle data
    theta = 0:0.2:2*pi;
    xcircle = cos(theta)*L/2;
    ycircle = sin(theta)*L/2;
plot(x,y,'*',xcircle,ycircle,'r','linewidth',2)
axis([-L/2 L/2 -L/2 L/2]);  axis('square');
title(['Pi = ' num2str(myPi)]);
