5

Consider the following MWE:

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
    \foreach \x in {1,1.2,...,3}
    {
        \pgfmathsetmacro{\a}{10*\x}
        \fill[blue!\a,shift={(-1,-2)}] (\x,-.3) rectangle (\x+1,2.3);
    }
        \fill[white] (0,0) .. controls (1,1) and (1.5,-1) .. (3,0) -- (3,.4) -- (0,.4) -- cycle;
        \fill[white] (0,-2) .. controls (1,-1) and (1.5,-3) .. (3,-2) -- (3,-2.3) -- (0,-2.3) -- cycle;
            \draw (0,0) .. controls (1,1) and (1.5,-1) .. (3,0);
            \draw (0,-2) .. controls (1,-1) and (1.5,-3) .. (3,-2);
        \begin{scope}[yshift=4cm]
            \foreach \x in {0,.1,...,.8}
            {
                \pgfmathsetmacro{\a}{\x*40}
                \fill[blue!\a] ($(0,0)!\x!(2,.5)$) -- ([yshift=-1cm]$(0,0)!\x!(2,.5)$) -- ([yshift=-1cm,xshift=.5cm]$(0,0)!\x!(2,.5)$) -- ([yshift=.11cm,xshift=.5cm]$(0,0)!\x!(2,.5)$);
            }
            \draw[shorten >=.1cm,thick] (0,0) -- (2,.5);
            \begin{scope}[shift={(0,-2)}]
            \foreach \x in {0,.1,...,.9}
            {
                \pgfmathsetmacro{\a}{\x*40}
                \pgfmathsetmacro{\b}{\x+.1}
                \fill[blue!\a] ($(0,0)!\x!(2,.5)$) -- ([yshift=-1cm]$(0,0)!\x!(2,.5)$) -- ($(0,-1)!\x!(2,-.5)$) -- ($(0,-1)!\b!(2,-.5)$) -- ($(0,0)!\b!(2,.5)$) -- cycle;
            }
            \draw[shorten >=.2cm,thick] (0,0) -- (2,.5);
            \end{scope}
        \end{scope}
    \end{tikzpicture}
\end{document}

As you can see in the above part (see the first image) I can \usetikzlibrary{calc} to achieve a convient coloring of the areas. But in the above part (see second image) I had to manually overlay areas which may not get "painted". I tried here the calc library, too, but it calulates the points just as the are at a straight line, not as a curve with some band angle(s). My question is: How can I use the calc library to have a more ellegant code for the second part?

Screenshot


Screenshot

  • 1
    +1 but why could you not just clip against the bounding curves and fill just rectangles that are large enough? – user121799 Aug 24 '18 at 17:24
  • @marmot: I've got such a version, too, I just use the "rectangle option" because I want to do something with it and thanks! ;) – current_user Aug 24 '18 at 17:26
  • 1
    In the lower part I would just do \fill[blue!\a] ($(0,0)!\x!(2,.5)$) -- ++ (0,-1) -- ++ ($(0,0)!0.1!(2,.5)$) -- ++(0,1) -- cycle;. I really think it is so much simpler with relative coordinates. – user121799 Aug 24 '18 at 17:50
6

Here another option, using nodes in a path, defining certain points in a path and drawing a line from them that changes the color.

RESULT:

enter image description here

MWE:

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
        %Iterative tricks
        \foreach \x in {1,2,...,30}{
            \path
            (1,0) 
                .. controls +(1,1) and +(-1,-1) .. ++(3,0) node[pos=\x/30](a\x){};
            \draw[line width = 4pt,blue!\x] (a\x.center) -- ++ (0,-2);
        }       
        \draw[line width = 1pt]
        (1,0) 
            .. controls +(1,1) and +(-1,-1) .. ++(3,0)
        (1,-2) 
            .. controls +(1,1) and +(-1,-1) .. ++(3,0);
        %Another path
        \foreach \x in {1,2,...,50}{
            \path
            (1,-3.5) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0) node[pos=\x/50](a\x){};
            \draw[line width = 3pt,blue!\x] (a\x.center) -- ++ (0,-2);
        }       
        \draw[line width = 1pt]
        (1,-3.5) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0)
        (1,-5.5) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0);

    \end{tikzpicture}
\end{document}

UPDATE:

