3

I want to create two irregular lines, one on top of each other, that follow the same pattern.

Following an answer from @vialrogo on the post Drawing randomly ragged lines in technical drawings I got this far, but I need that the lines are identical, irregular and appear one on top of each other.

Is there a way I can exactly duplicate random lines? Or is there any other code that will allow me to generate two irregular lines that will be equal but appear on top of each other?

\documentclass[]{standalone}
\usepackage{tikz} % To plot almost everything.
\usetikzlibrary{fit, calc, matrix, positioning, arrows.meta, intersections, through, backgrounds, patterns}
\usepackage{xparse}

\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;
}}

\irregularline[1mm]{(0,0)}{(5,0)}
\irregularline[1mm]{(0,1)}{(5,1)}
\irregularline[1mm]{(0,1.5)}{(5,1.5)}
\end{tikzpicture}

\end{document}
3

Add \pgfmathsetseed{42} (or with some other number of your choosing) just before the loop in the definition of \irregularline.

output of cod

\documentclass[]{standalone}
\usepackage{tikz} % To plot almost everything.
\usetikzlibrary{fit, calc, matrix, positioning, arrows.meta, intersections, through, backgrounds, patterns}
\usepackage{xparse}

\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;
  \pgfmathsetseed{42} % <-- added
  \foreach \i in {1,2,...,#4}{
  \draw (old) -- ($ ($#2!\i/(#4+1)!#3$) + (0,#1*rand) $) coordinate (old);
  }
  \draw (old) -- #3;
}}

\irregularline[1mm]{(0,0)}{(5,0)}
\irregularline[1mm]{(0,1)}{(5,1)}
\irregularline[1mm]{(0,1.5)}{(5,1.5)}
\end{tikzpicture}

\end{document}
2

Just for completeness, i.e. to mention that you do not need this nice macro to draw a random line, nor all the libraries you load. Using Torbjørn T.'s answer and section 48.2 of the pgfmanual:

\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}
    \pgfmathsetseed{42}
    \draw[decorate,decoration={random steps,segment length=4pt,amplitude=2pt}] 
    (0, 0) -- (5,0);
    \pgfmathsetseed{42}
    \draw[decorate,decoration={random steps,segment length=4pt,amplitude=2pt}] 
    (0, -1) -- (5,-1);
\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.