8

I'm having trouble filling a region between two curves. I would prefer to work in tikz rather than overlay pgfplots. Here is what I've tried:

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

But for some strange reason, it only work on the right side of the figure, and on the left it fills up to a horizontal line, instead of between.

enter image description here

Bobyandbob
  • 4,899
Daniel
  • 211
  • Hint: You could use the (complex) solution here (redefinition of drawing arcs): https://tex.stackexchange.com/a/123189/124842. And then \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.1};. You need àlso the packages \usepackage{pgfplots} and \usetikzlibrary{intersections, pgfplots.fillbetween}. – Bobyandbob Jul 21 '17 at 06:44
  • @Bobyandbob, please convert your comment to answer! – Zarko Jul 21 '17 at 07:23
  • @Zarko i added the workaround answer. But i can't add more information why this works. I'm not an expert. This exceeds my capacity. Sorry. Iif you (or someone else, maybe @Qrrbrbirlbel) could add some explanation, feel free to edit my answer... . Also i think the OP is still open. – Bobyandbob Jul 21 '17 at 08:01
  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – Bobyandbob Nov 13 '17 at 12:03

2 Answers2

9

A workaround: If you redefine (the way TikZ draws its arc) from @Qrrbrbirlbel, you could fill the region between two curves with:

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

Length and radius are defined with x radius=6 and y radius=1.5.

enter image description here

MWE:

\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}
Bobyandbob
  • 4,899
5

The line from (5,8) to the starting point of the partial ellipse is not drawn but part of path A. The same happens for (5,0) and B.

Maybe you can use (5,8) and (5,0) as values of option shift. Then the starting point of the path is the same as the starting point of the drawn partial ellipse:

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

Result:

enter image description here


Additional remark regarding a comment below:

You could also fill the region using patterns:

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

enter image description here

esdd
  • 85,675