![使用 \tikzfillbetween 在两条曲线之间着色](https://linux22.com/image/358796/%E4%BD%BF%E7%94%A8%20%5Ctikzfillbetween%20%E5%9C%A8%E4%B8%A4%E6%9D%A1%E6%9B%B2%E7%BA%BF%E4%B9%8B%E9%97%B4%E7%9D%80%E8%89%B2.png)
我在填充两条曲线之间的区域时遇到了麻烦。我更愿意使用 tikz 而不是覆盖 pgfplots。以下是我尝试过的方法:
\documentclass[11pt]{amsart}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{intersections, pgfplots.fillbetween}
\begin{document}
\begin{tikzpicture}
\tikzset{partial ellipse/.style args={#1:#2:#3}{ insert path={+ (#1:#3) arc (#1:#2:#3)} } }
\draw [name path=A] (5,8) [partial ellipse=180:360:6cm and 1.5cm];
\draw[name path=B] (5,0) [partial ellipse=180:360:6cm and 1.5cm];
\tikzfillbetween[of=A and B]{blue, opacity=0.1};
\end{tikzpicture}
\end{document}
但是由于某些奇怪的原因,它只在图形的右侧起作用,而在左侧它填充到水平线,而不是在中间。
答案1
解决方法:如果你重新定义(TikZ 绘制圆弧的方式)来自@Qrrbrbirlbel,您可以用以下方法填充两条曲线之间的区域:
\draw[green,name path=A] (0,2) arc [start angle=180, end angle=360];
\draw[green,name path=B] (0,0) arc [start angle=180, end angle=360];
\tikzfillbetween[of=A and B]{blue, opacity=0.2};
长度和半径用x radius=6
和 定义y radius=1.5
。
梅威瑟:
\documentclass[tikz]{standalone}
\makeatletter
\def\tikz@arc@opt[#1]{% over-write!
{%
\tikzset{every arc/.try,#1}%
\pgfkeysgetvalue{/tikz/start angle}\tikz@s
\pgfkeysgetvalue{/tikz/end angle}\tikz@e
\pgfkeysgetvalue{/tikz/delta angle}\tikz@d
\ifx\tikz@s\pgfutil@empty%
\pgfmathsetmacro\tikz@s{\tikz@e-\tikz@d}
\else
\ifx\tikz@e\pgfutil@empty%
\pgfmathsetmacro\tikz@e{\tikz@s+\tikz@d}
\fi%
\fi
\tikz@arc@moveto
\xdef\pgf@marshal{\noexpand%
\tikz@do@arc{\tikz@s}{\tikz@e}
{\pgfkeysvalueof{/tikz/x radius}}
{\pgfkeysvalueof{/tikz/y radius}}}%
}%
\pgf@marshal%
\tikz@arcfinal%
}
\let\tikz@arc@moveto\relax
\def\tikz@arc@movetolineto#1{%
\def\tikz@arc@moveto{\tikz@@@parse@polar{\tikz@arc@@movetolineto#1}(\tikz@s:\pgfkeysvalueof{/tikz/x radius} and \pgfkeysvalueof{/tikz/y radius})}}
\def\tikz@arc@@movetolineto#1#2{#1{\pgfpointadd{#2}{\tikz@last@position@saved}}}
\tikzset{%
move to start/.code=\tikz@arc@movetolineto\pgfpathmoveto,%
line to start/.code=\tikz@arc@movetolineto\pgfpathlineto}
\makeatother
\usepackage{pgfplots}
\usetikzlibrary{intersections, pgfplots.fillbetween}
\begin{document}
\begin{tikzpicture}[x radius=6, y radius=1.5]
\draw[green,name path=A] (0,2) arc [start angle=180, end angle=360];
\draw[green,name path=B] (0,0) arc [start angle=180, end angle=360];
\tikzfillbetween[of=A and B]{blue, opacity=0.2};
%\tikzset{partial ellipse/.style args={#1:#2:#3}{ insert path={+ (#1:#3) arc (#1:#2:#3)} } }
%\draw [name path=AA,red] (6,0) [partial ellipse=180:360:6cm and 1.5cm];
%\draw [name path=BB,red] (6,2) [partial ellipse=180:360:6cm and 1.5cm];
%\tikzfillbetween[of=AA and BB]{red, opacity=0.1};
\end{tikzpicture}
\end{document}
答案2
(5,8)
从到部分椭圆的起点的线不是画出来的而是路径的一部分。和 也是A
同样的情况。(5,0)
B
也许你可以使用(5,8)
和(5,0)
作为 option 的值shift
。那么路径的起点与绘制的部分椭圆的起点相同:
\documentclass[11pt]{amsart}
\usepackage{pgfplots}% loads also tikz
\pgfplotsset{compat=newest}% to avoid the pgfplots warning
\usetikzlibrary{intersections, pgfplots.fillbetween}
\pgfdeclarelayer{pre main}
\begin{document}
\begin{tikzpicture}
\pgfsetlayers{pre main,main}
\tikzset{partial ellipse/.style args={#1:#2:#3}{ insert path={+ (#1:#3) arc (#1:#2:#3)} } }
\draw[name path=A, shift={(5,8)}, partial ellipse=180:360:6cm and 1.5cm];
\draw[name path=B, shift={(5,0)}, partial ellipse=180:360:6cm and 1.5cm];
\tikzfillbetween[of=A and B]{blue, opacity=0.1};
\end{tikzpicture}
\end{document}
结果:
关于以下评论的补充说明:
您还可以使用图案填充该区域:
\documentclass[11pt]{amsart}
\usepackage{pgfplots}% loads also tikz
\pgfplotsset{compat=newest}% to avoid the pgfplots warning
\usetikzlibrary{intersections, pgfplots.fillbetween}
\usetikzlibrary{patterns}% <- added
\pgfdeclarelayer{pre main}
\begin{document}
\begin{tikzpicture}
\pgfsetlayers{pre main,main}
\tikzset{partial ellipse/.style args={#1:#2:#3}{ insert path={+ (#1:#3) arc (#1:#2:#3)} } }
\draw[name path=A, shift={(5,8)}, partial ellipse=180:360:6cm and 1.5cm];
\draw[name path=B, shift={(5,0)}, partial ellipse=180:360:6cm and 1.5cm];
\tikzfillbetween[of=A and B]{pattern=vertical lines, pattern color=red!50!black};% <- changed
\end{tikzpicture}
\end{document}