4

I am trying to do something seemingly simple, but Latex keeps complaining. I want to draw two lines (not functions, just using the \draw command) and fill the area between them. Can somebody give me a simple answer? Thanks. Here is a sample of what I have in mind....

\begin{tikzpicture}
\draw[name path = A] (2,2) circle (3cm);
\draw[name path = B] (0.3,-0.5)..controls(2.55,2)..(0.3,4.5);
\draw[name path = C] (3.7,-0.5)..controls(1.5,2)..(3.7,4.5);
\draw (2,5)--(2,-1);
\fillbetween[of=A and B];
\draw[fill] (2,2) circle [radius=0.1];
\end{tikzpicture}
Fotis
  • 41

3 Answers3

10

You can also use intersection segments, although these require the pgfplots package and the fillbetween library.

The layers are needed for the fill to appear below the lines.

Ouput

figure 1

Code

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}

\pgfdeclarelayer{bg}
\pgfsetlayers{bg,main}
\begin{document}
\begin{tikzpicture}
\draw[clip, name path = A] (2,2) circle (3cm);
\draw[name path = B] (0.3,-0.5)..controls(2.55,2)..(0.3,4.5);
\draw[name path = C] (3.7,-0.5)..controls(1.5,2)..(3.7,4.5);
\draw (2,5)--(2,-1);
%\fillbetween[of=A and B];
%\draw[fill] (2,2) circle [radius=0.1];
\begin{pgfonlayer}{bg}
\fill [orange!50,
          intersection segments={
            of=A and B,
            sequence={L2--R2}
          }];
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
Alenanno
  • 37,338
4

With clipping, the calculation of the intersection points and the arc can be avoided. To limit the fill area and lines inside the circle also helps to remove the overshooting curve line parts.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \begin{scope}
    \clip (2,2) circle (3cm);
    \draw[fill=green]
      (current bounding box.south west) --
      (0.3,-0.5)..controls(2.55,2)..(0.3,4.5)
      -- (current bounding box.north west) -- cycle;
    \draw[] (3.7,-0.5)..controls(1.5,2)..(3.7,4.5); 
    \draw (2,5)--(2,-1);
    \draw[fill] (2,2) circle [radius=0.1];
  \end{scope}
  \draw (2,2) circle (3cm);
\end{tikzpicture}
\end{document}   

Result

Heiko Oberdiek
  • 271,626
3

Clipping and filldrawing:

enter image description here

The code:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture}
\draw[name path = A] (2,2) circle (3cm);
\path[name path = B] (0.3,-0.5)..controls(2.55,2)..(0.3,4.5);
\path[name path = C] (3.7,-0.5)..controls(1.5,2)..(3.7,4.5);

\draw (2,5)--(2,-1);
\path[name intersections={of=A and B,by={a,b}}];
\path[name intersections={of=A and C,by={c,d}}];
\begin{scope}
\clip (a) rectangle ([xshift=-2cm]b);
\filldraw[fill=green]
  (2,2) circle (3cm);
\end{scope}
\begin{scope}
\clip (a) rectangle ([xshift=2cm]b);
\filldraw[fill=green] 
  (0.3,-0.5)..controls(2.55,2)..(0.3,4.5);
\end{scope}
\begin{scope}
\clip (c) rectangle ([xshift=-2cm]d);
\draw 
  (3.7,-0.5)..controls(1.5,2)..(3.7,4.5);
\end{scope}
\draw[fill] (2,2) circle [radius=0.1];
\end{tikzpicture}

\end{document}

Initially, the curved paths are not draw, \paths are used to find the intersection points of the curves and the circle; then this points are used, together with clipping to perform actions (drawing, filling).

Gonzalo Medina
  • 505,128
  • Great! Thanks a lot for the answer! I was hoping that tikz would have simpler commands for such cases, but apparently it is not the case... – Fotis Jun 26 '15 at 14:47