使用 TikZ 制作流程图

使用 TikZ 制作流程图

我正在尝试使用 tikz 环境构建流程图,但在连接“否”线时遇到了一些问题决定整数(以及来自更新整数).如何画一条线避免重叠?

% Define block styles
\tikzstyle{decision} = [diamond, draw, fill=blue!20, 
    text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20, 
    text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm,
    minimum height=2em]

\begin{tikzpicture}[node distance = 4cm, auto]
    % Place nodes
    \node [block] (init) {Generazione scariche ($2^H$)};

    \node [block, left of=init] (W_legali) {$\mu_W$ \break $\rho_b^{-1}=\frac{P}{n_b+1}$};
    \node [block, right of=init] (W_nlegali) {$\mu_W$ \break $\rho_b^{-1}=\frac{P}{n_b}$};

     \node [block, below of=W_legali] (fo_legale) {$min$ $f.o_{W \ge W_L}$};
     \node [block, below of=W_nlegali] (fo_nlegale) {$min$ $f.o_{W < W_L}$};

    \node [decision, below of=init, node distance=6cm] (decide) {$min$ $f.o_{W \ge W_L}$ < $min$ $f.o_{W < W_L}$};
    \node [block, right of=fo_nlegale, node distance=3cm] (update) {Aggiorno $n_b$};

    % Draw edges
    \path [line] (init) -- node {$W_i \ge W_L$}(W_legali);
    \path [line] (init) -- node {$W_i < W_L$}(W_nlegali);
    \path [line] (W_legali) -- (fo_legale);
    \path [line] (W_nlegali) -- (fo_nlegale);

    \path [line] (fo_legale) -- (decide);
    \path [line] (fo_nlegale) -- (decide);

    \path [line] (decide) -| node [near start] {si} (update);
    \path [line] (decide) |- node {no} (init);

\end{tikzpicture}

我努力追求这样的效果(忘记线条粗细):

在此处输入图片描述

答案1

您需要将路径分成几个子路径:

\path [line] (decide) -| node [near start] {no} ([xshift=-1cm] W_legali.west)
                      |- ([yshift=+1cm, xshift=-.2cm] init.north) coordinate (aux)
                      -- ([xshift=-.2cm] init.north);
\path [line] (update) |- ([xshift=.4cm] aux) -- ([xshift=.2cm] init.north);

代码

\documentclass[tikz,]{standalone}
\usepackage[italian]{babel}
\usetikzlibrary{arrows,shapes.geometric,positioning}
\begin{document}
\begin{tikzpicture}[
  node distance = 2cm,
  auto,
  decision/.style={diamond, draw, fill=blue!20, 
    text width=4.5em, align=center, node distance=3cm, inner sep=0pt},
  block/.style={rectangle, draw, fill=blue!20, 
    text width=5em, align=center, rounded corners, minimum height=4em},
  line/.style={draw, -latex'},
  cloud/.style={draw, ellipse,fill=red!20, node distance=3cm,
    minimum height=2em},
%  on grid
  ]
  % Place nodes
  \node [block] (init) {Generazio-ne scariche ($2^H$)};

  \node [block, left=of init]   (W_legali) {$\mu_W$ \\ $\rho_b^{-1}=\frac{P}{n_b+1}$};
  \node [block, right=of init] (W_nlegali) {$\mu_W$ \\ $\rho_b^{-1}=\frac{P}{n_b}$};

  \node [block, below=of W_legali]  (fo_legale)  {$\min$ $f.o_{W \ge W_L}$};
  \node [block, below=of W_nlegali] (fo_nlegale) {$\min$ $f.o_{W < W_L}$};

  \node [decision, below=3cm of init]   (decide)
                             {$\min$ $f.o_{W \ge W_L}$ \\${}< \min$ $ f.o_{W < W_L}$};
  \node [block,    right=of fo_nlegale] (update) {Aggiorno $n_b$};

  % Draw edges
  \path [line] (init) -- node[swap] {$W_i \ge W_L$} (W_legali);
  \path [line] (init) -- node       {$W_i < W_L$}   (W_nlegali);

  \path [line] (W_legali)   -- (fo_legale);
  \path [line] (W_nlegali)  -- (fo_nlegale);
  \path [line] (fo_legale)  -- (decide);
  \path [line] (fo_nlegale) -- (decide);

  \path [line] (decide) -| node [near start] {si} (update);
  \path [line] (decide) -| node [near start] {no} ([xshift=-1cm] W_legali.west)
                        |- ([yshift=+1cm, xshift=-.2cm] init.north) coordinate (aux)
                        -- ([xshift=-.2cm] init.north);
  \path [line] (update) |- ([xshift=.4cm] aux) -- ([xshift=.2cm] init.north);
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

相关内容