# Combine tikz curve with random variation

I would like to combine a curve defined with \draw with a random variation along the path.

So far I have found quite some interesting discussions on tex.SE like How to draw Brownian motions in tikz/pgf, How to fix the trajectory of Brownian motions which generated by the "rand" function with tikz in beamer frames, and (thanks to Kpym, I have overseen this) Simulating hand-drawn lines, but they do not work as expected.

Using tikz-decorations looks promising, but only for relatively straight curves.

The following MWE gives this result:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}

\begin{document}
\begin{tikzpicture}[x=5mm,y=5mm,decoration={random steps,segment length=3mm,amplitude=1mm}]
\draw[thick,green] (0, 1) -- (14.5, 1);
\draw[thick,red,decorate,rounded corners]   (0,-0.5) -- (14.5,-0.5);
\draw[thick,blue,decorate,rounded corners] (0, 0.5) -- (1,0.5) -- (1.5,-4) -- (2,-3.5) -- (3.5,-2) -- (14.5, 0);
\end{tikzpicture}
\end{document}


The third (blue) \draw command produces not the result that I try to get - even the red line has a "hickup" at about 75% of the way in x direction.

According to the tikz-manual the reason is: The end of each step is perturbed both in x- and in y-direction by two values drawn uniformly from the interval [−d,d], where d is the value of amplitude. This conflicts with the curved option where variing only the y value should give the desired output. Since the plots describe the change of a value in time they may not turn towards the negative x direction.

Is there any way to restrict the random steps decoration to manipulate only the x value but not y? The alternative would be to overlay a curve by a random distortion at descrete intervals like brownian motions...

Hoping the results are somehow stable (which conflicts with the idea of random shifts - lol) there's no real need to control seeds...

• How about \draw (0,0) \foreach \x in {1,...,10} { -- ++(0.1,rand*0.1) }; from tex.stackexchange.com/a/59927/121799 ? Or could you try to make your question a bit clearer? Your code has rounded corners, which is the reason for the "hick-ups" but you mention these not in your question. – user121799 Mar 2 '19 at 22:45
• @marmot \draw (0,0) \foreach \x in {1,...,10} { -- ++(0.1,rand*0.1) } will give me a basically straight line, right? So this would result in loosing control over the general shape. Working for the red line, but not for the blue. The rounded corners are used to make the line smoother... Without them the interval of variation has to be reduced which makes the line too "waggy"... – AndiW Mar 2 '19 at 23:12
• rounded corners and smooth are two very different things You can draw a smooth curve but this is very different from rounded corners where you get the same radius (unless you make additional efforts). Anyway, I added an answer that should answer "Is there any way to restrict the random steps decoration to manipulate only the x value but not y?". If that's not what you want, it would be great if you could clarify your question. – user121799 Mar 2 '19 at 23:24

This is an answer to the question: can one have random steps in the y direction? The answer is yes, all one needs to do is to copy the definition of random steps and to set the x shift to zero.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\pgfdeclaredecoration{random y steps}{start}
{%
\state{start}[width=+0pt,next state=step,persistent precomputation=\pgfdecoratepathhascornerstrue]{}%
\state{step}[auto end on length=1.5\pgfdecorationsegmentlength,
auto corner on length=1.5\pgfdecorationsegmentlength,
width=+\pgfdecorationsegmentlength]
{
\pgfpathlineto{
{\pgfpoint{\pgfdecorationsegmentlength}{0pt}}
{\pgfpoint{0pt}{rand*\pgfdecorationsegmentamplitude}}
}
}%
\state{final}
{}%
}%

\begin{document}
\begin{tikzpicture}[x=5mm,y=5mm,decoration={random y steps,segment length=3mm,amplitude=1mm}]
\draw[thick,green] (0, 1) -- (14.5, 1);
\draw[thick,red,decorate,rounded corners]   (0,-0.5) -- (14.5,-0.5);
\draw[thick,blue,decorate,rounded corners] (0, 0.5) -- (1,0.5) -- (1.5,-4) -- (2,-3.5) -- (3.5,-2) -- (14.5, 0);
\end{tikzpicture}
\end{document}


Of course one can draw a smooth random curve. At this point, this has to go in two steps.

1. \path[decorate] <path>;
2. \draw[<options>] plot[variable=\x,samples at={1,...,\arabic{randymark}},smooth] (randymark\x);

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\newcounter{randymark}
\pgfdeclaredecoration{mark random y steps}{start}
{%
\state{start}[width=+0pt,next state=step,%
persistent precomputation={\pgfdecoratepathhascornerstrue%
\setcounter{randymark}{0}}]{
\stepcounter{randymark}
\pgfcoordinate{randymark\arabic{randymark}}{\pgfpoint{0pt}{0pt}}
}%
\state{step}[auto end on length=1.5\pgfdecorationsegmentlength,
auto corner on length=1.5\pgfdecorationsegmentlength,
width=+\pgfdecorationsegmentlength]
{ \stepcounter{randymark}
\pgfcoordinate{randymark\arabic{randymark}}{\pgfpoint{\pgfdecorationsegmentlength}{rand*\pgfdecorationsegmentamplitude}}
}%
\state{final}
{
\stepcounter{randymark}
\pgfcoordinate{randymark\arabic{randymark}}{\pgfpointdecoratedpathlast}}%
}%

\begin{document}
\begin{tikzpicture}[x=5mm,y=5mm,decoration={mark random y steps,segment length=3mm,amplitude=1mm}]
\path[decorate]   (0,-0.5) -- (14.5,-0.5);
\draw[red,thick] plot[variable=\x,samples at={1,...,\arabic{randymark}},smooth]
(randymark\x);
\path[decorate] (0, 0.5) -- (1,0.5) -- (1.5,-4) -- (2,-3.5) -- (3.5,-2) -- (14.5, 0);
\draw[blue,thick] plot[variable=\x,samples at={1,...,\arabic{randymark}},smooth]
(randymark\x);
\path[decorate] (4,4) circle(3cm);
\draw[orange,thick] plot[variable=\x,samples at={1,...,\arabic{randymark}},smooth]
(randymark\x);
\end{tikzpicture}
\end{document}


Notice that this is certainly not the first post that draws a smooth random curve through a path, there are several earlier posts, including this one, this one, the answers to this question and the answers to this question.

• May be you can make reference to this questions : TikZ two-blocks matrix, Simulating hand-drawn lines and Create xkcd style diagram in TeX ? – Kpym Mar 3 '19 at 8:12
• @marmot the crazy thing about marmots is that they come up with solutions to questions that i can not make clearer at the moment. I'd like to vote +3 and will accept the answer even if the result is not exactly what I need. But since I'm no 'do-it-for-me' I'll go through the decorations engine to enhance both your answer to fit my needs and myself :-) Helped a lot as starting point. Thx! – AndiW Mar 3 '19 at 8:44
• @Kpym Yep, thanks for the hint. – AndiW Mar 3 '19 at 8:54
• @Kpym Absolutely. Thanks! I added a few references. If you give me a hint why tex.stackexchange.com/a/74881 is to be added, I will be happy to add it, too. – user121799 Mar 3 '19 at 15:24