# How do rotation and shifting transformation work in TikZ?

I'm new to TikZ so I was reading the TikZ documentation. On page 43, there is an example like below

\begin{tikzpicture}[even odd rule,x=10pt, y=10pt, scale=4]
\filldraw[fill=yellow!80!black] (0,0) rectangle (1,1)
[xshift=5pt, yshift=5pt] (0,0) rectangle (1,1)
[rotate=30] (-1,-1) rectangle (2,2);
\end{tikzpicture}


which draws

I understand how the small rectangles are shifted. The bottom left corner of the rectangle shifted by .5 units right to each axis.

But I can't figure out how the rotation of the big square works. If we look at the graph below

the green square is the original square before the roation (\draw (-1,-1) rectangle (2,2);) and the black tilted square without filling is the green square rotated 30 degrees. If we shift the black unfilled square 5 units to the positive direction of each axis, the most bottom corner should be at the red circle which is (-0.5,-0.5). But the result doesn't show like I described. How does shift and rotate work?

• Shifting and rotating accumulate. The center of rotation is (5pt, 5pt), the shifted origin. And the square that is being rotated is not the green one, but the one touching that red circle Commented Jan 13, 2022 at 6:32
• One good side of this design is that it is good at "first-person movement". You can easily accumulate [yshift = 1cm] (move forward 1cm), [rotate = 30] (turn left 30 degrees), [yshift = 2cm] (move forward 2cm), ... Commented Jan 13, 2022 at 6:37
• To rephrase a crucial part of what Symbol 1 said, both the square and its centre of rotation are shifted. Commented Jan 13, 2022 at 6:48

In TikZ writting, as in a mathematical composition of functions, say t composed with r and applied to a, which is t(r(a)), the function appearing last in the writting process is firstly applied. Here, for example, r acts firstly on a, and then t acts on the result g(a). Going back to the shift+rotation or rotation+shift, the result depends on which of the two elementary isometries appears last.

In the initial drawing of the question, as for the blue filled square in Figure 1 above, the rotation appears last. So the black square is rotated with respect to the origin and then the result is translated with the vector (2.8, 0.8).

Remark Every rotation, independently of its position in the \draw command, is performed with respect to the center (0, 0).

In Figure 2, the two composed isometries are graphically decomposed.

The code

\documentclass[11pt, a4paper]{article}
\usepackage{geometry}
\usepackage{tikz}
\usetikzlibrary{math}
\begin{document}

\tikzmath{%
real \dx, \dy, \da, \r, \R;
\dx = 2.8;
\dy = .8;
\da = -23;
\r = {sqrt(2)};
}
\begin{figure}[htb!]
\centering
\begin{tikzpicture}[every node/.style={inner sep=.1cm, fill opacity=1},
scale=.8]
\draw[gray!50] (-2, -2) grid (5, 3);
\draw[red, ->] (-3, 0) -- (6, 0);
\draw[red, ->] (0, -3) -- (0, 4);

\draw[very thick](-1, -1) rectangle ++(3, 3);

\filldraw[fill=blue!90!green, fill opacity=.4]
[xshift=\dx cm, yshift=\dy cm, rotate=\da]
(-1, -1) rectangle (2, 2);

\filldraw[fill=violet, fill opacity=.4]
[rotate=\da, xshift=\dx cm, yshift=\dy cm]
(-1, -1) rectangle (2, 2);
\end{tikzpicture}
\caption{The blue filled square is issued from the initial question.
It is the result a rotation of angle $-22^\circ$ with respect to
the center $(0, 0)$, and then of a translation with the vector
$(2.8, .8)$.  The red filled square is the result of the
translation and then the rotation operations applied on the same
black non filled square.}
\label{fig:1}
\end{figure}

\begin{figure}[htb!]
\centering
\begin{tikzpicture}[every node/.style={inner sep=.1cm, fill opacity=1},
scale=.8]
\draw[gray!50] (-2, -2) grid (5, 3);
\draw[red, ->] (-3, 0) -- (6, 0);
\draw[red, ->] (0, -3) -- (0, 4);

\draw[very thick] (-1, -1) rectangle ++(3, 3);
\filldraw[fill=blue!90!green, fill opacity=.1][rotate=\da]
(-1, -1) rectangle ++(3, 3);

\begin{scope}[xshift=\dx cm, yshift=\dy cm]
\filldraw[fill=blue!90!green, fill opacity=.4][rotate=\da]
(-1, -1) rectangle ++(3, 3);
\end{scope}

\draw (170: \r) arc (170: 250: \r);
\end{tikzpicture}
%
\begin{tikzpicture}[every node/.style={inner sep=.1cm, fill opacity=1},
scale=.8]
\draw[gray!50] (-2, -2) grid (5, 3);
\draw[red, ->] (-3, 0) -- (6, 0);
\draw[red, ->] (0, -3) -- (0, 4);

\draw[very thick](-1, -1) rectangle ++(3, 3);

\filldraw[fill=violet, fill opacity=.1]
(-1+\dx , -1+\dy ) rectangle ++(3, 3);
\filldraw[fill=violet, fill opacity=.4][rotate=\da]
(-1+\dx , -1+\dy ) rectangle ++(3, 3);
\end{tikzpicture}
\caption{The two compositions form Figure 1 decomposed in elementary
\texttt{TikZ} drawing commands.  In both cases, the center of
rotation is the origin, the point $(0, 0)$.}
\label{fig:2}
\end{figure}
\end{document}