连接两个节点的最佳方式,路径绕过另一个节点

连接两个节点的最佳方式,路径绕过另一个节点

想象一下以下 MWE:

\documentclass{article}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

\node [draw, circle, minimum width=1cm] at (0,0)  (n1) {n1};
\node [draw, circle, minimum width=1cm] at (3,-1) (n2) {n2};
\node [draw, circle, minimum width=1cm] at (4,0)  (n3) {n3};

\path [in = 270, out = 0] (n1) edge (n3);

\end{tikzpicture}
\end{document}

如何正确控制路径绕过节点n2?我知道有多种方法,使用辅助coordinate,或者使用controls,但我不确定哪种方法会给出更好的结果。

在此处输入图片描述

答案1

下面我展示了两种可能性:一种使用语法.. controls ..,另一种使用through pointAndrew Stacey's answer自动连接节点而不重叠其他节点或连接决定哪一个能产生更好的结果将取决于几个因素(其中一些是主观因素):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\makeatletter
% code by Andrew Stacey: https://tex.stackexchange.com/a/27996/3954
\tikzset{
  through point/.style={
    to path={%
      \pgfextra{%
        \tikz@scan@one@point\pgfutil@firstofone(\tikztostart)\relax
        \pgfmathsetmacro{\pt@sx}{\pgf@x * 0.03514598035}%
        \pgfmathsetmacro{\pt@sy}{\pgf@y * 0.03514598035}%
        \tikz@scan@one@point\pgfutil@firstofone#1\relax
        \pgfmathsetmacro{\pt@ax}{\pgf@x * 0.03514598035 - \pt@sx}%
        \pgfmathsetmacro{\pt@ay}{\pgf@y * 0.03514598035 - \pt@sy}%
        \tikz@scan@one@point\pgfutil@firstofone(\tikztotarget)\relax
        \pgfmathsetmacro{\pt@ex}{\pgf@x * 0.03514598035 - \pt@sx}%
        \pgfmathsetmacro{\pt@ey}{\pgf@y * 0.03514598035 - \pt@sy}%
        \pgfmathsetmacro{\pt@len}{\pt@ex * \pt@ex + \pt@ey * \pt@ey}%
        \pgfmathsetmacro{\pt@t}{(\pt@ax * \pt@ex + \pt@ay * \pt@ey)/\pt@len}%
        \pgfmathsetmacro{\pt@t}{(\pt@t > .5 ? 1 - \pt@t : \pt@t)}%
        \pgfmathsetmacro{\pt@h}{(\pt@ax * \pt@ey - \pt@ay * \pt@ex)/\pt@len}%
        \pgfmathsetmacro{\pt@y}{\pt@h/(3 * \pt@t * (1 - \pt@t))}%
      }
      (\tikztostart) .. controls +(\pt@t * \pt@ex + \pt@y * \pt@ey, \pt@t * \pt@ey - \pt@y * \pt@ex) and +(-\pt@t * \pt@ex + \pt@y * \pt@ey, -\pt@t * \pt@ey - \pt@y * \pt@ex) .. (\tikztotarget)
    }
  }
}

\makeatother


\begin{document}

\begin{tikzpicture}
\node [draw, circle, minimum width=1cm] at (0,0)  (n1) {n1};
\node [draw, circle, minimum width=1cm] at (3,-1) (n2) {n2};
\node [draw, circle, minimum width=1cm] at (4,0)  (n3) {n3};
\path [through point=(n2.east)] (n1) edge (n3);
\begin{scope}[xshift=6cm]
\node [draw, circle, minimum width=1cm] at (0,0)  (n1) {n1};
\node [draw, circle, minimum width=1cm] at (3,-1) (n2) {n2};
\node [draw, circle, minimum width=1cm] at (4,0)  (n3) {n3};
\draw (n1) .. controls ([yshift=-13pt]n2.south west) and ([yshift=-33pt]n2.south east) .. (n3);
\end{scope}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容