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}

1 Answer 1

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

3
  • 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?
    – user121799
    Commented Jun 29, 2018 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") Commented Jul 20, 2018 at 8:12
  • Thanks a lot! Unfortunately I only can upvote once...
    – user121799
    Commented Jul 20, 2018 at 15:39

You must log in to answer this question.

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