2

I have some problems bending the edges of my grap properly.

I want to draw a flow network, so far I have done the following:

enter image description here

What I would like to have is that the edges going out from the Source node (S) stay in the height of that node as long as possible, until they bend to go to the corresponding node in the lower layer. Seen from the top-view the edges should be a straight line. Furthermore, they should enter the nodes of the middle layer vertically.

For example consider the nodes at the very left of the middle layer: The edges do not reach the node at the north anchor but at bit shifted to the left. So I want the edges connect these nodes at the north anchor with the arrow tip oriented vertically, showing at the bottom.

Attached is the code used to produce this graph.

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}

\usetikzlibrary{shapes,fit,calc,positioning} %use shapes library if you need ellipse
\usetikzlibrary{arrows}

\usepackage[english]{babel} % The dictionary for hyphenation
\usepackage[utf8]{inputenc} % input-encoding
\usepackage[T1]{fontenc}    % Schriftfamilie T1 ermöglicht Trennung und
                            % Suche von Wörtern mit Umlauten. 

\definecolor{linkNode_color}{RGB}{60,60,60}
\definecolor{detNode_color}{RGB}{60,60,60}
\definecolor{sourceNode_color}{RGB}{60,60,60}



\newcommand{\myGlobalTransformation}[2]
{
    \pgftransformcm{1}{0}{0.4}{0.5}{\pgfpoint{#1cm}{#2cm}}
}

% draw a 4x4 helper grid in 3D
% Input: point of origins x and y coordinate and additional drawing-parameters
\newcommand{\gridThreeD}[3]
{
    \begin{scope}
        \myGlobalTransformation{#1}{#2};
        \draw [#3,step=1.5cm] grid (10.5,10.5);
    \end{scope}
}


\begin{document}

\newlength{\sizeCircle}
\setlength{\sizeCircle}{0.2cm}
\def \NodeDistance{4}

\tikzstyle{linkNode}=[draw,circle,fill=linkNode_color,inner sep=0pt,minimum size=\sizeCircle]
\tikzstyle{detNode}=[draw,circle,fill=detNode_color,inner sep=0pt,minimum size=\sizeCircle]
\tikzstyle{sourceNode}=[draw,circle,fill=sourceNode_color,inner sep=0pt,minimum size=\sizeCircle]
\tikzstyle{linkEdge}=[->,>=stealth',black,thick]
\tikzstyle{sourceEdge}=[->,>=stealth',black,thick]
\tikzstyle{detEdge}=[->,>=stealth',black,thick]

 \begin{tikzpicture}
 \begin{scope}[scale=0.2]
      \gridThreeD{0}{9}{black!50};
      \gridThreeD{0}{-5}{black!50};
    \begin{scope}
     \myGlobalTransformation{0}{5}
     \node[linkNode] (link_1_1) at (0,0) {};
     \node[linkNode] (link_1_2) at (0,2) {};
     \node[linkNode] (link_2_1) at (2*\NodeDistance,0) {};
     \node[linkNode] (link_2_2) at (2*\NodeDistance,2) {};
     \node[linkNode] (link_3_1) at (4*\NodeDistance,0) {};
     \node[linkNode] (link_3_2) at (4*\NodeDistance,2) {};
     \node[detNode] (det_1_1) at (1*\NodeDistance,0) {};
     \node[detNode] (det_1_2) at (1*\NodeDistance,2) {};
     \node[detNode] (det_2_1) at (3*\NodeDistance,0) {};
     \node[detNode] (det_2_2) at (3*\NodeDistance,2) {};
     \node[detNode] (det_3_1) at (5*\NodeDistance,0) {};
     \node[detNode] (det_3_2) at (5*\NodeDistance,2) {};


     \draw[linkEdge] (det_1_1) -- (link_2_1);
     \draw[linkEdge] (det_1_1) -- (link_2_2);
     \draw[linkEdge] (det_1_2) -- (link_2_1);
     \draw[linkEdge] (det_1_2) -- (link_2_2);
     \draw[linkEdge] (det_1_1) to[out=300, in=270, looseness=1]  (link_3_1);
     \draw[linkEdge] (det_1_2) to[out=120, in=120, looseness=1]  (link_3_2);
     \draw[linkEdge] (det_2_1) -- (link_3_1);
     \draw[linkEdge] (det_2_2) -- (link_3_1);
     \draw[linkEdge] (det_2_1) -- (link_3_2);
     \draw[linkEdge] (det_2_2) -- (link_3_2);

     \draw[detEdge] (link_1_1) -- (det_1_1);
     \draw[detEdge] (link_1_2) -- (det_1_2);
     \draw[detEdge] (link_2_2) -- (det_2_2);
     \draw[detEdge] (link_3_1) -- (det_3_1);
     \draw[detEdge] (link_3_2) -- (det_3_2);
     \draw[detEdge] (link_2_1) -- (det_2_1);
    \end{scope}

    \begin{scope}
     \myGlobalTransformation{0}{9}
     \node[sourceNode] (source) at (4,4) {S};
     \draw[sourceEdge] (source.west) to[out=180, in=120,looseness=2]  (link_1_1);
     \draw[sourceEdge] (source.west) to[out=180, in=120,looseness=2]  (link_1_2);
     \draw[sourceEdge] (source.south) to[out=180, in=120,looseness=1]  (link_2_1);
     \draw[sourceEdge] (source.south) to[out=180, in=120,looseness=1]  (link_2_2);
     \draw[sourceEdge] (source.east) to[out=0, in=110,looseness=1]  (link_3_1.north);
     \draw[sourceEdge] (source.east) to[out=0, in=110,looseness=1]  (link_3_2.north);
      \pgftransformreset
    \end{scope}

        \begin{scope}
     \myGlobalTransformation{0}{-5}
     \node[sourceNode] (sink) at (6,10) {T};
     \draw[sourceEdge] (det_1_1.south) to[out=270, in=180,looseness=1]  (sink.west);
     \draw[sourceEdge] (det_1_2.south) to[out=270, in=180,looseness=1]  (sink.west);
     \draw[sourceEdge] (det_2_1.south) to[out=270, in=0,looseness=1]  (sink.east);
     \draw[sourceEdge] (det_2_2.south) to[out=270, in=0,looseness=1]  (sink.east);
     \draw[sourceEdge] (det_3_1.south) to[out=270, in=0,looseness=1]  (sink.east);
     \draw[sourceEdge] (det_3_2.south) to[out=270, in=0,looseness=1]  (sink.east);

    \end{scope}


       \begin{scope}
     \myGlobalTransformation{0}{5}
     \node[linkNode] (link_1_1) at (0,0) {};
     \node[linkNode] (link_1_2) at (0,2) {};
     \node[linkNode] (link_2_1) at (2*\NodeDistance,0) {};
     \node[linkNode] (link_2_2) at (2*\NodeDistance,2) {};
     \node[linkNode] (link_3_1) at (4*\NodeDistance,0) {};
     \node[linkNode] (link_3_2) at (4*\NodeDistance,2) {};
     \node[detNode] (det_1_1) at (1*\NodeDistance,0) {};
     \node[detNode] (det_1_2) at (1*\NodeDistance,2) {};
     \node[detNode] (det_2_1) at (3*\NodeDistance,0) {};
     \node[detNode] (det_2_2) at (3*\NodeDistance,2) {};
     \node[detNode] (det_3_1) at (5*\NodeDistance,0) {};
     \node[detNode] (det_3_2) at (5*\NodeDistance,2) {};
    \end{scope}
 \end{scope}
  \end{tikzpicture}



\end{document}
  • 1
    You can make it easier for people to help you if you reduce your code to the absolute minimum necessary to reproduce the problem. Oterhwise, everyone needs to dig through your code in order to understand what part does what, when two nodes would probably be enough to replicate the problem. This is called a Minimal Working Example (MWE). – Fritz Sep 19 '14 at 16:20
4

Possibly something like this? I've basically reimplemented most of the code from scratch, primarily to illustrate how it can be done more compactly:

\documentclass[tikz,border=5]{standalone}
\begin{document}
\begin{tikzpicture}[z=(240:.25cm), >=stealth, 
   every node/.style={circle, draw, fill=black!50, inner sep=.5ex}]

\foreach \x in {1,...,6}
  \foreach \z in {1,2}
    \node (node-\x-\z) at (\x,0,\z*2-3) {}; 

\foreach \i [evaluate={\j=int(\i+1);}] in {1,...,5}
  \foreach \z in {1,2}
    \draw [->] (node-\i-\z) -- (node-\j-\z);

\foreach \i/\j in {2/3, 4/5}{
  \draw [->] (node-\i-1) -- (node-\j-2);
  \draw [->] (node-\i-2) -- (node-\j-1);
}

\node at (2, 2,0) (S) {S}; \node at (4,-2,0) (T) {T}; 

\foreach \i/\out in {1/180,3/270,5/0}
  \foreach \z in {1,2}
    \draw [->] (S) to [out=\out,in=90] (node-\i-\z);

\foreach \i/\in in {2/180,4/90,6/0}
  \foreach \z in {1,2}
    \draw [->] (node-\i-\z) to [out=270,in=\in] (T);

\foreach \z in {1,2}
  \draw [->] (node-2-\z) to [out=\z*180-135, in=\z*180-135, looseness=.5] (node-5-\z);

\end{tikzpicture}
\end{document}

enter image description here

  • thanks, looks nice and code it's very short. So I just wonder if it is possible to control the way the edges are drawn in the following way: consider the edge from the very right node (at y=0) going down to the "T" node. How can I change the image such that this edges goes for a longer time just down, before it bends to left and connects to the "T" node. So basically, the edges from the node to the "T" lies on a circle. How can I change the radius ? Still, I want the outgoing/incoming angle to be the same. – Graph4Me Sep 23 '14 at 15:50
  • @Graph4Me if I understand what you want correctly, then I think the only way is explicitly use Bézier curves which would just require manual tweaking to get the desired curviness. – Mark Wibrow Sep 24 '14 at 6:04
  • @Wibrow ok, I thought there might be another way to do that. Thanks though – Graph4Me Oct 13 '14 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.