1

I use the following tikz code as a part of a more complicated drawing with other curves using the defined coordinates.

How can I fill the area between the intersection of these compound curves?

I have some codes to fill intersection between 2 circles, but they do not apply for non-closed curves.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{trees, overlay-beamer-styles, arrows, backgrounds, positioning, calc, intersections}
\begin{document}
\begin{center}
\tikzset{lw/.style={line width=.04cm}}
\begin{tikzpicture}[scale=.8, transform shape]
\coordinate (uplt) at (0,0);
\coordinate (strt) at ([shift={(-32.:4.2cm)}]uplt);
\coordinate (strtd) at ([shift={(-120.:.36cm)}]strt);
\coordinate (strtdd) at ([shift={(-148.:1.cm)}]strt);
%
\draw [very thick, blue, looseness=.88] (strtd.center) to [out=140,in=-70] node [pos=.71, circle,black, fill=black, scale=0.8](b){} +(125.:2.6cm) (strtd.center) to [out=-40,in=170] +(-24.:4.cm);
%
\draw [very thick, blue, looseness=.88] (strtdd.center) to [out=140,in=-70] node [pos=1., circle,black, fill=black, scale=0.8](a){} +(125.:2.6cm) (strtdd.center) to [out=-40,in=170] +(-24.:4.4cm);
\begin{scope}[on background layer]
\draw [lw, red, looseness=.8] ([shift={(45:.4cm)}]strt.center) to [out=140,in=-6] +(160.:3.2cm) ([shift={(45:.4cm)}]strt.center) to [out=-40,in=94] +(-65.:4.2cm);
\draw [lw, red, looseness=.8] ([shift={(36:.86cm)}]strt.center) to [out=140,in=-6] +(160.:3.2cm) ([shift={(36:.86cm)}]strt.center) to [out=-40,in=94] +(-65.:4.2cm);
\end{scope}
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

enter image description here

2
  • Have you had a look at this: Filling area between two curves in tikz
    – Werner
    May 23 at 17:46
  • @ Werner Thank you for your comment; but this does not answer my post. This answer fills an area between 2 curves. I need to fill only the area between the intersections of the curves.
    – Hany
    May 27 at 4:08

1 Answer 1

1

You could use the intersections library to precisely position the nodes and the fillbetween library that comes with PGFPlots to fill the area between the paths. (If the latter is loaded, you don't even need to additionally load the former.)

For this to work, however, it is better to draw the paths in one go, not as two sub-paths as you do, but this can easily be achieved by reordering some values in your code.

By the way, you don't need to add center as anchor to a coordinate since it has no shape.

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots} % loads tikz
\pgfplotsset{compat=newest}
\usetikzlibrary{backgrounds}
\usepgfplotslibrary{fillbetween} % includes intersections library

\tikzset{lw/.style={line width=.04cm}}

\begin{document}
\begin{tikzpicture}[scale=.8, transform shape]

\coordinate (uplt) at (0,0);
\coordinate (strt) at ([shift={(-32:4.2)}]uplt);
\coordinate (strtd) at ([shift={(-120:.36)}]strt);
\coordinate (strtdd) at ([shift={(-148:1)}]strt);

\draw[very thick, blue, looseness=.88, name path=curveA] 
    (strtd) +(125:2.6) to[out=-70, in=140] 
    (strtd) to[out=-40, in=170] +(-24:4);
\draw[very thick, blue, looseness=.88] 
    (strtdd) +(125:2.6) to [out=-70, in=140] 
    (strtdd) to[out=-40, in=170] +(-24:4.4);

\draw[lw, red, looseness=.8, name path=curveB] 
    ([shift={(45:.4)}]strt) +(160:3.2) to[out=-6, in=140] 
    ([shift={(45:.4)}]strt) to[out=-40, in=94] +(-65:4.2);
\draw[lw, red, looseness=.8] 
    ([shift={(36:.86)}]strt) +(160:3.2) to[out=-6, in=140] 
    ([shift={(36:.86)}]strt) to[out=-40, in=94] +(-65:4.2);

\path[name intersections={of=curveA and curveB}]
    (intersection-1) node[circle, black, fill=black, scale=0.8] (a) {}
    (intersection-2) node[circle, black, fill=black, scale=0.8] (b) {};

\begin{scope}[on background layer]
    \fill[green, intersection segments={of=curveA and curveB, sequence={R2 -- L2}}] 
    -- cycle;
\end{scope}

\end{tikzpicture}
\end{document}

enter image description here


Filling the area between the other two curves needs a small tweak. Note that they seem not to intersect at the upper left coordinate which is why you cannot place a node using intersections there. Maybe you need to adjust the curves a bit.

If you wonder how to use the intersection segments and sequence options, have a look at the PGFPlots manual. Chapter 5.7. (in version 1.18.1 of the manual) discusses the fillbetween library.

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{backgrounds}
\usepgfplotslibrary{fillbetween}

\tikzset{lw/.style={line width=.04cm}}

\begin{document}
\begin{tikzpicture}[scale=.8, transform shape]
\coordinate (uplt) at (0,0);
\coordinate (strt) at ([shift={(-32:4.2)}]uplt);
\coordinate (strtd) at ([shift={(-120:.36)}]strt);
\coordinate (strtdd) at ([shift={(-148:1)}]strt);

\draw[very thick, blue, looseness=.88, name path=curveA] 
    (strtd) +(125:2.6) to[out=-70, in=140] 
    (strtd) to[out=-40, in=170] +(-24:4);
\draw[very thick, blue, looseness=.88, name path=curveB] 
    (strtdd.center) +(125:2.6) to [out=-70, in=140] 
    (strtdd.center) to[out=-40, in=170] +(-24:4.4);

\draw[lw, red, looseness=.8, name path=curveC] 
    ([shift={(45:.4)}]strt.center) +(160:3.2) to[out=-6, in=140] 
    ([shift={(45:.4)}]strt.center) to[out=-40, in=94] +(-65:4.2);
\draw[lw, red, looseness=.8, name path=curveD] 
    ([shift={(36:.86)}]strt.center) +(160:3.2) to[out=-6, in=140] 
    ([shift={(36:.86)}]strt.center) to[out=-40, in=94] +(-65:4.2);

\path[name intersections={of=curveA and curveC}]
    (intersection-1) node[circle, black, fill=black, scale=0.8] (a) {}
    (intersection-2) node[circle, black, fill=black, scale=0.8] (b) {};

\begin{scope}[on background layer]
    \fill[yellow, intersection segments={of=curveB and curveD, sequence={R1 -- L1[reverse]}}] -- cycle;
    
    \fill[green, intersection segments={of=curveA and curveC, sequence={R2 -- L2}}] -- cycle;
\end{scope}

\end{tikzpicture}
\end{document}

enter image description here

2
  • @ Jasper Habicht Thank you very much for your answer. Just one more request. I tried to apply your code to fill the area in this picture (between the other 2 curves, instead of the area in my post) i.stack.imgur.com/50hNn.jpg , to make another drawing; but I failed to edit your code. Would you please update your answer to include this addition.
    – Hany
    May 24 at 4:07
  • @ Jasper Habicht Thank you very much for your answer update. I appreciate it very much.
    – Hany
    May 24 at 11:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .