Tikz-用多条线连接节点

Tikz-用多条线连接节点

我正在尝试重现我在“Power point”中制作的以下图像。

在此处输入图片描述

虽然设计方框并不困难,但我发现正确定位红线和黑线相当困难。一种解决方案是使用纯坐标,但我认为一定有最简单的方法,因为我已经定义了节点。

我的代码是

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

    \begin{document}

    \begin{tikzpicture}

    \tikzstyle{blockYellow}=[top color=white, bottom color=yellow!60, draw=yellow!90!black!100, minimum size=1cm, rounded corners]
    \tikzstyle{blockBrown}=[top color=white, bottom color=brown!60, draw=brown!90!black!100, minimum size=1cm, rounded corners]
    \tikzstyle{blockBlack}=[top color=white, bottom color=black!60, draw=black!90!black!100, minimum width=1cm, minimum height=5cm, rounded corners]
    \tikzstyle{blockBlue}=[top color=white, bottom color=blue!60, draw=blue!90!black!100, minimum size=1cm, rounded corners]

    \pgfdeclarelayer{background}
    \pgfsetlayers{background,main}

  %PreAmp
 \node[blockYellow] (PreAmp) {\eng{(Pre)Amp}};
 %Detectors
 \node [blockBrown, left of=PreAmp, node distance=3.5cm, anchor=east] (E2) {$E_2$};
 \node [blockBrown, above of=E2, node distance=2cm] (E1) {$E_1$};
 \node [blockBrown, below of=E2, node distance=2cm] (E3) {$E_3$};
 \node [blockBrown, above of=PreAmp, node distance=3.5cm] (DE2) {$\Delta E_2$};
 \node [blockBrown, left of=DE2, node distance=2cm] (DE1) {$\Delta E_1$};
 \node [blockBrown, right of=DE2, node distance=2cm] (DE3) {$\Delta E_3$};
 %ADC
 \node [blockBlack, right of=PreAmp, node distance=10cm] (ADC) {\textcolor{white}{$ADC$}};
 %Disc
 \node [blockBlue, below of=PreAmp, node distance=5cm] (Disc) {$Discriminator$};
 %FIFO
 \node [blockBlue, right of=Disc, node distance=5cm] (FIFO) {$FIFO$};
 %DGG
 \node [blockBlue, below of=ADC, node distance=5cm] (DGG) {$DGG$};
 %cables
 \draw[red,very thick] (PreAmp)--(Disc);
 %\draw[red,very thick] (PreAmp.north)--(Disc.north);
 \draw[red,very thick] (Disc)--(FIFO);
 \draw[red,very thick] (FIFO)--(DGG);
 \draw[red,very thick] (DGG)--(ADC);
 \draw[very thick] (DE2)--(PreAmp);
 \draw[red, very thick] (E2)--(PreAmp);
 \draw[red, very thick] ($(PreAmp.south west)!0.33!(PreAmp.south east)$)coordinate (a)--(a|-Disc.north);
 \draw[red, very thick] ($(PreAmp.south west)!0.66!(PreAmp.south east)$)coordinate (b)--(b|-Disc.north);
 %\draw[red, very thick] ($(Disc.north east)!0.33!(Disc.south east)$)coordinate (c)--(c|-FIFO.west);
 \draw[red, very thick] ($(Disc.north east)!0.33!(Disc.south east)$) -- ($(FIFO.north west)!0.33!(FIFO.south west)$);
 \draw[red, very thick] ($(Disc.north east)!0.66!(Disc.south east)$) -- ($(FIFO.north west)!0.66!(FIFO.south west)$);
 \draw[very thick] ($(PreAmp.north east)!1/7!(PreAmp.south east)$)--+(0:8.525cm);%(c|-[]ADC.north);%-- ($(ADC.north west)!0.166!(ADC.south west)$);
 \draw[very thick] ($(PreAmp.north east)!2/7!(PreAmp.south east)$)--+(0:8.525cm);
 \draw[very thick] ($(PreAmp.north east)!3/7!(PreAmp.south east)$)--+(0:8.525cm);
 \draw[red, very thick] ($(PreAmp.north east)!4/7!(PreAmp.south east)$)--+(0:8.525cm);
 \draw[red, very thick] ($(PreAmp.north east)!5/7!(PreAmp.south east)$)--+(0:8.525cm);
 \draw[red, very thick] ($(PreAmp.north east)!6/7!(PreAmp.south east)$)--+(0:8.525cm);
    \end{tikzpicture}
    \end{document}

我的输出是

在此处输入图片描述

如何使用节点而不是坐标来绘制线条?我在想类似

\draw[very thick] (PreAmp.south/3)--(Disc.north/3);

即不必定义线的长度,只需连接两个节点。

直线

编辑根据@ClaudioFiandrino 在个人聊天中提出的建议,我使用以下代码来连接节点。

\draw ($(PreAmp.south west)!0.33!(PreAmp.south east)$)coordinate (a)--(a|-Disc.north);

虽然这适用于垂直线,但我无法让它适用于水平线,因为当我使用

\draw ($(Disc.north east)!0.33!(Disc.south east)$)coordinate (c)--(c|-FIFO.west);

我得到一条垂直线,如下图所示的黑线。我坚持使用calc和,|-因为此图中的节点不具有相同的尺寸。

在此处输入图片描述

“曲线”

重现曲线的唯一方法是使用相对坐标,但我觉得一定有更方便的方法。

所以我用