Added another line shifted in preactión modifier and shorten the lines terminations to hide the rectangles; but is not good solution if you work with some backgrounds; best option is to work with clip, with shades, or even rectangles.

RESULT: enter image description here

MWE:

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{calc,patterns}
\begin{document}
    \begin{tikzpicture}[
        ShiftLine/.style={
            preaction={
                transform canvas={
                    shift={(#1)},
                },
                draw=white,
                line width=3pt,
            }
        }
    ]
    \fill [blue!30](0.5,-8) rectangle (12.5,0.5);
    \pattern [pattern=checkerboard,pattern color=blue!10](0.5,-8) rectangle (12.5,0.5);
        %Avoid use clip
        \foreach \x in {1,2,...,49}{
            \path
            (1,0) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0) node[pos=\x/50](a\x){};
            \draw[line width = 3pt,blue!\x,shorten <=-2pt, shorten >=-2pt] (a\x.center) -- ++ (0,-2);
        }       
        \draw[line width = 1pt,ShiftLine={0,2pt},shorten <=2pt, shorten >=2pt]
        (1,0) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0);
        \draw[line width = 1pt,ShiftLine={0,-2pt},shorten <=2pt, shorten >=2pt]
        (1,-2) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0);

        %using Clip and shades
        \begin{scope}
        \clip
        (1,-3.5) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0)
                -- ++ (0,-2)
                .. controls +(-1.5,-3) and +(1,0.5) .. ++(-4.5,0)
                -- cycle;
        \shade[right color=blue!50, left color=white]
        (1,-3) rectangle ++ (4.5,-4);
                \draw[line width = 2pt]
        (1,-3.5) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0);
        \draw[line width = 2pt]
        (1,-5.5) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0);
        \end{scope}

        %Using Rectangles in a foreach shifting.
        \begin{scope}[shift={(4,2)}]
        \clip
        (3,-3.5) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0)
                -- ++(0,-2)
                .. controls +(-1.5,-3) and +(1,0.5) .. ++(-4.5,0)
                -- cycle;
        \foreach \k in {0,1,...,50}{
            \fill[blue!\k]
            (3+\k*0.1,-3) rectangle ++(0.5,-4);
        }
        \draw[line width=2pt]
        (3,-3.5) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0);
        \draw[line width=2pt]
        (3,-5.5) 
                .. controls +(1,0.5) and +(-1.5,-3) .. ++(4.5,0);
        \end{scope}
        %Some labels
        \draw[font=\tiny,fill opacity=0.2,text opacity=1,align=center]
        (3.5,-1.8) node[anchor=center,fill=yellow]{\verb+Shift preaction white line  +\\ \verb+foreach lines from nodes in a path+}
        (3.5,-5.3) node[anchor=center,fill=yellow]{\verb+clip shape  +\\ \verb+and shades+}
        (9.5,-3.3) node[anchor=center,fill=yellow]{\verb+clip shape +\\ \verb+rectangles in \foreach shifting+};
    \end{tikzpicture}
\end{document}
  • +1 Nearly perfect! Just one little detail: Could you edit your answer, such that the rectangles are not visible outside the Bézier-curve (just clipping against it). Then you will geht the check-mark! – current_user Aug 24 '18 at 20:04
  • 1
    wait a bit, I think I can add other tricks xD – J Leon V. Aug 24 '18 at 20:08
4

I don't know if that's what you're asking but one can simplify the shaded contours quite a bit by working with relative coordinates. No manual shift is needed then.

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
            \begin{scope}[shift={(0,-2)}]
            \foreach \x in {0,.1,...,.9}
            {
               \pgfmathsetmacro{\a}{\x*40}        
                \fill[blue!\a] ($(0,0)!\x!(2,.5)$) -- 
                ++ (0,-1) -- ++ ($(0,0)!0.1!(2,.5)$) -- ++(0,1) -- cycle;
            }
            \draw[shorten >=.2cm,thick] (0,0) -- (2,.5);
            \end{scope}
    \end{tikzpicture}
\end{document}

enter image description here

  • Nearly, but that is really interessting. Yes, I should use relative coordinates more often … +1 – current_user Aug 24 '18 at 20:05
  • 1
    @current_user I simply do not understand the question, I think... ;-) – user121799 Aug 24 '18 at 21:59

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.