TiKZ:使平滑循环的各个部分呈现不同的颜色

TiKZ:使平滑循环的各个部分呈现不同的颜色

我正在尝试将循环的某些部分涂成与其他部分不同的颜色。在下面的代码中,我希望红色曲线位于底层黑色曲线的顶部。有没有办法使用平滑循环或其他类型的路径来实现这一点?

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}
\begin{document}
\begin{tikzpicture}
  \coordinate (d0) at (0,0);
  \coordinate (d1) at (2.5,0);
  \coordinate (d2) at (0,2.5);
  \coordinate (f0) at (4,0);
  \coordinate (f1) at (6.5,0);
  \coordinate (f2) at (4,2.5);
  \draw [<->,thick] (d2) node (yaxis) [left] {$x_2$}
  |- (d1) node (xaxis) [below] {$x_1$};
  \draw plot [smooth cycle] coordinates {(0.4,.2)(1.4,.2)(2.2,.5) (2.3,1.5)(2.1,2.2)(1.2,2.1)(0.4,0.8)} node at (1.5,1.3) {$D$};
  \path[->] (2.1,1.5) edge [bend left] node[above] {$F$} (5.0,1.3);
  \draw [<->,thick] (f2) node (yaxis) [left] {$f_2$}
  |- (f1) node (xaxis) [below] {$f_1$};
  \draw plot [smooth cycle] coordinates
  {(6,0.25) (6,2) (4.5,2) (4.75, 1) (5,0.5)};
  \draw[ultra thick, color=red] plot [smooth]  coordinates
  {(4.5,2) (4.75,1) (5,0.5) (6,0.25)};
\end{tikzpicture}
\end{document}

答案1

您可以重复相同的路径,但根据您的需要进行剪辑:

\documentclass{article}
\usepackage{tikz}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}
\begin{document}
\begin{tikzpicture}
  \coordinate (d0) at (0,0);
  \coordinate (d1) at (2.5,0);
  \coordinate (d2) at (0,2.5);
  \coordinate (f0) at (4,0);
  \coordinate (f1) at (6.5,0);
  \coordinate (f2) at (4,2.5);
  \draw [<->,thick] (d2) node (yaxis) [left] {$x_2$}
  |- (d1) node (xaxis) [below] {$x_1$};
  \draw plot [smooth cycle] coordinates {(0.4,.2)(1.4,.2)(2.2,.5) (2.3,1.5)(2.1,2.2)(1.2,2.1)(0.4,0.8)} node at (1.5,1.3) {$D$};
  \path[->] (2.1,1.5) edge [bend left] node[above] {$F$} (5.0,1.3);
  \draw [<->,thick] (f2) node (yaxis) [left] {$f_2$}
  |- (f1) node (xaxis) [below] {$f_1$};
  \draw plot [smooth cycle] coordinates
  {(6,0.25) (6,2) (4.5,2) (4.75, 1) (5,0.5)};
  \begin{scope}
  \clip (4,0) rectangle (5.9,1.9);
  \draw[ultra thick, red] plot [smooth cycle] coordinates
  {(6,0.25) (6,2) (4.5,2) (4.75, 1) (5,0.5)};
  \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容