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

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}
  • you can use fillbetween as a TikZ library too. – percusse Jun 26 '15 at 15:06
  • @percusse Oh. I tried but I get the error Package pgf Error: Please load pgfplots before pgfplots.fillbetween.. Is the library spelled the same way? – Alenanno Jun 26 '15 at 15:07
  • Thyere are some imperfections: the curved path extends beyond the circle at the intersection points. – Gonzalo Medina Jun 26 '15 at 15:17
  • @GonzaloMedina Yep, that's from the OP's code though. – Alenanno Jun 26 '15 at 15:32
  • @GonzaloMedina Fixed. :) – Alenanno Jun 26 '15 at 15:37
2

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).

  • 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
1

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

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.