正交路径布线

正交路径布线

TLDR;有没有办法可以指定起点和终点,以便它能够(希望是正交的)绕过所有节点和其他路径。

我正在将大量 visio 流程图转换为 ,TikZ但一直遇到一个问题。具有多个弯道的正交*路径很难实现。我还试图让图表保持动态,这样将来就可以进行任何更改,而无需对图表进行大量调整。

现在,我可以制作它们并保持它们的动态,但这似乎比它应该的要难得多。以红线为例:该线至少需要 3 个不同的点,为了保持其动态,我最终使用了 5 个!如果我可以指定一个起点和终点,它将绕过所有节点和其他路径,那就太好了。

*我使用的是 Omnigraffle 的术语,所以我的意思是路径的任何方向的变化都是 90 度的倍数。

正交路径2

\documentclass{article}

\usepackage{tikz}
\usepackage[margin=0.5in]{geometry}
\pagestyle{empty}

\begin{document}

%\input{../tikz-setup.tex}
\usetikzlibrary{shapes, arrows, calc, positioning}

% Define block styles
\tikzstyle{state}   = [ rounded rectangle, 
                        draw, 
                        text centered, 
                        minimum height=3em ,
                        minimum width = 6em,
                        inner sep = 5pt
                      ]
\tikzstyle{test}    = [ diamond, 
                        draw, 
                        shape aspect=2, 
                        inner sep = 0pt,
                        text width = 7em,
                        text centered
                      ]
\tikzstyle{action}  = [ rectangle, draw,
                        text width=8em,
                        inner sep = 5pt, 
                        minimum height=5em
                      ]
\tikzstyle{data}    = [ trapezium, 
                        draw, 
                        trapezium left angle=60, 
                        trapezium right angle=120pt,
                        minimum height = 6em, 
                        text width = 5em
                       ]
\tikzstyle{line}    = [ draw, -triangle 45 ]

\begin{center}
\begin{tikzpicture}[align = flush center, font = \small]
    % Place nodes
    \matrix [column sep = 2.5em, row sep = 2em] (mtrx)
    {

    \node [state] (a) {a}; &[1em]
    \node (b) {}; \\

    \node [action] (c) {c}; &
    \node [action] (d) {d}; \\

    \node [test] (e) {e}; &
    \node [test] (f) {f}; \\

   \node [action] (g) {g}; &
    \node [action] (h) {h}; \\

    \node [action] (i) {i};&
    \node [action] (j) {j}; \\

    &
    \node [state] (k) {k}; \\
    };

    % Draw edges
    \path [line] (a) -- (c);
    \path [line] (c) -- (e);

    \path [line] (e) -- node [right, near start] {Yes} (g);
    \path [line] (e.east) -- node [above] {No} ( $(e.east)!0.3!(f.west)$ ) |- ( $(g)!0.45!(i)$ );

    \path [line] (g) -- (i);

    \path [line,red] let \p1=( $(a.south)!0.6!(b.south)$ ) in (i.south) -- +(0, -0.5) -| (\p1) -| (d.north);


\end{tikzpicture}
\end{center}
\end{document}

原始图像

答案1

如果你经常需要这个功能,我建议创建一个特定的命令,例如

\newcommand{\renvoi}[3][pos=0.5]{
\path (#2) -- (#3)coordinate[#1](mm);
\draw[-latex,green] (#2) --($(#2.south)+(0,-0.5)$)-| (mm) |- ($(#3.north)+(0,0.5)$)--(#3);
}

您将能够使用几乎所有的回报,如下例所示

在此处输入图片描述

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage[margin=0.5in]{geometry}
\pagestyle{empty}


\newcommand{\renvoi}[3][pos=0.5]{
\path (#2) -- (#3)coordinate[#1](mm);
\draw[-latex,red] (#2) --($(#2.south)+(0,-0.5)$)-| (mm) |- ($(#3.north)+(0,0.5)$)--(#3);
}

\begin{document}

%\input{../tikz-setup.tex}
\usetikzlibrary{shapes, arrows, calc, positioning}

% Define block styles
\tikzstyle{state}   = [ rounded rectangle, 
                        draw, 
                        text centered, 
                        minimum height=3em ,
                        minimum width = 6em,
                        inner sep = 5pt
                      ]
\tikzstyle{test}    = [ diamond, 
                        draw, 
                        shape aspect=2, 
                        inner sep = 0pt,
                        text width = 7em,
                        text centered
                      ]
\tikzstyle{action}  = [ rectangle, draw,
                        text width=8em,
                        inner sep = 5pt, 
                        minimum height=5em
                      ]
\tikzstyle{data}    = [ trapezium, 
                        draw, 
                        trapezium left angle=60, 
                        trapezium right angle=120pt,
                        minimum height = 6em, 
                        text width = 5em
                       ]
\tikzstyle{line}    = [ draw, -triangle 45 ]

\begin{center}
\begin{tikzpicture}[align = flush center, font = \small]
    % Place nodes
    \matrix [column sep = 2.5em, row sep = 2em] (mtrx)
    {

    \node [state] (a) {a}; &[1em]
    \node (b) {}; \\

    \node [action] (c) {c}; &
    \node [action] (d) {d}; \\

    \node [test] (e) {e}; &
    \node [test] (f) {f}; \\

   \node [action] (g) {g}; &
    \node [action] (h) {h}; \\

    \node [action] (i) {i};&
    \node [action] (j) {j}; \\

    &
    \node [state] (k) {k}; \\
    };

    % Draw edges


\renvoi{i}{j}
\renvoi{e}{g}
\renvoi[left=3em of g]{e}{i}
\renvoi[pos=0.7]{f}{c}
\renvoi[right=3em of f]{f}{d}

\end{tikzpicture}
\end{center}
\end{document}

指示您选择的选项或传递您的链接

  • 在两个节点的中间 \renvoi{i}{j} 默认
  • \renvoi\[pos=0.7\]{f}{c} 交点位于线段 (f) (c) 上的 0.7
  • \renvoi[right=3em of f]{f}{d}交叉点位于 3em 右侧(使用此选项需要定位库)

它还需要改进这个命令,以便在所有情况下绘制链接,甚至添加文本或更改链接的样式。

相关内容