Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Simple question:

Is it possible to fill the following shape:

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\begin{documet}
\begin{tikzpicture}
\draw [decorate,decoration = {snake,amplitude =.4mm, segment length = 5mm}] (0,0)--(10,0);
\draw (0,0)..controls(0,-1)and(10,-1)..(10,0);
\end{tikzpicture}
\end{document}

enter image description here

Apologies if this question is rather simplistic.

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

The typical approach to fill complex shapes is trying to envision our area as the intersection of two simpler closed paths, and then fill one of those paths areas while clipping to the another.

For example, your region can be seen as the intersection of the following two paths:

\draw  (0,0) .. controls +(0,-1) and +(0,-1)..(10,0) -- (10,1) -- (0,1) --cycle ;
\draw [decorate,decoration = {snake,amplitude =.4mm, segment length = 5mm}] (0,0)--(10,0) -- (10,-1) -- (0,-1) -- cycle;

Idea

So the idea is to fill the "wavy rectangle", and clipping the result to the "curved bottom rectangle". The part of the code which does this trick has to be enclosed in a scope to prevent the clipping to be applied to the whole figure (in case that it is part of something bigger).

The following code implements this idea:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}
\begin{scope}
\clip  (0,0) .. controls +(0,-1) and +(0,-1)..(10,0) -- (10,1) -- (0,1) --cycle ;
\draw  [fill=orange!50, decorate,decoration = {snake,amplitude =.4mm, segment length = 5mm}] (0,0)--(10,0) -- (10,-1) -- (0,-1) --cycle ;
\end{scope}
\draw (0,0) ..controls +(0,-1)and +(0,-1)..(10,0);
\end{tikzpicture}
\end{document}

Result

share|improve this answer
1  
I would enclose the \clip path in the pgfinterruptboundingbox environment. Otherwise you have nearly 1cm more height in the resulting box containing the TikZ picture. Of course, this doesn’t matter if you add more stuff above the wavy line. –  Qrrbrbirlbel Mar 6 '13 at 21:07
add comment

In this simple case, you can use just one path that gets filled.

A few notes:

  • The decoration is activated with decorate {<part of path that shall be decorated>}, so we put (0,0) -- (10,0) in it.

  • The control syntax understands relative coordinates which I introduced because I needed to switch the direction of the curve (it now goes from (10, 0) to (0, 0)).

  • Instead of the .. controls (<c1>) and (<c2>) .. syntax you can also use curve to paths. In this case with the angles set to 270 and a distance of 1cm:

    to[distance=1cm, out=270, in = 270] (water-start)
    
  • The -- cycle makes a nice line at the start.

  • I have also introduced two coordinates:

    • water-start and
    • water-end.

    As water-start is used twice in the path, you now only need to change the \coordinate line, if you want to start elsewhere and not two values in a path.

Code

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}
\coordinate (water-start) at (0,0);
\coordinate (water-end) at (10,0);
\draw[
    decoration={
        snake,
        amplitude =.4mm,
        segment length = 5mm
    },
    fill=blue
] decorate {(water-start) -- (water-end)}
%   .. controls +(0,-1) and +(0,-1) .. (water-start) % this is the same
   to[distance=1cm, out=270, in = 270] (water-start) % as this
   -- cycle
   ;
\end{tikzpicture}
\end{document}

Output

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.