Before jumping straight to a spherical triangle we need to do a bit more ground work on how to draw 3D diagrams?

Before delving into the 3D world I will go over some useful things like the basic mathematical operations that can be used in TikZ/PGF:

**The PGF Mathematical Engine:**p, li { white-space: pre-wra

- \begin{tikzpicture}[scale = 4, rotate=-20]
- See previous post for missing code.
- % Graph Content
- \coordinate (point1) at (1/2, 3*2-5.5);
- \draw (0,0) circle (\R);
- \draw (0,0) — (point1);
- \filldraw [fill = blue!50, draw = black] (point1) circle (\R/30);
- \foreach \ang in {0,10,…,80} {
- \pgfmathparse{\R*2/2*sin(90)};
- \filldraw [fill = green!\ang, draw = black] (\ang:\pgfmathresult) circle (\R/30);
- \pgfmathsetmacro{\XVal}{\R*cos(-\ang)};
- \pgfmathsetmacro{\YVal}{\R*sin(-\ang)};
- \filldraw [fill = yellow!\ang, draw = black] (\XVal, \YVal) circle (\R/30);
- }
- \end{tikzpicture}

- The first and most obvious thing you will probably note is that one image is rotated. Whilst I have applied this transform to the entire figure/diagram a transform can be applied to any primitive.
- Although touched on briefly, this example shows how to save a coordinate. And just as important that the coordinates can be entered as mathematical expressions that will be evaluated with PGF’s mathmatical engine.
- What happens if we want a more complicated expression? It turns out we can do this too. But I found that they couldn’t be placed inline, and instead has to be parsed using \pgfmathparse{}. The result is then stored in the \pgfmathresult variable/macro.
- But what happens if we need two
*complicated*expressions? After some reading and experimenting I found you can use this function: \pgfmathsetmacro{\YVal}{expression} to save an answer to a variable which can the be used later!

Ok, so back to drawing 3D stuff…

**Transformations:**

*transformed*using the following function/drawing option:

In the first example we are rotating a 2D shape in the default plane; this means we want to rotate about the z-axis. This is the Rz matrix:

- \begin{tikzpicture}
- \DrawAxis{0}{0}{2}{2};
- \draw [fill = yellow] (0,0) — (1,1) — (1,0);
- \begin{scope}[xshift=3cm]
- \def\Ang{45};
- \DrawAxis{0}{0}{2}{2};
- \draw [cm={cos(\Ang),sin(\Ang),-sin(\Ang),cos(\Ang),(0,0)}
- (0,0) — (1,1) — (1,0);
- \end{scope}
- \begin{scope}[xshift=6cm]
- \DrawAxis{0}{0}{2}{2};
- \draw [cm={cos(\Ang),sin(\Ang),-sin(\Ang),cos(\Ang),(1,1)}
- (0,0) — (1,1) — (1,0);
- \end{scope}
- \end{tikzpicture}

**Drawing in 3D:**

I will start be drawing a parabola, which we will then rotate about the y-axis creating a cone-like shape.

We will now add some ‘circles’ and planes rotated about the x-axis and offset in the y-direction to add some depth to the figure:

p, li { white-space: pre-wra

- \begin{tikzpicture}
- \def\fcn{\x*\x};
- \def\phi{10};
- \def\xmin{-1.5}; \def\xmax{1.5};
- \draw[color=black, domain=\xmin:\xmax]
- plot (\x, \fcn) node [above right] {$y = x^2$};
- \foreach \x in {0, .25, …, \xmax} {
- \tikzset{xyplane/.estyle={cm={1, 0, 0, cos(90 + \phi),(0, \fcn)}}}
- \draw[xyplane, color=black] (0, 0) circle (\x);
- }
- \end{tikzpicture}

- \phi is the view angle. We rotate by 90deg to get the plane perpendicular to the page and then adjust a little.

Now we’re starting to have something that looks 3-dimensional. Lets now rotate the parabola around the y-axis:

p, li { white-space: pre-wra

- \begin{tikzpicture} [scale =2]
- \def\fcn{\x*\x};
- \def\phi{10};
- \def\xmin{-1.5}; \def\xmax{1.5};
- \foreach \x in {0, 0.25, …, \xmax} {
- \tikzset{xyplane/.estyle={cm={1, 0, 0, cos(90 + \phi),(0, \fcn)}}}
- \draw[xyplane, color=black] (0, 0) circle (\x);
- }
- \foreach \theta in {0, 30, …, 360} {
- \tikzset{xyplane/.estyle={cm={cos(\theta), 0, 0, 1,(0, 0)}}}
- \draw[xyplane, color=black, domain=\xmin:\xmax]
- plot (\x, \fcn);
- }
- \end{tikzpicture}\\

Close, but no dice. We need to account for the viewing angle. So to get the correct plane, we need to rotate about the y-axis *and* the x-axis:

p, li { white-space: pre-wra

- \begin{tikzpicture} [scale =2]
- \def\fcn{\x*\x};
- \def\phi{10};
- \def\xmin{-1.5}; \def\xmax{1.5};
- \foreach \x in {0, 0.25, …, \xmax} {
- \tikzset{xyplane/.estyle={cm={1, 0, 0, cos(90 + \phi),(0, \fcn)}}}
- \draw[xyplane, color=black] (0, 0) circle (\x);
- }
- \foreach \theta in {0, 30, …, 360} {
- \tikzset{xyplane/.estyle={cm={cos(\theta), sin(\theta)*sin(\phi), 0, cos(\phi),(0, 0)}}}
- \draw[xyplane, color=black, domain=\xmin:\xmax]
- plot (\x, \fcn);
- }
- \end{tikzpicture}\\

Close but the top circle does not quite line up with the rotated parabolas. It turns out this is also to do with the viewing angle (If we look at the Rx rotation matrix and project into the x-y plane we can see that the y value of a coordinate is multiplied by cos(\phi)).

Because we are looking from above the heights appear shorter by a factor of cos(\phi), so when we draw the circles, instead of offsetting the plane by x^2, we need to offset it by cos(\phi)*x^2:

**References:**

- https://en.wikibooks.org/wiki/LaTeX/Macros
- http://www.texample.net/tikz/examples/spherical-and-cartesian-grids/
- http://www.texample.net/tikz/examples/map-projections/
- http://www.tug.org/tugboat/tb30-1/tb94roegel-spheres.pdf
- http://tex.stackexchange.com/questions/53445/how-to-draw-spherical-geometries-with-tex
- http://tex.stackexchange.com/questions/46850/how-can-i-draw-an-arc-from-point-a-b-on-a-3d-sphere-in-tikz/49589#49589
- http://melusine.eu.org/syracuse/pstricks/pst-solides3d/bonus/doc_trigo_spherique.pdf
- http://cahiers.gutenberg.eu.org/cg-bin/article/CG_2007___48_7_0.pdf
- www.math.nus.edu.sg/aslaksen/projects/Wu%20ChengYuan.pdf
- http://www.bu.edu/math/files/2013/08/tikzpgfmanual.pdf