% by Konstantin Ninidze
% Monte Carlo method for estimating the area for a plane figure.
% Suppose we need to calculate an area S(A) of a figure A, bounded by a
% Jordan curve (which in our case is constructed by a cubic spline
% approximation). We'll place A inside a square of known area S(sq) and
% then place a known number of points (N) at random locations inside the
% square. After this we'll count the number of the random points (M) that lie
% inside the contour A and taking in mind, that the area S(A) of the object
% A is proportional to the number of points (M) that lie inside square
% we'll obtain a formula for the area: S(A)=S(sq)*M/N.
% A slight modification of Matlab "getcurve" function is needed to obtaine
% the closed curve, see mygetcurve1.
in = inpolygon(x,y,X(1,:),X(2,:));
title([' area estimation is ',num2str(S)])
xlabel([' N= ',num2str(N)])
function [xy, spcv] = mygetcurve1
% do not repeat points!
w = [-1 1 -1 1];
axis(w), hold on, grid on
title('Use mouse clicks to pick points INSIDE the gridded area.')
pts = line('Xdata',NaN,'Ydata',NaN,'marker','o','erase','none');
maxpnts = 100; xy = zeros(2,maxpnts);
[x,y] = ginput(1);
if isempty(x)||x<w(1)||x>w(2)||y<w(3)||y>w(4), break, end
xy(:,j) = [x;y];
xlabel('When you are done, click OUTSIDE the gridded area.')
if j>1, break, end
xlabel(' You need to click INSIDE the gridded area at least once')
if norm(xy(:,1)-xy(:,j-1))<.05, xy(:,j-1)=xy(:,1); end
spcv = cscvn(xy); fnplt(spcv)