Search code examples
3dgeometryprocessingcubeparticles

generation of 3D random points on the surface of a cube


I'm trying to randomly and uniformly generate points on a cube surface in processing. I'm trying to make an animation so I'd need the x, y and z final position of the points.

Any suggestions? Thanks.


Solution

  • Just combine three uniform random distributions. This method assumes you have a unit cube [0..1]^3. If that is not the case, just scale and offset the points after you generate them.

    1. Pick a cube side by getting an integer from [0..6)
    2. Pick an x coordinate in that side's plane
    3. Pick an y coordinate in that side's plane

    Here's some C++-ish pseudocode:

    vec3 result;
    int s=randomSide(); // returns 0 to 5, uniformly distributed
    int c=s%3; // get the axis perpendicular to the side you just picked
    
    result[c]=s>2 ? 1.f : 0.f;
    result[(c+1)%3]=random01();
    result[(c+2)%3]=random01();
    

    If you have a more general box instead of a cube, you have to bias the side-picking according to the areas of the box sides.