# Best way to draw line between two nodes

I have a simply example to connect two node with a arrow line. but I doubt it's not the best way to do such things.

Requirement is:

1. Node location is fixed but maybe distance is unknow.
2. Connection line should be vertical or horizontal line.
3. Change direction between the middle of two nodes.

Current MWE as below:

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%

\tikzset{
block/.style={draw,text width=2em,minimum height=1em,align=center},
arrow/.style={->}
}

\begin{document}
\begin{tikzpicture}[>=stealth']
\node[block] (N1) {N1};
\node[block,below=1cm of N1,xshift=-1cm] (N2) {N2};
\path (N1) -- (N1 |- N2) coordinate [midway] (C);
\draw [arrow] (N2) |- (C) -| (N1);
\path[fill=red] (C) circle (2pt); % help dot,not must have
\end{tikzpicture}
\end{document}


Current Output is:

You could use the calc library to avoid having to draw the path separately. Is this the kind of thing you want?

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{arrows,positioning,calc}

\tikzset{
block/.style={draw,text width=2em,minimum height=1em,align=center},
arrow/.style={->}
}

\begin{document}
\begin{tikzpicture}[>=stealth']
\node[block] (N1) {N1};
\node[block,below=1cm of N1,xshift=-1cm] (N2) {N2};
\draw [arrow] (N2) |- ($(N1)!1/2!(N1 |- N2)$) coordinate (C) -| (N1);
\end{tikzpicture}
\end{document}


Or you could define a pic:

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{arrows,positioning,calc}

\tikzset{
block/.style={draw,text width=2em,minimum height=1em,align=center},
arrow/.style={->},
pics/block connector/.style n args=2{
code={
\path [pic actions] (#1) |- ($(#2)!1/2!(#2 |- #1)$) -| (#2);
},
},
}

\begin{document}
\begin{tikzpicture}[>=stealth']
\node[block] (N1) {N1};
\node[block, below=1cm of N1, xshift=-1cm] (N2) {N2};
\pic [draw=green, arrow] {block connector={N2.north west}{N1.north west}};
\pic [draw=red, arrow] {block connector={N2.south east}{N1.south east}};
\pic [draw=blue, arrow] {block connector={N1}{N2}};
\end{tikzpicture}
\end{document}


• Good answer, I am thinking about if it's possible to use chain library do it! – Beatlej Mar 14 '15 at 20:34
• @Beatlej Thanks! I'm sure you can combine them but chains do seem to be inherently unidirectional unless I've missed something. So you'd still need invisible nodes for the changes of direction, right? Just the change of direction would be for a chain? – cfr Mar 14 '15 at 21:15

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows,positioning,calc}

\tikzset{
block/.style={draw,text width=2em,minimum height=1em,align=center},
arrow/.style={->}
}
\newcommand\connect[2]{\path[draw,arrow] (#1) |- ($(#1)!1/2!(#2)$) -| (#2)}

\begin{document}
\begin{tikzpicture}[>=stealth']
\node[block] (N1) {N1};
\node[block,below=1cm of N1,xshift=-1cm] (N2) {N2};
\connect{N1}{N2};
\end{tikzpicture}
\end{document}


Result the same: