7

I am trying to draw a rough surface using TikZ. This question - Automatically generate graphics which shows light diffusion on a rough surface - tackles the same problem, and using this answer by percusse I've been able to generate this

enter image description here

However, I would like to achieve a diagram that looks more like this (Source: Contacting surfaces.jpg from https://commons.wikimedia.org)

enter image description here

How could I go about achieving a roughness profile which looks like this?

Code

\documentclass[tikz,margin=3.14mm]{standalone}
\usetikzlibrary{decorations.pathmorphing}

\begin{document}
\begin{tikzpicture}

\draw[fill=green!40!black!50!white]
{decorate[decoration={random steps,segment length=0.5mm,amplitude=1pt}] %Roughness is amplitude
    {(0,0) -- ++(4,0)}} -- ++(0,-10mm) -- ++(-4cm,0) -- cycle;

\begin{scope}[yshift=0.1cm,yscale=-1]
\draw[fill=blue!40!black!50!white]
{decorate[decoration={random steps,segment length=0.5mm,amplitude=1pt}] %Roughness is amplitude
    {(0,0) -- ++(4,0)}} -- ++(0,-10mm) -- ++(-4cm,0) -- cycle;
\end{scope}    

\end{tikzpicture}
\end{document}
Milo
  • 9,440

1 Answers1

15

Armed with a jiggly decoration from this answer for making making randomness only in the y direction, it is then a case of nesting multiple decorations with appropriate parameters, for example:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations.pathmorphing}

\pgfdeclaredecoration{jiggly}{step}
{
  \state{step}[width=+\pgfdecorationsegmentlength]
  {\pgfmathsetmacro{\delta}{rand*\pgfdecorationsegmentamplitude}
    \pgfpathlineto{
      \pgfpointadd
      {\pgfpoint{\pgfdecorationsegmentlength}{0pt}}
      {\pgfpointpolar{90-\pgfdecoratedangle}
          {\delta}}
    }
  }
  \state{final}
  {
    \pgfpathlineto{\pgfpointdecoratedpathlast}
  }
}

\begin{document}
\begin{tikzpicture}[line join=round]

\draw[fill=green!40!black!50!white]
  {decorate[decoration={jiggly, segment length=0.25,amplitude=0.25}]
  {decorate[decoration={jiggly, segment length=1,amplitude=1}]
  {decorate[decoration={jiggly, segment length=4,amplitude=4}]     
  {(0,0) -- ++(4,0)}}}} -- ++(0,-10mm) -- ++(-4cm,0) -- cycle;

\begin{scope}[yshift=0.5cm,yscale=-1]
\draw[fill=blue!40!black!50!white]
  {decorate[decoration={jiggly, segment length=0.25,amplitude=0.25}]
  {decorate[decoration={jiggly, segment length=1,amplitude=1}]
  {decorate[decoration={jiggly, segment length=4,amplitude=4}]     
  {(0,0) -- ++(4,0)}}}} -- ++(0,-10mm) -- ++(-4cm,0) -- cycle;
\end{scope}   

\end{tikzpicture}
\end{document}

enter image description here

Mark Wibrow
  • 70,437
  • Wow! Looks great! I didn't know that one can nest decorations like that, could you perhaps hint at a place where that is documented? –  Jun 29 '18 at 14:21
  • 1
    @marmot it is (very) briefly mentioned in on page 353 of the Manual for version 3.0.1a (at the end of section 24.2 "Decorating a Subpath Using the Decorate Path Command") – Mark Wibrow Jul 20 '18 at 08:12
  • Thanks a lot! Unfortunately I only can upvote once... –  Jul 20 '18 at 15:39