# Drawing randomly ragged lines in technical drawings

I am having issues getting started on drawing a randomly ragged line, with finite variation, between two endpoints. The random function which uses variable rnd I think can be used, for instance, Drawing random paths in TikZ, but I have no idea how to use it for this purpose.

I use this environment:

\documentclass[12pt]{article} % SIZE OF FONT AND ITS LAYOUT ON EACH PAGE.
\usepackage[top = 1in, bottom = 1in, left = 1in, right = 1in]{geometry} % MARGINS.
\usepackage{amsmath, booktabs, graphicx, setspace}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz,tkz-graph,tikz-cd} % DIAGRAMS.
\usetikzlibrary{arrows, calc, decorations.markings}
\begin{document}

\begin{figure}[h]
\begin{center}
\begin{tikzpicture}[scale=1.8,auto]

% replace with randomly ragged line \draw [-,line width=1pt] (0,2) edge (2,2);
\draw [-,line width=1pt,dashed] (0,0) edge (0,2);% solid left boundary
\draw [-,line width=1pt,white] (0,0) edge (8,0);% solid lower boundary
\draw [-,line width=1pt,dashed] (8,0) edge (8,2);% solid right boundary
% make a vertical rule fill; how? How would I make a tiled rule fill, in case that looks better?

\end{tikzpicture}
\end{center}
\caption{CAPTION GOES HERE}
\label{fig:FIGURE NAME GOES HERE}
\end{figure}
\end{document}


I would like to re-create this:

EDIT: I think Jesse's example most produces the required result, but it cannot remove fill line segments that cross it, unlike the other two solutions.

How do I copy a generated random line after it is generated and change its color so that I have two parallel random lines? (Then the user can place one slightly above the other, but white and thick, but below it in code. This will crop all fill lines going beyond it manually.) I am trying TikZ copy command but this shifts the entire picture right and creates the copy of the random line beside it.

-
The first error that I see, is that the command \begin{document} is missed. –  OSjerick Nov 9 '13 at 3:14
... fixed; forgot it when I pasted this out of the document. –  Guido Jorg Nov 9 '13 at 20:10
Related: Simulating hand-drawn lines –  Werner Nov 9 '13 at 20:51

Another alternative, inspired by http://tex.stackexchange.com/a/126179/34618

Code

