連接 2 個節點且路徑繞過其他節點的最佳方式

連接 2 個節點且路徑繞過其他節點的最佳方式

想像以下 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或 with controls,但我不確定什麼會產生更好的結果。

在此輸入影像描述

答案1

下面我展示了兩種可能性;一種使用.. controls ..語法,另一種使用through point中實現的樣式Andrew 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}

在此輸入影像描述

相關內容