10

Hi I am trying to fill the area between two curves. One curve is two joined arcs, and the other is one freehand curve. The part I want to fill is the white area. I use the fillbetween library but I just can't get the upper curve right. enter image description here enter image description here

Your help is much appreciated. A MWE is as follows

\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}
14

You only need to add a [reverse] to one of the segments.

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

enter image description here

How can one debug this? Just draw curves with arrows on the segments in question. Here one would add

\draw[red,thick,-latex,
intersection segments={of=unduloid and arcs,sequence={L2}}];

\draw[blue,thick,-latex,
intersection segments={of=unduloid and arcs,sequence={R2}}];

and then get

enter image description here

This means that the paths start and end at the same points. To fix this, i.e. to produce a cycle, you have to run through one of the paths in the reverse direction. If you don't, TikZ will close the path on its own by a straight line. That's why you got the straight line for one of the bounding curves.

ADDENDUM: In this situation you do not really need all the segments nor the fillbetween library. Personally I would also refrain from redefining \alpha, \beta etc. since you may need these Greek symbols.

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

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.