不同宽度的线条以声明的形状相交

不同宽度的线条以声明的形状相交

因此,我正在尝试制作一个饱和块,以便在 tikz 框图中使用,如下所示:

饱和块

我尝试适应这个答案,但我无法制作图形的左侧。我知道我必须使用\pgfusepath{stroke}在形状中使用不同的宽度。这是我的尝试:

\pgfdeclareshape{satnode}{
\inheritsavedanchors[from={rectangle}]
\inheritbackgroundpath[from={rectangle}]
\inheritanchorborder[from={rectangle}]
\foreach \x in {center,north east,north west,north,south,south east,south west}{
    \inheritanchor[from={rectangle}]{\x}
}
\foregroundpath{
    \pgfpointdiff{\northeast}{\southwest}
    \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast
    \pgfsetnonzerorule
    \pgfsetlinewidth{0.4pt}
    \pgfpathmoveto{\pgfpoint{0}{0.45\pgf@ya}}
    \pgfpathlineto{\pgfpoint{0}{-0.45\pgf@ya}}
    \pgfpathmoveto{\pgfpoint{-0.4\pgf@xa}{0}}
    \pgfpathlineto{\pgfpoint{0\pgf@xa}{0}}
    \pgfusepath{stroke}
    \pgfsetlinewidth{1pt}
    \pgfpathmoveto{\pgfpoint{0.3\pgf@xa}{0\pgf@ya}}
    \pgfpathlineto{\pgfpoint{0\pgf@xa}{0\pgf@ya}}
    \pgfpathlineto{\pgfpointadd{\northeast}{\pgfpoint{-0.7\pgf@xa}{-0.3\pgf@ya}}}
    \pgfpathlineto{\pgfpointadd{\northeast}{\pgfpoint{-0.4\pgf@xa}{-0.3\pgf@ya}}}

    }
}

结果如下:

第一次失败

或者,如果我改变顺序:

\pgfdeclareshape{satnode}{
\inheritsavedanchors[from={rectangle}]
\inheritbackgroundpath[from={rectangle}]
\inheritanchorborder[from={rectangle}]
\foreach \x in {center,north east,north west,north,south,south east,south west}{
    \inheritanchor[from={rectangle}]{\x}
}
\foregroundpath{
    \pgfpointdiff{\northeast}{\southwest}
    \pgf@xa=\pgf@x \pgf@ya=\pgf@y
    \northeast
    \pgfsetlinewidth{1pt}
    \pgfpathmoveto{\pgfpoint{0.3\pgf@xa}{0\pgf@ya}}
    \pgfpathlineto{\pgfpoint{0\pgf@xa}{0\pgf@ya}}
    \pgfpathlineto{\pgfpointadd{\northeast}{\pgfpoint{-0.7\pgf@xa}{-0.3\pgf@ya}}}
    \pgfpathlineto{\pgfpointadd{\northeast}{\pgfpoint{-0.4\pgf@xa}{-0.3\pgf@ya}}}
    \pgfusepath{stroke}
    \pgfsetlinewidth{0.4pt}
    \pgfpathmoveto{\pgfpoint{0}{0.45\pgf@ya}}
    \pgfpathlineto{\pgfpoint{0}{-0.45\pgf@ya}}
    \pgfpathmoveto{\pgfpoint{-0.4\pgf@xa}{0}}
    \pgfpathlineto{\pgfpoint{0.4\pgf@xa}{0}}
    }
}

我得到以下信息:

失败二

编辑:我试图将其用作 tikz 中框图的节点。这是一个用例示例:

\node[satnode, right of=other_node,draw,minimum size=1cm] (mysatnode) {label};

答案1

是的,你说得对,你需要使用两个不同的\pgfusepath{stroke}命令。我无法告诉你我在你的代码中做了什么改动,因为我从percusse 的回答但我确实或多或少按照你用文字描述的方式做了。我还添加了两个锚点,它们在 percusse 的答案中不存在,但与定位相关。

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{positioning}
\makeatletter
\pgfdeclareshape{satnode}{
\inheritsavedanchors[from={rectangle}]
\inheritbackgroundpath[from={rectangle}]
\inheritanchorborder[from={rectangle}]
\foreach \x in {center,north east,north west,north,south,south east,south
west,west,east}{
\inheritanchor[from={rectangle}]{\x}
}
\foregroundpath{
\pgfpointdiff{\northeast}{\southwest}
\pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast
\pgfpathmoveto{\pgfpoint{0}{0.45\pgf@ya}}
\pgfpathlineto{\pgfpoint{0}{-0.45\pgf@ya}}
\pgfpathmoveto{\pgfpoint{0.45\pgf@xa}{0}}
\pgfpathlineto{\pgfpoint{-0.45\pgf@xa}{0}}
\pgfusepath{stroke}
\pgfsetlinewidth{1pt}
\pgfpathmoveto{\pgfpointadd{\southwest}{\pgfpoint{-0.3\pgf@xa}{-1\pgf@ya}}}
\pgfpathlineto{\pgfpointadd{\southwest}{\pgfpoint{-\pgf@xa}{-1\pgf@ya}}}
\pgfpathlineto{\pgfpointadd{\northeast}{\pgfpoint{-0.5\pgf@xa}{-0.3\pgf@ya}}}
\pgfpathlineto{\pgfpointadd{\northeast}{\pgfpoint{-0.2\pgf@xa}{-0.3\pgf@ya}}}
\pgfusepath{stroke}
}
}
\makeatother
\begin{document}
\begin{tikzpicture}[nodes={minimum width=2cm,minimum
    height=1cm,draw,font=\sffamily},>=latex]
\node (A){A};
\node[satnode,right=of A] (B){};
\node[right=of B] (C){C};
\draw[->] (A) -- (B);
\draw[->] (B) -- (C);
\end{tikzpicture}
\end{document}

在此处输入图片描述

PS,我知道可以chains在这里使用,但我觉得这会分散对形状声明问题的注意力(IMHO非常好)。

答案2

这个怎么样?

PS:如果你在文档中多次使用它,pic则应使用a。

在此处输入图片描述

\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}[>=latex,font=\sffamily]
\def\a{1.5} \def\b{1} 
\draw 
(.8*\a,0)--(-.8*\a,0) (0,.8*\b)--(0,-.8*\b);
\draw[thick,cyan]
(-.5*\a,0)--(0,0)--(.5*\a,.8*\b)--(.8*\a,.8*\b);
\draw[very thick] (-\a,-\b) rectangle (\a,\b);
\draw[->|] (\a,0)--+(0:.3) node[below left,scale=.5]{+};
\draw[<-] (-\a,0)--+(180:.3);
\path (0,\b) node[above,xscale=.7]{DRIVING BLOCK};
\end{tikzpicture}
\end{document}

相关内容