jeudi 6 avril 2017

Generating uniform random numbers on a sphere/circle filled with a cube/square

I am trying to generate random points on a sphere that is filled with a cube. Because I had no idea how to do that i started with 2d. (A circle filled with a quadrat.)

What I am trying to do: Generating random points inside the outer circle, but outside the green square. enter image description here

Basically in the blue areas.

The square is located at (-1|-1),(1|-1),(1|1),(-1|1).
The circle has a radius of r = sqrt(2) and is centered at (0|0).

I already have scripts to:

  • generate a random point on a circle (uniformly):

    float a = 2 * MathUtils.PI * MathUtils.random(1f); // angle between 0 and 2pi
    float r = radius * Math.sqrt(MathUtils.random(0, 1f)
    float x = r * MathUtils.cos(a);
    float y = r * MathUtils.sin(a);
    
    
  • calculating the radius for a given angle to form a square:

    float r = (1/Math.sqrt(2)) / MathUtils.cos(((a+45)%90-45)/180*MathUtils.PI);
    
    

    with (1/Math.sqrt(2)) being half the side length of the square

Before anyone asks: I know that I could just re-generate points which are inside the green square until I get one that is outside, but I don't want to do it this way.

I appreciate any help. Thank you :)




Aucun commentaire:

Enregistrer un commentaire