0
$\begingroup$

I am not an expert in mathematics, I am only a young programmer. I am trying to construct a spherical tag cloud and I've found this formula:

for i,t in enumerate(tags)     phi = acos((2* (i+1) -1) / tag_num -1)     theta = sqrt(tag_num * pi) * phi      x = radius * cos(theta) * sin(phi)     y = radius * sin(theta) * sin(phi)     z = radius * cos(phi)      t.setPosition(x,y,z) 

and it works fine. but I thought, for a spherical disposition, have to do 2 nested for: one for latitude and one for longitude. What kind of formula do I use in these codes? Can you explain me what this acos and sqrt do?

  • 0
    does that source help you in finding an answer?2011-10-07

1 Answers 1

1

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.