3

I want to draw a convex hull with tikz.

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{center}
\begin{tikzpicture}

\def\us{(0,2),(1,4),(2,0),(3,2),(4,1),(6,2),(6,4)}

\foreach \u [count=\i from 1] in \us {\node (u\i) at \u [right] {$u_\i$};}

\fill[fill=blue] (u1) -- (u2) -- (u3);
% \fill[red] (0,2) -- (2,0) -- (3,2);

\end{tikzpicture}
\end{center}

\end{document}

If I'm right, this should draw a filled blue triangle. But it doesn't! Why? If instead, I comment out the \fill[red] line, I do see a result.

Thank you, Adrian.

2 Answers 2

3

This is because TikZ automatically uses the most appropriate anchor to connect two nodes, so you do not actually have a triangle, you have two edges.

Try

\fill[fill=blue] (u1.center) -- (u2.center) -- (u3.center) ;

instead, it will show the difference.

Other ways are to use

\def\us{0/2,1/4,2/0,3/2,4/1,6/2,6/4}
\foreach \x/\y [count=\i from 1] in \us {
   \coordinate[label=right:$u_\i$] (u\i) at (\x,\y);
} 

instead, as coordinates are automatically connected using their "center" anchor.

Finally, a more sophisticated way might be this one:

\def\us{(0,2)/left,(1,4)/right,(2,0)/right,
   (3,2)/above,(4,1)/above,(6,2)/below,(6,4)/below}

\foreach \u/\pos [count=\i from 1] in \us {
   \node[shape=coordinate,label=\pos:$u_\i$] (u\i) at \u {};
}

This gives you the possibility to set the label position individually for each coordinate so that they are not interfering with connecting lines etc.

2
  • Thanks. It works. But it's still a mystery to me. With anchor, you mean something like "east", etc.? Also, what is wrong, if instead of nodes I used coordinates: \foreach \u [count=\i from 1] in \us {\coordinate (u\i) at \u;} \fill[fill=blue] (u1) -- (u2) -- (u3);
    – Adiji
    Jul 31, 2015 at 14:26
  • Yes, anchors are the "east", "center" etc. attachment points for connecting lines. I don't quite get your question, but you use \node[shape=coordinate,label=right:$u_\i$] (u\i) at \u {}; instead, if you want to define coordinates and still keep the labels. (I actually do not see why your variant does not work.) PS: You might to want to use backticks to have your inline code typeset as such. Jul 31, 2015 at 14:37
2

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}    
\begin{tikzpicture}

\def\us{(0,2),(1,4),(2,0),(3,2),(4,1),(6,2),(6,4)}
\foreach \u [count=\i from 1] in \us {\node (u\i) at \u [right] {$u_\i$};}

\begin{scope}[on background layer]
\fill[blue!20] (u1.center)--(u2.center)--(u3.center);
\end{scope}

%% For Test
\draw (u4)--(u5)--(u6)--cycle;

\end{tikzpicture}

\end{document}

I added a test using nodes u4, u5, u6 to see why the fill didn't work. As you see, you have segments of lines rather than a triangle, a segment of line is an open object which cannot be filled.

Also note that node labels will hide under the filled triangle, so I used the fill on the background layer to leave the labels on top.

3
  • 1
    TeXSE community is amazingly quick! Thanks for your answer as well. Can you explain why there is no edge from (u6) to (u4)?
    – Adiji
    Jul 31, 2015 at 14:30
  • @user52963 Because you must say (u6)--(u4) explicitly since the previous edges are not actually connected they cannot be closed as a consequence.
    – AboAmmar
    Jul 31, 2015 at 14:37
  • Is it possible to configure a scope so that .center anchors are automatically used?
    – user87690
    Mar 23, 2016 at 9:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .