This just a rough sketch to show the general behaviour to answer a question. The details should be refined. Px, Py, Pz, are not really needed, but are there to clarify the spherical coordinates. Edit: I changed the clumsy variables so latitudes is variable instead of angleStep.
Spherical coordinates: https://en.wikipedia.org/wiki/Spherical_coordinate_system