# How can I draw an arc from point A -> B on a 3D sphere in TikZ?

I'm basing myself on http://www.texample.net/tikz/examples/spherical-and-cartesian-grids/ and now I need to add a arc from point P to Q. What I was able to get so far:

\begin{tikzpicture}[scale=1,every node/.style={minimum size=1cm}]
%% some definitions

\def\angEl{25} % elevation angle
\def\angAz{-100} % azimuth angle
\def\angPhiOne{-110} % longitude of point P
\def\angPhiTwo{-45} % longitude of point Q
\def\angBeta{30} % latitude of point P and Q

%% working planes

\pgfmathsetmacro\H{\R*cos(\angEl)} % distance to north pole
\LongitudePlane[xzplane]{\angEl}{\angAz}
\LongitudePlane[pzplane]{\angEl}{\angPhiOne}
\LongitudePlane[qzplane]{\angEl}{\angPhiTwo}
\LongitudePlane[nzplane]{\angEl}{-86}
\LatitudePlane[equator]{\angEl}{0}
\fill[ball color=white!10] (0,0) circle (\R); % 3D lighting effect
\coordinate (O) at (0,0);
\coordinate[mark coordinate] (N) at (0,\H);
\coordinate[mark coordinate] (S) at (0,-\H);

\DrawLongitudeCircle[\R]{\angPhiOne} % pzplane
\DrawLongitudeCircle[\R]{\angPhiTwo} % qzplane
\DrawLatitudeCircle[\R]{\angBeta}
\DrawLatitudeCircle[\R]{0} % equator
%labelling north and south
\node[above=8pt] at (N) {$\mathbf{N}$};
\node[below=8pt] at (S) {$\mathbf{S}$};
\draw[-,dashed, thick] (N) -- (S);

%setup coordinates P and Q
\path[pzplane] (0:\R) coordinate (P);
\draw[->] (O) -- node[above=4pt] {$\overrightarrow{P}$} (P);
\path[qzplane] (\angBeta:\R) coordinate (Q);
\draw[->] (O) -- node[above=2pt] {$\overrightarrow{Q}$} (Q);
\path[nzplane] (153:\R) coordinate (N);
\draw[->,color=red] (O) -- node[right=2pt] {$\overrightarrow{N}$} (N);
\draw (P) arc (-110:-45:\R) (Q);
\end{tikzpicture}


Is there a possibility to make an arc from P to Q with the center O?

Upon request, the full compilable sourcecode: http://pastebin.com/m809Jwp7

-
mark coordinate ?? –  Alain Matthes Mar 5 '12 at 10:11
While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. This is especially important with tikz as there are numerous libraries. –  Peter Grill Mar 5 '12 at 15:46
Sorry, my fault! I added a complete dump of the code via pastebin. Works perfectly with MacTeX 2011 and pdflatex. –  mhk Mar 5 '12 at 17:12
I think you need to define the plane POQ like the others planes (qzplane, etc.) with \tikzset{POQ/.estyle={cm={..,..,..,..,(0,0)}}} but the problem is to find ..,..,..,.., !! –  Alain Matthes Mar 5 '12 at 17:52
How "mark coordinate" is defined ? –  Alain Matthes Mar 5 '12 at 17:53

