I am using this Wikipedia page translation formulae when writing my Cartesian2sherical function (sorry I don't have enough reputaton to post in-line image, but here is the link, and I will rewrite it too): link_to_wiki_formulae_image
$$r = \sqrt{x^2 + y^2 +z^2}$$ $$\theta = \arctan(\frac{\sqrt{x^2+y^2}}{z})$$ $$\phi = \arctan(\frac{y}{x}) =\arccos(\frac{x}{\sqrt{x^2+y^2}}) = \arcsin(\frac{y}{\sqrt{x^2+y^2}})$$
However, the above formulae do not handle the case where x or y is negative: the first two formulae squares x and y so the signs do not matter. Formula 3 falls apart when x or y are negative. For example, when x = -1 and y = -1, the 3 equalities in equation 3 don't even hold:
$$\arctan(\frac{y}{x}) = \arctan(1) = 0.7853982$$ $$\arccos(\frac{x}{\sqrt{x^2+y^2}}) = \arccos(-0.7071068) = 2.356194$$ $$\arcsin(\frac{y}{\sqrt{x^2+y^2}}) = \arcsin(-0.7071068) = -0.7853982$$
Does that mean the Wikipedia formulation is lacking some pre-assumptions of the x and y value? What would I change the formulae if I wanted to handel the case where x and y could have arbitrary signs? Thanks!
If you follow the link from that page to the main article on spherical coordinates, in particular, to the Cartesian Coordinates section of that article, you will find the following caveat:
This is a common gotcha when working with the inverse tangent function as part of a coordinate transformation.