[Tex/LaTex] Draw curved lines simply

diagramspstrickstikz-pgf

How to draw nearly-arbitrary curved lines simply? For example, how to draw this graph:

Source Introduction to Algorithms (3rd edition) by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein | Page 45 | Figure 3.1

Now I have two ideas of how to draw such figures:

  1. Divide each curved line to many smaller curved lines which can be drawn by a TikZ command, for example

    \draw (0,0) to [in=80, out=-30] (3,2);
    

    This way is quite natural. However, it is difficult to divide, for instance, the lines in the above figure, in this way. Also, when I have already divided, it is very possible that I will get confused with the coordinates.

  2. Find a formula for each of the curved line, and then draw the graph of the curved line.

    This way is good for normal graphs, but for the complicated curved lines as in the above figure, it seems to be impossible.

So none of the ways stated is good enough. Do you have any idea to draw such figures? Your help is much appreciated!


Notes

  1. I'm sorry if the question is a duplicate. I have searched for a while without success.

  2. Your help will be great if you can help me draw the above figure. However, I strongly prefer a general solution for this, because the aim of the question is not just to ask how to draw the figure only.

  3. You can see that there is no MWE in my question. As I explained above, all of my idea so far are just bad, so I haven't applied any of them.

  4. I strongly prefer using TikZ. However, answers using PGF, PStricks, etc. are all very useful to me.

Best Answer

This is an attempt to collect some key methods on one page. Clearly, this discussion is not exhaustive, so I am hoping that this post gets complemented by others, who have other methods and/or opinions. (I made zero effort to precisely reproduce your curves, sorry.)

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{multicol}
\usepackage{tikz}
\usetikzlibrary{hobby}
\begin{document}
\section*{A few ideas to draw smooth curves}

\begin{multicols}{2}
\subsection*{Your idea: use \texttt{out} and \texttt{in}}
This works, and the result looks smooth if you make sure that the  \texttt{in}
of one point and the \texttt{out} of the next point differ by 180. In the
following pic, we have \texttt{\dots to[out=50,in=\textbf{210}] (1,2) circle(1pt)
to[\textbf{out=30},in=240] \dots} to illustrate this. Notice that the
\texttt{looseness} key can be of great help here.

\begin{tikzpicture}
\draw[latex-latex] (0,4) |- (6,0);
\draw[blue] (0,0) circle(1pt) to[out=50,in=210] (1,2) circle(1pt)
to[out=30,in=240,looseness=0.5] (3,3) circle(1pt);
\end{tikzpicture}

\subsection*{Your idea: find an analytic formula}
This is IMHO often the simplest way. One only needs to keep in mind a few basic
functions like $\sin$ and $\tanh$.

\begin{tikzpicture}
\draw[latex-latex] (0,4) |- (6,0);
\draw[blue] plot[variable=\x,domain=0:6,smooth]
({\x},{1+0.3*(tanh(3-\x)+1)*sin(180*\x)+0.2*\x});
\end{tikzpicture}

\subsection*{Use \texttt{plot[smooth] coordinates}}

A very convenient option, mentioned by Torj{\o}rn T.\ in the comments, is to use
\texttt{plot[smooth] coordinates}. You may want to play with the
\texttt{tension} key.

\begin{tikzpicture}
\draw[latex-latex] (0,4) |- (6,0);
\draw[blue] plot[smooth] coordinates
{(0,0) (1,2) (2,2) (3,3) (4,3)};
\draw[red] plot[smooth,tension=2] coordinates
{(0,0) (1,2) (2,2) (3,3) (4,3)};
\end{tikzpicture}

\subsection*{Use the \texttt{hobby} library}

The \texttt{hobby} library is sort of an el Dorado for smooth curve
constructors. It has way too many options to be listed here. One thing I find
very useful is the \texttt{tangent} style from the manual. 

\begin{tikzpicture}[tangent/.style={%
 in angle={(180+#1)},Hobby finish ,
designated Hobby path=next , out angle=#1,
}]
\draw[latex-latex] (0,4) |- (6,0);
\draw[blue] plot[smooth,hobby,tension=0.3] coordinates
{(0,0) (1,1.2) (2,2) (3,3) (4,3)};
\draw [red,use Hobby shortcut] 
   (0,0)  .. ([tangent=30]1.5,1) ..  ([tangent=-10]3,2) .. (5,1);
\end{tikzpicture}

I'd also like to mention that, when it comes to decorations, in my experience
the Hobby paths are advantageous. They often do not lead to \texttt{dimension too
large} errors when their almost identically looking \texttt{plot[smooth]}
counterparts do.

\subsection*{Not sure about \texttt{controls}}

There is also the possibility of using Bezier curves (without \texttt{hobby}).
Many users are able to use that to obtain great results. Unfortunately, I am not
one of those since IMHO the relation between position of the control points and
the outcome is not too obvious. But this is of course a very subjective
statement.
\end{multicols}
\end{document}

enter image description here