% Scoring
%
%  If cloud does not have N points, -2.
%  Failing to convert from degrees to radians, -1.
%  Generating number of points in [-a,a]x[-b,b] inconsistent with
%    relative areas of rectange and ellipse, -2.
%  Failing to vectorize, up to -2.
%  Generating non-uniform distribution, -2.

function [x,y] = Cloud(a,b,phi,x0,y0,N)
% a,b,x0,y0,phi are scalars with 0<b<=a and -180<=phi<=180
% N is a positive integer with N>=100
% x and y are column N-vectors that specify a cloud of uniformly distributed
% random points that are inside or on E(a,b,phi,x0,y0).

% Generate m random points inside the rectangle with
%    vertices (-b,a),(b,a),(-a,b),(-a,-b).
% Since we want at least N points inside E(a,b) we note that
%    m/N should be approximately areaRect/areaEllipse.
%    We incorporate a small scale factor to ensure that statistically
%    we get at least N points inside E
m = ceil(1.1*N*4/pi);
x = a*(-1+2*rand(m,1));
y = b*(-1+2*rand(m,1));
% Note that a point (u,v) is inside E(a,b) if 
%        sqrt((u+f)^2+v^2) + sqrt((u-f)^2+v^2) <= s 
% where
s = 2*a;
f = sqrt(a^2-b^2);
% Get the set of indices of points inside E(a,b)...
idx = find(sqrt((x+f).^2+y.^2) + sqrt((x-f).^2 + y.^2)<=s);
idx = idx(1:N);
x = x(idx);
y = y(idx);
% Rotate and translate...
phi = pi*phi/180;
B = [x y]*[cos(phi) sin(phi); -sin(phi) cos(phi)];
x = B(:,1)+x0;
y = B(:,2)+y0;

