Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)


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?

share|cite|improve this question
I can't quite see where the i and tag_num came from, and an angle with a $\sqrt \pi$ factor sounds fishy to me. In any event the effort in z looks wasted, since you have an arccosine and then a cosine... where precisely did you find these bits? – J. M. Sep 30 '11 at 14:18
You may find the blog post How I made wine glasses from sunflowers by Cris Carlson very relevant. – Sasha Sep 30 '11 at 14:46
@J.M. tag_num is the total number of tag, and i is a paramater that goes from 0 to num_tag-1 – nkint Oct 1 '11 at 21:30
I'm going to repeat J.M.'s last question and ask again, where exactly did you find this formula? – Rahul Oct 1 '11 at 22:35
is it so important? i found it here: and the formula is at line 72 – nkint Oct 3 '11 at 22:44

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.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.