Random vectors directions within a sphere

Way to get the random vectors direction vec3 (x, y, z) within a sphere with a fixed radius 1.0?

Use three angles:
$$
-180° <= a1 < +180° $$ $$
0° <= a2 <= 90° $$ $$
0° <= a3 <= 90°
$$

Steps:
1. Select a random direction in the XY plane: {cos (a 3), sin (a 3) 0}.
2. turn the Z axis to a random angle a2 around the random vector {cos (a1), sin (a1), 0}, which lies in the XY plane.

rotX(p) := matrix(
[1,0,0],
[0,cos(p),-sin(p)],
[0,sin(p),cos(p)]
);
rotZ(p) := matrix(
[cos(p),-sin(p),0],
[sin(p),cos(p),0],
[0,0,1]
);
dir(p) := matrix(
[ cos(p) ],
[ sin(p) ],
[ 0 ]
);
calc_dir(p1, p2, p3) := rotX(p1) . rotZ(p2) . dir(p3);

Current example you can try in Maxima Lisp.
The result of matrix multiplication:
$$
vx = cos (p2) * cos (p3) -sin (p2) * sin (p3) $$ $$
vy = cos (p1) * (cos (p2) * sin (p3) + sin (p2) * cos (p3))$$ $$
vz = sin (p1) * (cos (p2) * sin (p3) + sin (p2) * cos (p3)) $$

And this is a generated image, as you you see no visible sphere poles, wich is great!

sphere-good

We can use it in the raytracing and gi.

Leave a Reply

Your email address will not be published. Required fields are marked *