如何避免 tikz 中的元素交叉

如何避免 tikz 中的元素交叉

如何才能实现下图中黑点位于填充的前面呢?

在此处输入图片描述

这是 MWE。

\documentclass{article}
\usepackage{tikz}

\begin{document}
        \begin{tikzpicture}
            \tikzstyle{point}=[circle,thick,draw=black,fill=black,inner sep=0pt,minimum width=6pt,minimum height=6pt]
            \node (a)[point,label={[label distance=-.6cm]0:$a$}] at (0,0) {};
            \node (b)[point,label={[label distance=0cm]0:$b$}] at (3,0) {};
            \node (c)[point,label={[label distance=.-.5cm]5:$c$}] at (4,3) {};
            \node (d)[point,label={[label distance=-.5cm]5:$d$}] at (1,2) {};
            \draw [fill = gray](a.center) -- (b.center) -- (d.center) -- cycle;
            \draw (b.center) -- (c.center) -- (d.center) -- cycle;
        \end{tikzpicture}
\end{document}

答案1

按照您希望它们显示的顺序绘制它们。只是因为你定义开始时的坐标并不意味着你必须在那时绘制它们。事实上,它可以使图片代码更简洁,在开始时将所有坐标设置为坐标,然后再进行绘制。这可以更好地控制绘制顺序等事情。

\documentclass{article}
%\url{http://tex.stackexchange.com/q/335249/86}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\coordinate (a) at (0,0);
\coordinate (b) at (3,0);
\coordinate (c) at (4,3);
\coordinate (d) at (1,2);
\draw [fill=gray](a) -- (b) -- (d) -- cycle;
\draw (b) -- (c) -- (d) -- cycle;
\foreach \coord/\pos in {
  a/left,
  b/right,
  c/right,
  d/left%
} {
  \fill (\coord) circle[radius=3pt];
  \node[\pos=1mm] at (\coord) {\(\coord\)};
}
\end{tikzpicture}
\end{document}

结果:

带坐标的四边形

答案2

这是第三个选项,它既不需要重新排序代码,也不需要加载额外的库。

这依赖于黑色圆圈比灰色填充更暗的事实。鉴于此,我们可以添加blend mode=darken添加填充的命令,这样灰色就不会覆盖后面较暗的圆圈。

\documentclass[border=10pt,multi,tikz]{standalone}
\begin{document}
\begin{tikzpicture}
  [
    point/.style={circle,thick,draw=black,fill=black,inner sep=0pt,minimum width=6pt,minimum height=6pt},
  ]
  \node (a) [point, label={[label distance=-.6cm]0:$a$}] at (0,0) {};
  \node (b) [point, label={[label distance=0cm]0:$b$}] at (3,0) {};
  \node (c) [point, label={[label distance=.-.5cm]5:$c$}] at (4,3) {};
  \node (d) [point, label={[label distance=-.5cm]5:$d$}] at (1,2) {};
  \draw [blend mode=darken, fill = gray] (a.center) -- (b.center) -- (d.center) -- cycle;
  \draw (b.center) -- (c.center) -- (d.center) -- cycle;
\end{tikzpicture}
\end{document}

使用<code>混合模式</code>调整表观顺序

答案3

您可以使用背景库来引用点后面的图层。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
        \begin{tikzpicture}
            [ point/.style={circle,thick,draw=black,fill=black,inner sep=0pt,minimum width=6pt,minimum height=6pt}, ]
            \node (a)[point,label={[label distance=-.6cm]0:$a$}] at (0,0) {};
            \node (b)[point,label={[label distance=0cm]0:$b$}] at (3,0) {};
            \node (c)[point,label={[label distance=.-.5cm]5:$c$}] at (4,3) {};
            \node (d)[point,label={[label distance=-.5cm]5:$d$}] at (1,2) {};
\begin{scope}[on background layer]
            \draw [fill = gray](a.center) -- (b.center) -- (d.center) -- cycle;
            \draw (b.center) -- (c.center) -- (d.center) -- cycle;
\end{scope}
        \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容