TikZ 中的阴影区域

TikZ 中的阴影区域

对于使用它的人来说,这是一个简单的 TikZ 问题。我尝试阅读 TikZ 手册第 112 页几次,但填充部分似乎太复杂了,所有的无穷大等等......所以我目前有这个代码:

\documentclass[letterpaper,12pt]{exam}
\usepackage{pgfplots}
\begin{document}

\begin{center}
    \begin{tikzpicture}
    \draw (0,0) -- (9.2106,4.228) -- (10.857,0) -- (9.2106,-4.228) -- cycle (9.2106,-4.228) arc (-22.5:22.5:11cm)
    node [left] at (0,0) {$O$}
    node [above] at (9.2106,4.228) {$A$}
    node [below] at (9.2106,-4.228) {$B$}
    node [right] at (10.857,0) {$P$};
    node at (2,1) {0.8 rad};
    \end{tikzpicture}

    \begin{tikzpicture}
    \draw (-3,0) -- (3,0);
    \draw (3,0) arc (0:180:3cm);
    \draw (2.5980762,1.5) -- (-2.12132,2.121320)
    node [yshift=-2pt,below] at (0,0) {$O$}
    node [xshift=-2pt,left] at (-3,0) {$A$}
    node [xshift=-2pt,left] at (-2.12132,2.121320) {$B$}
    node [xshift=2pt,right] at (2.5980762,1.5) {$C$}
    node [xshift=2pt,right] at (3,0) {$D$};
    \fill (0,0) circle [radius=2pt] (3,0) circle [radius=2pt] (-3,0) circle [radius=2pt] (-2.12132,2.121320) circle [radius=2pt] (2.5980762,1.5) circle [radius=2pt];
    \end{tikzpicture}
\end{center}
\end{document}

它生成的是这两张图片,没有阴影。我需要为这张图片中的阴影区域添加阴影,并添加 0.8 rad,但正如您所见,节点由于某种原因无法正常工作。 在此处输入图片描述

答案1

这是一个稍微不同的解决方案,使用intersection segments(您实际上需要pgfplots这个)和tkz-euclide角度包。

既然你提到了阴影,我就加了一些阴影。我不得不重新创建一些路径,例如圆弧 AB,因为它并不“完美”。基本上圆弧实际上并没有到达坐标 A:

在此处输入图片描述

因此我使用了该命令<coordinate> to[bend right=20] <coordinate>,除了微小(可忽略的)差异外,它描述了相同的弧。

输出

在此处输入图片描述

代码

\documentclass[letterpaper,12pt]{exam}
\usepackage{pgfplots}
\usepackage{tikz,tkz-euclide}
\usetkzobj{all}

\usetikzlibrary{fillbetween,backgrounds}

\begin{document}
\begin{center}
\begin{tikzpicture}                 
    \draw (0,0) -- (9.2106,4.228) -- (10.857,0) -- (9.2106,-4.228) -- cycle (9.2106,-4.228) to[bend right=20] (9.2106,4.228)
    node [left] at (0,0) {$O$}
    node [above] at (9.2106,4.228) {$A$}
    node [below] at (9.2106,-4.228) {$B$}
    node [right] at (10.857,0) {$P$};
    node at (2,1) {0.8 rad};

\filldraw[draw=black,bottom color=black, top color=white] (9.2106,4.228) -- (10.857,0) -- (9.2106,-4.228) to[bend right=20] (9.2106,4.228);

\coordinate (O) at (0,0);
\coordinate (A) at (9.2106,4.228);
\coordinate (B) at (9.2106,-4.228);
\tkzFindAngle(B,O,A) 
\tkzMarkAngle[draw=black,size=2.5](B,O,A)
\tkzLabelAngle[dist=1.5](B,O,A){0.8 rad}
\end{tikzpicture}
\begin{tikzpicture} 
    \draw (-3,0) -- (3,0);
    \draw[name path=curve] (3,0) arc (0:180:3cm);
    \draw (2.5980762,1.5) -- (-2.12132,2.121320)
    node [yshift=-2pt,below] at (0,0) {$O$}
    node [xshift=-2pt,left] at (-3,0) {$A$}
    node [xshift=-2pt,left] at (-2.12132,2.121320) {$B$}
    node [xshift=2pt,right] at (2.5980762,1.5) {$C$}
    node [xshift=2pt,right] at (3,0) {$D$};

\fill (0,0) circle [radius=2pt] (3,0) circle [radius=2pt] (-3,0) circle [radius=2pt] (-2.12132,2.121320) circle [radius=2pt] (2.5980762,1.5) circle [radius=2pt];

\begin{scope}[on background layer]
    \path[name path=line1] (2.5980762,1.5) -- (-2.13132,2.121320);
    \path[name path=myarc,intersection segments={of=line1 and curve}];
    \filldraw[draw=black,bottom color=black, top color=white]
    [intersection segments={of=line1 and myarc}];
\end{scope}
\end{tikzpicture}
\end{center}
\end{document}

有些颜色!

用以下颜色替换阴影颜色bottom color=blue!50!black, top color=cyan!50

图 2

答案2

