用 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\yaup 到\xa\xf,以便以後可以使用它們。那麼它只是連接很多plotand--指令。這--很重要,否則每個單獨的圖都會被封閉,導致奇怪的菱形形狀。

\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);}

它們對應於正(負)未移位和移位部分中的較大(較低)部分。

然後可以在堆積圖中使用這些來為該區域著色。為了確保只有 yourp1和之間的部分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}

相關內容