使用 TikZ 剪切路径

使用 TikZ 剪切路径

例如,我知道如何用矩形进行裁剪。但是如何使用不同的路径进行裁剪。我用贝塞尔曲线写了很多行(.. 控件 (,) 和 (,) ..)。所以现在我有了一条包含所有这些贝塞尔曲线的路径。那么,我如何在此路径内进行裁剪?这是一个 MWE:

\begin{tikzpicture}[line width=0.8pt]
  \draw (0.1,4.95) .. controls (0.6,4.5) and (1.8,4.15) .. (1.8,3.65);
  \draw (1.8,3.65) .. controls (1.8,2.75) and (1.9,3.15) .. (1.9,2.55);
  \draw (1.9,2.55) .. controls (1.9,2.35) and (2.25,2.15) .. (2.25,1.65);
  \draw (2.25,1.65) .. controls (2.25,1.4) and (1.65,0.8) .. (1.65,0.75);
  \draw (1.65,0.75) .. controls (1.65,0.6) and (1.8,0.54) .. (1.9,0.5);
  \draw (1.9,0.5) .. controls (2,0.44) and (2.5,0.6) .. (2.7,1.05);
  \draw (2.7,1.05) .. controls (3,1.45) and (3,1.5) .. (3,1.8);
  \draw (3,1.8) .. controls (3.05,2.1) and (3.1,2.1) .. (3.4,2);
  \draw (3.4,2) .. controls (3.6,2) and (3.6,1.95) .. (3.8,1.95);
  \draw (3.8,1.95) .. controls (3.85,1.95) and (4.1,1.85) .. (4.2,1.85);
  \draw (4.2,1.85) .. controls (4.4,1.85) and (4.5,1.95) .. (5,2);
  \draw (5,2) .. controls (5.4,1.95) and (5.5,1.95) .. (5.8,1.96);
  \draw (5.8,1.96) .. controls (6.55,1.92) and (6.7,1.94) .. (6.7,2.2);
  \draw (6.7,2.2) .. controls (6.7,2.45) and (6.1,2.5) .. (5.9,2.43);
  \draw (5.9,2.43) .. controls (5.8,2.47) and (5.2,2.55) .. (5.1,2.5);
  \draw (5.1,2.5) .. controls (5.0,2.55) and (5,2.57) .. (4.9,2.6);
  \draw (4.9,2.6) .. controls (4.95,3.0) and (4.95,3.0) .. (4.9,3.6);
  \draw (4.9,3.6) .. controls (4.93,4.0) and (4.9,4.0) .. (4.85,4.5);
  \draw (4.85,4.5) .. controls (4.83,4.8) and (4.8,4.6) .. (4.78,5);
  \draw (4.78,5) .. controls (4.76,5.2) and (4.8,5.0) .. (4.75,5.35);
  \draw (4.75,5.35) .. controls (4.35,5.57) and (4.23,5.0) .. (4.33,4.5);
  \draw (4.33,4.5) .. controls (4.3,4.38) and (4.31,4.4) .. (4.3,4.18);
  \draw (4.3,4.18) -- (4.25,4.16);
  \draw (4.25,4.16) .. controls (4,4.5) and (3.4,4.7) .. (3,4.75);
  \draw (3,4.75) .. controls (2.8,4.8) and (2.65,5.03) .. (2.6,5.05);
  \draw (2.6,5.05) .. controls (2.4,5.15) and (1.75,5.8) .. (1.3,6.25);
\end{tikzpicture}

我无法关闭路径,因为我不想在第一个点和最后一个点之间有一条线(或一条看不见的线)

答案1

我认为你需要关闭这条路径:

\documentclass[tikz, border=2mm]{standalone}

\begin{document}
\begin{tikzpicture}
\draw[clip] (0,0)..controls (1,1).. (4,0)..controls (3,-1)..cycle;
\fill[red] (-1,-1) rectangle (2,1);
\fill[green] (2,1) rectangle (4,-1);
\end{tikzpicture}
\end{document}

在此处输入图片描述

更新:

OP 的提议显示了多条贝塞尔线,这些线在新的线开始的地方结束。这样更容易将它们构建为一条路径。可以关闭此路径以划定区域clip,并且可以在没有关闭线的情况下再次绘制。

\documentclass[tikz,border=2mm]{standalone}

