#003

Multi-path TikZ sphere

Last updated:

TikZ has a library called shadings that allows you to fill a path with a shading that makes it look like a sphere.

\usetikzlibrary{shadings}
\begin{tikzpicture}
    \shade[ball color=blue] (0,0) circle (1);
\end{tikzpicture}

Circle with blue shading that makes it look like a sphere.

For my master’s thesis, I wanted to use this to draw the symbol for the center of gravity, conventionally a quartered black-and-white circle, but for a three-dimensional drawing. However, I couldn’t find any examples in the documentation of the ball shading applied to multiple paths. So, I just tried.

\usetikzlibrary{shadings}
\begin{tikzpicture}
    \shade[ball color=white] (0,0) -- (-1,0) arc (180:90:1)
        -- (0,-1) arc (270:360:1) --cycle;
    \shade[ball color=black] (0,0) -- (1,0) arc (0:90:1)
        -- (0,-1) arc (270:180:1) --cycle;
\end{tikzpicture}

Quartered black and white circle with shading that makes it look like a sphere.

To my surprise, it worked! But why? Well, it turns out that I just got lucky. Apparently, the shading is applied relative to the bounding boxes of the individual paths. Since both paths have rectangular bounding boxes with lower left corners at (-1,-1) and upper right corners at (1,1), their shadings overlap. Had I drawn the same symbol with four paths such that their bounding boxes did not coincide, this would not have worked.

\usetikzlibrary{shadings}
\begin{tikzpicture}
    \shade[ball color=white] (0,0) -- (-1,0) arc (180:90:1) --cycle;
    \shade[ball color=white] (0,0) -- (1,0) arc (360:270:1) --cycle;
    \shade[ball color=black] (0,0) -- (0,1) arc (90:0:1) --cycle;
    \shade[ball color=black] (0,0) -- (0,-1) arc (270:180:1) --cycle;
\end{tikzpicture}

Quartered black and white circle with different shading for each quarter.