嗨,我想填充两条曲线之间的区域。一条曲线是两个连接的圆弧,另一条是一条自由曲线。我想要填充的部分是白色区域。我使用了库,fillbetween
但就是无法正确绘制上方的曲线。
非常感谢您的帮助。MWE 如下
\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}%[x=1cm, y=1cm]
\def\rsmall{2}
\def\rlarge{4}
\def\alpha{60}
\def\beta{40}
\def\gamma{20}
\def\Aout{\alpha+\gamma-90}
\def\Bin{270-\beta-\gamma}
\pgfmathsetmacro\ax{-\rsmall+cos(\alpha)*\rsmall}
\pgfmathsetmacro\ay{sin(\alpha)*\rsmall}
\pgfmathsetmacro\bx{\rlarge-cos(\beta)*\rlarge}
\pgfmathsetmacro\by{sin(\beta)*\rlarge}
\filldraw[name path=arcs, fill=gray!20, thick]
(180:2*\rsmall) arc (180:0:\rsmall) arc (180:0:\rlarge);
\coordinate (A) at (\ax, \ay);
\coordinate (B) at (\bx, \by);
\draw[name path=unduloid, thick] (A) to[out=\Aout, in=\Bin] (B);
\fill[blue!20,intersection segments={of=unduloid and arcs,sequence={L2--R2}}];
\end{tikzpicture}
\end{document}
答案1
[reverse]
您只需要在其中一个段中添加 即可。
\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}%[x=1cm, y=1cm]
\def\rsmall{2}
\def\rlarge{4}
\def\alpha{60}
\def\beta{40}
\def\gamma{20}
\def\Aout{\alpha+\gamma-90}
\def\Bin{270-\beta-\gamma}
\pgfmathsetmacro\ax{-\rsmall+cos(\alpha)*\rsmall}
\pgfmathsetmacro\ay{sin(\alpha)*\rsmall}
\pgfmathsetmacro\bx{\rlarge-cos(\beta)*\rlarge}
\pgfmathsetmacro\by{sin(\beta)*\rlarge}
\filldraw[name path=arcs, fill=gray!20, thick]
(180:2*\rsmall) arc (180:0:\rsmall) arc (180:0:\rlarge);
\coordinate (A) at (\ax, \ay);
\coordinate (B) at (\bx, \by);
\draw[name path=unduloid, thick] (A) to[out=\Aout, in=\Bin] (B);
\path[%draw=red,thick,
fill=blue!20,intersection segments={of=unduloid and arcs,sequence={L2--R2[reverse]}}];
\end{tikzpicture}
\end{document}
如何调试?只需在相关线段上画带箭头的曲线即可。这里可以添加
\draw[red,thick,-latex,
intersection segments={of=unduloid and arcs,sequence={L2}}];
\draw[blue,thick,-latex,
intersection segments={of=unduloid and arcs,sequence={R2}}];
然后得到
这意味着路径的起点和终点都相同。要解决这个问题,即产生一个循环,你必须沿着该reverse
方向穿过其中一条路径。如果你不这样做,Ti钾Z 会自行用直线闭合路径。这就是为什么你会得到一条边界曲线的直线。
附录:在这种情况下,您实际上并不需要所有段或fillbetween
库。我个人也不建议重新定义等\alpha
,\beta
因为您可能需要这些希腊符号。
\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}%[x=1cm, y=1cm]
\def\rsmall{2}
\def\rlarge{4}
\def\myalpha{60}
\def\mybeta{40}
\def\gamma{20}
\def\Aout{\myalpha+\gamma-90}
\def\Bin{270-\mybeta-\gamma}
\filldraw[fill=gray!20, thick]
(180:2*\rsmall) arc (180:0:\rsmall) arc (180:0:\rlarge);
\coordinate (A) at ($(-\rsmall,0)+(\myalpha:\rsmall)$);
\coordinate (B) at ($(\rlarge,0)+(180-\mybeta:\rlarge)$);
\draw[red,fill=blue!20, thick] (A) to[out=\Aout, in=\Bin] (B)
arc(180-\mybeta:180:\rlarge) arc(0:\myalpha:\rsmall) -- cycle;
\end{tikzpicture}
\end{document}