share
TeX - LaTeXHow to draw spherical geometries with TeX?
[+53] [2] Ingo
[2012-04-26 14:38:38]
[ tikz-pgf graphics 3d ]
[ https://tex.stackexchange.com/questions/53445/how-to-draw-spherical-geometries-with-tex ]

I am working in the field of Astrodynamics and have to do many computations using spherical geometry. To visualize some of the concepts I would like to prepare drawings of things like spherical triangles, planes around the earth, or circles projected onto the sphere. Here are some examples (1):

Examples of plots Examples of plots

How is this possible to do in TeX? I had a look whether this could be done with TikZ but could not find any comparable examples. I am convinced that in theory it should be possible to prepare a TikZ extension that does things like this rather simply, but at the moment I am looking for an already existing solution to accomplish such tasks. When I get more experienced with making such plots I might consider writing a basic package.

The question is thus actually threefold:

  1. Is it possible to make such plots in LaTeX with current packages?
  2. If yes, with TikZ? Could you give me an example how to start?
  3. If not, what software would you recommend to prepare and include such kind of 3D graphics?

Please note that this is not the kind of question you sometimes get here that asks "Please do this for me!" I honestly tried solving this problem but did not get far at all. It would be really great to get an answer, a lot of people in my field would benefit from this.

(1) Wertz, James R. (2009). Orbit & Constellation Design & Management. New York: Springer.

(8) You should work with Altermundus! See How can I draw an arc from point A -> B on a 3D sphere in TikZ?. I'd love to see this come together! - Jake
(1) I have great respect for Altermunduns and this would be my first package, so I'd rather get started first ;-). It does not seem like an easy issue though. - Ingo
drawing the spheres could be done with pgfplots such as \documentclass{article} \usepackage{pgfplots} \begin{document} \begin{tikzpicture} \begin{axis} \addplot3[mesh, samples=20, domain=0:2*pi,y domain=0:pi, ] ({cos(deg(x)) * sin(deg(y))}, {sin(deg(x)) * sin(deg(y))}, {cos(deg(y))}); \end{axis} \end{tikzpicture} \end{document} not sure about the other bits though :) - cmhughes
(6) I started some works about spherical geometry and the link given by Jake is the first step. I think it's possible with TikZ to create all the pictures of the question. Actually I try to create a macro to draw spherical triangles. - Alain Matthes
I forgot to give this link melusine.eu.org/syracuse/pstricks/pst-solides3d/bonus/… You can work with pst-solides3d and pst-eucl - Alain Matthes
Another possibility is to use Sweave with the R package GeoMap. Which allows you to generate a spherical projection of the globe with map included. Have not used it except to verify it compiles ok. - R. Schumacher
There are MetaPost examples there which could possibly help you. - morbusg
I've never drawn 3D stuff with TikZ, but I know it's possible. You can find some quite good examples at TeXamle.net under the 3D tag. This might be a good starting point. - szantaii
Have you seen tikz-3dplot? Be sure to use the updated (2010) version, which has been worked into tikz, not the standalone version you see on texample.net - bobobobo
The question keeps getting upvotes and we just hit the 25 vote mark! Does anyone have any news? Sadly I could not find much time to work on this issue and resorted to mayavi for such purposes, which admittedly is a much less elegant solution. - Ingo
Is it possible to raise the bounty? - Ingo
(1) If you're willing to go outside TikZ, Asymptote is very good at 3d stuff and should be able to draw everything fairly easily that does not require information about continents, etc. If you have a file that contains geographical information, Asymptote can also read data from files. - Charles Staats
@Ingo : The basic elements of geometry are points and lines (segments too). This is true for plane and spherical geometry. In the case of spherical geometry points are easy to draw. Lines are great circles on a sphere. There is a duality between points and lines in the sphere. For each point (north pole) there is a unique equator and vice-versa. The easiest way to draw a line (great circle) in a sphere that I can think of is by providing is north pole. Please look at this link for that: tex.stackexchange.com/questions/277810/… - Herman Jaramillo
[+20] [2015-11-11 01:48:12] Herman Jaramillo

This is a long answer since there are good tools for spherical geometry scattered all around, so I created a few sections addressing those tools.

tikz-3dplot:especially tdplotdrawarc

I suggest to use \tdplotdrawarc . This is explained in the TikZ and PGF Manual. You need to define three angles $\alpha$, $\beta$ and $\gamma$ for the arc, theen the radius, origin, initial and final angle. I include here and example with the angles used. With this example you can build new examples explaining other angle combinations.

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{enumerate}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{tikz-3dplot}
\usepackage{hyperref}
\usepackage{pgfplots}
\usetikzlibrary{calc,3d,intersections, positioning,intersections,shapes}

