9

To create something that looks like the shape of a lake, the following code transforms the border of a blue ellipse with smooth random steps. The idea is taken from another answer.

\documentclass[tikz]{standalone}
\usetikzlibrary{decorations.pathmorphing}
\pgfmathsetseed{42}
% The seed is only set explicitly to always generate the same picture.
% The problem occurs independent of seed.

\begin{document}
\begin{tikzpicture}
  \fill[blue, draw=brown,
    decoration={random steps,segment length=1cm,amplitude=.5cm},
    decorate,
    rounded corners=.3cm
  ] (0, 0) ellipse (3 and 2);
  \draw[ultra thick, red] (3.1, 0) circle (.3);
\end{tikzpicture}
\end{document}

enter image description here

As marked with a red circle, at the point where the ellipse begins (at 0 degrees) the random step is not correctly smoothed but adds an ungly corner. How can this be fixed?

16

You can use the options pre and post to control what's happening at the beginning and the end of the path you're decorating. More information in the Tikz/PGF manual, section 21.4.2, page 247.

I suggest this:

decoration={random steps,segment length=1cm,amplitude=.5cm,
            pre=lineto,pre length=.25cm,post=lineto,post length=.25cm}

A complete source file and the result follow.

\documentclass{article}
\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}

\newcommand\rndellipse[2][]{%
  \pgfmathsetseed{#2}
  \begin{tikzpicture}
    \fill[blue, draw=brown,
      decoration={random steps,segment length=1cm,amplitude=.5cm,#1},
      decorate,
      rounded corners=.3cm
    ] (0, 0) ellipse (3 and 2);
  \end{tikzpicture}}
\newcommand\try[1]{%
  \noindent
  \textbf{Seed #1} \\
  \begin{tabular}{@{}cc@{}}
    \rndellipse{#1} &
    \rndellipse[pre=lineto,pre length=.25cm,post=lineto,post length=.25cm]{#1}
  \end{tabular}
  \par
}

\begin{document}
\try{42}
\try{54}
\try{666}
\try{4217}
\end{document}

result

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.