%curved cables
 \draw[red, very thick] (E1.east)-- ++(1.3cm,0) -- ++(0,-1.5cm)-- ++(1.3cm,0);%($(PreAmp.north west)!1/3!(PreAmp.south west)$);
 \draw[red, very thick] (E3.east)-- ++(1.3cm,0) -- ++(0,1.5cm)-- ++(1.3cm,0);
 \draw[very thick] (DE3.south)-- ++(0,-1.1cm) -- ++(-1.5cm,0)-- ++(0,-1.15cm);
 \draw[very thick] (DE1.south)-- ++(0,-1.1cm) -- ++(1.5cm,0)-- ++(0,-1.15cm);

输出为

在此处输入图片描述

答案1

您可以使用calc-library 来设置线路的连接点。这是一种简单的方法,在大多数情况下应该足够了。一个小的 MWE:

\documentclass[tikz, border=5mm]{standalone}
\usetikzlibrary{calc}

\begin{document}
 \begin{tikzpicture}
  \node at (0,0) [draw, rectangle, minimum height=2cm, minimum width=.5cm] (mynode) {};
  \foreach \y in {-.75,-.5,-.25}{
    \draw [red, ->] (-1,\y) -- ($(mynode.west) +(0,\y)$);
  }
  % ... more lines
 \end{tikzpicture}
\end{document}

渲染图像

这些信息是否足够让您满意或者您是否需要更多信息?

编辑:根据节点使用百分比值以下代码使用calc-library 将线连接器定位在节点边缘的百分比值上。(从您的答案中假设这一点)

节点需要相同的宽度/高度利用这个!

\documentclass[tikz, border=5mm]{standalone}
\usetikzlibrary{calc, positioning}

\begin{document}
 \begin{tikzpicture}
  \node [draw, rectangle, minimum width=3cm, minimum height=1cm] (PreAmp) {PreAmp};
  \node [draw, rectangle, minimum height=1cm, right=2cm of PreAmp] (ADC) {ADC};
  \node [draw, rectangle, minimum width=3cm, below=1cm of PreAmp] (Disc) {Discriminator};
  \foreach \x in {.25,.5,.75} {
   \draw ($(PreAmp.south west)!\x!(PreAmp.south east)$) -- ($(Disc.north west)!\x!(Disc.north east)$);
   \draw ($(PreAmp.north east)!\x!(PreAmp.south east)$) -- ($(ADC.north west)!\x!(ADC.south west)$);
  }
 \end{tikzpicture}
\end{document}

渲染图像

并排显示这两个解决方案:相对定位(1 点)与百分比定位(边缘)

  • 蓝色:节点边缘的百分比定位(节点需要相同的宽度/高度)
  • 红色:使用节点的锚点进行相对定位(节点的中心需要位于一个轴上才能实现此功能)

代码:

\documentclass[tikz, border=5mm]{standalone}
\usetikzlibrary{calc, positioning}

\begin{document}
 \begin{tikzpicture}
  \node [draw, rectangle, minimum width=3cm, minimum height=1cm] (PreAmp) {PreAmp};
  \node [draw, rectangle, minimum height=1cm, right=2cm of PreAmp] (ADC) {ADC};
  \node [draw, rectangle, minimum width=3cm, below=1cm of PreAmp] (Disc) {Discriminator};
  \foreach \x in {.25,.5,.75} {
   % Percentual on node edge
   \draw [ultra thick, blue] ($(PreAmp.south west)!\x!(PreAmp.south east)$) -- ($(Disc.north west)!\x!(Disc.north east)$);
   \draw [ultra thick, blue] ($(PreAmp.north east)!\x!(PreAmp.south east)$) -- ($(ADC.north west)!\x!(ADC.south west)$);
  }
  \foreach \x in {-.25,0,.25} {
   % Relative to one point
   \draw [red] ($(PreAmp.south) +(\x,0)$) -- ($(Disc.north) +(\x,0)$);
   \draw [red] ($(PreAmp.east)  +(0,\x)$) -- ($(ADC.west) +(0,\x)$);
  }
 \end{tikzpicture}
\end{document}

渲染图像

答案2

下一个代码展示了不使用 tikzlibrary 的情况下在节点之间绘制平行线的另一种可能性calc。它使用(node.angle)语法作为起点,|--|使用交点坐标作为终点。

如果您想要精确的坐标,例如节点垂直长度的 1/3,您需要calc库,但如果您可以接受某些视觉上足够好的东西,可以进行一些测试来找到正确的angle

\documentclass[tikz, border=5mm]{standalone}
\usetikzlibrary{positioning}

\begin{document}
 \begin{tikzpicture}
  \node [draw, rectangle, minimum width=3cm, minimum height=1cm] (PreAmp) {PreAmp};
  \node [draw, rectangle, minimum height=1cm, right=2cm of PreAmp] (ADC) {ADC};
  \node [draw, rectangle, minimum width=3cm, below=1cm of PreAmp] (Disc) {Discriminator};
  \foreach \x in {-7,0,7} {
   \draw [ultra thick, blue] (PreAmp.\x) -- (PreAmp.\x-|ADC.west);
   \draw [red] (PreAmp.\x) -- (PreAmp.\x-|ADC.west);
  }
  \foreach \x in {210,270,330} {
   \draw [ultra thick, blue] (PreAmp.\x) -- (PreAmp.\x|-Disc.north);
  }
  \foreach \x in {250,270,290} {
   \draw [red] (PreAmp.\x) -- (PreAmp.\x|-Disc.north);
  }
    \draw[purple,dashed] (PreAmp.center) -- ++(7:2cm);
    \draw[purple,dashed] (PreAmp.center) -- ++(330:2cm);
 \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容