使用 TikZ 填充复杂区域

使用 TikZ 填充复杂区域

我正在尝试填充由 4 条双曲线的 6 个交点定义的复杂区域。如您所见:

在此处输入图片描述

具体来说,我想填充由 6 个点界定的区域。我知道 的精确坐标p_i和每条双曲线的精确方程。我该如何将它们连接起来并填充该区域?

为了完整起见,下面是我目前的代码:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\def\bndmax{5}
\def\bndmin{0.2}
\def\xS{1.5}
\def\gR{1.618034} % The golden ratio

\begin{tikzpicture}
  \draw (-3,-3) grid (3,3);
  \tikzset{func/.style={thick,color=orange!90}}
  \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (\x,{-1/\x});
  \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (-\x,{1/\x});
  \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (\x,{1/\x});
  \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (-\x,{-1/\x});

  \begin{scope}[shift={(\xS,1/\xS)}]
    \tikzset{func/.style={thick,color=orange!60,dashed}}
    \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (\x,{-1/\x});
    \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (-\x,{1/\x});
    \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (\x,{1/\x});
    \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (-\x,{-1/\x});
  \end{scope}

  \fill (\xS,1/\xS) circle (2pt);

  \pgfmathsetmacro\x{-\gR*\xS}
  \pgfmathsetmacro\y{1/(\gR*\xS)}
  \coordinate (p1) at (\x,\y);

  \pgfmathsetmacro\x{-(1/\gR)*\xS}
  \pgfmathsetmacro\y{\gR*(1/\xS)}
  \coordinate (p2) at (\x,\y);

  \pgfmathsetmacro\x{1/(\gR*\gR)*\xS}
  \pgfmathsetmacro\y{\gR*\gR/\xS)}
  \coordinate (p3) at (\x,\y);

  \pgfmathsetmacro\x{(1/\gR)*\xS}
  \pgfmathsetmacro\y{-\gR*(1/\xS)}
  \coordinate (p4) at (\x,\y);

  \pgfmathsetmacro\x{\gR*\xS}
  \pgfmathsetmacro\y{-1/(\gR*\xS)}
  \coordinate (p5) at (\x,\y);

  \pgfmathsetmacro\x{\gR*\gR*\xS}
  \pgfmathsetmacro\y{1/(\gR*\gR*\xS)}
  \coordinate (p6) at (\x,\y);

  \foreach \i in {1,2,3,4,5,6}
  \fill[red] (p\i) circle (2pt) node[right]{$p_{\i}$};

\end{tikzpicture}
\end{document}

答案1

\xs这是一个非全自动解决方案。如果我在 中的函数定义中使用,它不起作用,plot所以我不得不手动(因此是静态的)将其放入。对于相应的域,我将您重复使用的帮助宏\x和重命名\y为 和\xa\ya\xa\xf因此它们可以在以后使用。然后它只是连接了很多plot--命令。--很重要,否则每个单独的图都是封闭的,导致奇怪的菱形。

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\def\bndmax{5}
\def\bndmin{0.2}
\def\xS{1.5}
\def\gR{1.618034} % The golden ratio

