5

I would like to be able to draw, using TikZ, a picture as the one below, which consists of:

  • two vertical line segments
  • a wavy line going from the top point of the line segment on the left to the top point of the line segment on the right;
  • a wavy line going from the bottom point of the line segment on the left to the bottom point of the line segment on the right.

Furthermore, I want to be able to fill this region with some color.

enter image description here

I did it using this code:

\begin{tikzpicture}
  \filldraw[fill=cyan,domain=0:2,samples=100]
  plot(\x,{(\x)^3-3*(\x)^2+2*\x})
  --plot({2-\x},{(2-\x)/(1+(2-\x)^2)-2})--cycle;
\end{tikzpicture}

What bothers me was the need of finding two concrete curves in order to obtain the wavy lines. I think that I should be able to get them using something similar to the rounded corners option of the draw command. Is it possible?

1
  • Always post compilable code, when possible. Commented Jun 2, 2021 at 20:27

2 Answers 2

6

I find it easier to do a smooth plot with a couple of coordinates instead of finding control points. -also no need to clip or adjust bounding box.

\documentclass[tikz, border=1mm]{standalone}
\begin{document}
\begin{tikzpicture}
 \draw[fill=cyan] plot [smooth, tension=1] coordinates {(0,0) (0.6,0.4) (1.4,-0.4) (2,0)} -- plot [smooth, tension=1] coordinates {(2,-1.5) (0.8,-1.4) (0,-2)} -- cycle;
\end{tikzpicture}
\end{document}

Filled area

9

Do it with Bezier curves:
wavy fill with Bezier

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
    \begin{tikzpicture}
        \clip (0,.5) rectangle (2,-2);
        \draw[fill=cyan] (0,0) .. controls ++(1,1.5) and ++(-1,-1.5).. (2,0) -- (2,-1.5) .. controls ++(-1,.2) and ++(.5,.75) .. (0,-2) --cycle;
    \end{tikzpicture}
\end{document}

EDIT: a bit of explanation

While you can look for what Bezier curves are (there are an incredible amount of example on TeX-SE and elsewhere, and of course, you can find them in the manual), here are some explanations:
Pierre Bézier was a French engineer who worked for Renault cars and created these parametrical curves.
In TikZ, you can draw cubic Bezier curves, which allow you to use a start point, an end point, and two other points that conduct the curve. There's some magical maths in here ;) but let's see what it gives graphically:

Bezier points

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
    \begin{tikzpicture}[lin/.style={red,line width=0.1pt},dot/.style={circle,inner sep=0.5pt,fill=red}]
        \draw[fill=cyan!50]
            (0,0) .. controls ++(1,1.5)
            and ++(-1,-1.5).. (2,0) --

            (2,-1.5) .. controls ++(-1,.2) 
            and ++(.5,.75) .. (0,-2) 

            --cycle;

        \draw[lin] (0,0) --++ (1,1.5) node[dot]{};
        \draw[lin] (2,0) --++ (-1,-1.5) node[dot]{};
        \draw[lin] (2,-1.5) --++ (-1,.2) node[dot]{};
        \draw[lin] (0,-2) --++ (.5,.75) node[dot]{};

    \end{tikzpicture}
\end{document}

Here in the code, I added what you need to understand (a bit) how it works. This is the same that you do when using softwares like Illustrator (SVG files).

3
  • I know nothing about Bezier curves (in TikZ), but this is just what I wanted. Thank you. Commented Jun 2, 2021 at 18:12
  • @JoséCarlosSantos I edited my answer to give some explanations (you may find more on the Internet if you're interested)
    – SebGlav
    Commented Jun 2, 2021 at 19:06
  • 1
    Thank you, but there was no need. I went straight to the TikZ manual. Commented Jun 2, 2021 at 19:35

You must log in to answer this question.

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