# Filling a percentage of a regular polygon

I want to fill a polygon with a color. First of all the n-polygon should be divided in n equal parts. I've found a question similar to this: Drawing polygons And the solution of @Yori was almost wat I wanted. So I want it to be possible that for example of a 8-polygon part 1,3,5 is colored, or from a 13-polyon part 2,3,4,5,9 is colored.

The code (which is not mine) to adapt could be:

 \documentclass{standalone}
\usepackage{tikz}
\newcommand\polygon[]{
\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}


(Students must calculate in percentages, what part is colored)

I'm late (question already answered), but I was working in a different approach which perhaps can be useful in other scenarios (for this one, Tom's answer is simpler and better).

My approach is to define a bunch of colors named color1, color2, etc up to colorN being N twice the number of corners of the polygon. These represent the color to apply to each triangle. Initially all colors are defined equal, but then some of them (the ones to fill differently) are redefined. This allows to paint the whole polygon in a simple loop, one triangle at a time, using fill=colorX, being X the number of that triangle.

Code:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{xcolor}

\globalcolorstrue

\newcommand\polygon[]{
\pgfmathtruncatemacro{\x}{2*#2}
\foreach \c in {0,1,...,\x}{
\colorlet{color\c}{#3}
}
\foreach \c in {#4} {
\colorlet{color\c}{#5}
}
\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}
\pgfmathtruncatemacro{\colorA}{\i*2}
\pgfmathtruncatemacro{\colorB}{\i*2-1}
\draw[fill=color\colorB] (0, 0) -- (\x:1 cm) -- (\x + \angle/2:\y cm) -- cycle;
\draw[fill=color\colorA] (0, 0) -- (\x + \angle/2:\y cm) -- (\x + \angle:1 cm) -- cycle;
}
\end{scope}
}

\begin{document}
\begin{tikzpicture}
\polygon{5}{blue!20}{1,...,5}{blue!60}
\polygon[xshift=2.2cm]{6}{red!20}{7,8,9}{red}
\polygon[xshift=4.4cm]{7}{green!20}{5,6,...,12}{green}
\polygon[xshift=6.6cm]{8}{yellow!30}{1,2,5,7}{orange!70}
\end{tikzpicture}
\end{document}


Result: • Using \globalcolorstrue with TikZ can be very dangerous if you need TikZ later. I understand you used it here because of the foreach loop, but I think you'd rather use \xglobal\colorlet{...}{...}. – mvienney Aug 23 '17 at 21:48

Just add a few more parameters for fill colors and which tiles to fill:

## Code

\documentclass[tikz, border=2mm]{standalone}

\newcommand\polygon[]%
% 1: options for scope
% 2: num corners
% 3: size
% 4: colored tiles
% 5: fill color
{   \pgfmathsetmacro{\angle}{360/#2}
\pgfmathsetmacro{\startangle}{-90 + \angle/2}
\pgfmathsetmacro{\y}{cos(\angle/2)}
\begin{scope}[#1]
\foreach \i in {#4}
{   \pgfmathsetmacro{\x}{\startangle + \angle*\i}
\fill[#5] (0, 0) -- (\x:#3) --  (\x + \angle:#3) -- cycle;
}
\foreach \i in {1,2,...,#2}
{
\pgfmathsetmacro{\x}{\startangle + \angle*\i}
\draw (0, 0) -- (\x:#3) --  (\x + \angle:#3) -- cycle;
}
\end{scope}
}

\begin{document}
\begin{tikzpicture}
\polygon{6}{1}{1,4}{cyan}
\polygon[xshift=3cm]{7}{2}{1,2,4,7}{magenta}
\polygon[xshift=8cm]{8}{3}{5,6,1,4}{yellow}
\end{tikzpicture}
\end{document}


## Output • @{Tom Bombadil} especially thanks for the comment in your code! – Arne Timperman Oct 15 '15 at 9:23
• @Arne: You're very welcome. – Tom Bombadil Oct 15 '15 at 9:30

Another version, which allows you to specify the colors separately:

\documentclass{standalone}
\usepackage{tikz}

\newcommand\polygon[]{
% Count input length.
\pgfmathsetmacro{\n}{0}
\foreach \color in {#2} {
\pgfmathsetmacro{\n}{\n+1}
\global\let\n=\n
}

% Draw polygon.
\pgfmathsetmacro{\angle}{360/\n}
\pgfmathsetmacro{\startangle}{-90 + \angle/2}
\begin{scope}[#1]
\pgfmathsetmacro{\x}{\startangle}
\foreach[count=\i] \color in {#2} {
\pgfmathsetmacro{\x}{\startangle + \angle*\i}
\draw[fill=\color] (0, 0) -- (\x:1 cm) -- (\x + \angle:1 cm) -- cycle;
}
\end{scope}
}

\begin{document}
\begin{tikzpicture}
\polygon{blue!50, red!50, green!50, blue!50, red!50, green!50, blue!50, red!50, green!50}
\end{tikzpicture}
\end{document} And another one using regular polygons and pics.

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

\begin{document}

\tikzset{
pics/polygon/.style n args = {3}{
code = {
\node[regular polygon, regular polygon sides=#1,
minimum size=4cm, draw,
outer sep=0pt] at (0,0) (-node){};
\foreach \i [evaluate=\i as \next using {ifthenelse(\i+1>#1,1,int(\i+1))}] in {#2}{
%               \typeout{\i,\next}
\draw[fill=#3] (-node.corner \i)
--(-node.corner \next)--(-node.center)--cycle;}
}
}
}

\begin{tikzpicture}

\draw pic (a) {polygon={5}{1,3,2}{red}};
\draw (4,0) pic (a) {polygon={7}{1,3,2}{green}};
\draw (0,4) pic (a) {polygon={10}{1,3,2}{cyan}};
\draw (4,4) pic (a) {polygon={4}{1,3,2}{orange}};

\end{tikzpicture}

\end{document} 