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


i trying generate random points on sphere filled cube. because had no idea how started 2d. (a circle filled quadrat.)

what trying do: generating random points inside outer circle, outside green square. enter image description here

basically in blue areas.

the square located @ (-1|-1),(1|-1),(1|1),(-1|1).
circle has radius of r = sqrt(2) , centered @ (0|0).

i have scripts to:

  • generate random point on circle (uniformly):

    float = 2 * mathutils.pi * mathutils.random(1f); // angle between 0 , 2pi float r = radius * math.sqrt(mathutils.random(0, 1f) float x = r * mathutils.cos(a); float y = r * mathutils.sin(a); 
  • calculating radius given angle form square:

    float r = (1/math.sqrt(2)) / mathutils.cos(((a+45)%90-45)/180*mathutils.pi); 

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

before asks: know re-generate points inside green square until 1 outside, don't want way.

i appreciate help. thank :)

it rather hard generate points in region of sphere outside cube (caps , wedges), rejecting method looks reasonable.

but can diminish number of useless points, generating points in ring only in 2d case , in spherical shell in 3d case.

so pseudocode might

 //2d  squaredr  = randomuniforminrange(0.5, 1)  r = sqrt(squaredr)   //3d  cubedr  = randomuniforminrange(pow(3, -3/2), 1)  r = pow(cubedr, 1/3)   //generate point on circle or on sphere radius r   if abs(x) > sqrt(2)/2 or sqrt(3)/3  , on - reject 

having r, can generate point on sphere using approach here


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -