30

I am looking for a way to draw with TikZ something that looks like a random path.

Here is an inspiration:

example

Is it possible to have TikZ generate something that would be similar to a random path instead of manually choosing points to connect together?

Thanks!

P.S. This question falls into a more general question (that does not necessarily needs to be answered): is there a (meta-)way to use TikZ to draw diagrams as they would appear on a chalkboard during a class. I mean TikZ generates very professional looking diagrams, but it's hard to draw a "random" curve just to express some notion, or draw a "random" diagram or "random" example to explain a notion.

1
  • I guess that one of the problems with this is that things that look random often aren't! With the corollary that if something is truly random, it may not look "random" to Joe Reader (or Jo Seminar-Audience). Commented Feb 22, 2011 at 9:19

3 Answers 3

18

This is an example taken directly from the pgf manual:

 \pgfmathsetseed{1}
 \foreach \col in {black,red,green,blue}
 {
   \begin{tikzpicture}[x=10pt,y=10pt,ultra thick,baseline,line cap=round]
     \coordinate (current point) at (0,0);
     \coordinate (old velocity) at (0,0);
     \coordinate (new velocity) at (rand,rand);
     \foreach \i in {0,1,...,100}
     {
       \draw[\col!\i] (current point) 
       .. controls ++([scale=-1]old velocity) and
                    ++(new velocity) .. ++(rand,rand)
          coordinate (current point);
       \coordinate (old velocity) at (new velocity);
       \coordinate (new velocity) at (rand,rand);
     }
   \end{tikzpicture}
 }

It is at the beginning of the Part VI: Mathematical and Object Oriented Engines. The whole section on mathematical engine is what you want to read if you want to do random drawing in tikz.

1
  • Thanks for referencing, I forgot to look it up first. :-S Commented Feb 21, 2011 at 23:58
21

A few weeks back, I was attending a course on polymers, so I threw together a quick package to draw randomwalks. For a path that looks like the one you describe, the parameters below should be ok:

\documentclass{article}
\usepackage{randomwalk}
\begin{document}
 \framebox{\RandomWalk {number = 300, length = 2pt, 
     angles = {0,10,20,50,-10,-20,-50}, degree, angles-relative}}
\end{document}

The output of the code above.

If you need additionnal features, I can try to add them.

5
  • Looks very interesting. Can you add an image? (I don't want to bother installing your package at the moment.) Commented Feb 22, 2011 at 8:15
  • @Hendrik: I just uploaded an example of the result of the code above. Commented Feb 22, 2011 at 9:07
  • This is great. I don't need to use this package, however an idea for something you could add would be depth-cued 3D random walks - that is, vary the x and y coordinates by their position in the figure, and cue z by varying the opacity / white balance of the line as if it's fading in or out of distance fog. Commented Feb 22, 2011 at 10:31
  • @eutactic: I'll think about that. But I need to learn a bit more TikZ first ;-). Commented Feb 22, 2011 at 19:31
  • I just happened across a gallery of 3D random walks in various styles achieved with Asymptote. You may find them interesting. Commented Feb 23, 2011 at 2:27
17

You can use the random function, which generates a pseudo-random number between 0 and 1, and use it to define the direction and distance of line segments (or any other parameter of your drawing). The seed can be set using \pgfmathsetseed. You can achieve something quite akin to the drawing you linked to using the [round corners] style in the \draw command:

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\pgfmathsetseed{2}
\begin{tikzpicture}
\draw [rounded corners] (0,0)
  \foreach \i in {1,...,300} {
    -- ++(rnd*360:rnd)
};
\end{tikzpicture}
\end{document}

random smoothed walk

I don't know about the "natural-looking" drawings, though. I think that's a very difficult task for any programmatic drawing program, because you'd need to set quite a few constraints to stop the picture from going totally random.

8
  • Would there be a reason why it tells me "Package PGF Math Error: Unknown function 'random'" ? Commented Feb 22, 2011 at 0:59
  • 2
    Should random not be rand? Commented Feb 22, 2011 at 1:06
  • 1
    @Matthew: rand is between -1 and 1, random is between 0 and 1, but maybe that's a function that's been added in one of the more recent versions of TikZ.
    – Jake
    Commented Feb 22, 2011 at 7:36
  • @Alex: random might be a function that's not included in your version of TikZ. rand should work in this case as well.
    – Jake
    Commented Feb 22, 2011 at 7:59
  • @Alex: in my manual (2.10) it documents the usage of rnd or random() to get a pseudo-random number between 0 and 1, but not random by itself. I wondered if that were the cause of Alex's error. Commented Feb 22, 2011 at 13:11

You must log in to answer this question.

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