在 tikz 中旋转节点形状

在 tikz 中旋转节点形状

编辑1

Claudio 很好地解答了我的问题。但是图片应该包含在beamer演示文稿中。我为此使用了该选项transform canvas={scale=0.5},但这在节点位置方面存在一个问题。将下图与 Claudio 的答案中提供的图片进行比较。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,positioning}
\pgfdeclarelayer{foreground}
\pgfsetlayers{main,foreground}


\begin{document}
\thispagestyle{empty}
\tikzset{every picture/.style={transform canvas={scale=0.5}}}
\begin{tikzpicture}[
 cross appereance/.style={
   white,very thick,
 },
 cross/.style={append after command={
    \pgfextra{
    \begin{pgfinterruptpath}
    \begin{pgfonlayer}{foreground}
     \draw[cross appereance,shorten <=0.35\pgflinewidth,shorten >=0.35\pgflinewidth]
  (\tikzlastnode.south) -- 
  (\tikzlastnode.north);
     \draw[cross appereance,shorten <=0.4\pgflinewidth,shorten >=0.4\pgflinewidth]
  (\tikzlastnode.west) -- 
  (\tikzlastnode.east);
    \end{pgfonlayer}
    \end{pgfinterruptpath}
    }
  } 
 },
  quad/.style={cross,fill=purple, cross,rectangle, thick, label=below:{quad},minimum width=1cm},
]

   \node[quad]  (quad1) {};

 \begin{scope}[shift={(1cm,1cm)}, every node/.style={rotate=-60}]
   \node[quad, right= of quad1] (quad2) {};
 \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

原始问题

我使用和形状锚点(北、南……)制作了一个中心带有白色十字的矩形picture path。我的问题是,当我旋转节点时,十字不会旋转。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[
 cross/.style={path picture={ 
  \draw[white,very thick]
  (path picture bounding box.south) -- (path picture bounding box.north) (path picture bounding box.west) -- (path picture bounding box.east);}},
  quad/.style={fill,cross, purple, rectangle, thick, label=below:{quad},minimum width=1cm},
]

   \node[quad]  (quad1) {};

 \begin{scope}[xshift=1cm, every node/.style={rotate=-60}]
   \node[quad, right= of quad1] (quad2) {};
 \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

我猜这里的问题取决于十字架是如何创建的:我的感觉是利用path picture bounding box作为参考会使事情变得相当复杂。我不确定是否可以旋转它(也许可以,但要付出什么努力?)并且肯定有一种更简单的方法。事实上,出于调试目的,可以绘制path picture bounding box并注意到它根本没有用你的代码旋转:

\documentclass[tikz,png,border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds,positioning}
\pgfdeclarelayer{foreground}
\pgfsetlayers{main,foreground}

\begin{document}
\begin{tikzpicture}[
 cross/.style={path picture={ 
  \begin{pgfonlayer}{foreground}
  \draw[white,very thick]
  (path picture bounding box.south) -- (path picture bounding box.north) (path picture bounding box.west) -- (path picture bounding box.east);
  \draw[blue]  
  (path picture bounding box.south west) -- 
  (path picture bounding box.north west) -- 
  (path picture bounding box.north east) -- 
  (path picture bounding box.south east) -- cycle;
  \end{pgfonlayer}
  }},
  quad/.style={fill,cross, purple, rectangle, thick, label=below:{quad},minimum width=1cm},
]

   \node[quad]  (quad1) {};

 \begin{scope}[xshift=1cm, every node/.style={rotate=-60}]
   \node[quad, right= of quad1] (quad2) {};
 \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

类似于如何修改 TikZ 中的节点以自动在其顶部添加一条线?可以按如下方式进行:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds,positioning}
\pgfdeclarelayer{foreground}
\pgfsetlayers{main,foreground}


\begin{document}
\begin{tikzpicture}[
 cross appereance/.style={
   white,very thick,
 },
 cross/.style={append after command={
    \pgfextra{
    \begin{pgfinterruptpath}
    \begin{pgfonlayer}{foreground}
     \draw[cross appereance,shorten <=0.35\pgflinewidth,shorten >=0.35\pgflinewidth]
  (\tikzlastnode.south) -- 
  (\tikzlastnode.north);
     \draw[cross appereance,shorten <=0.4\pgflinewidth,shorten >=0.4\pgflinewidth]
  (\tikzlastnode.west) -- 
  (\tikzlastnode.east);
    \end{pgfonlayer}
    \end{pgfinterruptpath}
    }
  } 
 },
  quad/.style={cross,fill=purple, cross,rectangle, thick, label=below:{quad},minimum width=1cm},
]

   \node[quad]  (quad1) {};

 \begin{scope}[xshift=1cm, every node/.style={rotate=-60}]
   \node[quad, right= of quad1] (quad2) {};
 \end{scope}
