%% MonteCarlo_Triangle
% Approximate area of triangle ${(0,0),(1,0),(0,1)}$
% by Monte Carlo-type simulation.
%% Draw square and triangle T
close all
sqx=[0 1 1 0 0 ]; 
sqy=[0 0 1 1 0];
Tx=[0 1 1 0];
Ty=[0 0 1 0];
plot(sqx,sqy,Tx,Ty)
axis([-.2 1.2 -.1 1.2]);shg

%%

%% Draw points
n = 10000; %  (Test with smaller n.)
X = linspace(0,1,n); % "Deterministic" x-coords on [0,1].
Y = rand(1,n); % Random y-coords on (0,1). 
in = Y<=X;
out=~in;
sum(X(in));

%%
hold on
plot(X(in),Y(in),'r.',X(out),Y(out),'k.');shg
N=sum(in);   % in is a logical vector sum=nr. of ones. 
%N = nnz(in); % Matlab has valmis a function "number of nonzeros"
%             but let's stick to the "basic core"
%% 
disp(['  inside/n  ' '   Area'])
disp([N/n, 1/2])
hold off
%% In case of disk:
% x=rand(...);
% y=rand(...);
% in= ?



