Fast uniformly distributed random points on the surface of a unit hemisphere

前端 未结 7 1040
天涯浪人
天涯浪人 2020-12-15 18:26

I am trying to generate uniform random points on the surface of a unit sphere for a Monte Carlo ray tracing program. When I say uniform I mean the points are uniformly distr

7条回答
  •  失恋的感觉
    2020-12-15 18:59

    This should be quick if you have a fast RNG:

    // RNG::draw() returns a uniformly distributed number between -1 and 1.
    
    void drawSphereSurface(RNG& rng, double& x1, double& x2, double& x3)
    {
        while (true) {
            x1 = rng.draw();
            x2 = rng.draw();
            x3 = rng.draw();
            const double radius = sqrt(x1*x1 + x2*x2 + x3*x3);
            if (radius > 0 && radius < 1) {
                x1 /= radius;
                x2 /= radius;
                x3 /= radius;
                return;
            }
        }   
    }
    

    To speed it up, you can move the sqrt call inside the if block.

提交回复
热议问题