如何剪切双路径内的形状

如何剪切双路径内的形状

我需要剪切圆圈,以便只有路径内的圆圈部分可见(我的输出是相反的)。

这是我试过的代码。我做错了什么?

\documentclass{book}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \draw [fill=red](0.05,5.09) circle(8pt) node {};
  \path [
    preaction={draw,double=black!10, double distance=45pt,color=white},
    clip,
    scale=1.8
  ] plot [smooth] coordinates {
    (0.53,4.31)(0.46,4.16)(0.39,4.01)(0.32,3.82)
    (0.27,3.66)(0.23,3.45)(0.23,3.29)(0.31,3.14)
    (0.47,3.01)(0.63,2.93)(0.79,2.88)(1.02,2.86)
    (1.24,2.87)(1.5,2.94)(1.72,3.02)(1.86,3.08)
    (2.01,3.14)(2.18,3.23)(2.34,3.32)(2.49,3.39)
    (2.65,3.47)(2.83,3.58)(3.05,3.7)(3.24,3.81)
    (3.46,3.93)(3.66,4.03)(3.89,4.13)(4.1,4.22)
    (4.32,4.32)(4.53,4.36)(4.75,4.39)(4.97,4.4)
    (5.19,4.36)(5.36,4.3)(5.54,4.19)(5.7,4.02)
    (5.79,3.81)(5.85,3.58)(5.83,3.4)(5.73,3.21)
    (5.58,2.89)(5.45,2.71)(5.31,2.57)(5.16,2.44)
    (4.99,2.33)(4.84,2.26)(4.62,2.16)(4.33,2.08)(4,2)
  };
  \draw [fill=blue](-0.55,3.09) circle(8pt) node {};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