\documentclass[tikz,border=1cm]{standalone}
%\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand\irregularline[2]{%
let \n1 = {rand*(#1)} in
+(0,\n1)
\foreach \a in {0.1,0.2,...,#2}{
let \n1 = {rand*(#1)} in
-- +(\a,\n1)
}
}  % #1=seed, #2=length of horizontal line

\begin{document}
\begin{tikzpicture}
\draw[blue] (0,0) \irregularline{0.1cm}{6} -- (6,0)--(6,-2) --(0,-2) -- (0,0);
\foreach \t in {0,0.1,...,6} {\draw (\t,0) -- (\t,-2);}

\draw[red]  (0,2) \irregularline{2mm}{6};
\end{tikzpicture}
\end{document}


UPDATE: for vertical drawing. One simply switches the coordinates in the command line as displayed below and an example.

\newcommand\irregularliney[2]{%
let \n1 = {rand*(#1)} in
+(\n1,0)
\foreach \a in {0.1,0.2,...,#2}{
let \n1 = {rand*(#1)} in
-- +(\n1,\a)
}
}  % #1=seed, #2=length

\draw[yellow,thick] (0,0) \irregularliney{0.1cm}{6} -- (0,6)--(-2,6) --(-2,0) -- (0,0);

-
Can I make a similar command that connects two arbitrary points? Or at least, how do I make a similar command that is vertical? Can the line be made thicker? Also, how can I clip the fill? If I can copy the line once it is generated and turn it white and thick I could clip the fill manually by making a white thick line identical to the ragged line and shifting it up a few points. –  Guido Jorg Nov 10 '13 at 1:59
The easiest way is to switch the coordinates in (0,\n1) and (\a,\n1) in the command definition for vertical line. –  Jesse Nov 10 '13 at 2:11
@GuidoJorg -- Please see my update. –  Jesse Nov 10 '13 at 2:22
@GuidoJorg -- for line thickness. Use the option [line width=xxpt] or ultra thin, very thin, thin,..., semithick, thick, very thick, ultra thick. As to clipping, sorry, don't quite follow you. –  Jesse Nov 10 '13 at 2:58
@GuidoJorg -- Not sure if this is what you want, thus did not update my OP. I'll simply give a shot. Check this please. \begin{scope} \clip (0,2) rectangle(6,2.3); \draw (0,2) \irregularline{2mm}{6}; \end{scope} \foreach \t in {0,0.1,...,6} {\draw[red](\t,2) -- (\t,0);} –  Jesse Nov 11 '13 at 14:13

You can obtain similar result using the tikz library decorations.pathmorphing, that allows you to create decorations with radom steps. Also, using the clip option in path you can cut the vertical lines at the upper line of random form, but this should draw all boundary region of the clipping path, hence a horizontal line at bottom has been drawn, also vertical lines has been drawn at right and left but this goes with the drawing.

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}

\begin{document}
\begin{figure}\centering
\begin{tikzpicture}
\begin{scope}[gray,thick]
\draw (-0.1,-0.5) -- (-0.1,-1) (-0.1,-1.5) -- (-0.1,-2) (-0.1,-2.5) -- (-0.1,-3);
\draw (10.1,-0.5) -- (10.1,-1) (10.1,-1.5) -- (10.1,-2) (10.1,-2.5) -- (10.1,-3);
\end{scope}
\path[draw,clip,decoration={random steps,segment length=2pt,amplitude=2pt}] decorate {(0,0) -- (10,0)} |- (0,-3.5);
\foreach \x in {0,0.1,...,10} {
\draw (\x,1) -- (\x,-3.5);
}
\end{tikzpicture}
\end{figure}
\end{document}


Result:

-
Are there any more parameters that can control the shape of the random line? This works well but I am trying to go for a more sawtooth result? –  Guido Jorg Nov 9 '13 at 20:17
No, there are no additional parameters. The decoration should be random? You can use decoration={saw,segment length=3pt,amplitude=2pt} for a beautiful sawtooth. –  OSjerick Nov 9 '13 at 20:27
What about quasi-random, in sense that more spikes but they don't intersect? My idea is to compress the entire line once drawn by a multiple, but keep segment length and amplitude 2.7 and 2.3; can this be done? Also, how do I make the line thicker? –  Guido Jorg Nov 10 '13 at 2:02
To change the thickness in a clipping path you need use a preaction, so the command is: \path[clip,preaction={draw,very thick},decoration={random steps,segment length=2pt,amplitude=2pt}]. Maybe you're interested in this question: Custom decoration along curved paths. –  OSjerick Nov 11 '13 at 18:26

A recommended solution with PSTricks.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node,pst-plot}
\pstVerb{realtime srand}
\psset{linejoin=1}
\def\N{30}

\begin{document}
\begin{pspicture}(6,5)
\psline(6,0)
\curvepnodes[plotpoints=\N]{0}{6}{t 5 Rand 2 mul sub}{A}
\psnline[linecolor=red](0,\Anodecount){A}
\multido{\i=0+1}{\N}{\psline(A\i|0,0)(A\i)}
\end{pspicture}
\end{document}


## Animated

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node,pst-plot}
\pstVerb{realtime srand}
\psset{linejoin=1}
\def\N{30}

\begin{document}
\psLoop{10}{%
\begin{pspicture}(6,3)
\psline(6,0)
\curvepnodes[plotpoints=\N]{0}{6}{t 3 Rand 2 mul sub}{A}
\psnline[linecolor=red](0,\Anodecount){A}
\multido{\i=0+1}{\N}{\psline(A\i|0,0)(A\i)}
\end{pspicture}}
\end{document}


## Attention

Note that Rand no longer produces a random real number between 0 and 0.5 inclusive. Its definition had been tacitly changed. Now it produces a random real number between 0 and 1 inclusive. It is not documented, nor announced, but it is still fun!

The code given above has not been updated yet so it will produce different output. I have no time to update it right now. Sorry for this inconvenience.

-
Is there anyway to do exactly this in tikz? Or combine this with tikz to make a complicated graphic? –  Guido Jorg Nov 10 '13 at 1:55
@GuidoJorg: PSTricks without TikZ can make more complicated than this one. Merging PSTricks and TikZ in the same canvas might not be a good idea. –  kiss my armpit Nov 10 '13 at 5:06

I know is almost one year ago question, but I want share my solution. This is the code:

\documentclass[tikz,margin=5pt,12pt]{standalone}
\usepackage{xparse}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}

\NewDocumentCommand{\irregularline}{%
O     {2mm}   % Amplitude of irregularity. Optional. Default value = 2mm
m             % First point
m             % Second point
D   <> {20}   % Number of peaks. Optional. Default value = 20
}{{%
\coordinate (old) at #2;
\foreach \i in {1,2,...,#4}{
\draw (old) -- ($($#2!\i/(#4+1)!#3$) + (0,#1*rand)$) coordinate (old);
}
\draw (old) -- #3;
}}

% Examples
\irregularline{(0,0)}{(5,5)}
\irregularline[1mm]{(1,0)}{(6,5)}
\irregularline[1mm]{(2,0)}{(7,5)}<100>

\end{tikzpicture}
\end{document}


That produce the follow output:

-
Welcome to TeX.SX! –  Christian Hupfer Sep 4 '14 at 21:16