POV-Ray : Newsgroups : povray.general : Slightly faster VRand_On/In_Sphere() : Re: Slightly faster VRand_On/In_Sphere() Server Time
4 Aug 2024 02:18:32 EDT (-0400)
  Re: Slightly faster VRand_On/In_Sphere()  
From: David Wallace
Date: 17 Nov 2003 18:30:06
Message: <3fb959fe@news.povray.org>
#macro EvenSpherePoint(pos, rad, sd)
    #local irad = sqrt(min(rand(sd),0);
    #local long = rand(sd)*2*pi;
    #local hemi = select(rand(sd)-0.5, -1, 1)
    #local azim = pow(rand(sd), x)*pi*0.5;
    #local dir = <cos(azim)*cos(long), hemi*sin(azim), cos(azim)*sin(long)>;
    #local pnt = pos+dir*irad*rad;
    pnt
#end

What should x be in order to get a truly even distribution (or at least
something close)?

I set up an Excel spreadsheet comparing cos(x*2/pi) to 1-(x*x)^n
for -1<=x<=1 in steps of 0.01 and 0.002.  I calculated the sum of the
differences between the functions, essentially looking of a value of n where
the integrals of these functions were identical.  Excel came up with
0.875969064, or 1/(pi-2).  x in the phi function is double that.

"Wolfgang Wieser" <wwi### [at] gmxde> wrote in message
news:3fae530a@news.povray.org...
> Wolfgang Wieser wrote:
> > B: VRand_In_Sphere(), full sphere.
> >
> > For the full sphere, there is not much difference, just the
> > radius is chosen to be randomly distributed using
> > pow(rand(Stream),1/3)).
> > I don't believe that this is right until someone (me?) proves
> > it to be right - ;)
> >
> As shown last time, we already have a function for the uniform
> distribution on a sphere surface. For the VRand_In_Sphere() function,
> it is therefore sufficient to prove that the radius calculation
> (i.e. vector length) is correct.
>
> For that purpose, it must be shown that the probability for the
> vector being of a certain length range is equal to the volume
> enclosed by that radius range (divided by 4*pi/3, of course).
>
> The sphere volume between radii r1 and r2 (r2>r1) is:
>   V = 4*pi/3 * (r2^3 - r1^3)
>
> The vector length l is calculated as l=a^(1/3) with a=rand(Stream),
> hence l^3 is uniformly distributed in the range 0..1.
>
> The probability for the vector to end in the volume V (i.e. radius
> range r1..r2) is:
>   P(vector in V) = P(r1 <= l < r2) = P(r1^3 <= l^3 < r2^3) =
> and as l^3=a:
>   = P(r1^3 <= a < r2^3) = r2^3 - r1^3
>
> Hence, the quotient (probability per volume) is:
>   P/V = 3/(4*Pi)
> ...which is what we want.
>
> - q.e.d.
>
> So, it works; VRand_In_Sphere() is uniformly distributed in the sphere.
>
> Any doubts or problems?
>
> Wolfgang
>


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.