12

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?

6
  • 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? May 14, 2013 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, 2013 at 2:13
  • Look it up. Maybe you mean the TikZ library 3d? And this library already provides a xyz spherical coordinate system. May 14, 2013 at 2:19
  • @Qrrbrbirlbel in the manual it says on ch 5 that spherical polar coordinates is on the todo list.
    – dustin
    May 14, 2013 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. May 14, 2013 at 2:28

2 Answers 2

16

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!
  \pgfqkeys{/tikz/cs}{angle=0,radius=0,latitude=0,#1}%
  \pgfpointspherical{\tikz@cs@angle}{\tikz@cs@latitude}{\tikz@cs@xradius}% fix \tikz@cs@radius to \tikz@cs@xradius
}
\makeatother

\tikzset{my color/.code=\pgfmathparse{(#1+90)/180*100}\pgfkeysalso{every path/.style={color=red!\pgfmathresult!blue}}}
\begin{document}
\begin{tikzpicture}[radius=+0.4pt]% (this is the radius of little dots on the lines)
\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}

Output

enter image description here

enter image description here

3
  • Can you provide a brief example on how to draw an axis (like x-axis)? I understand how it works but would definitely help me having an example like that (relatively easy)
    – Peluche
    Feb 25 at 18:46
  • @Peluche I'm no expert on 3d in TikZ. If you want real plotting, use the pgfplots package (or the datavisualization library). Otherwise \draw (0,0,0) -- (10,0,0); should draw a x axis in the current coordinate system. Feb 25 at 18:54
  • Yes, yes. But what I meant was drawing the x-axis by using spherical coordinates that you've defined above (I want the example to understand better and draw other things in this coordinates)
    – Peluche
    Feb 25 at 19:06
9

I believe this is an interesting question.

First, I want to point out that the notation for physicist is not the same as for mathematicians. What physicist call theta (ϴ), mathematicians call phi (ϕ) and vice-versa. I refer the reader to the Wikipedia website for the conventions used. The macro is simple and I include it next with the convention asked here.

\newcommand{\sphToCart}[3]
        {
          \def\rpar{#1}
          \def\thetapar{#2}
          \def\phipar{#3}

          \pgfmathsetmacro{\x}{\rpar*sin(\phipar)*cos(\thetapar)}
          \pgfmathsetmacro{\y}{\rpar*sin(\phipar)*sin(\thetapar)}
          \pgfmathsetmacro{\z}{\rpar*cos(\phipar)}
        }

Here is complete example where we use this macro several times to create a spherical triangle.

\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{tikz-qtree}
\usepackage{tkz-berge}
\usepackage{tikz-3dplot}
\usetikzlibrary{calc,3d,decorations.markings, backgrounds, positioning,intersections,shapes}


 \newcommand{\sphToCart}[3]
        {
          \def\rpar{#1}
          \def\thetapar{#2}
          \def\phipar{#3}

          \pgfmathsetmacro{\x}{\rpar*sin(\phipar)*cos(\thetapar)}
          \pgfmathsetmacro{\y}{\rpar*sin(\phipar)*sin(\thetapar)}
          \pgfmathsetmacro{\z}{\rpar*cos(\phipar)}
        }


\begin{document}

\begin{tikzpicture}[scale=1.3]
  \coordinate (O) at (0,0,0);

  \tdplotsetmaincoords{60}{135}
  \pgfmathsetmacro\R{sqrt(3)} 
  \fill[ball color=white!10, opacity=0.2, name path global=C] (O) 
      circle (\R); % 3D lighting effect
  \begin{scope}[tdplot_main_coords, shift={(0,0)}]
    \pgfmathsetmacro\R{sqrt(3)} 
    \pgfmathsetmacro{\thetavec}{0};
    \pgfmathsetmacro{\phivec}{0};
    \pgfmathsetmacro{\gammav}{0};
    \tdplotsetrotatedcoords{\phivec}{\thetavec}{\gammav};


    % draw point with azimuth -20 degrees, polar angle 90
    \def\thetaA{-20}
    \def\phiA{90}
    \sphToCart{\R}{\thetaA}{\phiA}
    \coordinate (A) at (\x,\y,\z);

    % save legend location
    \pgfmathsetmacro{\dx}{\x+1.2};
    \pgfmathsetmacro{\dy}{\y+0.9};
    \pgfmathsetmacro{\dz}{\z-1.0};


    \node[] at (\dx,\dy,\dz) {Point $A:( r=\R, \theta=\thetaA, \phi=\phiA)$};
    \node[yshift=-5mm, xshift=6mm] at (\dx,\dy,\dz) 
        { $( x=\x, y=\y, z=\z)$};


    \def\thetaA{110}
    \def\phiA{90}
    \sphToCart{\R}{\thetaA}{\phiA}
    \coordinate (B) at (\x,\y,\z);

    % save legend location  (relative to this point)
    \pgfmathsetmacro{\dx}{\x-1.2};
    \pgfmathsetmacro{\dy}{\y+2.5};
    \pgfmathsetmacro{\dz}{\z-1.0};

    \node[] at (\dx,\dy,\dz) {Point $B:( r=\R, \theta=\thetaA, \phi=\phiA)$};
    \node[yshift=-5mm, xshift=6mm] at (\dx,\dy,\dz) 
        { $( x=\x, y=\y, z=\z)$};


    \def\thetaA{70}
    \def\phiA{-20}
    \sphToCart{\R}{\thetaA}{\phiA}
    \coordinate (C) at (\x,\y,\z);


    % save legend location  (relative to this point)
    \pgfmathsetmacro{\dx}{\x-2};
    \pgfmathsetmacro{\dy}{\y+3};
    \pgfmathsetmacro{\dz}{\z+1.0};

    \node[] at (\dx,\dy,\dz) {Point $C:( r=\R, \theta=\thetaA, \phi=\phiA)$};
    \node[yshift=-5mm, xshift=6mm] at (\dx,\dy,\dz) 
        { $( x=\x, y=\y, z=\z)$};


    \draw[fill=red, opacity=0.4] (A) to [bend right] (B) 
        to [bend right] (C) to [bend right]  (A);






    \draw[-latex, color=red, line width=1] (O)--(A) node[anchor=east] {\tiny $A$};
    \draw[-latex, color=red, line width=1] (O)--(B) node[anchor=west] {\tiny $B$};
    \draw[-latex, color=red, line width=1] (O)--(C) node[anchor=south] {\tiny $C$};

    %legend


    % axis
    \coordinate (XX) at (3,0,0) ;
    \coordinate (YY) at (0,3,0) ;
    \coordinate (ZZ) at (0,0,3) ;


    \draw[-latex] (O) -- (XX) node[anchor=east] {$X$};
    \draw[-latex] (O) -- (YY) node[anchor=north] {$Y$};
    \draw[-latex] (O) -- (ZZ) node[anchor=south] {$Z$};

  \end{scope}

\end{tikzpicture}

\end{document}

The figure is next:

Spherical Triangle

Please observe that I included legends showing the coordinates in both systems: Spherical and Cartesians.

3
  • @Tom Bombadil : How do you write Greek fonts so here? Thanks for your corrections. Nov 23, 2015 at 14:00
  • Depending on your OS, there is a program where you can choose special symbols to copy and use elsewhere. In Windows it's "character map" (in Programs --> Accessories --> System Tools), with Linux (GNOME/Unity) its "Gucharmap" (in Applications --> Accessories --> Character Map). If all else fails, you can also copy the chars directly from the wikipedia article you referenced, but some of them are not glyphs but rather SVG images so this will not allways work. Furthermore, here you can draw a character and get the unicode representation. Nov 23, 2015 at 14:27
  • 1
    @Tom Bombadil: Thanks for your answer. While waiting on your answer I tried something and I believe it worked. I copy/pasted from a post in StackExchange Mathematics the following small expression " ϵ>0 " If you see it, then this method also works. I can create, the equation I want to add into the text here, in the StackExchange of the Mathematics page then copy/paste here. Thanks. Nov 23, 2015 at 16:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .