如何在 tikz 中用曲线为多边形着色?

如何在 tikz 中用曲线为多边形着色?

我正在尝试用红色曲线着色一个多边形,如 MWE 中所示。我想着色由点 (A、B、C、D) 包围的曲线多边形,即多边形 ABCD。

平均能量损失

\documentclass{article}
\usepackage{standalone}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \draw [->] (0, 0) -- (5, 0) node [right] {$X$};
        \draw [->] (0, 0) -- (0, 3) node [above] {$Y$};

        \draw [domain=0.66:4.5, thick, blue] plot (\x, {2/\x});
        \draw [domain=1.66:4.5, thick, blue] plot (\x, {6/\x});

        \draw [red, ultra thick] (3, 2) edge [bend right=10] (3.5, 0.57);
        \draw [red, ultra thick] (2, 3) edge [bend right=10] (2.5, 0.8);

        \draw [domain=2.5:3.5, ultra thick, red] plot (\x, {2/\x});
        \draw [domain=2:3, ultra thick, red] plot (\x, {6/\x});

        \node [above] at (2, 3) {$C$};
        \node [below] at (2.5, 0.8) {$B$};
        \node [above] at (3, 2) {$D$};
        \node [below] at (3.5, 0.57) {$A$};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

如何用 tikz 为 ABCD 包围的多边形着色?

答案1

我不知道你想要如何给它着色,但如果你把它画成一条路径,你可以使用例如\shadedraw[left color=.., right color.., ]

在此处输入图片描述

\documentclass{article}
\usepackage{standalone}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \draw [->] (0, 0) -- (5, 0) node [right] {$X$};
        \draw [->] (0, 0) -- (0, 3) node [above] {$Y$};

        \draw [domain=0.66:4.5, thick, blue] plot (\x, {2/\x});
        \draw [domain=1.66:4.5, thick, blue] plot (\x, {6/\x});

        \shadedraw[
            draw=red,
            ultra thick,
            left color=blue,
            right color=red,
            bend angle=10
          ]
          plot[domain=2.5:3.5] (\x, {2/\x})
          to[bend left] (3,2)
          plot[domain=3:2] (\x, {6/\x})
          to[bend right] (2.5, 2/2.5);

        \node [above] at (2, 3) {$C$};
        \node [below] at (2.5, 2/2.5) {$B$};
        \node [above] at (3, 2) {$D$};
        \node [below] at (3.5, 2/3.5) {$A$};
    \end{tikzpicture}
\end{document}

答案2

使用fadingsTi 库頁面:

\documentclass[tikz,margin=5mm]{standalone}
\usetikzlibrary{fadings}
\makeatletter
\pgfdeclareverticalshading{pgf@lib@fade@north}{100bp}
{color(0bp)=(pgftransparent!0);
 color(5bp)=(pgftransparent!10);
 color(60bp)=(pgftransparent!100); color(80bp)=(pgftransparent!100)}%
\pgfdeclarefading{myfade}{%
  \pgfuseshading{pgf@lib@fade@east}%
}
\makeatother
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
        \draw [->] (0, 0) -- (5, 0) node [right] {$X$};
        \draw [->] (0, 0) -- (0, 3) node [above] {$Y$};

        \draw [domain=0.66:4.5, thick, blue] plot (\x, {2/\x});
        \draw [domain=1.66:4.5, thick, blue] plot (\x, {6/\x});

        \draw [red, ultra thick] (3, 2)coordinate(D) edge [bend right=10] (3.5, 0.57)coordinate(A);
        \draw [red, ultra thick] (2, 3)coordinate(C) edge [bend right=10] (2.5, 0.8)coordinate(B);

        \draw [domain=2.5:3.5, ultra thick, red] plot (\x, {2/\x});
        \draw [domain=2:3, ultra thick, red] plot (\x, {6/\x});

        \node [above] at (2, 3) {$C$};
        \node [below] at (2.5, 0.8) {$B$};
        \node [above] at (3, 2) {$D$};
        \node [below] at (3.5, 0.57) {$A$};
        \fill[red,path fading=myfade]  (3,2) to [bend right=10] (3.5,0.57) 
to [bend left=10] (2.5,0.8) to [bend left=10] (2,3) to [bend right=10] (3,2);

    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容