I will end this series of posts on generating uniformly random points inside geometric shapes by considering regular polygons. Regular polygons are equilateral and equiangular. When the number of sides is equal to 4 we have a square (it is fairly easy to generate uniformly distributed random points inside a square). As the number of sides goes to infinity we have a circle (I already showed how to generate uniformly distributed random points inside a circle here).
For all the other regular polygons with n sides you could use the technique presented in this paper to generate uniformly distributed random points. The paper is by Myron Hlynka and Deborah Loach from the Department of Mathematics and Statistics at University of Windsor in Canada.
Friday, 9 November 2012
Uniformly Distributed Random Points Inside a Circular Ring
I will continue my series of posts on generating uniformly distributed points within different shapes by considering a circular ring (a.k.a. annulus or doughnut). Just like my previous post we have the joint probability density function (PDF) of the x and y coordinate of the random points as:
fX,Y(x,y)={1A=1π(R2c2−R2c1)R2c1≤x2+y2≤R2c20otherwise,
where Rc2 is the radius of the outer circle and Rc1 is the radius of the inner circle, and Rc1<Rc2
Using a similar technique as in the previous post we have:
fR,Θ(r,θ)=12π×2rR2c2−R2c1=fΘ(θ)fR(r)
where
fΘ(θ)=12π for 0≤θ≤2π
and
fR(r)=2rR2c2−R2c1 for Rc1≤r≤Rc2.
Therefore, Θ is uniformly distributed between 0 and 2π. The random variable R can be generated by first calculating its cumulative distribution function as
FR(r)=∫rRc12αR2c2−R2c1dα=r2−R2c1R2c2−R2c1,
and then using a uniformly distributed random variable U over the interval [0,1] to get
U=R2−R2c1R2c2−R2c1⟹R=√(R2c2−R2c1)U+R2c1.
The following MATLAB code generates 1000 random numbers inside a circular ring with outer radius 20, and inner radius 10 centered at −30, −40.
Rc2 = 20;
Rc1 = 10;
Xc = -30;
Yc = -40;
theta = rand(1,n)*(2*pi);
r = sqrt((Rc2^2-Rc1^2)*rand(1,n)+Rc1^2);
x = Xc + r.*cos(theta);
y = Yc + r.*sin(theta);
plot(x,y,'.'); axis square
fX,Y(x,y)={1A=1π(R2c2−R2c1)R2c1≤x2+y2≤R2c20otherwise,
where Rc2 is the radius of the outer circle and Rc1 is the radius of the inner circle, and Rc1<Rc2
Using a similar technique as in the previous post we have:
fR,Θ(r,θ)=12π×2rR2c2−R2c1=fΘ(θ)fR(r)
where
fΘ(θ)=12π for 0≤θ≤2π
and
fR(r)=2rR2c2−R2c1 for Rc1≤r≤Rc2.
Therefore, Θ is uniformly distributed between 0 and 2π. The random variable R can be generated by first calculating its cumulative distribution function as
FR(r)=∫rRc12αR2c2−R2c1dα=r2−R2c1R2c2−R2c1,
and then using a uniformly distributed random variable U over the interval [0,1] to get
U=R2−R2c1R2c2−R2c1⟹R=√(R2c2−R2c1)U+R2c1.
The following MATLAB code generates 1000 random numbers inside a circular ring with outer radius 20, and inner radius 10 centered at −30, −40.
%**********************************************n = 10000;
Rc2 = 20;
Rc1 = 10;
Xc = -30;
Yc = -40;
theta = rand(1,n)*(2*pi);
r = sqrt((Rc2^2-Rc1^2)*rand(1,n)+Rc1^2);
x = Xc + r.*cos(theta);
y = Yc + r.*sin(theta);
plot(x,y,'.'); axis square
%**********************************************
Uniformly Distributed Random Points Inside a Circle (2)
So much for posting the answer in a few days! Sorry, I have been Very Very busy.
So here is the answer. Lets assume we have the Cartesian coordinate system with (x,y) representing the points. Then joint probability distribution function (PDF) of our random points inside the circle (i.e. the joint distribution of random variable X and random variable Y representing the x and y coordinate of the random point) is given by:
fX,Y(x,y)={1A=1πR2cx2+y2≤R2c0otherwise,
where Rc is the radius of the circle.
Now lets consider the polar coordinate system where r=g1(x,y)=√x2+y2 and θ=g2(x,y)=arctan(y/x). Also, from inverting the functions g1 and g2 we have x=h1(r,θ)=rcosθ and y=h2(r,θ)=rsinθ. Now calculating the Jacobian of the transformation we have:
J(x,y)=det
Also we have
|J(r,\theta)|=\frac{1}{|J(x,y)|}
The joint PDF of random variables R and \Theta can then be calculated using the joint PDF of X and Y as:
f_{R,\Theta}(r,\theta)= f_{X,Y}\left( h_1(r,\theta),h_2(r,\theta) \right) = \begin{cases} \frac{r}{\pi R_c^2} & 0 \leq \theta \leq 2\pi, 0 \leq r \leq R_c \\ 0 & \text{otherwise} \end{cases}.
We can rewrite the non zero part as:
f_{R,\Theta}(r,\theta) = \frac{1}{2\pi} \times \frac{2r}{R_c^2} = f_\Theta(\theta)f_R(r)
where
f_\Theta(\theta) = \frac{1}{2\pi}
and
f_R(r)=\frac{2r}{R_c^2}.
Therefore, \Theta is uniformly distributed between 0 and 2\pi. The random variable R can be generated by first calculating its cumulative distribution function as
F_R(r) = \int_0^r \frac{2\alpha}{R_c^2}d\alpha = \frac{r^2}{R_c^2},
and then using a uniformly distributed random variable U over the interval [0,1] to get
U = \frac{R^2}{R_c^2} \implies R=R_c \sqrt{U}.
The following MATLAB code generates 1000 random numbers inside a circle with radius 20 centered at -30, -40.
If you don't understand how I did the PDF transformation see
I hope this helps someone out there.
So here is the answer. Lets assume we have the Cartesian coordinate system with (x,y) representing the points. Then joint probability distribution function (PDF) of our random points inside the circle (i.e. the joint distribution of random variable X and random variable Y representing the x and y coordinate of the random point) is given by:
fX,Y(x,y)={1A=1πR2cx2+y2≤R2c0otherwise,
where Rc is the radius of the circle.
Now lets consider the polar coordinate system where r=g1(x,y)=√x2+y2 and θ=g2(x,y)=arctan(y/x). Also, from inverting the functions g1 and g2 we have x=h1(r,θ)=rcosθ and y=h2(r,θ)=rsinθ. Now calculating the Jacobian of the transformation we have:
J(x,y)=det
Also we have
|J(r,\theta)|=\frac{1}{|J(x,y)|}
The joint PDF of random variables R and \Theta can then be calculated using the joint PDF of X and Y as:
f_{R,\Theta}(r,\theta)= f_{X,Y}\left( h_1(r,\theta),h_2(r,\theta) \right) = \begin{cases} \frac{r}{\pi R_c^2} & 0 \leq \theta \leq 2\pi, 0 \leq r \leq R_c \\ 0 & \text{otherwise} \end{cases}.
We can rewrite the non zero part as:
f_{R,\Theta}(r,\theta) = \frac{1}{2\pi} \times \frac{2r}{R_c^2} = f_\Theta(\theta)f_R(r)
where
f_\Theta(\theta) = \frac{1}{2\pi}
and
f_R(r)=\frac{2r}{R_c^2}.
Therefore, \Theta is uniformly distributed between 0 and 2\pi. The random variable R can be generated by first calculating its cumulative distribution function as
F_R(r) = \int_0^r \frac{2\alpha}{R_c^2}d\alpha = \frac{r^2}{R_c^2},
and then using a uniformly distributed random variable U over the interval [0,1] to get
U = \frac{R^2}{R_c^2} \implies R=R_c \sqrt{U}.
The following MATLAB code generates 1000 random numbers inside a circle with radius 20 centered at -30, -40.
%**********************************************
n = 10000;
Rc = 20;
Xc = -30;
Yc = -40;
theta = rand(1,n)*(2*pi);
r = Rc*sqrt(rand(1,n));
x = Xc + r.*cos(theta);
y = Yc + r.*sin(theta);
plot(x,y,'.'); axis square;
%**********************************************
If you don't understand how I did the PDF transformation see
Chapter 6 (6.2.3 to be exact) or
Chapter 8 (8.3 to be exact).
I hope this helps someone out there.
Subscribe to:
Posts (Atom)