\end{tikzpicture}
\end{document}

结果:

在此处输入图片描述

评论之后

如果您应用了转换,那么也许您想要稍微改变一下样式,quad否则除了标签太远之外还会产生不好的影响。

这就是发生在我身上的事情:

\documentclass{beamer}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{backgrounds,calc,positioning}
\pgfdeclarelayer{foreground}
\pgfsetlayers{main,foreground}

\tikzset{every picture/.style={transform canvas={scale=0.5}}}

\tikzset{white circle/.style={circle,minimum size=4cm, draw}}
\tikzset{green circle/.style={circle,minimum size=0.1cm, draw,bottom color=green!80}}
\tikzset{red circle/.style={circle,minimum size=0.1cm, draw,bottom color=red!80}}
\tikzset{blue circle/.style={circle,minimum size=0.1cm, draw,bottom color=blue!80}}

\begin{document}

\begin{frame}{A picture alone}
\centering
\begin{tikzpicture}
\pgfmathsetseed{123456}
\node (inc) at (0,5) [white circle,label=INCLUSIONE] {};

\foreach \circle in {1,...,10}{
  \pgfmathrandom{0,360}
  \pgfmathtruncatemacro\angle\pgfmathresult
  \pgfmathrnd
  \pgfmathsetmacro\distance\pgfmathresult
  \node[green circle] at ($(inc.center)!\distance!(inc.\angle)$){};
}

\foreach \circle in {1,...,10}{
  \pgfmathrandom{0,360}
  \pgfmathtruncatemacro\angle\pgfmathresult
  \pgfmathrnd
  \pgfmathsetmacro\distance\pgfmathresult
  \node[red circle] at ($(inc.center)!\distance!(inc.\angle)$){};
}

\foreach \circle in {1,...,10}{
  \pgfmathrandom{0,360}
  \pgfmathtruncatemacro\angle\pgfmathresult
  \pgfmathrnd
  \pgfmathsetmacro\distance\pgfmathresult
  \node[blue circle] at ($(inc.center)!\distance!(inc.\angle)$){};
}
\end{tikzpicture}

\end{frame}

\begin{frame}{Title}


Without change the style:

\begin{center}
\begin{tikzpicture}[
 cross appereance/.style={
   white,very thick,
 },
 cross/.style={append after command={
    \pgfextra{
    \begin{pgfinterruptpath}
    \begin{pgfonlayer}{foreground}
     \draw[cross appereance,shorten <=0.35\pgflinewidth,shorten >=0.35\pgflinewidth]
  (\tikzlastnode.south) -- 
  (\tikzlastnode.north);
     \draw[cross appereance,shorten <=0.4\pgflinewidth,shorten >=0.4\pgflinewidth]
  (\tikzlastnode.west) -- 
  (\tikzlastnode.east);
    \end{pgfonlayer}
    \end{pgfinterruptpath}
    }
  } 
 },
  quad/.style={fill,cross, purple, rectangle, thick, label=below:{quad},minimum width=1cm},
]

   \node[quad]  (quad1) {};

 \begin{scope}[every node/.style={rotate=-60}]
   \node[quad, right= of quad1] (quad2) {};
 \end{scope}
\end{tikzpicture}
\end{center}

By changing the style:

\begin{center}
\begin{tikzpicture}[
 cross appereance/.style={
   white,very thick,
 },
 cross/.style={append after command={
    \pgfextra{
    \begin{pgfinterruptpath}
    \begin{pgfonlayer}{foreground}
     \draw[cross appereance,shorten <=0.35\pgflinewidth,shorten >=0.35\pgflinewidth]
  (\tikzlastnode.south) -- 
  (\tikzlastnode.north);
     \draw[cross appereance,shorten <=0.4\pgflinewidth,shorten >=0.4\pgflinewidth]
  (\tikzlastnode.west) -- 
  (\tikzlastnode.east);
    \end{pgfonlayer}
    \end{pgfinterruptpath}
    }
  } 
 },
  quad/.style={cross,fill=purple, cross,rectangle, thick,minimum width=1cm},
]

   \node[quad,label=below:quad]  (quad1) {};

 \begin{scope}[every node/.style={rotate=-60}]
   \node[quad, right= of quad1,label=quad,xshift=-1] (quad2) {};
 \end{scope}
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}

结果:

在此处输入图片描述

在此处输入图片描述

相关内容