How to obtain Rectangular Lines between nodes with tikz?

The code below uses the following path constructions. There are many more, see the tikz manual or any of the many examples on the internet).

  • (a) -- (b) describes a straight path from a to b.

  • (a) -| (b) describes a path from a to b, first horizontally until it is below or above b and then vertically.

  • (a) |- (b) is the same but starting vertically and then continuing horizontally.

  • ++(1,-2) denotes a position relative to the preceding one, 1 unit to the right and 2 units down. So (a) -| ++(1,-2) is a path starting at a, going 1 unit to the right and 2 units down. As a side effect, the new starting position has moved to the end of the path. So

    \draw (a) -| ++(1,-2) -| ++(-1,2);
    

    draws a rectangle with the final position being again at a.

  • +(1,-2) is basically the same as ++(1,-2), except that the position does not move.

    \draw (a) -| +(1,-2) -| +(-1,2);
    

    draws two lines with an angle each, one to the right of a and one to the left of a.

enter image description here

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning,automata}

\begin{document}
\begin{tikzpicture}
  [>=latex,
   action/.style={draw,thick},
   test/.style={draw, thick, shape aspect=2.7, diamond}
  ]
\node[action] (0) {Data arrival};
\node[action, below=of 0] (1) {Coks backoff procedure};
\node[action, below=of 1] (2) {Get trigger-frame?};
\node[action, below=of 2] (3) {Performs backoff procedure};
\node[test, below= 0.5cm of 3] (4) {$b<0$?};
\node[action, left=of 3] (5) {Do something};
\path[->]
  (0) edge node {} (1)
  (1) edge node {} (2)
  (2) edge node {} (3)
  (3) edge node {} (4);
\draw[->] (4) -- node[below right,pos=0.2]{N} ++(3,0) |- (2);     
\draw[->] (4) -| node[below left,pos=0.1]{Y} (5);
\draw[->] (5) |- (2); 
\draw[->] (4) --node[right] {maybe} +(0,-1.5);     
\end{tikzpicture}
\end{document}

Slightly modified gernot answer (for joy and exercise):

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows, chains, positioning, shapes}% added chains
\makeatletter
\tikzset{supress chain/.code={\def\tikz@after@path{}}}% added for suppress joining of nodes
\makeatother

\begin{document}
\begin{tikzpicture}[
            > = latex,
node distance = 5mm and 7mm,% added (not used default value)
  start chain = going below,% activation of chains
action/.style = {draw, thick, on chain, join= by ->},% nodes are in chain and connected by ->
  test/.style = {diamond, draw, thick, shape aspect=2.4, on chain, join= by ->}% node is in the chain and connected by -> with previous node
  ]
\node[action]   (n0) {Data arrival};
\node[action]   (n1) {Coks backoff procedure};
\node[action]   (n2) {Get trigger-frame?};
\node[action]   (n3) {Performs backoff procedure};
\node[test]     (n4) {$b<0$?};
\node[action, 
      supress chain, % this node is not connected with join
      left=of n3]   (n5) {Do something};
\draw[->] (n4) -| node[below,pos=0.25] {Y}  (n5); % left feedback loop
\draw[->] (n5) |- (n0); % left feedback loop
\draw[->] (n4) -|  ([xshift=5mm] n3.east) node[below,pos=0.25] {N} |- (n2); % right feedback loop
\draw[->] (n4.south) -- node[right] {maybe} ++ (0,-1.1);
\end{tikzpicture}
\end{document}

The results is almost the same:

enter image description here

Tags:

Tikz Pgf