使用 TikZ 跳过绘制形状中的一个边框

使用 TikZ 跳过绘制形状中的一个边框

我正在用 TikZ 绘制形状,并且我想有选择地绘制它周围的边框:

\documentclass{standalone}

\usepackage{tikz}

\begin{document}


\begin{tikzpicture}[scale=3., line width=1pt]

\coordinate (a) at (0,0);
\coordinate (b1) at (1,1);
\coordinate (c1) at (2,0);
\coordinate (b2a) at (0.7,-1);
\coordinate (b2b) at (0.9,-1.2);
\coordinate (b2c) at (1.1,-1.2);
\coordinate (b2d) at (1.3,-1);

\draw[fill=green!10!white] (a) .. controls +(0,0.5) and +(-0.5,0) ..
      (b1) .. controls +(0.5,0) and +(0.,0.5) ..
      (c1) .. controls +(0.,-0.5) and +(0.5,0.2) ..
      (b2d) .. controls +(-0.125,-0.05) and +(-0.,0.05) ..
      (b2c)
      -- % I want to skip this line!
      (b2b) .. controls +(0.,0.05) and +(0.125,-0.05) ..
      (b2a) .. controls +(-0.5,0.2) and +(0,-0.5) .. (a);

\end{tikzpicture}

\end{document}

在此处输入图片描述

但我想获得:

在此处输入图片描述

对于底部图形,我绘制了三次:一次仅填充(draw=none),然后是没有填充的一半边框(在坐标处停止(b2c)),然后是另一半(从坐标开始(b2b))。我希望能够“连接”(b2c)(b2b)而不需要在它们之间画线。也就是说,我只想绘制一次,并以某种方式告诉 tikz 跳过这些特定坐标之间的线条绘制。我该如何实现?

编辑:

这只是对我的实际图形的简化,以说明我的问题。我通常想跳过属于某个形状的任意数量的边框。

答案1

这是一个新decoration功能,您可以用它选择不绘制的段。

要填充封闭路径并绘制除线段 2 和 5 之外的所有线段,语法为:

\path[fill,apply={draw} except on segments {2,5}] ... closed path ... ;

在此处输入图片描述

\documentclass{standalone}
\usepackage{etoolbox}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\makeatletter
\newcounter{aaa}
\tikzset{
  apply/.style args={#1 except on segments #2}{postaction={
      /utils/exec={
        \@for\mattempa:=#2\do{\csdef{aaa@\mattempa}{}}
        \setcounter{aaa}{0}
      },
      decorate,decoration={show path construction,
        moveto code={},
        lineto code={
          \stepcounter{aaa}
          \ifcsdef{aaa@\theaaa}{}{
            \path[#1] (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
          }
        },
        curveto code={
          \stepcounter{aaa}
          \ifcsdef{aaa@\theaaa}{}{
            \path [#1] (\tikzinputsegmentfirst) .. controls
            (\tikzinputsegmentsupporta) and (\tikzinputsegmentsupportb)
            ..(\tikzinputsegmentlast);
          }
        },
        closepath code={
          \stepcounter{aaa}
          \ifcsdef{aaa@\theaaa}{}{
            \path [#1] (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
          }
        },
      },
    },
  },
}
\makeatother
\begin{document}


\begin{tikzpicture}[scale=3., line width=1pt]

\coordinate (a) at (0,0);
\coordinate (b1) at (1,1);
\coordinate (c1) at (2,0);
\coordinate (b2a) at (0.7,-1);
\coordinate (b2b) at (0.9,-1.2);
\coordinate (b2c) at (1.1,-1.2);
\coordinate (b2d) at (1.3,-1);

\path[fill=green!10!white,apply={draw=red} except on segments {5}]
  (a) .. controls +(0,0.5) and +(-0.5,0) ..
  (b1) .. controls +(0.5,0) and +(0.,0.5) ..
  (c1) .. controls +(0.,-0.5) and +(0.5,0.2) ..
  (b2d) .. controls +(-0.125,-0.05) and +(-0.,0.05) ..
  (b2c)
  -- % I want to skip this line!
  (b2b) .. controls +(0.,0.05) and +(0.125,-0.05) ..
  (b2a) .. controls +(-0.5,0.2) and +(0,-0.5) .. (a);

\end{tikzpicture}

\end{document}

答案2

您可以简单地从开始b2b而不是a

\draw[fill=green!10!white]
      (b2b) .. controls +(0.,0.05) and +(0.125,-0.05) ..
      (b2a) .. controls +(-0.5,0.2) and +(0,-0.5) ..
      (a) .. controls +(0,0.5) and +(-0.5,0) ..
      (b1) .. controls +(0.5,0) and +(0.,0.5) ..
      (c1) .. controls +(0.,-0.5) and +(0.5,0.2) ..
      (b2d) .. controls +(-0.125,-0.05) and +(-0.,0.05) ..
      (b2c);

由于填充的工作方式,省略带有注释的行对轮廓有效,但对阴影区域无效。

一般来说,只需使用移动到操作就可以绘制“间歇”路径:

\draw (a) -- (b) -- (c) (d) -- (e) -- (f);

(c)和(d)之间不会划出任何界线。

当你填充这样的中断路径时

\fill (a) -- (b) -- (c) (d) -- (e) -- (f);

你说的是“填充三角形 abc”,然后“填充三角形 def”。

因此,为了获得预期的结果,您需要

\draw (a) -- (b) -- (c) (d) -- (e) -- (f);
\fill (a) -- (b) -- (c) -- (d) -- (e) -- (f);

相关内容