4

Today I was trying to draw a rectangle rotated by -45° using TikZ, and came across some (in my opinion) weird behaviour: The rotate around key in the \draw command changes its behaviour based on whether the following path is specified per hand or via predefined coordinates, and also on whether the end point of the path (rectangle in this case) is specified directly or relative to the starting point. An example:

\documentclass[border=2mm,tikz]{standalone}

\begin{document}
\begin{tikzpicture}

%Original rectangle (square in this case):
\coordinate (A) at (10,10);
\draw[black,ultra thick] (A) rectangle +(10,10);

%Point I want to rotate around:
\filldraw[blue] (15,15) circle (5pt);

%Option 1 (does NOT work as expected):
\draw[red,ultra thick,rotate around={45:(15,15)}] (A) rectangle +(10,10);
%This option seems not to care about the rotation point specification at all.
%I've tried a few different coordinates but it always just rotates around (A)

%Option 2 (also does NOT work, but in a different way which I don't understand at all):
\draw[orange,ultra thick,rotate around={45:(15,15)}] (A) rectangle (20,20);

%Option 3 (works as expected):
\draw[green,ultra thick,rotate around={45:(15,15)}] (10,10) rectangle +(10,10); %also works with (10,10) rectangle (20,20);
\end{tikzpicture}
\end{document}

enter image description here

Is there a way to get the command to work properly with predefined coordinates? And, in the same move, is there also a comparably easy way to rotate a shape around its center without having to explicitly calculate the center coordinates (easy in this case, but might be harder in others)? I'm planning to draw something which might involve a lot of rotated shapes, and I'd like to specify as few coordinates as possible. Here's an example of what I would ideally like to have in the end:

\coordinate (A) at (10,10);
\draw[other options, rotate around={45:center}] (A) rectangle +(5,5);

Thanks in advance!

4
  • Have no time to give a proper answer now, but it all comes down to the fact that named coordinates are not transformed like the other three corners of the rectangles. You can transform the canvas instead of the coordinates: transform canvas={rotate around={45:(15,15)}} (gives same result for all examples) Commented Feb 23, 2022 at 10:57
  • An other way is to use ([rotate around={45:(15,15)}]A) instead of (A) Commented Feb 23, 2022 at 11:00
  • see e.g. tex.stackexchange.com/q/98924/2388 Commented Feb 23, 2022 at 11:07
  • Thanks for the replies! If I understand correctly, the transform canvas command rotates the entire figure. That is not what I want to do; I only want to rotate individual shapes within a figure. Also, if I use ([rotate around={45:(15,15)}]A) instead of (A), somehow the entire figure is shifted instead of rotated... Commented Feb 23, 2022 at 11:18

2 Answers 2

3

One way is to use canvas transformation:

\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (10,10);
\draw[ultra thick] (A) rectangle +(10,10);
\filldraw[blue] (15,15) circle[radius=5pt];
\draw[red, ultra thick, transform canvas={rotate around={45:(15,15)}}] (A) rectangle +(10,10);
\draw[orange, ultra thick, transform canvas={rotate around={45:(15,15)}}] (A) rectangle (20,20);
\draw[green, ultra thick, transform canvas={rotate around={45:(15,15)}}] (10,10) rectangle +(10,10);
\end{tikzpicture}
\end{document}

Rotated squares cut off

As can be seen, the bounding box is not correct as the coordinates are not transformed. This can be corrected with \useasboundingbox (see below)

An other way is to insist that the named coordinate is also transformed:

\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (10,10);
\draw[ultra thick] (A) rectangle +(10,10);
\filldraw[blue] (15,15) circle[radius=5pt];
\draw[red, ultra thick, rotate around={45:(15,15)}] ([rotate around={45:(15,15)}]A) rectangle +(10,10);
\draw[orange, ultra thick, rotate around={45:(15,15)}] ([rotate around={45:(15,15)}]A) rectangle (20,20);
\draw[green, ultra thick, rotate around={45:(15,15)}] (10,10) rectangle +(10,10);
\end{tikzpicture}
\end{document}

Rotated squares

The calc library can be used to find the midpoint of two points:

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\useasboundingbox (8,8) rectangle (22,22);
\coordinate (A) at (10,10);
\coordinate (B) at (20,20);
\filldraw[blue] ($(A)!0.5!(B)$) circle[radius=5pt];
\draw[ultra thick] (A) rectangle (B);
\draw[green, ultra thick, transform canvas={rotate around={45:($(A)!0.5!(B)$)}}] (A) rectangle (B);
\end{tikzpicture}
\end{document}

Rotated squares

3

My answer is inspired by the answer of hpekristiansen. But I use pos=.5 to find the center of a rectangle and I define the rotation key to simplify its use.

\documentclass[tikz, border=1mm]{standalone}
\begin{document}
\begin{tikzpicture}
  \coordinate (A) at (2,2);
  \draw[thick] (A) rectangle +(2,2) coordinate (B) coordinate[pos=.5] (center);
  \filldraw[orange] (center) circle[radius=1pt];
  \tikzset{rotation/.style={rotate around={45:(center)}}}
  \draw[orange, thick, rotation] ([rotation]A) rectangle ([rotation]B);

  \coordinate (A) at (5,4);
  \draw[thick] (A) rectangle +(1,1) coordinate (B) coordinate[pos=.5] (center);
  \filldraw[green] (center) circle[radius=1pt];
  \tikzset{rotation/.style={rotate around={30:(center)}}}
  \draw[green, thick, rotation] ([rotation]A) rectangle ([rotation]B);
\end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .