TikZ:用 U 形线连接两个未对齐的节点

TikZ:用 U 形线连接两个未对齐的节点

我想连接两个在 X 或 Y 维度上都不对齐的节点,节点 2 位于节点 1 的右下方(为了论证的目的)。该线应从节点 1 开始,水平向右延伸,直到到达节点 2 上方,然后我希望它掉头并从右侧连接到节点 2。

以下代码产生了我想要的结果:

\documentclass[12pt]{standalone}
\usepackage{amssymb,amsfonts,amsmath}
\usepackage{tikz}

\usetikzlibrary{shapes.geometric}
\usetikzlibrary{scopes}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}


\tikzset{copy/.style={circle,inner sep=0pt,fill=black,minimum
    size=5pt} }

\def\tnboxsize{6mm}

\tikzset{box/.style={rectangle,draw=black,fill=none, minimum
    width=\tnboxsize, minimum height=\tnboxsize,
    shape border uses incircle}}

\tikzset{blank/.style={rectangle,inner
    sep=0pt,draw=none,fill=none,minimum size=0pt} }

\tikzset{blankbig/.style={rectangle,inner sep=0pt,draw=none,fill=none,minimum
    size=\tnboxsize} }


\begin{document}
    \begin{tikzpicture}[thick]
      \node[blank] (out1) {};
      \node[blank] (out2) [below=0.5 of out1] {};
      \node[blank] (out3) [below=0.5 of out2] {};

      \node[blank] (anchorout) at ($(out1)!0.5!(out2)$) {};
      \node[copy] (copy) [right=0.6 of anchorout] {};
      \node[box] (c) [right=of out3] {$C$};

      \draw [-] (out1) -- (copy);
      \draw [-] (out2) -- (copy);
      \draw [-] (out3) -- (c);
      \draw [-] (copy) -- +(0.75,0) .. controls +(0.7,0) and +(0.7,0) .. (c);
\end{tikzpicture}

\end{document}

像这样:

好的

然而,要做到这一点,我必须手动调整控制点的数量 \draw [-] (copy) -- +(0.75,0) .. controls +(0.7,0) and +(0.7,0) .. (c)

例如,当这些数字乱七八糟的时候,图就是这样的:

在此处输入图片描述

有没有办法强制执行像第一张图片中的行为,而无需手动调整控制点的位置?

答案1

与上述评论中的主张有些相似,但也解决了一些题外话:

\documentclass[12pt, tikz, margin=3mm]{standalone}
\usepackage{amssymb,amsfonts,amsmath}

\usetikzlibrary{calc,
                decorations.pathreplacing,
                positioning,
                shapes.geometric, scopes}
\tikzset{
 copy/.style = {circle,inner sep=0pt,fill=black,minimum size=5pt},
  box/.style = {rectangle,draw=black,fill=none, 
                minimum width=\tnboxsize, minimum height=\tnboxsize,
                shape border uses incircle},
blank/.style = {rectangle,inner sep=0pt, draw=none,fill=none, minimum size=0pt}, % <--- not needed, rather use coordinate 
blankbig/.style = {rectangle,inner sep=0pt,draw=none,fill=none, minimum size=\tnboxsize} 
        }
\def\tnboxsize{6mm}


\begin{document}
    \begin{tikzpicture}[thick,
    node distance = 5mm and 6mm  % <--- added
                        ]
    \coordinate                 (out1);
    \coordinate[below=of out1]  (out2);
    \coordinate[below=of out2]  (out3);

    \node[copy] (copy)  [right=of $(out1)!0.5!(out2)$]  {}; % <--- removed one coordinate
    \node[box]  (c)     [right=of out3 -| copy]         {$C$};

    \draw   (out1) -- (copy)  % <--- slightly shorter code
            (out2) -- (copy) 
            (out3) -- (c) 
            (copy.east) -- (copy -| c.east) % <--- auxiliary coordinate 

                        .. controls +(0.5,0) and +(0.5,0) .. (c.east);
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

您可以执行以下操作(只需更改上次的\draw命令):

  1. copy从 y 坐标为点copy,以 的东点c为 x 坐标绘制:

    (copy) -- (copy -| c.east)
    
  2. 然后使用c.east从角度 0 开始,到达角度 0,松散度为 2(应该是半个圆,或多或少)的曲线:

    to [out=0, in=0, looseness=2]
    

像那样:

 \draw (copy) -- (copy -| c.east) 
      to[out=0, in=0, looseness=2] (c.east);

你得到:

在此处输入图片描述

完整 MWE:

\documentclass[12pt]{standalone}
\usepackage{amssymb,amsfonts,amsmath}
\usepackage{tikz}

\usetikzlibrary{shapes.geometric}
\usetikzlibrary{scopes}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}


\tikzset{copy/.style={circle,inner sep=0pt,fill=black,minimum
    size=5pt} }

\def\tnboxsize{6mm}

\tikzset{box/.style={rectangle,draw=black,fill=none, minimum
    width=\tnboxsize, minimum height=\tnboxsize,
    shape border uses incircle}}

\tikzset{blank/.style={rectangle,inner
    sep=0pt,draw=none,fill=none,minimum size=0pt} }

\tikzset{blankbig/.style={rectangle,inner sep=0pt,draw=none,fill=none,minimum
    size=\tnboxsize} }


\begin{document}
    \begin{tikzpicture}[thick]
      \node[blank] (out1) {};
      \node[blank] (out2) [below=0.5 of out1] {};
      \node[blank] (out3) [below=0.5 of out2] {};

      \node[blank] (anchorout) at ($(out1)!0.5!(out2)$) {};
      \node[copy] (copy) [right=0.6 of anchorout] {};
      \node[box] (c) [right=of out3] {$C$};

      \draw [-] (out1) -- (copy);
      \draw [-] (out2) -- (copy);
      \draw [-] (out3) -- (c);
      %
      \draw (copy) -- (copy -| c.east)
          to[out=0, in=0, looseness=2] (c.east);
\end{tikzpicture}

\end{document}

相关内容