\begin{tikzpicture}
  \draw (-3,-3) grid (3,3);
  \tikzset{func/.style={thick,color=orange!90}}
  \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (\x,{-1/\x});
  \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (-\x,{1/\x});
  \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (\x,{1/\x});
  \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (-\x,{-1/\x});

  \begin{scope}[shift={(\xS,1/\xS)}]
    \tikzset{func/.style={thick,color=orange!60,dashed}}
    \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (\x,{-1/\x});
    \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (-\x,{1/\x});
    \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (\x,{1/\x});
    \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (-\x,{-1/\x});
  \end{scope}

  \fill (\xS,1/\xS) circle (2pt);

  \pgfmathsetmacro\xa{-\gR*\xS}
  \pgfmathsetmacro\ya{1/(\gR*\xS)}
  \coordinate (p1) at (\xa,\ya);

  \pgfmathsetmacro\xb{-(1/\gR)*\xS}
  \pgfmathsetmacro\yb{\gR*(1/\xS)}
  \coordinate (p2) at (\xb,\yb);

  \pgfmathsetmacro\xc{1/(\gR*\gR)*\xS}
  \pgfmathsetmacro\yc{\gR*\gR/\xS)}
  \coordinate (p3) at (\xc,\yc);

  \pgfmathsetmacro\xd{(1/\gR)*\xS}
  \pgfmathsetmacro\yd{-\gR*(1/\xS)}
  \coordinate (p4) at (\xd,\yd);

  \pgfmathsetmacro\xe{\gR*\xS}
  \pgfmathsetmacro\ye{-1/(\gR*\xS)}
  \coordinate (p5) at (\xe,\ye);

  \pgfmathsetmacro\xf{\gR*\gR*\xS}
  \pgfmathsetmacro\yf{1/(\gR*\gR*\xS)}
  \coordinate (p6) at (\xf,\yf);

  \foreach \i in {1,2,3,4,5,6}
  \fill[red] (p\i) circle (2pt) node[right]{$p_{\i}$};



  \clip (p1) plot[domain=\xa:\xb] (\x,{-1/\x}) -- plot[domain=\xb:\xc] (\x,{-1/(\x-1.5)+1/1.5}) -- plot[domain=\xc:\xf] (\x,{1/\x}) -- plot[domain=\xf:\xe] (\x,{-1/(\x-1.5)+1/1.5}) -- plot[domain=\xe:\xd] (\x,{-1/\x}) -- plot[domain=\xd:\xa] (\x,{1/(\x-1.5)+1/1.5}) --cycle;
  \fill[opacity=0.3,blue!30!cyan] (\xa,\yd) rectangle (\xf,\yc);

\end{tikzpicture}
\end{document}

在此处输入图片描述


编辑1:仅进行了一些小改进:

  • 将网格扩大到 10x10
  • 改善整体剪辑
  • 固定边界,使得所有函数都绘制在整个域上
  • 将蓝色填充放在背景层上,这样它就不会与功能或点部分重叠

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}

\pgfdeclarelayer{background layer}
\pgfsetlayers{background layer,main}

\begin{document}

\def\bndmax{6.5}
\def\bndmin{0.15}
\def\xS{1.5}
\def\gR{1.618034} % The golden ratio

\begin{tikzpicture}
    \clip (-5cm-0.2pt,-5cm-0.2pt) rectangle (5cm+0.pt,5cm+0.2pt);
  \draw (-5,-5) grid (5,5);
  \draw[thick] (-5,0) -- (5,0);
  \draw[thick] (0,-5) -- (0,5);
  \tikzset{func/.style={thick,color=orange!90}}
  \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (\x,{-1/\x});
  \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (-\x,{1/\x});
  \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (\x,{1/\x});
  \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (-\x,{-1/\x});

  \begin{scope}[shift={(\xS,1/\xS)}]
    \tikzset{func/.style={thick,color=orange!60,dashed}}
    \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (\x,{-1/\x});
    \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (-\x,{1/\x});
    \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (\x,{1/\x});
    \draw[func,domain=-\bndmax:-\bndmin] plot [samples=200] (-\x,{-1/\x});
  \end{scope}

  \fill (\xS,1/\xS) circle (2pt);

  \pgfmathsetmacro\xa{-\gR*\xS}
  \pgfmathsetmacro\ya{1/(\gR*\xS)}
  \coordinate (p1) at (\xa,\ya);

  \pgfmathsetmacro\xb{-(1/\gR)*\xS}
  \pgfmathsetmacro\yb{\gR*(1/\xS)}
  \coordinate (p2) at (\xb,\yb);

  \pgfmathsetmacro\xc{1/(\gR*\gR)*\xS}
  \pgfmathsetmacro\yc{\gR*\gR/\xS)}
  \coordinate (p3) at (\xc,\yc);

  \pgfmathsetmacro\xd{(1/\gR)*\xS}
  \pgfmathsetmacro\yd{-\gR*(1/\xS)}
  \coordinate (p4) at (\xd,\yd);

  \pgfmathsetmacro\xe{\gR*\xS}
  \pgfmathsetmacro\ye{-1/(\gR*\xS)}
  \coordinate (p5) at (\xe,\ye);

  \pgfmathsetmacro\xf{\gR*\gR*\xS}
  \pgfmathsetmacro\yf{1/(\gR*\gR*\xS)}
  \coordinate (p6) at (\xf,\yf);

  \foreach \i in {1,2,3,4,5,6}
  \fill[red] (p\i) circle (2pt) node[right]{$p_{\i}$};

  \begin{pgfonlayer}{background layer}
      \clip (p1) plot[domain=\xa:\xb] (\x,{-1/\x}) -- plot[domain=\xb:\xc] (\x,{-1/(\x-1.5)+1/1.5}) -- plot[domain=\xc:\xf] (\x,{1/\x}) -- plot[domain=\xf:\xe] (\x,{-1/(\x-1.5)+1/1.5}) -- plot[domain=\xe:\xd] (\x,{-1/\x}) -- plot[domain=\xd:\xa] (\x,{1/(\x-1.5)+1/1.5}) --cycle;
    \fill[opacity=0.3,blue!30!cyan] (\xa,\yd) rectangle (\xf,\yc);
  \end{pgfonlayer}  

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

