Fill the area between two curves









up vote
10
down vote

favorite
4












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 hereenter image description here



Your help is much appreciated. A MWE is as follows



documentclass[margin=10pt]standalone
usepackagetikz
usepackagepgfplots
pgfplotssetcompat=newest
usepgfplotslibraryfillbetween
usetikzlibraryintersections


begindocument
begintikzpicture%[x=1cm, y=1cm]
defrsmall2
defrlarge4
defalpha60
defbeta40
defgamma20
defAoutalpha+gamma-90
defBin270-beta-gamma

pgfmathsetmacroax-rsmall+cos(alpha)*rsmall
pgfmathsetmacroaysin(alpha)*rsmall
pgfmathsetmacrobxrlarge-cos(beta)*rlarge
pgfmathsetmacrobysin(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];


endtikzpicture
enddocument









share|improve this question

























    up vote
    10
    down vote

    favorite
    4












    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 hereenter image description here



    Your help is much appreciated. A MWE is as follows



    documentclass[margin=10pt]standalone
    usepackagetikz
    usepackagepgfplots
    pgfplotssetcompat=newest
    usepgfplotslibraryfillbetween
    usetikzlibraryintersections


    begindocument
    begintikzpicture%[x=1cm, y=1cm]
    defrsmall2
    defrlarge4
    defalpha60
    defbeta40
    defgamma20
    defAoutalpha+gamma-90
    defBin270-beta-gamma

    pgfmathsetmacroax-rsmall+cos(alpha)*rsmall
    pgfmathsetmacroaysin(alpha)*rsmall
    pgfmathsetmacrobxrlarge-cos(beta)*rlarge
    pgfmathsetmacrobysin(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];


    endtikzpicture
    enddocument









    share|improve this question























      up vote
      10
      down vote

      favorite
      4









      up vote
      10
      down vote

      favorite
      4






      4





      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 hereenter image description here



      Your help is much appreciated. A MWE is as follows



      documentclass[margin=10pt]standalone
      usepackagetikz
      usepackagepgfplots
      pgfplotssetcompat=newest
      usepgfplotslibraryfillbetween
      usetikzlibraryintersections


      begindocument
      begintikzpicture%[x=1cm, y=1cm]
      defrsmall2
      defrlarge4
      defalpha60
      defbeta40
      defgamma20
      defAoutalpha+gamma-90
      defBin270-beta-gamma

      pgfmathsetmacroax-rsmall+cos(alpha)*rsmall
      pgfmathsetmacroaysin(alpha)*rsmall
      pgfmathsetmacrobxrlarge-cos(beta)*rlarge
      pgfmathsetmacrobysin(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];


      endtikzpicture
      enddocument









      share|improve this question













      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 hereenter image description here



      Your help is much appreciated. A MWE is as follows



      documentclass[margin=10pt]standalone
      usepackagetikz
      usepackagepgfplots
      pgfplotssetcompat=newest
      usepgfplotslibraryfillbetween
      usetikzlibraryintersections


      begindocument
      begintikzpicture%[x=1cm, y=1cm]
      defrsmall2
      defrlarge4
      defalpha60
      defbeta40
      defgamma20
      defAoutalpha+gamma-90
      defBin270-beta-gamma

      pgfmathsetmacroax-rsmall+cos(alpha)*rsmall
      pgfmathsetmacroaysin(alpha)*rsmall
      pgfmathsetmacrobxrlarge-cos(beta)*rlarge
      pgfmathsetmacrobysin(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];


      endtikzpicture
      enddocument






      tikz-pgf fillbetween






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 23 at 3:28









      noir

      43538




      43538




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          14
          down vote



          accepted










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



          documentclass[margin=10pt]standalone
          usepackagetikz
          usepackagepgfplots
          pgfplotssetcompat=newest
          usepgfplotslibraryfillbetween
          usetikzlibraryintersections


          begindocument
          begintikzpicture%[x=1cm, y=1cm]
          defrsmall2
          defrlarge4
          defalpha60
          defbeta40
          defgamma20
          defAoutalpha+gamma-90
          defBin270-beta-gamma

          pgfmathsetmacroax-rsmall+cos(alpha)*rsmall
          pgfmathsetmacroaysin(alpha)*rsmall
          pgfmathsetmacrobxrlarge-cos(beta)*rlarge
          pgfmathsetmacrobysin(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]];


          endtikzpicture
          enddocument


          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
          usepackagetikz
          usetikzlibrarycalc


          begindocument
          begintikzpicture%[x=1cm, y=1cm]
          defrsmall2
          defrlarge4
          defmyalpha60
          defmybeta40
          defgamma20
          defAoutmyalpha+gamma-90
          defBin270-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;

          endtikzpicture
          enddocument


          enter image description here






          share|improve this answer






















            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "85"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f447265%2ffill-the-area-between-two-curves%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            14
            down vote



            accepted










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



            documentclass[margin=10pt]standalone
            usepackagetikz
            usepackagepgfplots
            pgfplotssetcompat=newest
            usepgfplotslibraryfillbetween
            usetikzlibraryintersections


            begindocument
            begintikzpicture%[x=1cm, y=1cm]
            defrsmall2
            defrlarge4
            defalpha60
            defbeta40
            defgamma20
            defAoutalpha+gamma-90
            defBin270-beta-gamma

            pgfmathsetmacroax-rsmall+cos(alpha)*rsmall
            pgfmathsetmacroaysin(alpha)*rsmall
            pgfmathsetmacrobxrlarge-cos(beta)*rlarge
            pgfmathsetmacrobysin(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]];


            endtikzpicture
            enddocument


            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
            usepackagetikz
            usetikzlibrarycalc


            begindocument
            begintikzpicture%[x=1cm, y=1cm]
            defrsmall2
            defrlarge4
            defmyalpha60
            defmybeta40
            defgamma20
            defAoutmyalpha+gamma-90
            defBin270-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;

            endtikzpicture
            enddocument


            enter image description here






            share|improve this answer


























              up vote
              14
              down vote



              accepted










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



              documentclass[margin=10pt]standalone
              usepackagetikz
              usepackagepgfplots
              pgfplotssetcompat=newest
              usepgfplotslibraryfillbetween
              usetikzlibraryintersections


              begindocument
              begintikzpicture%[x=1cm, y=1cm]
              defrsmall2
              defrlarge4
              defalpha60
              defbeta40
              defgamma20
              defAoutalpha+gamma-90
              defBin270-beta-gamma

              pgfmathsetmacroax-rsmall+cos(alpha)*rsmall
              pgfmathsetmacroaysin(alpha)*rsmall
              pgfmathsetmacrobxrlarge-cos(beta)*rlarge
              pgfmathsetmacrobysin(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]];


              endtikzpicture
              enddocument


              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
              usepackagetikz
              usetikzlibrarycalc


              begindocument
              begintikzpicture%[x=1cm, y=1cm]
              defrsmall2
              defrlarge4
              defmyalpha60
              defmybeta40
              defgamma20
              defAoutmyalpha+gamma-90
              defBin270-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;

              endtikzpicture
              enddocument


              enter image description here






              share|improve this answer
























                up vote
                14
                down vote



                accepted







                up vote
                14
                down vote



                accepted






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



                documentclass[margin=10pt]standalone
                usepackagetikz
                usepackagepgfplots
                pgfplotssetcompat=newest
                usepgfplotslibraryfillbetween
                usetikzlibraryintersections


                begindocument
                begintikzpicture%[x=1cm, y=1cm]
                defrsmall2
                defrlarge4
                defalpha60
                defbeta40
                defgamma20
                defAoutalpha+gamma-90
                defBin270-beta-gamma

                pgfmathsetmacroax-rsmall+cos(alpha)*rsmall
                pgfmathsetmacroaysin(alpha)*rsmall
                pgfmathsetmacrobxrlarge-cos(beta)*rlarge
                pgfmathsetmacrobysin(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]];


                endtikzpicture
                enddocument


                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
                usepackagetikz
                usetikzlibrarycalc


                begindocument
                begintikzpicture%[x=1cm, y=1cm]
                defrsmall2
                defrlarge4
                defmyalpha60
                defmybeta40
                defgamma20
                defAoutmyalpha+gamma-90
                defBin270-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;

                endtikzpicture
                enddocument


                enter image description here






                share|improve this answer














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



                documentclass[margin=10pt]standalone
                usepackagetikz
                usepackagepgfplots
                pgfplotssetcompat=newest
                usepgfplotslibraryfillbetween
                usetikzlibraryintersections


                begindocument
                begintikzpicture%[x=1cm, y=1cm]
                defrsmall2
                defrlarge4
                defalpha60
                defbeta40
                defgamma20
                defAoutalpha+gamma-90
                defBin270-beta-gamma

                pgfmathsetmacroax-rsmall+cos(alpha)*rsmall
                pgfmathsetmacroaysin(alpha)*rsmall
                pgfmathsetmacrobxrlarge-cos(beta)*rlarge
                pgfmathsetmacrobysin(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]];


                endtikzpicture
                enddocument


                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
                usepackagetikz
                usetikzlibrarycalc


                begindocument
                begintikzpicture%[x=1cm, y=1cm]
                defrsmall2
                defrlarge4
                defmyalpha60
                defmybeta40
                defgamma20
                defAoutmyalpha+gamma-90
                defBin270-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;

                endtikzpicture
                enddocument


                enter image description here







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 23 at 4:03

























                answered Aug 23 at 3:36









                marmot

                77.4k487163




                77.4k487163



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f447265%2ffill-the-area-between-two-curves%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                    How do I collapse sections of code in Visual Studio Code for Windows?

                    Node.js puppeteer - Use values from array in a loop to cycle through pages