\newcommand{\InterSec}[3]{%
  \path[name intersections={of=#1 and #2, by=#3, sort by=#1,total=\t}]
  \pgfextra{\xdef\InterNb{\t}}; }

  \begin{document}
      \begin{center}
          \begin{tikzpicture}[scale=2]
            \pgfmathsetmacro\R{sqrt(3)} 
            \fill[ball color=white!10, opacity=0.1] (0,0,0) circle (\R); % 3D lighting effect
            \tdplotsetmaincoords{80}{110}
            \begin{scope}[tdplot_main_coords, shift={(0,0)}]
              \coordinate (O) at (0,0,0);
               % circle around Cp

            % rotate circle to make it look better.

              \pgfmathsetmacro{\thetavec}{0}
              \pgfmathsetmacro{\phivec}{0}
              \tdplotsetrotatedcoords{\phivec}{\thetavec}{0}
              \tdplotdrawarc[tdplot_rotated_coords,color=blue]{(O)}{\R}{-70}{110}{}{}
              \tdplotdrawarc[tdplot_rotated_coords,color=blue, dashed]{(O)}{\R}{110}{290}{}{}
              \node[] at (-1,2,1) {\textcolor{blue}{\scriptsize 
              $\alpha=\thetavec \, ,  \, $\beta=\phivec}};

              \pgfmathsetmacro{\thetavec}{90};
              \tdplotsetrotatedcoords{\phivec}{\thetavec}{0};
              \tdplotdrawarc[tdplot_rotated_coords,color=brown]{(O)}{\R}{0}{180}{}{};
              \tdplotdrawarc[tdplot_rotated_coords,color=brown, dashed]{(O)}{\R}{180}{360}{}{};
              \node[yshift=4 mm] at (-1,2,1) {\textcolor{brown}{\scriptsize $\alpha=\thetavec \, ,  \, 
                  $\beta=\phivec}};

              \pgfmathsetmacro{\phivec}{90}
              \tdplotsetrotatedcoords{\phivec}{\thetavec}{0};
              \tdplotdrawarc[tdplot_rotated_coords,color=red]{(O)}{\R}{0}{180}{}{};
              \tdplotdrawarc[tdplot_rotated_coords,color=red, dashed]{(O)}{\R}{180}{360}{}{};
              \node[yshift=8 mm] at (-1,2,1) {\textcolor{red}{\scriptsize $\alpha=\thetavec \, ,  \, 
                  $\beta=\phivec}};



              %axis
              \coordinate (X) at (5,0,0) ;
              \coordinate (Y) at (0,3,0) ;
              \coordinate (Z) at (0,0,3) ;

              \draw[-latex] (O) -- (X) node[anchor=west] {$X$};
              \draw[-latex] (O) -- (Y) node[anchor=west] {$Y$};
              \draw[-latex] (O) -- (Z) node[anchor=west] {$Z$};

            \end{scope}
          \end{tikzpicture}
        \end{center}
 \end{document}

The corresponding figure is: example of \tdplotdrawarc

Here is a post which addresses the drawing an equator when the north pole is given. A simple macro to speed up coding draw an equator when north pole is known [1] .

Fake 2D, intersections package, and the instruction to [bend right], to [bend left]

Sometimes is better to say away from thinking and trying to do 3D. So I am contradicting here myself with the advise of using tikz-3dplot . Think how to draw a 3D thinking 2D (that is ellipses and arcs).

The next example is an improvement over an example shown here Spherical triangles and great circles [2] . The code is based on @Tarass great insight. The example is shown here more to show the capabilities of Tikz and the use of it for other purposes. As I said, it is better to use, in general \tdplotdrawarc .

Here is the piece of code (copied and modified from @Tarass code)

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{enumerate}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{tikz-3dplot}
\usepackage{hyperref}
\usepackage{pgfplots}
\usetikzlibrary{calc,3d,intersections, positioning,intersections,shapes}
\pgfplotsset{compat=1.11} 


\newcommand{\InterSec}[3]{%
  \path[name intersections={of=#1 and #2, by=#3, sort by=#1,total=\t}]
  \pgfextra{\xdef\InterNb{\t}}; }

   \begin{document}
    \begin{center}
       \begin{tikzpicture}

       \pgfmathsetmacro\R{2} 
       \fill[ball color=white!10, opacity=0.2] (0,0,0) circle (\R); % 3D lighting effect



        \foreach \angle[count=\n from 1] in {-5,225,290} {

          \begin{scope}[rotate=\angle]
            \path[draw,dashed,name path global=d\n] (2,0) arc [start angle=0,
              end angle=180,
              x radius=2cm,
            y radius=1cm] ;
            \path[draw,name path global=s\n] (-2,0) arc [start angle=180,
              end angle=360,
              x radius=2cm,
            y radius=1cm] ;
          \end{scope}
        }
        \InterSec{s1}{s2}{I3} ;
        \InterSec{s1}{s3}{I2} ;
        \InterSec{s3}{s2}{I1} ;
        %
        \fill[fill=red,opacity=0.5] (I1) to [bend right=8.5]  (I2) to [bend left=7] 
        (I3) to [bend left=6] (I1);

        \InterSec{d1}{d2}{J3} ;
        \InterSec{d1}{d3}{J2} ;
        \InterSec{d3}{d2}{J1} ;
        %\fill[blue] (J1)--(J2)--(J3)--cycle ;

        \fill[fill=blue,opacity=0.5] (J1) to [bend right=8.5]  (J2) to [bend left=7] 
        (J3) to [bend left=6] (J1);

      \end{tikzpicture}
    \end{center}
    \end{document}

and here the picture. enter image description here

Drawing lunes could be hard sometimes. I first refer to an StackExchange link with a problem with drawing lunes and solutions both in metapost and TiKz. The link is: How to draw a lune and shade it in TiKz [3]

I offer here another figure showing the duality between a segment and its lune. In this particular example I combine 3D and 2D, so back into suggesting the use of tikz-3dplot: the code is next:

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

\newcommand{\InterSec}[3]{%
  \path[name intersections={of=#1 and #2, by=#3, sort by=#1,total=\t}]
  \pgfextra{\xdef\InterNb{\t}}; 
}


 \newcommand\getEquator[2]
  {
    \def\yt{#1}
    \def\zt{#2}

    \pgfmathsetmacro{\betav}{acos(\zt)};

    \def\gammav{0}
    \ifthenelse{\equal{\betav}{0.0}}
    {
      \def\alphav{0}
    }
    {
      \pgfmathsetmacro{\alphav}{asin(\yt/(sin(\betav))}
    };
  }


   % to color a line
        \tikzset{test/.style={
          postaction={
            decorate,
            decoration={
              markings,
              mark=at position \pgfdecoratedpathlength-0.5pt with 
              {\arrow[blue,line width=#1] {>}; },
              mark=between positions 0 and \pgfdecoratedpathlength step 0.5pt with {
                \pgfmathsetmacro\myval{multiply(divide(
                  \pgfkeysvalueof{/pgf/decoration/mark info/distance from start}, 
                \pgfdecoratedpathlength),100)};
                \pgfsetfillcolor{blue!\myval!green};
                \pgfpathcircle{\pgfpointorigin}{#1};
                \pgfusepath{fill};}
              }
            }
          }
        }




    \begin{document}

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

      \tdplotsetmaincoords{60}{110}
      \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};


        \def\angA{90}
        \def\angB{60}
        \pgfmathsetmacro{\ax}{cos(\angA)}
        \pgfmathsetmacro{\ay}{sin(\angA)}
        \pgfmathsetmacro{\z}{0}
        \pgfmathsetmacro{\bx}{cos(\angB)}
        \pgfmathsetmacro{\by}{sin(\angB)}
        \pgfmathsetmacro{\aax}{\R*cos(\angA)}
        \pgfmathsetmacro{\aay}{\R*sin(\angA)}
        \pgfmathsetmacro{\bbx}{\R*cos(\angB)}
        \pgfmathsetmacro{\bby}{\R*sin(\angB)}

        \coordinate (A) at (\aax,\aay,\z);
        \coordinate (B) at (\bbx,\bby,\z);




        \getEquator{\ay}{\z};
        \tdplotsetrotatedcoords{\alphav}{\betav}{\gammav};
        \tdplotdrawarc[tdplot_rotated_coords,color=green, name path global=GF, opacity=0]
           {(0,0)}{\R}{180}{360}{}{};
        \tdplotdrawarc[tdplot_rotated_coords,color=green, name path global=GB, opacity=0]
           {(0,0)}{\R}{0}{180}{}{};

        \tdplotdrawarc[tdplot_rotated_coords,color=yellow, name path=YB, opacity=0]
           {(0,0)}{\R}{90}{180}{}{};

        \getEquator{\by}{\z};
        \tdplotsetrotatedcoords{\alphav}{\betav}{\gammav};
        \tdplotdrawarc[tdplot_rotated_coords,color=blue, name path=BF, opacity=0]
            {(0,0)}{\R}{180}{360}{}{};
        \tdplotdrawarc[tdplot_rotated_coords,color=blue, name path=BB, opacity=0]
           {(0,0)}{\R}{0}{180}{}{};

        \tdplotdrawarc[tdplot_rotated_coords,color=red, name path=RB, opacity=0]
           {(0,0)}{\R}{90}{180}{}{};

        %\draw[color=red] (A) arc (\angA:\angB:\R); 
        \draw[test=0.2mm] (A) arc (\angA:\angB:\R); 



        \InterSec{GF}{BF}{F};
        \InterSec{GB}{BB}{B};
        \InterSec{C}{GF}{CG};
        \InterSec{C}{BF}{CB};
        \InterSec{C}{RB}{RC};
        \InterSec{GB}{RB}{RBF};
        \InterSec{YB}{C}{T};

        %\draw[] (F) circle (1pt) node[] {\; \; \tiny F};
        %\draw[] (CG) circle (1pt) node[] {\tiny CG};
        %\draw[] (CB) circle (1pt) node[] {\tiny CB};
        %\draw[] (B) circle (1pt) node[] {\tiny B};
        %\draw[] (RBF) circle (1pt) node[] {\; \; \tiny RBF};
        %\draw[] (T) circle (1pt) node[] {\tiny T};
        %\draw[] (RC) circle (1pt) node[] {\tiny RC};




              %axis
        \coordinate (X) at (4,0,0) ;
        \coordinate (Y) at (0,3,0) ;
        \coordinate (Z) at (0,0,3) ;

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

        \shade[left color=blue, right color=green, opacity=0.8] (F) to [bend right=50] (CB) to 
        [bend right=10] (CG) to [bend left]  (F);
        \shade[left color=blue, right color=green, opacity=0.3] (CB) to [bend right=10] (CG) to 
        [bend right]  (B) to [bend left] (CB);

        \shade[left color=green, right color=blue, opacity=0.3] (B) to [bend right=60] (RC) to 
        [bend right=10]  (RBF) to [bend left ] (B);
        \shade[left color=green, right color=blue, opacity=0.8] (F) to [bend left=10] (RC) to 
        [bend right=10]  (T) to [bend right] (F);

      \end{scope}
    \end{tikzpicture}



     \end{document}

and the figure is here: A segement and its dual: a lune

Conversion of Coordinates and alternatives for drawing arcs

In spherical geometry understanding where coordinates (a point) are and how to draw arcs is a fundamental issue.

There could be confusion because spherical coordinates for mathematicians and phycisist use different symbols, the following link provides macros for conversion between spherical (azimuth, polar) and cartesian coordinates and addresses conversions in terms of geografic (latidue, altitude) coordinates as well: spherical coordinates in 3d [4] .

Finally since TiKz do not seem to have tools to draw arcs given a center and a radius I wrote a macro and posted here [5] .

[1] https://tex.stackexchange.com/questions/277810/draw-an-ecuator-circle-given-a-unit-3d-vector-the-north-pole
[2] https://tex.stackexchange.com/questions/168521/spherical-triangles-and-great-circles
[3] https://tex.stackexchange.com/questions/278015/how-to-draw-lune-in-a-sphere-and-shade-it-with-tikz
[4] https://tex.stackexchange.com/questions/114150/spherical-coordinates-in-tikz-3d
[5] https://tex.stackexchange.com/questions/46850/how-can-i-draw-an-arc-from-point-a-b-on-a-3d-sphere-in-tikz

(2) Interesting examples but could you please complete them so they compile? This code uses a lot of commands which are not standard LaTeX or TeX. It is hard to guess where they might be from. E.g. \InterSec, \tdplotsetrotatedcoords etc., not to mention the tikzpicture environment.... - cfr
Sorry. I will fix them. Thanks for your comment. - Herman Jaramillo
@crf : It should compile now. Again, thanks. - Herman Jaramillo
1
[+2] [2012-04-30 04:14:34] R. Schumacher

The R package GeoMap will create Spherical project of the earth with the continent maps. I have not used it except to verify it loads and builds a map. If you combine with the package tikzDevice you will get tikz code which could be modified. Be aware that it will be a large file due to the extensive use of points for plotting.

Once this is working you should be able to implement with Sweave so that all the code is contained within the LaTeX file.

I would consider this just a workaround until a tikz package was built with pure tikz.


2