当您拥有复杂区域每个顶点的坐标时,您可以用一条路径绘制它:

\documentclass{standalone}
\usepackage{tikz}
\def\xS{1.5}
\def\gR{1.618034} % The golden ratio
\begin{document}
\begin{tikzpicture}
  \draw[samples=30,line join=round,fill=lime]
     plot [domain=-\gR*\xS:-(1/\gR)*\xS] (\x,-{1/\x})
  -- plot [domain=-\gR*\xS:-(1/\gR)*\xS] (\x+\xS,{-(1/\x)+(1/\xS)})
  -- plot [domain=1/(\gR*\gR)*\xS:\gR*\gR*\xS] (\x,{1/\x})
  -- plot [domain=\gR*\xS:{(1/\gR)*\xS}] (\x+\xS,{-1/\x+1/\xS})
  -- plot [domain=\gR*\xS:{(1/\gR)*\xS}] (\x,{-1/\x})
  -- plot [domain=-1/(\gR*\gR)*\xS:-\gR*\gR*\xS] (\x+\xS,{1/\x+1/\xS})
  -- cycle;
\end{tikzpicture}
\end{document}

在此处输入图片描述

您甚至可以改变参数“\xS”:

\begin{tikzpicture}
  \foreach \gray in {10,20,...,90}{
    \pgfmathsetmacro{\xS}{.5+\gray/100*1.5}
    \draw[samples=30,line join=round,draw=black!\gray!yellow]
    plot [domain=-\gR*\xS:-(1/\gR)*\xS] (\x,-{1/\x})
    -- plot [domain=-\gR*\xS:-(1/\gR)*\xS] (\x+\xS,{-(1/\x)+(1/\xS)})
    -- plot [domain=1/(\gR*\gR)*\xS:\gR*\gR*\xS] (\x,{1/\x})
    -- plot [domain=\gR*\xS:{(1/\gR)*\xS}] (\x+\xS,{-1/\x+1/\xS})
    -- plot [domain=\gR*\xS:{(1/\gR)*\xS}] (\x,{-1/\x})
    -- plot [domain=-1/(\gR*\gR)*\xS:-\gR*\gR*\xS] (\x+\xS,{1/\x+1/\xS})
    -- cycle;
  }
\end{tikzpicture}

在此处输入图片描述

但是如果你不知道坐标,那么总有一个使用两个带有链式图的剪辑路径的解决方案(如果\bndmax\bndmin选择正确则有效):

\documentclass{standalone}
\usepackage{tikz}

