1

I looked up how to draw a random path in tikz here. I came up with the following:

\begin{tikzpicture}
  \pgfmathsetseed{2}

  \node[fill,circle,inner sep=0.5] (start) at (0,0) {};

  \draw (start.center)
    \foreach \i in {1,...,5} {
      -- ++(-rnd,rnd)
  }
\end{tikzpicture}

However, I would like to have arrow tips for every segment of the resulting path. Adding [->] to the draw operation only adds a tip at the final segment. Any ideas?

3

Like this?

enter image description here

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
  \pgfmathsetseed{2}

  \node[fill,circle,inner sep=0.5] (start) at (0,0) {};

    \foreach \i in {1,...,5} {
        \node (end) at ($(start)+(-rnd,rnd)$) {};
        \draw[->] (start.center) -- (end.center);
        \node (start) at (end) {};
    }
\end{tikzpicture}
\end{document}
  • I did not know you could redefine nodes within tikz. Awesome :) – hfhc2 Jan 5 '16 at 14:55
4

Simply push the \draw[->] command inside the loop and make the current end point be the next start point by adding coordinate(i) at the end of each \draw[->] path. The initial (i) is of course taken as (0,0).

\documentclass[tikz]{standalone}
\begin{document}

\begin{tikzpicture}
  \pgfmathsetseed{2}

  \node[fill,circle,inner sep=0.5] (start) at (0,0) {} coordinate (i);

  \foreach \i in {1,...,5} { 
    \draw[->] (i)  
      -- ++(-rnd,rnd) coordinate(i); 
  }
\end{tikzpicture}

\end{document}

enter image description here

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.