以下示例计算了外部形状的点,以便进行适当的裁剪。为了便于说明,显示了中间点和计算出的点:

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
  % \DoubleDistance takes the distance of the double line of the question
  \def\DoubleDistance{45pt}

  % \DoubleDistance is converted to a radius and stored in \dsRadius
  \pgfmathsetlengthmacro\dsRadius{\DoubleDistance/2}

  % Macro \dsPrev returns the predecessor of the number given in #1
  \newcommand*{\dsPrev}[1]{\the\numexpr(#1)-1\relax}

  % Point definitions
  \path
    % Middle points get names: (dsM\i) with \i as number from 1, ..., \dsN
    \foreach [
      var=\point,
      count=\i,
    ] in {%
      (0.53,4.31), (0.46,4.16), (0.39,4.01), (0.32,3.82),
      (0.27,3.66), (0.23,3.45), (0.23,3.29), (0.31,3.14),
      (0.47,3.01), (0.63,2.93), (0.79,2.88), (1.02,2.86),
      (1.24,2.87), (1.5,2.94), (1.72,3.02),  (1.86,3.08),
      (2.01,3.14), (2.18,3.23), (2.34,3.32), (2.49,3.39),
      (2.65,3.47), (2.83,3.58), (3.05,3.7),  (3.24,3.81),
      (3.46,3.93), (3.66,4.03), (3.89,4.13), (4.1,4.22),
      (4.32,4.32), (4.53,4.36), (4.75,4.39), (4.97,4.4),
      (5.19,4.36), (5.36,4.3),  (5.54,4.19), (5.7,4.02),
      (5.79,3.81), (5.85,3.58), (5.83,3.4),  (5.73,3.21),
      (5.58,2.89), (5.45,2.71), (5.31,2.57), (5.16,2.44),
      (4.99,2.33), (4.84,2.26), (4.62,2.16), (4.33,2.08), (4,2)%
    } {
      \point coordinate (dsM\i)
    }
    % Remember number of points in \dsN
    \pgfextra{\global\let\dsN\i}%
    %
    % Define points of the end lines
    ($(dsM1)!\dsRadius!-90:(dsM2)$) coordinate (dsM1r)
    ($(dsM1)!\dsRadius!90:(dsM2)$) coordinate (dsM1l)
    ($(dsM\dsN)!\dsRadius!90:(dsM\dsPrev\dsN)$) coordinate (dsM\dsN r)
    ($(dsM\dsN)!\dsRadius!-90:(dsM\dsPrev\dsN)$) coordinate (dsM\dsN l)
    % Define other points of the outer shape
    \foreach \i in {3, ..., \dsN} {
      let \n2 = {\i},
          \n1 = {\dsPrev\i},
          \n0 = {\dsPrev{\dsPrev\i}},
          \p0 = (dsM\n0),
          \p1 = (dsM\n1),
          \p2 = (dsM\n2),
          \n{a} = {(atan2(\y2-\y1, \x2-\x1) - atan2(\y1-\y0, \x1-\x0))/2}
      in
        ($(\p1)!\dsRadius!-90-\n{a}:(\p2)$) coordinate (dsM\n1r)
        ($(\p1)!\dsRadius!90-\n{a}:(\p2)$) coordinate (dsM\n1l)
    }
  ;

  % Illustrate
  \filldraw[
    fill=black!10,
    draw=black,
    very thin,
    variable=\i,
    smooth,
    mark=*,
    mark size=.5pt,
  ]
    % mark the middle points
    plot[
      samples at={1, ..., \dsN},
      only marks,
      mark options=black,
    ] (dsM\i)
    % right points
    plot[
      samples at={1, ..., \dsN},
      mark options=red,
    ] (dsM\i r)
    --
    % left points
    plot[
      samples at={\dsN, \dsPrev\dsN, ..., 1},
      mark options=blue,
    ] (dsM\i l)
    -- cycle
  ;

  % clip path
  \clip[variable=\i, smooth]
    plot[samples at={1, ..., \dsN}] (dsM\i r)
    -- plot[samples at={\dsN, \dsPrev\dsN, ..., 1}] (dsM\i l)
    -- cycle
  ;

  % clipped large red point
  \draw [fill=red](0,2.5) circle(8pt) node {};

\end{tikzpicture}
\end{document}

结果

答案2

这很粗糙,但很有效:我喜欢 percusse 的评论和 Heiko Oberdiek 的代码,它们对我有用。我没有投票的声誉。请接受答案,它满足我的问题标准。

\documentclass{book}
\usepackage{tikz}
\usepackage[english]{babel}
\begin{document}
\begin{tikzpicture}

\clip plot[smooth cycle,clip]coordinates{
(0.91,7.04)(0.51,5.4)(1.57,4.6)(3.12,4.84)(6.36,6.39)(7.4,5.79)(6.87,4.84)(5.43,4.44)(3.15,4.24)(1.37,3.67)(0.4,2.38)(0.51,1)(1.9,0.27)(3.37,0.69)(6.23,2.07)(7.12,1.98)(6.98,1.31)(7.98,0.98)(8.09,2.22)(7.29,3.04)(6.14,3.13)(2.97,1.62)(1.31,1.69)(2.01,2.82)(5.83,3.44)(7.14,3.8)(8.38,5.15)(7.8,7.06)(5.87,7.33)(2.46,5.71)(1.57,5.71)(1.75,6.59)}; 
\draw [color=black!10, fill=black!10] plot[smooth cycle]coordinates{ 
(0.91,7.04)(0.51,5.4)(1.57,4.6)(3.12,4.84)(6.36,6.39)(7.4,5.79)(6.87,4.84)(5.43,4.44)(3.15,4.24)(1.37,3.67)(0.4,2.38)(0.51,1)(1.9,0.27)(3.37,0.69)(6.23,2.07)(7.12,1.98)(6.98,1.31)(7.98,0.98)(8.09,2.22)(7.29,3.04)(6.14,3.13)(2.97,1.62)(1.31,1.69)(2.01,2.82)(5.83,3.44)(7.14,3.8)(8.38,5.15)(7.8,7.06)(5.87,7.33)(2.46,5.71)(1.57,5.71)(1.75,6.59)};

\draw [fill=blue](0.75,3.09) circle(8pt);
\draw [fill=red](0.6,5.6) circle(8pt);
\end{tikzpicture}

\end{document} 

答案3

下面显示的黄色部分是您要剪切的部分,预处理与剪切无关,因为它是路径的表示方式。剪切使用内部区域,因此您的蓝点不在,但红点在剪切操作之前出现,因此保留了下来,但被预处理覆盖了。

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw [fill=red](0.05,5.09) circle(8pt);
\begin{scope}[]
\path [preaction={fill=yellow},clip,scale=1.8] plot [smooth,clip] coordinates
{
(0.53,4.31)(0.46,4.16)(0.39,4.01)(0.32,3.82)(0.27,3.66)(0.23,3.45)(0.23,3.29)(0.31,3.14)(0.47,3.01)(0.63,2.93)(0.79,2.88)(1.02,2.86)(1.24,2.87)(1.5,2.94)(1.72,3.02)(1.86,3.08)(2.01,3.14)(2.18,3.23)(2.34,3.32)(2.49,3.39)(2.65,3.47)(2.83,3.58)(3.05,3.7)(3.24,3.81)(3.46,3.93)(3.66,4.03)(3.89,4.13)(4.1,4.22)(4.32,4.32)(4.53,4.36)(4.75,4.39)(4.97,4.4)(5.19,4.36)(5.36,4.3)(5.54,4.19)(5.7,4.02)(5.79,3.81)(5.85,3.58)(5.83,3.4)(5.73,3.21)(5.58,2.89)(5.45,2.71)(5.31,2.57)(5.16,2.44)(4.99,2.33)(4.84,2.26)(4.62,2.16)(4.33,2.08)(4,2)
};
\end{scope}
\draw [fill=blue](-0.55,3.09) circle(8pt); %Outside yellow region gets clipped
\end{tikzpicture}
\end{document}

在此处输入图片描述

如果您希望剪辑,那么您需要通过圆弧和角明确构建封闭的 S 曲线。

相关内容