\def\bndmax{5}
\def\bndmin{0.2}
\def\xS{1.5}
\def\gR{1.618034} % The golden ratio
\begin{document}
\begin{tikzpicture}
  \draw (-\bndmax,-1/\bndmin) grid (\bndmax,1/\bndmin);

  \path[clip] plot [samples=200,domain=-\bndmax:-\bndmin] (-\x,{1/\x})
  -- plot [samples=200,domain=-\bndmin:-\bndmax] (\x,{1/\x})
  -- plot [samples=200,domain=-\bndmax:-\bndmin] (\x,{-1/\x})
  -- plot [samples=200,domain=-\bndmin:-\bndmax] (-\x,{-1/\x})
  -- cycle;

  \fill[green,fill opacity=.3]
  (-\bndmax,-1/\bndmin) rectangle (\bndmax,1/\bndmin);

  \begin{scope}[shift={(\xS,1/\xS)}]
    \path[clip] plot [samples=200,domain=-\bndmax:-\bndmin] (-\x,{1/\x})
    -- plot [samples=200,domain=-\bndmin:-\bndmax] (\x,{1/\x})
    -- plot [samples=200,domain=-\bndmax:-\bndmin] (\x,{-1/\x})
    -- plot [samples=200,domain=-\bndmin:-\bndmax] (-\x,{-1/\x})
    -- cycle;

    \fill[red,fill opacity=.7]
    (-\bndmax,-1/\bndmin) rectangle (\bndmax,1/\bndmin);
  \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

您可以为此使用 PGFPlots。

我定义了两个函数,

declare function={f(\x)=min(1/\x,-1/\x);},
declare function={g(\x)=max(1/\x,-1/\x);}

对应于双曲线的负(正)部分,然后用它们定义两个新函数

declare function={h(\x)=max(f(x),f(x-1.5)+1/1.5);},
declare function={i(\x)=min(g(x),g(x-1.5)+1/1.5);}

其对应于正(负)未移位部分和移位部分中较大者(较小者)。

然后可以在堆叠图中使用这些来为该区域着色。为了确保只有p1和之间的部分p6被着色,我们可以利用未定义的坐标会被自动丢弃的事实,所以我添加了术语

*1/(h(x)<i(x))

这会导致除以零超出我们感兴趣的区域,因此绘图在我们想要的地方开始和停止。

\documentclass{article}

\usepackage{pgfplots}

\begin{document}


\begin{tikzpicture}[
    declare function={f(\x)=min(1/\x,-1/\x);},
    declare function={g(\x)=max(1/\x,-1/\x);},
    declare function={h(\x)=max(f(x),f(x-1.5)+1/1.5);},
    declare function={i(\x)=min(g(x),g(x-1.5)+1/1.5);}
]
\begin{axis}[
    domain=-5:5,
    ymin=-5,ymax=5,
    samples=101,
    no markers,
    smooth
]

\addplot [draw=none, stack plots=y] {h(x)*1/(h(x)<i(x))};
\addplot [draw=none, fill=yellow, thick, stack plots=y] {i(x)*1/(h(x)<i(x))- h(x)*1/(h(x)<i(x))}\closedcycle;

\addplot [black] {f(x)};
\addplot [black] {g(x)};

\addplot [black, dashed] {f(x-1.5)+1/1.5};
\addplot [black, dashed] {g(x-1.5)+1/1.5};


\end{axis}
\end{tikzpicture}
\end{document}

答案4

对于那些正在寻找 PSTricks 等效产品的人来说。

在此处输入图片描述

\documentclass[pstricks,border=0pt]{standalone}
\usepackage{pst-eucl,pst-plot}

