Now the result with a uniform area sampling of the upper hemisphere
functionuniform_hemisphere(){const u_1 = Math.random();const u_2 = Math.random();// Math.acos(u_1) is also validconst theta = Math.acos(1.0- u_1);const phi = u_2 *2.0* Math.PI;return[theta,phi]}
Algorithm
Generate uniform points on the hemisphere
Choose uniform numbers u1,u2 in [0,1]
Calculate θ=cos−1(1−u1) or θ=cos−1(u1)
Calculate ϕ=2πu2
Optionally convert to cartesian coordinates with:
xyz=sinθcosϕ=sinθsinϕ=cosθ
PDF Used In Monte Carlo Integration
p(ω)=2π1
p(θ,ϕ)=2π1sinθ
Derivation
This is a special case of the general form discussed in the next section, but is shown as an example in more detail.
We want the pdf to be uniform over the unit hemisphere, so it will just be some constant for each solid angle ⇒p(ω)=c
∫Ωp(ω)dω=∫Ωcdω=c∫Ωdω=1
The easy way:
We know that the solid angle of the hemisphere is 2π . In general, for uniform sampling the pdf is just Vol(Ω)1 , where Vol is the volume of the sample space.