TikZ“旋转”不适用于预定义坐标

TikZ“旋转”不适用于预定义坐标

今天,我尝试使用 TikZ 绘制一个旋转 -45° 的矩形,并遇到了一些(在我看来)奇怪的行为:命令rotate around中的键\draw会根据以下路径是按手指定还是通过预定义坐标指定来更改其行为,以及路径的终点(在本例中为矩形)是直接指定还是相对于起点指定。一个例子:

\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}

在此处输入图片描述

有没有办法让命令在预定义坐标下正常工作?并且,在同样的动作中,有没有一种相对简单的方法可以围绕其中心旋转形状,而无需明确计算中心坐标(在这种情况下很容易,但在其他情况下可能更难)?我打算绘制一些可能涉及大量旋转形状的东西,我想指定尽可能少的坐标。下面是我最终理想结果的一个例子:

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

提前致谢!

答案1

一种方法是使用画布变换:

\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}

旋转的正方形被切断

可以看出,边界框不正确,因为坐标没有变换。可以使用以下方法纠正\useasboundingbox(见下文)

另一种方式是坚持命名的坐标也被变换:

\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}

旋转的正方形

calc库可用于查找两点的中点:

\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}

旋转的正方形

答案2

我的回答受到了hpekristiansen 的回答。但我用来pos=.5查找center矩形的,并定义rotation键以简化其使用。

\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}

在此处输入图片描述

相关内容