节点间的曲线边缘如何画得更美观?

节点间的曲线边缘如何画得更美观?

输出的屏幕截图

我想要绘制像上面的有序线程二叉树的图片一样的前序/后序线程二叉树(来自 wiki 线程二叉树页面)。

我尝试了下面的代码,但是边缘C->B、E->F、H->G与原始图片有很大不同。我阅读了 tkz&pgf 手册,但没有找到一些方法可以使边缘像原始图片一样漂亮。

\documentclass[10pt, compress,xcolor=x11names,UTF8, aspectratio=169,border=2pt,tikz]{standalone}
% \documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{xeCJK}
%\setCJKmainfont{微软雅黑}
\setCJKmainfont{Hei}

\XeTeXlinebreaklocale "zh"          % Following set to break line automatically.
\XeTeXlinebreakskip = 1pt plus 1pt 

\usetikzlibrary{arrows,automata,positioning,trees,calc,positioning}
\tikzset{
    every picture/.style={
        semithick,shorten >=1pt,>=stealth',node distance=4em,
        every state/.style={minimum size=2em,align=center},
        accepting/.style={double distance=.1em,outer sep=\pgflinewidth}
    }
}

\begin{document}

\begin{tikzpicture}[smooth]

    \node[state]   (F)                                {$F$};

    \node[state]   (B)  [below left=2em and 2.5em of F]  {$B$};
    \node[state]   (G)  [below right=2em and 2.5em of F]  {$G$};

    \node[state]   (A)  [below left=1.5em and 1em of B]  {$A$};
    \node[state]   (D)  [below right=1.5em and 1em of B]  {$D$};
    \node[state]   (I)  [below right=1.5em and 1em of G]  {$I$};

    \node[state]   (C)  [below left=1.5em and 0.6em of D]  {$C$};
    \node[state]   (E)  [below right=1.5em and 0.6em of D]  {$E$};
    \node[state]   (H)  [below left=1.5em and 0.6em of I]  {$H$};

    \path[->]   (F) edge              (B)
                (F) edge              (G)

                (B) edge              (A)
                (B) edge              (D)
                (G) edge              (I)

                (D) edge              (C)
                (D) edge              (E)
                (I) edge               (H)

                (A) edge [->,dashed, out=-60,in=-110]            (B)
                (C) edge [->,dashed, out=-130,in=-85,looseness=1.5]           (B)
                (C) edge [->,dashed, out=-50,in=-100,looseness=1.5]           (D)
            (E) edge [->,dashed, out=-120,in=-80,looseness=1.5]           (D)
                (E) edge [->,dashed, out=-50,in=-100]           (F)
            (G) edge [->,dashed, out=-120,in=-70]           (F)
            (H) edge [->,dashed, out=-120,in=-95,looseness=1.5]           (G)
            (H) edge [->,dashed, out=-60,in=-100,looseness=1.5]           (I)
            ;
\end{tikzpicture}

\end{document}

我只是想知道如何绘制像红线这样的曲线边缘:

在此处输入图片描述

答案1

第 280 页Tikz & PGF 手册您可以找到很多to[]命令选项。特别是,controls=⟨coordinate⟩ and ⟨coordinate⟩为您的路径添加更多控制点。

您可以尝试以下操作

\path[->,green]
  (C) edge [->,dashed, out=-130,in=-85,looseness=1.5,controls=+(200:1.5) and +(-40:1)] (B)    
  (E) edge [->,dashed, out=-70,in=-100,controls=+(-30:1.7) and +(240:1.3)] (F)
  (H) edge [->,dashed, out=-120,in=-95,looseness=1.5,controls=+(240:1.5) and +(260:.7)]  (G);

(angle:radius)其中控制由分别相对于边的第一端和第二节点的极坐标给出。

在此处输入图片描述

尽情寻找最佳道路吧!

答案2

使用贝塞尔曲线代替边缘可以实现更多自由:

\documentclass[10pt, compress,xcolor=x11names,UTF8, aspectratio=169,border=2pt,tikz]{standalone}
% \documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
%\usepackage{xeCJK}
%%\setCJKmainfont{微软雅黑}
%\setCJKmainfont{Hei}
%
%\XeTeXlinebreaklocale "zh"          % Following set to break line automatically.
%\XeTeXlinebreakskip = 1pt plus 1pt 

\usetikzlibrary{arrows,automata,positioning,trees,calc,positioning,topaths}
\tikzset{
    every picture/.style={
        semithick,shorten >=1pt,>=stealth',node distance=4em,
        every state/.style={minimum size=2em,align=center},
        accepting/.style={double distance=.1em,outer sep=\pgflinewidth}
    }
}

\begin{document}

\begin{tikzpicture}[smooth]

    \node[state]   (F)                                {$F$};

    \node[state]   (B)  [below left=2em and 2.5em of F]  {$B$};
    \node[state]   (G)  [below right=2em and 2.5em of F]  {$G$};

    \node[state]   (A)  [below left=1.5em and 1em of B]  {$A$};
    \node[state]   (D)  [below right=1.5em and 1em of B]  {$D$};
    \node[state]   (I)  [below right=1.5em and 1em of G]  {$I$};

    \node[state]   (C)  [below left=1.5em and 0.6em of D]  {$C$};
    \node[state]   (E)  [below right=1.5em and 0.6em of D]  {$E$};
    \node[state]   (H)  [below left=1.5em and 0.6em of I]  {$H$};

    \draw[->,dashed] (H.south west) .. controls (0.5,-3.7) and (1.5,-2.7) .. (G.south);
\end{tikzpicture}

\end{document}

相关内容