8

I want to construct a rectangle with two sides having a slight curvature and the other two sides being zigzag lines. In the end, I want to fill it. While the construction of the path worked the way I expected, the filling did not. How can I tell TikZ to fill the area inside this rectangle?

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections, decorations}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}[scale=2]

\coordinate (A) at (0,0);
\coordinate (B) at (8,0);
\coordinate (C) at (8,2);
\coordinate (D) at (0,2);

\begin{scope}[decoration=zigzag]
\def\mypath{ (A)  to[out=10,in=170] (B)
decorate[decoration=zigzag] {(B)-- (C)}  (C) to[out=170,in=10] (D)
decorate[decoration=zigzag]{(D)--(A)}
  };
  \draw [fill=red] \mypath;
\end{scope}
\end{tikzpicture}
\end{document}
12

You are breaking the path into pieces by repeating the coordinate names (B) and (D) hence each piece is filled individually. Removing them inside the braces (and the extra (C)) fixes the problem.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (2,0);
\coordinate (C) at (2,2);
\coordinate (D) at (0,2);
\begin{scope}[decoration=zigzag]
  \draw[fill=red] (A) to[out=10,in=170] (B) 
                decorate {-- (C)}  
                to[out=170,in=10] (D) 
                decorate {--(A)};
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |
  • Just a note: since the default decoration for the scope is already set with \begin{scope}[decoration=zigzag] you can remove [decoration=zigzag] from the path to clean it up a bit. – erik Apr 23 '13 at 11:34

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.