To give a correct answer, we need to define cross product and vector product (this work is done with metapost in cahier gutemberg 48 but it's in french)

I don't have enough time to define all these macros but it's possible to find a way to draw the arc. First we know that the arc PQ (blue) is in the plane OPQ and is a part of a circle of center O and radius OP. So I search a Coordinate system xyz with x=OP and y=OA'. A is a point of the equator of longitude = -20. Why ? because I want OP and OA radius of the equator and OP perpendicular at OA. Then I need to find A' of longitude-20 and latitude >30 but I need to calculate the value.

Update How to determine the latitude of A' ?

In the next pictures, H is the projection of Q on the plane (OPA). It's possible to calculate PH with two sides (OP=1 and OH=0.866) I find 1.001. Then the lines PH and OA have an intersection at the point I. Now i calculate OI=1.238 and PI=1.591. J is a point of the line OA' and I is the projection of J on the plane (OPA). P, Q, J are aligned and IJ= 0.795. IJ/OI=0.641=tan(32.7). The latitude of A' is 32.7. Now I can draw the circle of radius 1 that passes through P and A' with center O.

Now I need to draw the circle of center O and radius 1. The circle passes through P and A' but also by Q. I draw the diameter POP' and QOQ'.

Todo : Calculus to determine correctly the latitude of A', cross product to determine N'. A macro to place a point with longitude and latitude.

In my code, I redefined personal macro with names that I understand correctly.

\documentclass[11pt]{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset{%
to path={%
($(\tikztostart)!-#1!(\tikztotarget)$)--($(\tikztotarget)!-#2!(\tikztostart)$)%
}

\tikzset{%
mark coordinate/.style={inner sep=0pt,outer sep=0pt,minimum size=2pt,
fill=black,circle}%
}

\newcommand\pgfmathsinandcos[3]{%
\pgfmathsetmacro#1{sin(#3)}%
\pgfmathsetmacro#2{cos(#3)}%
}
\newcommand\LongitudePlane[2][current plane]{%
\pgfmathsinandcos\sinEl\cosEl{\Elevation} % elevation
\pgfmathsinandcos\sint\cost{#2} % azimuth
\tikzset{#1/.estyle={cm={\cost,\sint*\sinEl,0,\cosEl,(0,0)}}}
}
\newcommand\LatitudePlane[2][current plane]{%
\pgfmathsinandcos\sinEl\cosEl{\Elevation} % elevation
\pgfmathsinandcos\sint\cost{#2} % latitude
\pgfmathsetmacro\ydelta{\cosEl*\sint}
\tikzset{#1/.estyle={cm={\cost,0,0,\cost*\sinEl,(0,\ydelta)}}} %
}
\newcommand\DrawLongitudeCircle[1]{
\LongitudePlane{#1}
\tikzset{current plane/.prefix style={scale=\R}}
\pgfmathsetmacro\angVis{atan(sin(#1)*cos(\Elevation)/sin(\Elevation))} %
\draw[current plane,thin,black]  (\angVis:1)     arc (\angVis:\angVis+180:1);
\draw[current plane,thin,dashed] (\angVis-180:1) arc (\angVis-180:\angVis:1);
}%

\newcommand\DrawLatitudeCircle[1]{
\LatitudePlane{#1}
\tikzset{current plane/.prefix style={scale=\R}}
\pgfmathsetmacro\sinVis{sin(#1)/cos(#1)*sin(\Elevation)/cos(\Elevation)}
\pgfmathsetmacro\angVis{asin(min(1,max(\sinVis,-1)))}
\draw[current plane,thin,black] (\angVis:1) arc (\angVis:-\angVis-180:1);
\draw[current plane,thin,dashed] (180-\angVis:1) arc (180-\angVis:\angVis:1);
}%

\newcommand\DrawPointOnSphere[3]{%
\pgfmathsinandcos\sinLoM\cosLoM{#1}
\pgfmathsinandcos\sinLaM\cosLaM{#2}
}

\begin{document}
\null\vfill
\begin{center}
\begin{tikzpicture}
\def\Elevation{25} % elevation angle
\def\angleLongitudeP{-110} % longitude of point P
\def\angleLongitudeQ{-45} % longitude of point Q
\def\angleLatitudeQ{30} % latitude  Q    ; 0 latitude of P
\def\angleLongitudeA{-20} % longitude of point A

\pgfmathsetmacro\H{\R*cos(\Elevation)} % distance to north pole
\LongitudePlane[PLongitudePlane]{\angleLongitudeP}
\LongitudePlane[QLongitudePlane]{\angleLongitudeQ}
\LongitudePlane[ALongitudePlane]{\angleLongitudeA}

\fill[ball color=white!10] (0,0) circle (\R); % 3D lighting effect
\coordinate (O) at (0,0);
\coordinate[] (N) at (0,\H);
\coordinate[] (S) at (0,-\H);

\DrawLongitudeCircle{\angleLongitudeP} % PLongitudePlane
\DrawLongitudeCircle{\angleLongitudeQ} % QLongitudePlane
\DrawLongitudeCircle{\angleLongitudeA}
\DrawLatitudeCircle{\angleLatitudeQ}
\DrawLatitudeCircle{0} % equator
\DrawLongitudeCircle{0}
%setup coordinates P and Q
\path[ALongitudePlane] (0:\R) coordinate (A);
\path[ALongitudePlane] (32.5:\R) coordinate (A');
\path[ALongitudePlane] (122.5:\R) coordinate (N');
\path[PLongitudePlane] (0:\R) coordinate (P);
\draw[dashed,add= 1 and 0] (O) to  (P);
\path[QLongitudePlane] (\angleLatitudeQ:\R) coordinate (Q);
\draw[dashed,add= 1 and 0] (O) to  (Q) ;
\path[QLongitudePlane] (0:\R) coordinate (B);
\draw [dashed] (O) --  (B) ;
\draw [dashed] (O) --  (N) ;

\foreach \v in {A,O,N,S,P,Q,A',B,N'} {\coordinate[mark coordinate] (v) at (\v);
\node [above] at (\v) {\v};}
\begin{scope}[ x={(P)}, y={(A')}, z={(N')}]
\draw[dashed,fill opacity=.3] circle (1);
\draw[blue] ( 0:1) arc (0:68:1) ;
\draw[] ( 68:1) arc (68:115:1) ;
\draw[] (-55:1) arc (-55:0:1);
\draw[red,->](0,0,0)--(0,0,1);
\draw[red,->](0,0,0)--(0,1,0);
\draw[red,->](0,0,0)--(1,0,0);
\end{scope}
\end{tikzpicture}
\end{center}
\vfill

\end{document}

-
Really nice answer! –  Gonzalo Medina Mar 27 '12 at 22:56
Thanks Gonzalo but if I have enough time it will be interesting to write some macros. The paper of "cahier gutemberg" is very interesting but with metapost. I hope to translate the macro from metapost to tikz ! –  Alain Matthes Mar 27 '12 at 23:00
I'll be looking forward to seeing those macros. –  Gonzalo Medina Mar 27 '12 at 23:13
The french article is available in english as a TUGboat article tug.org/tugboat/tb30-1/tb94roegel-spheres.pdf –  Scott Prahl Apr 2 '12 at 5:03
I just started drawing on spheres with tikz and it would be really nice to have a function/command to draw geodesics on the sphere (i.e. arbitrary points P and Q) - have you made any progress in translating the macros @AlainMatthes ? That would be really neat. –  Ronny Oct 30 '14 at 16:05

The trick is to rotate the coordinate system. This bit of code shows the great circle passing through your particular values for P and Q, as well as a blue arc from P to Q.

\begin{scope}[rotate around={30:(0,0)}]
\DrawLatitudeCircle[\R]{11}
\draw[current plane,blue,thick] (240:1) arc (240:310:1);
\end{scope}


(I dropped a couple of your drawing commands to make the result a bit clearer. Obviously all the parameters are specific to this problem and, less obviously, were chosen visually.)

-
Are you sure of the method ? If you take Q with longitude 0 and altitude 30, I think you get a problem ! What is exactly the rotation of 30 degree of the equator ? And last point, if you take P with longitude 135 and altitude 0 then Q,O,P,N and S are on the same plane, the arc PQ contains N –  Alain Matthes Mar 26 '12 at 18:46
I confess that I did not bother to work out all the trig. I just rotated the equator until it was parallel to the great circle that passed through P and Q. I then futzed with the arc parameters to get the right section of the great circle. –  Scott Prahl Mar 26 '12 at 20:01
"Is there a possibility to make an arc from P to Q with the center O? ?" but the blue arc is not in the plane OPQ because O is not the center of the circle. –  Alain Matthes Mar 27 '12 at 6:23
I added too a visual answer and if you compare the circle of center O that passes through P and Q, you can see the problem in your picture. –  Alain Matthes Mar 27 '12 at 7:19
Yes, my answer is not a great circle. I blithely overlooked the need for a great circle solution and opted for one that looked reasonable. –  Scott Prahl Apr 2 '12 at 5:00