Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm: Calculate pseudo-random point inside an ellipse

For a simple particle system I'm making, I need to, given an ellipse with width and height, calculate a random point X, Y which lies in that ellipse.

Now I'm not the best at maths, so I wanted to ask here if anybody could point me in the right direction.

Maybe the right way is to choose a random float in the range of the width, take it for X and from it calculate the Y value?

like image 641
L. Nlson Avatar asked Apr 03 '11 10:04

L. Nlson


1 Answers

  1. Generate a random point inside a circle of radius 1. This can be done by taking a random angle phi in the interval [0, 2*pi) and a random value rho in the interval [0, 1) and compute

    x = sqrt(rho) * cos(phi)
    y = sqrt(rho) * sin(phi)
    

    The square root in the formula ensures a uniform distribution inside the circle.

  2. Scale x and y to the dimensions of the ellipse

    x = x * width/2.0
    y = y * height/2.0
    
like image 115
Sven Marnach Avatar answered Sep 21 '22 03:09

Sven Marnach