矩形在旋转范围内表现异常

矩形在旋转范围内表现异常

这是我的最小例子:

\documentclass[]{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

    \begin{figure}
      \centering
      \begin{tikzpicture}[text=black,text centered,text width=1.5cm]

        \def \n {5}
        \def \radius {3cm}
        \node[circle, draw] (c) at (0, 0) {C};

        \foreach \s in {1,...,\n} {

            \begin{scope}[shift={({-(360/\n * (\s-1)) + 90}:\radius)},rotate={-360/\n*(\s-1)}]
                \node[] (id) at (0, 4) {Id : \s};
                \node[circle, draw] (a) at (0, 3) {A};
                \node[circle, draw] (b) at (0, 0) {B};
                \draw[->] (a) -- (b);
                \draw[->] (b) -- (c);
                \draw[thick] ($(id.north west)+(-0.6,0.6)$)  rectangle ($(b.south east)+(0.6,-0.6)$);
            \end{scope}

        }

      \end{tikzpicture}
    \end{figure}

\end{document}

如您所见,我创建了三个节点,在它们周围画了一个框,并将该图旋转一个角度。我的问题是矩形的坐标似乎是在旋转发生后计算的,这不应该发生。

在此处输入图片描述

附注:是否可以旋转(id)节点中的文本?

[编辑] 对于文本的旋转,我只需将其添加rotate=...到节点即可完美运行。

答案1

默认情况下,nodes对转换不敏感。要应用它们,必须添加选项tranform shape

我引用 TikZ 3.1.4b 手册:

17.7 变换

可以变换节点,但默认情况下,变换不适用于节点。原因是你通常这样做不是即使主图形被变换,也希望文本被缩放或旋转。缩放文本是邪恶的,旋转则没那么邪恶。但是,有时你确实希望变换一个节点,例如,有时将节点旋转 90 度肯定是有意义的

截屏

\documentclass[]{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

    \begin{figure}
      \centering
      \begin{tikzpicture}[text=black,text centered,text width=1.5cm]

        \def \n {5}
        \def \radius {3cm}
        \node[circle, draw] (c) at (0, 0) {C};

        \foreach \s in {1,...,\n} {

            \begin{scope}[shift={({-(360/\n * (\s-1)) + 90}:\radius)},rotate={-360/\n*(\s-1)},transform shape]
                \node[] (id) at (0, 4) {Id : \s};
                \node[circle, draw] (a) at (0, 3) {A};
                \node[circle, draw] (b) at (0, 0) {B};
                \draw[->] (a) -- (b);
                \draw[->] (b) -- (c);
                \draw[thick] ($(id.north west)+(-0.6,0.6)$)  rectangle ($(b.south east)+(0.6,-0.6)$);
            \end{scope}

        }

      \end{tikzpicture}
    \end{figure}

\end{document}

答案2

另一种方法:

\documentclass[tikz, margin=3mm]{standalone}

\begin{document}
\begin{tikzpicture}[
C/.style = {circle, draw, fill=white, minimum size=\radius, sloped},
R/.style = {draw, minimum height=\radius+2mm, minimum width=3*\radius+3mm,
             rotate=\ang, right}
                    ]

\def\n{5}
\def\radius {7mm}
\node (c) [C] {C};
%
\foreach \i [count=\s from 0] in {1,...,\n}
{
\pgfmathsetmacro{\ang}{90+\s*360/\n}
\draw[<-] (c) -- node[C] {B} ++ (\ang:22mm)
              -- node [C, near end] {A} ++ (\ang:\radius);
\node (r\i) [R] at (\ang:\radius+1mm) {};
\node[rotate=\ang,right] at (r\i.east) {Id=\i};
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

如果您希望将分支标签与矩形的外线对齐,则只需将最后一行代码替换为

\node[rotate=270+\ang,right, anchor=north] at (r\i.east) {Id=\i};

结果如附录中的示例所示。

附录: 受到 @Schrödinger's cat 评论的启发(非常感谢,我学到了一些关于 TikZ 的新知识),解决方案是使用 key rotate fit。使用它,矩形的定义变得更简单,并且还可以自动适应圆形节点的位置:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                fit}

\begin{document}
\begin{tikzpicture}[
C/.style = {circle, draw, minimum size=\radius, 
            inner sep=0pt, sloped},
R/.style = {draw, inner sep=1mm, rotate fit=\ang, fit=#1},  % <--- for rotate fit nodes
every edge/.style = {draw, semithick, -{Straight Barb[angle=60:2pt 3]}}
                    ]

\def\n{5}
\def\radius {7mm}
\node (c) [C] {C};
%
\foreach \i [count=\s from 0] in {1,...,\n}
{
\pgfmathsetmacro{\ang}{90+\s*360/\n}
\path   (c) -- node (c1\i) [C] {B} ++ (\ang:3*\radius)
            -- node (c2\i) [C, at end] {A} ++ (\ang:0.5*\radius);
\draw   (c2\i) edge (c1\i)  (c1\i) edge (c);
\node (r\i) [R=(c1\i)(c2\i)] {}; % <--- "used rotate fit"
\node[rotate=270+\ang,
      right, anchor=south, font=\footnotesize] at (r\i.east) {$\mathrm{Id}=\i$};
}
\end{tikzpicture}
\end{document}

它给:

在此处输入图片描述

相关内容