The number of nested for
loop reflects the dimensionality of the input, not output.
You would need two for
loops if you were to iterate over points on a sphere, e.g., taking the temperature at various places on the globe. In this case the input space is two-dimensional (surface of the sphere). The fact that the output (temperature) is a scalar does not matter. If you sample temperature, humidity, and pressure (thus outputting a 3d vector), you still run only two nested for
loops to cover the sphere.
In your problem the input is a one-dimensional array of tags, while the output consists of points on a sphere. As the tags come one by one in the for
loop, the code places them on a sphere. A naive idea would be something like $\phi=\pi i/n$, $\theta=2\pi i/n$ where $n$ is the number of tags and $i$ is the index of the current tag. But this would result in all tags lying along the curve $\theta=2\phi$ on the sphere, which would not look nice. To distribute the tags uniformly, it suffices to break the pattern in either $\phi$ or $\theta$.
The code keeps a simple pattern in $\phi$: they decrease steadily, so as to make the $z$-coordinates grow from -radius
to radius
. The purpose of formula with acos
is to produce values of $\phi$ which will later make $z$ equally spaces (because $z$ involves $\cos\phi$). Then the code does something pretty wierd with $\theta$, using a factor of $\sqrt{\pi n}$. I think all that matters here is that $\sqrt{\pi n}$ is a large irrational number. There's a good chance that the author of the code tried something different first, did not like the result, slapped the square root on the formula, and saw that it's better this way. I don't think this appearance of sqrt
is related to the sunflower algorithm linked to by Sasha, because we don't have the index $i$ under sqrt
, just a constant.