5

To illustrate what edge-coloring is, I'd like to colorize a regular polygon. I want to generate the shape using TikZ, then colorize the sides (edges) of the polygon separately, for instance by doing something like this:

\draw[red] (polygon.side 1);

Unfortunately, the world does not seem to work like this. For now my ugly workaround is to overdraw using dotted, dashed, and thick lines.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,positioning}

\begin{document}
\begin{tikzpicture}
  \node[draw, dotted, minimum size=2cm, regular polygon,
  regular polygon sides=5] (polygon) {};
  \draw[thick] (polygon.corner 1) -- (polygon.corner 2);
  \draw[dashed] (polygon.corner 2) -- (polygon.corner 3);
  \draw[thick] (polygon.corner 4) -- (polygon.corner 5);
  \draw[dashed] (polygon.corner 5) -- (polygon.corner 1);
\end{tikzpicture}
\end{document}

edge-colored pentagon

Thanks!

  • Do you really want to use a node? – Sigur Nov 21 '18 at 23:02
  • I want the simplest thing possible. I may have driven into the wrong direction. Also, why wouldn't I want a node? – Antonin Décimo Nov 21 '18 at 23:04
3

With regular polygon colors option, there's no need for extra commands to draw the sides. Everything is done within the node command.

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

\begin{document}
\begin{tikzpicture}[
regular polygon colors/.style 2 args={
    append after command={%
        \pgfextra
        \foreach \i [count=\ni, remember=\ni as \lasti (initially #1)] in {#2}{
            \draw[\i] (\tikzlastnode.corner \lasti) --(\tikzlastnode.corner \ni);}
        \endpgfextra
    }
},
]
  \node[minimum size=2cm, regular polygon,
    regular polygon sides=5, 
    regular polygon colors={5}{red,blue,orange,green,cyan}] (polygon) {};

  \node[minimum size=2cm, regular polygon,
    regular polygon sides=7, 
    regular polygon colors={7}{red, blue, orange, green, cyan, violet, brown},
    right=of polygon] (polygon2) {};
\end{tikzpicture}

\end{document}

enter image description here

5

Like this?

enter image description here

\documentclass[margin=1mm]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[thick]
\foreach \i/\c in {90/red,162/blue,234/green,306/pink,378/orange}{
 \draw[\c] (\i:1cm) -- (72+\i:1cm);
}
\end{tikzpicture}
\end{document}

Edit 1: Another option, to have a better join line:

enter image description here

\begin{tikzpicture}[thick]
\foreach \i/\c in {90/red,162/blue,234/green,306/pink,378/orange}{
\fill[\c] (\i:1cm) -- (72+\i:1cm) -- (0,0) -- cycle;
}
\fill[white] (90:.95cm)--(162:.95cm)--(234:.95cm)--(306:.95cm)--(378:.95cm)--cycle;
\end{tikzpicture}

Edit 2: just for fun.

enter image description here

\begin{tikzpicture}
\foreach \r in {1,.9,...,.1}{
\pgfmathparse{30 * \r}%
\pgfmathsetmacro\percent{\pgfmathresult}
\foreach \i/\c in {90/red,162/blue,234/green,306/pink,378/orange}{
\fill[\c!\percent] (\i:\r cm) -- (72+\i:\r cm) -- (0,0) -- cycle;
}}
\end{tikzpicture}
3

Here is a proposal that defines a style polygon side which can be used e.g. like \draw[red,polygon side=2]; to draw the second side red.

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

\begin{document}
\begin{tikzpicture}[get next vertex/.code n args={2}{%
\pgfmathparse{int(1+mod(#1,5))}
\xdef#2{\pgfmathresult}},
polygon side/.style={get next vertex={#1}{\tmpnextn},
insert path={(polygon.corner #1)-- (polygon.corner \tmpnextn)}}
]
  \node[draw, dotted, minimum size=2cm, regular polygon,
  regular polygon sides=5] (polygon) {};
  \foreach \X [count=\Y] in {red,blue,orange,green,cyan}
  {\draw[\X,polygon side=\Y];}
\end{tikzpicture}
\end{document}

enter image description here

Note that as of now the number of corners (or sides) as well as the name of the object (or node), polygon, are hard coded. One could of course use pgfkeys to store them in some keys, and if you ask me to I'll be happy to add this to this answer.

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.