\def\f(#1){1 #1 div}
\def\F(#1){\f(#1 1.5 sub) 1 1.5 div add}
\def\g(#1){\f(#1 neg)}
\def\G(#1){\g(#1 1.5 sub) 1 1.5 div add}
\def\x(#1){\psGetNodeCenter{#1}#1.x}

\psset{yMaxValue=4,yMinValue=-4,plotpoints=6001}


\begin{document}

\begin{pspicture}[showgrid=false](-4.25,-4.25)(5.5,4.5)
  \psclip{\psframe[linestyle=none,linewidth=0pt](-4,-4)(5,4)}
    \pstInterFF[PosAngle=135]{\g(x)}{\F(x)}{-2}{P_1}
    \pstInterFF[PosAngle=135]{\g(x)}{\G(x)}{-1}{P_2}
    \pstInterFF[PosAngle=180]{\G(x)}{\f(x)}{1}{P_3}
    \pstInterFF[PosAngle=90]{\G(x)}{\f(x)}{3}{P_4}
    \pstInterFF[PosAngle=-45]{\G(x)}{\g(x)}{2}{P_5}
    \pstInterFF[PosAngle=0]{\g(x)}{\F(x)}{1}{P_6}
    \pscustom*[linecolor=yellow]
    {
        \psplot{\x(P_1)}{\x(P_2)}{\g(x)}
        \psplot{\x(P_2)}{\x(P_3)}{\G(x)}
        \psplot{\x(P_3)}{\x(P_4)}{\f(x)}
        \psplot{\x(P_4)}{\x(P_5)}{\G(x)}
        \psplot{\x(P_5)}{\x(P_6)}{\g(x)}
        \psplot{\x(P_6)}{\x(P_1)}{\F(x)}
    }
    \psplot[linecolor=red]{-4}{5}{\f(x)}
    \psplot[linecolor=blue]{-4}{5}{\g(x)}
    \psset{linestyle=dashed,dash=3pt 1pt}
    \psplot[linecolor=red]{-4}{5}{\F(x)}
    \psplot[linecolor=blue]{-4}{5}{\G(x)}
    \endpsclip
  \psaxes[labelFontSize=\scriptscriptstyle,linecolor=gray]{->}(0,0)(-4,-4)(5,4)[$x$,0][$y$,90]
\end{pspicture}

\end{document}

在此处输入图片描述

笔记

\psset{saveNodeCoors}
\def\x(#1){N-#1.x}

可以用来代替

\def\x(#1){\psGetNodeCenter{#1}#1.x}

最新更新

为了方便起见,使用中缀表示法。

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-eucl,pst-plot}

\def\f(#1){(1/(#1))}
\def\F(#1){(\f(#1-1.5)+1/1.5)}
\def\g(#1){(\f(-(#1)))}
\def\G(#1){(\g(#1-1.5)+1/1.5)}
\def\x(#1){N-#1.x}

\pstVerb{/I2P {exec AlgParser cvx exec} def}

\begin{document}

\begin{pspicture}[showgrid=false,saveNodeCoors,algebraic,yMaxValue=4,yMinValue=-4,plotpoints=6001](-4.25,-4.25)(5.5,4.5)
  \psclip{\psframe[linestyle=none,linewidth=0pt](-4,-4)(5,4)}
    \pstInterFF[PosAngle=135]{{\g(x)} I2P}{{\F(x)} I2P}{-2}{P_1}
    \pstInterFF[PosAngle=135]{{\g(x)} I2P}{{\G(x)} I2P}{-1}{P_2}
    \pstInterFF[PosAngle=180]{{\G(x)} I2P}{{\f(x)} I2P}{1}{P_3}
    \pstInterFF[PosAngle=90]{{\G(x)} I2P}{{\f(x)} I2P}{3}{P_4}
    \pstInterFF[PosAngle=-45]{{\G(x)} I2P}{{\g(x)} I2P}{2}{P_5}
    \pstInterFF[PosAngle=0]{{\g(x)} I2P}{{\F(x)} I2P}{1}{P_6}
    \pscustom*[linecolor=yellow]
    {
        \psplot{\x(P_1)}{\x(P_2)}{\g(x)}
        \psplot{\x(P_2)}{\x(P_3)}{\G(x)}
        \psplot{\x(P_3)}{\x(P_4)}{\f(x)}
        \psplot{\x(P_4)}{\x(P_5)}{\G(x)}
        \psplot{\x(P_5)}{\x(P_6)}{\g(x)}
        \psplot{\x(P_6)}{\x(P_1)}{\F(x)}
    }
    \psplot[linecolor=red]{-4}{5}{\f(x)}
    \psplot[linecolor=blue]{-4}{5}{\g(x)}
    \psset{linestyle=dashed,dash=3pt 1pt}
    \psplot[linecolor=red]{-4}{5}{\F(x)}
    \psplot[linecolor=blue]{-4}{5}{\G(x)}
    \endpsclip
  \psaxes[labelFontSize=\scriptscriptstyle,linecolor=gray]{->}(0,0)(-4,-4)(5,4)[$x$,0][$y$,90]
    \foreach \i in {1,...,6}{\qdisk(P_\i){2pt}}
\end{pspicture}

\end{document}

相关内容