\newcommand{\myline}{%
(0.1,4.95) .. controls (0.6,4.5) and (1.8,4.15) .. (1.8,3.65)%;
%  \draw (1.8,3.65) 
  .. controls (1.8,2.75) and (1.9,3.15) .. (1.9,2.55)%;
%  \draw (1.9,2.55) 
  .. controls (1.9,2.35) and (2.25,2.15) .. (2.25,1.65)%;
%  \draw (2.25,1.65) 
  .. controls (2.25,1.4) and (1.65,0.8) .. (1.65,0.75)%;
%  \draw (1.65,0.75) 
  .. controls (1.65,0.6) and (1.8,0.54) .. (1.9,0.5)%;
%  \draw (1.9,0.5) 
  .. controls (2,0.44) and (2.5,0.6) .. (2.7,1.05)%;
%  \draw (2.7,1.05) 
  .. controls (3,1.45) and (3,1.5) .. (3,1.8)%;
%  \draw (3,1.8) 
  .. controls (3.05,2.1) and (3.1,2.1) .. (3.4,2)%;
%  \draw (3.4,2) 
  .. controls (3.6,2) and (3.6,1.95) .. (3.8,1.95)%;
%  \draw (3.8,1.95) 
  .. controls (3.85,1.95) and (4.1,1.85) .. (4.2,1.85)%;
%  \draw (4.2,1.85) 
  .. controls (4.4,1.85) and (4.5,1.95) .. (5,2)%;
%  \draw (5,2) 
  .. controls (5.4,1.95) and (5.5,1.95) .. (5.8,1.96)%;
%  \draw (5.8,1.96) 
  .. controls (6.55,1.92) and (6.7,1.94) .. (6.7,2.2)%;
%  \draw (6.7,2.2) 
  .. controls (6.7,2.45) and (6.1,2.5) .. (5.9,2.43)%;
%  \draw (5.9,2.43) 
  .. controls (5.8,2.47) and (5.2,2.55) .. (5.1,2.5)%;
%  \draw (5.1,2.5) 
  .. controls (5.0,2.55) and (5,2.57) .. (4.9,2.6)%;
%  \draw (4.9,2.6) 
  .. controls (4.95,3.0) and (4.95,3.0) .. (4.9,3.6)%;
%  \draw (4.9,3.6) 
  .. controls (4.93,4.0) and (4.9,4.0) .. (4.85,4.5)%;
%  \draw (4.85,4.5) 
  .. controls (4.83,4.8) and (4.8,4.6) .. (4.78,5)%;
%  \draw (4.78,5) 
  .. controls (4.76,5.2) and (4.8,5.0) .. (4.75,5.35)%;
%  \draw (4.75,5.35) 
  .. controls (4.35,5.57) and (4.23,5.0) .. (4.33,4.5)%;
%  \draw (4.33,4.5) 
  .. controls (4.3,4.38) and (4.31,4.4) .. (4.3,4.18)%;
%  \draw (4.3,4.18) 
  -- (4.25,4.16)%;
%  \draw (4.25,4.16) 
  .. controls (4,4.5) and (3.4,4.7) .. (3,4.75)%;
%  \draw (3,4.75) 
  .. controls (2.8,4.8) and (2.65,5.03) .. (2.6,5.05)%;
%  \draw (2.6,5.05) 
  .. controls (2.4,5.15) and (1.75,5.8) .. (1.3,6.25)}
\begin{document}

\begin{tikzpicture}[line width=0.8pt]
  \path[clip] \myline--cycle;
  \fill[green!90!black] (0,0) rectangle (7,7);
  \draw \myline;
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

您可以剪辑一条略有不同的路径(请注意末尾-| cycle),在该剪辑上绘制一些内容,然后在剪辑外面绘制未封闭的路径。

(或者,可以使用路径图片做类似的事情,该路径图片基本上是可以绘制剪切路径的单路径剪辑。但这对您没有太大帮助,因为您的剪切路径和绘制路径略有不同。)

代码

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[
  line width=0.8pt,
  hand/.style={insert path={
    (0.1,4.95) .. controls (0.6,4.5) and (1.8,4.15) .. (1.8,3.65)
               .. controls (1.8,2.75) and (1.9,3.15) .. (1.9,2.55)
               .. controls (1.9,2.35) and (2.25,2.15) .. (2.25,1.65)
               .. controls (2.25,1.4) and (1.65,0.8) .. (1.65,0.75)
               .. controls (1.65,0.6) and (1.8,0.54) .. (1.9,0.5)
               .. controls (2,0.44) and (2.5,0.6) .. (2.7,1.05)
               .. controls (3,1.45) and (3,1.5) .. (3,1.8)
               .. controls (3.05,2.1) and (3.1,2.1) .. (3.4,2)
               .. controls (3.6,2) and (3.6,1.95) .. (3.8,1.95)
               .. controls (3.85,1.95) and (4.1,1.85) .. (4.2,1.85)
               .. controls (4.4,1.85) and (4.5,1.95) .. (5,2)
               .. controls (5.4,1.95) and (5.5,1.95) .. (5.8,1.96)
               .. controls (6.55,1.92) and (6.7,1.94) .. (6.7,2.2)
               .. controls (6.7,2.45) and (6.1,2.5) .. (5.9,2.43)
               .. controls (5.8,2.47) and (5.2,2.55) .. (5.1,2.5)
               .. controls (5.0,2.55) and (5,2.57) .. (4.9,2.6)
               .. controls (4.95,3.0) and (4.95,3.0) .. (4.9,3.6)
               .. controls (4.93,4.0) and (4.9,4.0) .. (4.85,4.5)
               .. controls (4.83,4.8) and (4.8,4.6) .. (4.78,5)
               .. controls (4.76,5.2) and (4.8,5.0) .. (4.75,5.35)
               .. controls (4.35,5.57) and (4.23,5.0) .. (4.33,4.5)
               .. controls (4.3,4.38) and (4.31,4.4) .. (4.3,4.18)
               -- (4.25,4.16)
               .. controls (4,4.5) and (3.4,4.7) .. (3,4.75)
               .. controls (2.8,4.8) and (2.65,5.03) .. (2.6,5.05)
               .. controls (2.4,5.15) and (1.75,5.8) .. (1.3,6.25)}}]
\begin{scope}
  \clip[hand] -| cycle;
  \shade[left color=red, right color=green]
              (current bounding box.south west)
    rectangle (current bounding box.north east);
\end{scope}
\draw[hand];

\tikzset{xshift=7cm}
\path[hand] -| cycle [path picture={
  \shade[left color=red, right color=green]
              (path picture bounding box.south west)
    rectangle (path picture bounding box.north east);
}];
\draw[hand];
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

相关内容