Tikz,如何创建一个简单的新形状

Tikz,如何创建一个简单的新形状

我想switch在网络图中创建一个新形状。这很简单,基于圆形。

在此处输入图片描述

我手动得到了这个

\def \drawswitch {
  \node[circle,draw] (circle){};
  \draw (circle.south west) -- (circle.north east);
  \draw (circle.north west) -- (circle.south east);
}

但我仍然想通过使用 定义一个新形状来使其完美\pgfdeclareshape,但是我不知道如何覆盖\backgroundpath。我不知道如何找到saved anchor一个圆圈,并且注释让\pgf@xb我很困惑。

答案1

接下来是形状的代码。我从( )switch中取出代码并添加了缺失的行。forbidden signpgflibraryshapes.symbols.code.tex

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}

\makeatletter
\pgfdeclareshape{switch}
{
  \inheritsavedanchors[from=circle] % this is nearly a circle
  \inheritanchorborder[from=circle]
  \inheritanchor[from=circle]{north}
  \inheritanchor[from=circle]{north west}
  \inheritanchor[from=circle]{north east}
  \inheritanchor[from=circle]{center}
  \inheritanchor[from=circle]{west}
  \inheritanchor[from=circle]{east}
  \inheritanchor[from=circle]{mid}
  \inheritanchor[from=circle]{mid west}
  \inheritanchor[from=circle]{mid east}
  \inheritanchor[from=circle]{base}
  \inheritanchor[from=circle]{base west}
  \inheritanchor[from=circle]{base east}
  \inheritanchor[from=circle]{south}
  \inheritanchor[from=circle]{south west}
  \inheritanchor[from=circle]{south east}
  \inheritbackgroundpath[from=circle]
  \foregroundpath{
    \centerpoint%
    \pgf@xc=\pgf@x%
    \pgf@yc=\pgf@y%
    \pgfutil@tempdima=\radius%
    \pgfmathsetlength{\pgf@xb}{\pgfkeysvalueof{/pgf/outer xsep}}%  
    \pgfmathsetlength{\pgf@yb}{\pgfkeysvalueof{/pgf/outer ysep}}%  
    \ifdim\pgf@xb<\pgf@yb%
      \advance\pgfutil@tempdima by-\pgf@yb%
    \else%
      \advance\pgfutil@tempdima by-\pgf@xb%
    \fi%
    \pgfpathmoveto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{0.707107\pgfutil@tempdima}{-0.707107\pgfutil@tempdima}}}
    \pgfpathlineto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{-0.707107\pgfutil@tempdima}{0.707107\pgfutil@tempdima}}}
    \pgfpathmoveto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{-0.707107\pgfutil@tempdima}{-0.707107\pgfutil@tempdima}}}
    \pgfpathlineto{\pgfpointadd{\pgfqpoint{\pgf@xc}{\pgf@yc}}{\pgfqpoint{0.707107\pgfutil@tempdima}{0.707107\pgfutil@tempdima}}}
    \pgfsetarrowsstart{}
    \pgfsetarrowsend{}
  }
}
\makeatother

\begin{document}
\begin{tikzpicture}
\node[switch,draw,fill=red!30] (A) {Switch A};
\node[switch,draw,fill=red!30, right=1cm of A] (B) {Switch B};
\draw (A)--(B);
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容