填充三角形似乎不适用于 PGF/TikZ

填充三角形似乎不适用于 PGF/TikZ

我想知道是否有人知道为什么我的三角形在以下代码中没有用红色填充:

\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}
{
\addtocounter{number}{1}
\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})

答案1

发生这种情况是因为你在边缘center圆节点,这意味着生成的形状不是封闭的,因此无法填充。您可以使用节点s 而不是节点名称来填充三角形:

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

但是,这会导致填充的三角形部分覆盖圆圈。为了避免这种情况,您可以加载库backgrounds并将循环括在\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}

答案2

您需要center明确使用节点的,否则将采用节点的边界,这似乎会导致非封闭路径。

使用:

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

代替:

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

使用\coordinates 代替\nodes 可以避免这种情况。但是,这里您还想将节点绘制为小点,而这无法通过坐标来实现。

相关内容