10

I would like to draw the following picture using TikZ:

enter image description here

I've done this

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale=2]
    \foreach \x in {0,60,...,300} {
        \draw[fill] (\x:1 cm) -- (\x + 60:1 cm);
        \draw[fill] (\x:1 cm) -- (\x + 180:1 cm);}
    \foreach \x in {30,90,...,270} {
        \draw[fill] (\x:0.866 cm) -- (\x + 180:0.866 cm);}
\end{tikzpicture}

\begin{tikzpicture}[scale=2]
    \foreach \x in {0,72,...,288} {
        \draw[fill] (\x:1 cm) -- (\x + 72:1 cm);
        \draw[fill] (\x:1 cm) -- (\x + 180:0.809 cm);}
\end{tikzpicture}
\end{document}

But I would like know how to put this pictures together and fill the space within polygons.


This was my solution:

\documentclass{report}
\usepackage{tikz}

\definecolor{fondo}{rgb}{0.898,0.996,0.898}
\definecolor{diagonal}{rgb}{0.466,0,0}

\begin{document}

\begin{center}
\begin{tikzpicture}[scale=2]
\begin{scope}
    \draw[fill=fondo,very thick] (0:1 cm) -- (72:1 cm) -- (144:1 cm) -- (216:1 cm) -- (288:1 cm) -- cycle;
    \foreach \x in {0,72,...,288} {
        \draw[diagonal,thin] (\x:1 cm) -- (\x + 180:0.809 cm);}
\end{scope}

\begin{scope}[xshift=3cm]
    \draw[fill=fondo,very thick] (0:1 cm) -- (60:1 cm) -- (120:1 cm) -- (180:1 cm) -- (240:1 cm) -- (300:1 cm) -- cycle;
    \foreach \x in {0,60,...,300} {
        \draw[diagonal,thin] (\x:1 cm) -- (\x + 180:1 cm);}
    \foreach \x in {30,90,...,270} {
        \draw[diagonal,thin] (\x:0.866 cm) -- (\x + 180:0.866 cm);}
\end{scope}
\end{tikzpicture}
\end{center}

\end{document}
14

You can put the two figures together by using scope. To fill polygons, you need to make sure you actually have a polygon (i.e., at least three line segments in one \draw). I've done that in the code below:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale=2]
\begin{scope}
    \foreach \x in {0,60,...,300} {
        \draw[fill=blue!25] (0, 0) -- (\x:1 cm) -- (\x + 30:0.866 cm) -- cycle;
        \draw[fill=blue!50] (0, 0) -- (\x + 30:0.866 cm) -- (\x + 60:1 cm) -- cycle;
    }
\end{scope}
\begin{scope}[xshift=2cm]
    \foreach \x in {0,72,...,288} {
        \draw[fill=blue!25] (0, 0) -- (\x:1 cm) -- (\x + 36:0.809 cm) -- cycle;
        \draw[fill=blue!50] (0, 0) -- (\x + 36:0.809 cm) -- (\x + 72:1 cm) -- cycle;
    }
\end{scope}
\end{tikzpicture}
\end{document}

(The cycle keyword just means "draw a line segment back to the initial point in the \draw statement.)

And, just for my own amusement, here is a generalization that works for any n-gon.

\documentclass{standalone}
\usepackage{tikz}

\newcommand\polygon[2][]{
  \pgfmathsetmacro{\angle}{360/#2}
  \pgfmathsetmacro{\startangle}{-90 + \angle/2}
  \pgfmathsetmacro{\y}{cos(\angle/2)}
  \begin{scope}[#1]
    \foreach \i in {1,2,...,#2} {
      \pgfmathsetmacro{\x}{\startangle + \angle*\i}
      \draw[fill=blue!35] (0, 0) -- (\x:1 cm) -- (\x + \angle/2:\y cm) -- cycle;
      \draw[fill=blue!50] (0, 0) -- (\x + \angle/2:\y cm) -- (\x + \angle:1 cm) -- cycle;
    }
  \end{scope}
}

\begin{document}
\begin{tikzpicture}
  \polygon{5}
  \polygon[xshift=2.2cm]{6}
  \polygon[xshift=4.4cm]{7}
  \polygon[xshift=6.6cm]{8}
\end{tikzpicture}
\end{document}

enter image description here

  • 1
    very nice code! And if you change this \pgfmathsetmacro{\y}{cos(\angle/2)} in to \pgfmathsetmacro{\y}{cos(\angle)}. You will get some very beautiful stars ! – Arne Timperman Oct 14 '15 at 19:49
  • I've tried to understand the code but couldn't find to solve my problem. (and I don't want to start a new question). Is their a way the make an n-polygon and to color x parts of it. So continuing on this drawing, an octagon should be divided in 8 parts, and I would like to color x parts of it. The parts can (but it is not necessary) be against each other. – Arne Timperman Oct 14 '15 at 19:55
  • 1
    @Arne: can you ask a new question referring to this one? I have a solution for you. – yyzz Oct 15 '15 at 8:14
  • @Yori I did now. Thanks. tex.stackexchange.com/questions/273107/… – Arne Timperman Oct 15 '15 at 8:38
7

Similar results can be obtained with a regular polygon shape. In this case, a pic helps to draw the node and fill it with those stripes in one macro.

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{shapes.geometric}

\begin{document}

\tikzset{
    bicolor polygon/.pic = {
        \node[regular polygon, regular polygon sides=#1,
        minimum size=4cm, draw, fill=blue!50,
        outer sep=0pt] at (0,0) (-node){};
        \foreach \i in {1,2,...,#1}
             \draw[fill=blue!25] (-node.corner \i)
                 --(-node.side \i)--(-node.center)--cycle;
    }}

\begin{tikzpicture}

\foreach \i [count=\d from 0]  in {5,6,...,10}
    \draw (4cm*\d,0) pic (a) {bicolor polygon=\i};

\end{tikzpicture}

\end{document}

enter image description here

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.