# spherical coordinates in tikz 3d

Can we specify direction in spherical coordinates?

I know we can do polar (angle:radius) but what is we are using tikz-3d and want to specify (r, theta, phi) where theta is the azimuthal angle?

-
Does it need to be implicit (this will be harder as one would delve into TikZ handler/parsing algorithm) or would a custom coordinate system work for you? Does “tikz-3d” refer to the tikz-3dplot package which provides something like this? –  Qrrbrbirlbel May 14 '13 at 2:05
@Qrrbrbirlbel I didn't know the exact package name but I have it standard in my preamble. What do you mean by custom coordinate system? Would it be portable or would it have to be recreated every time? –  dustin May 14 '13 at 2:13
Look it up. Maybe you mean the TikZ library 3d? And this library already provides a xyz spherical coordinate system. –  Qrrbrbirlbel May 14 '13 at 2:19
@Qrrbrbirlbel in the manual it says on ch 5 that spherical polar coordinates is on the todo list. –  dustin May 14 '13 at 2:25
What version of PGF/TikZ are you using (\pgfversion or the first page of the manual)? The manual of the current stable release version 2.10 takes about chains in chapter 5. Granted, the current manual doesn’t tell about xyz spherical either. –  Qrrbrbirlbel May 14 '13 at 2:28
show 1 more comment

TikZ provides with the apparently undocumented library 3d a xyz spherical coordinate system.

It accepts the keys radius (now fixed), angle = longitude, latitude and with my help rho and theta.

The first TikZ picture shows my example, the second a PGF picture example of the TikZ/PGF manual.

(I removed the 3d library again and implemented the xyz spherical similar to how it is done in tikzlibrary3d.code.tex. It simply uses the \pgfpointspherical macro, which does all the calculations and uses the appropriate vectors.)

## Code

\documentclass[tikz,convert=false]{standalone}
%\usetikzlibrary{3d}
\makeatletter
\pgfqkeys{/tikz/cs}{
latitude/.store in=\tikz@cs@latitude,% not needed with '3d' library
longitude/.style={angle={#1}},% not needed with '3d' library
theta/.style={latitude={#1}},
rho/.style={angle={#1}}
}
\tikzdeclarecoordinatesystem{xyz spherical}{% needed even with '3d' library!
}
\makeatother

\tikzset{my color/.code=\pgfmathparse{(#1+90)/180*100}\pgfkeysalso{every path/.style={color=red!\pgfmathresult!blue}}}
\begin{document}
\foreach \lat in {-90,-80,...,90} {
\tikzset{my color=\lat}
\foreach \lon in {0,10,...,359} {
\filldraw (xyz spherical cs: radius=1, angle=\lon,    latitude=\lat) circle[]
-- (xyz spherical cs: radius=1, angle=\lon+10, latitude=\lat);
}}
\end{tikzpicture}

\begin{tikzpicture}
\foreach \lat in {-90,-75,...,30}
\filldraw[line join=round, fill=lightgray]
\foreach \lon in {0,20,...,359} {
(xyz spherical cs: radius=1, rho=\lon,    theta=\lat   )
-- (xyz spherical cs: radius=1, rho=\lon+20, theta=\lat   )
-- (xyz spherical cs: radius=1, rho=\lon+20, theta=\lat+15)
-- (xyz spherical cs: radius=1, rho=\lon,    theta=\lat+15)
-- cycle
};
\end{tikzpicture}
\end{document}

-