您只需要划定区域。第一种情况很容易,但第二种情况比较棘手。在这种情况下,整个半圆是,filled但前一个clip命令定义了哪些区域将被保留以及哪些区域将被消除。两者fill都已在background层上绘制,这样之前绘制的线条就会出现在顶部。

\documentclass[letterpaper,12pt]{exam}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}

\begin{center}
    \begin{tikzpicture}
    \draw (0,0) -- (9.2106,4.228) -- (10.857,0) -- (9.2106,-4.228) -- cycle (9.2106,-4.228) arc (-22.5:22.5:11cm)
    node [left] at (0,0) {$O$}
    node [above] at (9.2106,4.228) {$A$}
    node [below] at (9.2106,-4.228) {$B$}
    node [right] at (10.857,0) {$P$};
    node at (2,1) {0.8 rad};

    \begin{scope}[on background layer]
    \fill[gray] (9.2106,4.228) arc (22.5:-22.5:11cm) -- (10.857,0) -- cycle;
    \end{scope}

    \draw (22.5:2.5cm) arc (22.5:-22.5:2.5) node[midway,left] {0.8 rad};
    \end{tikzpicture}

    \begin{tikzpicture}
    \draw (-3,0) -- (3,0);
    \draw (3,0) arc (0:180:3cm);
    \draw (2.5980762,1.5) -- (-2.12132,2.121320)
    node [yshift=-2pt,below] at (0,0) {$O$}
    node [xshift=-2pt,left] at (-3,0) {$A$}
    node [xshift=-2pt,left] at (-2.12132,2.121320) {$B$}
    node [xshift=2pt,right] at (2.5980762,1.5) {$C$}
    node [xshift=2pt,right] at (3,0) {$D$};
    \fill (0,0) circle [radius=2pt] (3,0) circle [radius=2pt] (-3,0) circle [radius=2pt] (-2.12132,2.121320) circle [radius=2pt] (2.5980762,1.5) circle [radius=2pt];

    \begin{scope}[on background layer]
    \clip (-2.12132,2.121320) --++(90:2cm) -| (2.5980762,1.5)--cycle;
    \fill[gray] (3,0) arc (0:180:3cm)--cycle;
    \end{scope}
    \end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

答案3

您的示例中没有任何内容需要pgfplots。您只需要tikz。不过,我保留了您的序言。

\documentclass[letterpaper,12pt]{exam}
\usepackage{pgfplots}
\begin{document}

\begin{center}
    \begin{tikzpicture}
    \path
        (0,0)           coordinate (O)
        (9.2106,4.228)  coordinate (A)
        (9.2106,-4.228) coordinate (B)
        (10.857,0)      coordinate (P);
    \draw   (O) -- 
            (A) -- 
            (P) -- 
            (B) -- 
            cycle 
            (B) arc (-22.5:22.5:11cm);
    \path
      node [left]  at (O) {$O$}
      node [above] at (A) {$A$}
      node [below] at (B) {$B$}
      node [right] at (P) {$P$};
      node at (2,1) {0.8 rad};

    \fill (B) arc (-22.5:22.5:11cm) -- (P) -- cycle;
    \end{tikzpicture}

    \begin{tikzpicture}
    \path
       coordinate (O) at (0,0)               
       coordinate (A) at (-3,0)                      
       coordinate (B) at (-2.12132,2.121320) 
       coordinate (C) at (2.5980762,1.5)     
       coordinate (D) at (3,0)               ;

    \draw (A) -- (D);
    \draw (D) arc (0:180:3cm);
    \draw (C) -- (B);
    \fill (O) circle [radius=2pt] node [yshift=-2pt,below] {$O$}
          (D) circle [radius=2pt] node [xshift=2pt,right]  {$D$}
          (A) circle [radius=2pt] node [xshift=-2pt,left]  {$A$}
          (B) circle [radius=2pt] node [xshift=-2pt,left]  {$B$}
          (C) circle [radius=2pt] node [xshift=2pt,right]  {$C$}
          ;
    \pgfmathanglebetweenpoints{\pgfpointanchor{O}{center}}
                              {\pgfpointanchor{C}{center}}
    \edef\aeAngOC{\pgfmathresult}
    \pgfmathanglebetweenpoints{\pgfpointanchor{O}{center}}
                              {\pgfpointanchor{B}{center}}
    \edef\aeAngOB{\pgfmathresult}

    \fill (B) -- (C) arc (\aeAngOC:\aeAngOB:3cm);

    \end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

为了方便起见,我命名了坐标。

第一张图片很容易创建,我就不解释它的代码了。

对于第二幅图,还需要做更多的工作。我只需要弦上方的圆弧部分。要仅创建圆弧的这一部分,我找到沿OC和的角度OB。这是使用以下命令完成的:

\pgfmathanglebetweenpoints{<point>}{<point>}

pgf不期望坐标或节点名称,为了解决这个问题,我使用宏

\pgfpointanchor{<coordinate name>}{center}

\pgfmathanglebetweenpoints不返回值;它将计算出的角度保存到\pgfmathresult。我需要在对 TikZ 进行任何其他操作之前保存此值。(TikZ 在其许多路径计算等中都使用此宏。)要提取的值,\pgfmathresult我使用

\edef<command sequence>{\pgfmathresult}

在这里使用很重要,因为您想要定义时\edef的值。\pgfmathresult

相关内容