There are various techniques to change the position of the \draw
lines:
- specify the start and end position in degrees on the node
- use
yshift
on the start and end node
- use the
calc
tikzlibrary to add an offset to the start and/or end positions
The first method is to specify the nodes as (nodename.degrees)
instead of just (nodename)
, so instead of (s5)
you use (s5.45)
for example. In that case you don't start the line at the point closest to the opposing node, but at the position 45 degrees counterclockwise from the start at 0 degrees which is on the right of the node.
The second method specifies a distance to shift the line node in the y position, with the syntax ([yshift=xx unit]nodename)
, in this case for example ([yshift=-25mm]s6)
. This only works if you shift the opposing node with exactly the same amount, so it is not really flexible.
The third method uses the calc
tikzlibrary to add a coordinate to the node, with the syntax ($(nodename)+(x,y)$)
. This looks a bit complex but it is the most flexible way. Note that this offsets the shorten =
attribute, so you need to add that back by adding or subtracting a bit from the x coordinate.
The MWE below shows all three methods. Additionally, I simplified the aming of the (f)
nodes by putting the subscript in the node label instead of in the node names. For the arrows that go both ways from and to a node I simplified by drawing with [<->]
. This also looks a bit better in terms of overlap.
Coloring an edge is done by putting the color name as an option to the draw command, for example \draw[red]
.
MWE:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{chains,fit,shapes}
\usetikzlibrary{calc}
\begin{document}
\definecolor{myblue}{RGB}{80,80,160}
\definecolor{mygreen}{RGB}{80,160,80}
\begin{tikzpicture}[thick,
every node/.style={draw,circle},
ssnode/.style={fill=myblue},
fsnode/.style={fill=mygreen},
every fit/.style={ellipse,draw,inner sep=-1pt,text width=1.75cm},
->,shorten >= 2pt,shorten <= 2pt
]
% the vertices of Slots
\begin{scope}[start chain=going below,node distance=1.5mm]
\foreach \i in {1,2,...,8}
\node[ssnode,on chain] (s\i) [label=left: \i] {};
\end{scope}
% the vertices of Flights
\begin{scope}[xshift=4cm,yshift=-0.5cm,start chain=going below,node distance=1.5mm]
\foreach \i in {2,5,6,7,8}
\node[fsnode,on chain] (f\i) [label=right: $f_{\i}$] {};
\end{scope}
% the set Slots
\node [myblue,fit=(s1) (s8),label=above:$Slots$] {};
% the set Flights
\node [mygreen,fit=(f2) (f8),label=above:$Flights$] {};
% the edges
\draw[red] (s2) -- (f2);
\draw (s5.45) -- (f5.220);
\draw ([yshift=-25mm]s6) -- ([yshift=-25mm]f6);
\draw (s7) -- (f7);
\draw (s8) -- (f8);
\draw[<->] ($(f2)+(-0.15,0.15)$) -- (s1);
\draw[<->] (f5) -- (s3);
\draw (f6) -- (s5);
\draw (f7) -- (s5);
\draw (f8) -- (s7);
\end{tikzpicture}
\end{document}
Result:
