TikZ:将平滑的循环划分为多个区域

TikZ:将平滑的循环划分为多个区域

我想制作一条封闭的平滑路径,然后将其分成几个子部分,每个子部分都有不同的颜色。目前,我能想到的最好的办法是将另一条封闭路径放在里面,并使用不同的背景颜色。

\begin{tikzpicture}
 \draw[ultra thick,color=black,fill=yellow]
  plot[smooth cycle]
  coordinates{
   (0,2) (2.2,2.2) (3.1,3.3) (6,3) (6,0) (0,0)
  };
 \draw[thick, draw=gray,fill=red]
  plot[smooth cycle] coordinates{(2,1.8) (3,2.3) (5,2.3) (5,1) (2,1)};
\end{tikzpicture}

但我真正想要做的是能够绘制线条将图形分隔成这些分区,然后更改每个分区中的背景。例如,使用如下线条:

事物

答案1

另一个示例是使用具有两个“任意”路径的多个剪切区域(有一些条件:两个路径应该分别与当前边界框的左/右和底部/顶部边界相交)。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
  \begin{scope}
    \draw[clip, preaction={draw, ultra thick, double distance=0pt}]
    plot[smooth cycle]
    coordinates{
      (0,2) (2.2,2.2) (3.1,3.3) (6,3) (6,0) (0,0)
    };
    % first path (a)
    \path[name path=a] plot[domain=-1:7,samples=100](\x,{.5*sin(\x*3 r)+1});
    % second path (b)
    \path[name path=b] plot[domain=-1:4,samples=100]({.5*sin(\x*4 r)+2},\x);
    % current bounding box (boundaries)
    \path[name path=boundaries]
    (current bounding box.south west)
    rectangle
    (current bounding box.north east);
    % two intersestion for each path (ai-1,ai-2) and (bi-1,bi-2)
    \path[name intersections={of=a and boundaries,name=ai}];
    \path[name intersections={of=b and boundaries,name=bi}];


    \foreach \regcol/\pta/\ptb in {%
      red/north/east,% north of path a and east of path b
      green/north/west,% north of path a and west of path b
      yellow/south/east,% ...
      blue/south/west% ...
    }{
      \begin{scope}
        % clipping region path a
        \clip
        plot[domain=-1:7,samples=100](\x,{.5*sin(\x*3 r)+1})
        |- (current bounding box.\pta) -| (ai-1);
        % clipping region path b
        \clip
        plot[domain=-1:4,samples=100]({.5*sin(\x*4 r)+2},\x)
        -| (current bounding box.\ptb) |- (bi-1);
        \fill[\regcol]
        (current bounding box.south west)
        rectangle
        (current bounding box.north east);
      \end{scope}
    }
    % drawing pathes
    \draw plot[domain=-1:7,samples=100](\x,{.5*sin(\x*3 r)+1});
    \draw plot[domain=-2:4,samples=100]({.5*sin(\x*4 r)+2},\x);
  \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

编辑:第二个示例具有路径的分解定义(以及分区的有趣效果)。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\newcommand\plota{plot[smooth,domain=-1:7,samples=100](\x,{2*sin(\x*3 r)+1})}
\newcommand\plotb{plot[smooth,domain=-1:4,samples=100]({.8*sin(\x*5 r)+2},\x)}
\begin{document}
\begin{tikzpicture}
  \begin{scope}
    \draw[clip, preaction={draw, ultra thick, double distance=0pt}]
    plot[smooth cycle]
    coordinates{(0,2) (2.2,2.2) (3.1,3.3) (6,3) (6,0) (0,0)};
    \path[name path=a] \plota;
    % second path (b)
    \path[name path=b]\plotb;
    % current bounding box (boundaries)
    \path[name path=boundaries]
    (current bounding box.south west)
    rectangle
    (current bounding box.north east);
    % two intersestion for each path (ai-1,ai-2) and (bi-1,bi-2)
    \path[name intersections={of=a and boundaries,name=ai}];
    \path[name intersections={of=b and boundaries,name=bi}];
    \foreach \regcol/\pta/\ptb in {%
      red/north/east,% north of path a and east of path b
      green/north/west,% north of path a and west of path b
      yellow/south/east,% ...
      blue/south/west% ...
    }{
      \begin{scope}
        % clipping region path a
        \clip \plota |- (current bounding box.\pta) -| (ai-1);
        % clipping region path b
        \clip \plotb -| (current bounding box.\ptb) |- (bi-1);
        \fill[\regcol]
        (current bounding box.south west)
        rectangle
        (current bounding box.north east);
      \end{scope}
    }
    % drawing pathes
    \draw \plota;
    \draw \plotb;
  \end{scope}
  % uncomment to show paths
  %\draw[dashed] \plota;
  %\draw[dashed] \plotb;
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

以下是解决方案的变体:

\begin{tikzpicture}
\draw[clip, preaction={draw, ultra thick, double distance=0pt}]
  plot[smooth cycle]
  coordinates{
   (0,2) (2.2,2.2) (3.1,3.3) (6,3) (6,0) (0,0)
  };

\draw[thick, draw=gridcolor, fill=red] (2,1) -- (2,4) -- (-2,1) -- cycle;
\draw[thick, draw=gridcolor, fill=blue] (2,1) -- (2,-4) -- (-2,1) -- cycle;
\draw[thick, draw=gridcolor, fill=yellow] (2,1) -- (2,-4) -- (10,1) -- cycle;
\draw[thick, draw=gridcolor, fill=green] (2,1) -- (2,6) -- (10,1) -- cycle;
\end{tikzpicture}

我过去常常clip修剪你指定的曲线之外的所有内容。剪切路径的线也被“切成两半”,这就是我使用 hack 的原因double。该命令现在实际上绘制了两条相同的路径,只是偏移了线宽的一半。(您可以通过double distance=0pt完全省略或指定不同的偏移距离进行实验。)此外,这必须作为执行preaction,因为clip不喜欢任何修改路径的附加选项。

然后,您只需绘制任意大小的彩色分区拼凑物,像往常一样,只有剪切路径内的部分才会可见。

编辑:顺便说一句,如果您不使用关闭分区路径cycle,线条本身也会被涂成与填充相同的颜色。

在此处输入图片描述

相关内容