老式

老式

我想将一条路径分成两个分支,如下面的 MWE 所示。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
  \node (A) [rectangle] {A};
  \node (B) [rectangle,above right=1cm and 4cm of A] {B};
  \node (C) [rectangle,below right=1cm and 4cm of A] {C};
  \draw [->, rounded corners=1cm] (A) -| +(2cm,0cm) |- (B);
  \draw [->, rounded corners=1cm] (A) -| +(2cm,0cm) |- (C);
\end{tikzpicture}
\end{document}

结果如下:

尴尬的结果

这其中存在很多问题。

  1. 左四分之一圆到 C 的方向错误
  2. 圆应该互相切线
  3. 它们应该恰好在 A 和 BC 的中间相遇

我认为我仍然不明白 TikZ 中的定位。有人能帮助我吗?

答案1

老式

定位

未使用此on grid选项时,节点的放置方式应使其边界之间的距离正确。使用此选项时,将使用on grid锚点.center进行计算。

代码

\documentclass[tikz,border=2pt]{standalone}
\usetikzlibrary{positioning}
\tikzset{
    every node/.style={draw,fill,fill opacity=.1},
    helper node/.style={draw=none,fill=none,fill opacity=1},
    ruler/.style={line width=1.2\pgflinewidth,red,|-|,shorten >=-.5\pgflinewidth,shorten <=-.5\pgflinewidth},
    helper/.style={line width=1.2\pgflinewidth,red,-,dashed},
    } % for debugging purposes

\begin{document}
\begin{tikzpicture}
    \draw[help lines] (-1,-2) grid (5,2);
  \node (A) [rectangle] {A};
  \node (B) [rectangle,above right=1cm and 4cm of A] {B};
  \node (C) [rectangle,below right=1cm and 4cm of A] {C};

  \draw[ruler] (A.north east) -- (A.north east |- B.south west) node[left,pos=.5,helper node] {1\,cm};
  \draw[helper] (A.north east |- B.south west) -- (B.south east);
  \draw[ruler] (A.north east) -- (A.north east -| B.south west) node[above,pos=.5,helper node] {4\,cm};
  \draw[helper] (A.north east -| B.south west) -- (B.north west);
\end{tikzpicture}

\begin{tikzpicture}[on grid]
    \draw (-1,-2) grid (5,2);
  \node (A) [rectangle] {A};
  \node (B) [rectangle,above right=1cm and 4cm of A] {B};
  \node (C) [rectangle,below right=1cm and 4cm of A] {C};

  \draw[ruler] (A.center) -- (A.center |- B.center) node[left,pos=.5,helper node] {1\,cm};
  \draw[helper] (A.center |- B.center) -- (B.east);
  \draw[ruler] (A.center) -- (A.center -| B.center) node[above,pos=.5,helper node] {4\,cm};
  \draw[helper] (A.center -| B.center) -- (B.north);
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

奇怪的垂直线条

不要将 用作-|路径的第一部分,因为下一个点(2cm,0cm)实际上已经与起点水平。为什么 缺少 这条奇怪的线C?因为它与 相同B,显然 TikZ 会>=与 进行比较- 起点和目标坐标的值来计算圆必须朝哪个方向(左或右)行进。

代码

  \draw [->, rounded corners=1cm] (A) -| +(2cm, 0cm   ) |- (B);
  \draw [->, rounded corners=1cm] (A) -| +(2cm,-0.01cm) |- (C);

输出

在此处输入图片描述

圆角圆圈

圆弧半径1cm至少需要2cm完成 90 度左转和 90 度右转。因此,您要么.5cm在版本中使用 (= 垂直距离的一半) on grid,要么在路径操作中计算所需的半径。

最简单的方法是使用let … in需要calc库的操作。
我提供了一种curvy采用一个可选参数(默认值2cm:)的样式。

代码

\documentclass[tikz,border=2pt]{standalone}
\usetikzlibrary{positioning,calc}
\tikzset{
    curvy/.style={
        to path={
            let \p1=(\tikztostart),
                \p2=(\tikztotarget),
                \n1={abs(\y2-\y1)/2}
            in
            [rounded corners=\n1]
            (\tikztostart) -- + (0:#1) |- (\tikztotarget) \tikztonodes
        }
    },
    curvy/.default=2cm,
}
\begin{document}
\begin{tikzpicture}
  \node (A) [rectangle] {A};
  \node (B) [rectangle,above right=1cm and 4cm of A] {B};
  \node (C) [rectangle,below right=1cm and 4cm of A] {C};
  \draw [->] (A) to[curvy]     (B);
  \draw [->] (A) to[curvy=3cm] (C);
\end{tikzpicture}

\begin{tikzpicture}[on grid]
  \node (A) [rectangle] {A};
  \node (B) [rectangle,above right=1cm and 4cm of A] {B};
  \node (C) [rectangle,below right=1cm and 4cm of A] {C};
  \draw [->] (A) to[curvy]     (B);
  \draw [->] (A) to[curvy=1cm] (C);
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

新风格

您可能想要查看 TikZ 的树生产设施。

代码

\documentclass[tikz,border=2pt]{standalone}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}[
    grow'=right,% clockwise, not counterclockwise (which would be "grow")
    level 1/.style={level distance=4cm,sibling distance=2cm},
    level 2/.style={level distance=2cm,sibling distance=1.5cm},
    edge from parent path={% the let ... in operator is only used to set rounded corners 
                           % to zero if parent and child are on the same height, but due to
                           % imprecision in the math-engine the comparison value is 0.001
        let \p1=(\tikzparentnode\tikzparentanchor),
            \p2=(\tikzchildnode\tikzchildanchor),
            \n1={abs(\y2-\y1) > 0.001} 
        in
        [rounded corners=\n1*\tikzsiblingdistance/4]
             (\tikzparentnode\tikzparentanchor)
            -- + (0:\tikzleveldistance/2)
            |-   (\tikzchildnode\tikzchildanchor)
    }
]
\node {A}
    child {node {B} 
        child {node {B1}}
        child {node {B2}}
        child {node {B3}}
    }
    child {node {C}}
;
\end{tikzpicture}

输出

在此处输入图片描述

答案2

是這樣嗎?

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
  \node (A) [rectangle] {A};
  \node (B) [rectangle,above right=1cm and 4cm of A] {B};
  \node (C) [rectangle,below right=1cm and 4cm of A] {C};
  \draw [->, rounded corners=.7cm] (A) -- (0:2cm)  |- (B);
  \draw [->, rounded corners=.7cm] (A) -- (0:2cm) |- (C);
\end{tikzpicture}
\end{document}

相关内容