# How does the TikZ shading angle key work?

When answering this question, I found out that the shading angle key does not do what I expected it to do. The following MWE illustrates the problem. I've defined a shading with a sharp edge to make it easier to see its direction.

\documentclass[tikz,margin=5pt]{standalone}

\begin{document}

\begin{tikzpicture}[scale=2]
(0:1)
arc (0:30:1)
--  (30:1.2)
arc (30:0:1.2)
-- cycle;
\draw (0,0) -- (15:1.2);
\end{tikzpicture}
\end{document}


This is the result. Note how the transition in the shading and the line are not parallel, even though the angle of the line and shading angle are both set to 15 degrees. This is what I would have expected: The pgfmanual's section about shading a path doesn't really explain how this key works.

Here's how I now think the key works from my own experiments, illustrated in five steps: 1. First, the selected shading is applied to a square canvas
2. Then, the shading angle key is applied.
3. The canvas is then resized to the size of the bounding box of the path that is to be shaded. If the result is non-square, this changes the angle of the gradient!
4. The canvas is moved so it's under the path.
5. Finally, it is clipped to lie entirely within the path.

Of course, none of the intermediate steps are actually drawn. In case anyone cares, here is how I produced the image:

\documentclass[tikz,margin=5pt]{standalone}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}[scale=2]
\newcommand*\outline[draw]{
\path[#1]
($(c) + (0:1)$)    coordinate (p1)
arc (0:30:1)           coordinate (p2)
--  ($(c) + (30:1.2)$) coordinate (p3)
arc (30:0:1.2)         coordinate (p4)
-- cycle;
\coordinate (sw) at (p1 -| p4);
\coordinate (ne) at (p2 |- p3);
\coordinate (center) at ($(sw)!.5!(ne)$);
\fill (c) circle (1pt);
\path[#1] (c) -- ($(c) + (15:1.2)$);
}

\coordinate (c) at (0,0);
\outline
\node[below] at (c) {1};

\coordinate (c) at (2,0);
\outline
\node[below] at (c) {2};

\coordinate (c) at (4,0);
\outline[]
\shade[shading=myshading,shading angle=15] ($(sw) - (center) + (c)$) rectangle ($(ne) - (center) + (c)$);
\outline
\node[below] at (c) {3};

\coordinate (c) at (1,-2);
\outline[]