# Filling triangle doesn't seem to work with PGF/TikZ

I was wondering if anyone had any clue on why my triangles are not filled in red within the following piece of code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,3d}

\usepackage[active, pdftex, tightpage]{preview}
\PreviewEnvironment[{[]}]{tikzpicture}
\newcounter{number}

\begin{document}
\begin{tikzpicture}[x  = {(-0.5cm,-0.5cm)},
y  = {(0.9659cm,-0.25882cm)},
z  = {(0cm,1cm)}]
\begin{scope}[canvas is yx plane at z=0]
\foreach \x / \y / \label in
{1 / 0.4 / 1,
0.9 / 0.4 / 0,
1 / 0.5 / 1,
0.7 / 0.3 / 0,
0.8 / 0.4 / 0,
0.9 / 0.5 / 0,
1 / 0.6 / 1,
0.7 / 0.4 / 0,
0.4 / 0.4 / 0,
0.6 / 0.4 / 0,
0.5 / 0.4 / 0,
0.8 / 0.5 / 0,
0.9 / 0.6 / 0,
1 / 0.7 / 1,
0.7 / 0.5 / 0,
0.5 / 0.5 / 0,
0.4 / 0.5 / 0,
0.6 / 0.5 / 0,
0.8 / 0.6 / 0,
0.9 / 0.7 / 0,
1 / 0.8 / 1,
0.7 / 0.6 / 0,
0.6 / 0.6 / 0,
0.5 / 0.6 / 0}
{
\pgfmathparse{\value{number}}\let\k\pgfmathresult
\draw (8 * \x, 8 * \y) node[inner sep=0, outer sep = -0.1pt,color=black,fill,circle] (node-\k) {};
};

\foreach \x / \y / \z / \trash in
{
4/5/8/0,
9/11/16/0,
9/16/17/0,
11/10/18/0,
11/18/16/0,
10/8/15/0,
10/15/18/0,
8/5/12/0,
8/12/15/0,
5/2/6/0,
5/6/12/0,
2/1/3/0,
2/3/6/0,
17/16/24/0,
16/18/23/0,
16/23/24/0,
18/15/22/0,
18/22/23/0,
15/12/19/0,
15/19/22/0,
12/6/13/0,
12/13/19/0,
6/3/7/0}
{
\draw[fill=red]  (node-\x) --  (node-\y) --  (node-\z) -- cycle;
}
\end{scope}
\end{tikzpicture}
\end{document})

-
Welcome to TeX.sx! Please indent code block with 4 spaces (not 8) or use CTRL+K to do this automatically. Please no signatures or gratitude lines at the end. –  Martin Scharrer Oct 6 '11 at 22:29
I executed your code, it seems you want to draw some strips of red triangles. Wouldn't there be much less complicated ways than this, e.g. nesting two \foreach loops? I mean I'm really puzzled why you want to draw some triangles this way? –  Tom Bombadil Oct 6 '11 at 22:46
First of all, those are not nested loops. Then, do you have something in mind more efficient to draw a bunch of triangles not knowing apriori the position of each vertices and the number of triangles ? –  BenDover Oct 7 '11 at 7:31
@BenDover You have lists of coordinates, so I assume you know things apriori, or are these list dynamically generated? Regarding the foreach: I just saw that your code produced more or less three stripes of similar triangles, and that would be a task for such a loop. –  Tom Bombadil Oct 8 '11 at 1:50

This happens because you're drawing your lines between the edges of your circle nodes, which means that the resulting shapes are not closed and thus cannot be filled. You can fill your triangles by using the node centers instead of just the node names:

\draw [fill=red]  (node-\x.center) --  (node-\y.center) --  (node-\z.center) -- cycle;


This will, however, lead to the filled triangles partially overpainting the circles. To avoid this, you can load the backgrounds library and enclose your loop in \begin{pgfonlayer}{background} ... \end{pgfonlayer}:

\begin{pgfonlayer}{background}
\foreach \x / \y / \z / \trash in
{
4/5/8/0,
9/11/16/0,
9/16/17/0,
11/10/18/0,
11/18/16/0,
10/8/15/0,
10/15/18/0,
8/5/12/0,
8/12/15/0,
5/2/6/0,
5/6/12/0,
2/1/3/0,
2/3/6/0,
17/16/24/0,
16/18/23/0,
16/23/24/0,
18/15/22/0,
18/22/23/0,
15/12/19/0,
15/19/22/0,
12/6/13/0,
12/13/19/0,
6/3/7/0}
{
\draw [fill=red]  (node-\x.center) --  (node-\y.center) --  (node-\z.center) -- cycle;
}
\end{pgfonlayer}

-

You need to use the center of the node explicitly, otherwise the boundary of the node is taken and this seems to cause an non-closed paths.

Use:

\draw[fill=red]  (node-\x.center) --  (node-\y.center) --  (node-\z.center) -- cycle;


\draw[fill=red]  (node-\x) --  (node-\y) --  (node-\z) -- cycle;

This can be avoided when you use \coordinates instead of \nodes. However, here you also want to draw the nodes as little dots, which can't be done with coordinates.