3

I use the following command to create regular polygons. The 5th argument is there to insert options for styling the polygons. Though the line width is 0.3mm as I set it, it looks like the fill=red option is not accepted.

\documentclass{article}
\usepackage{amsmath,tikz,tkz-euclide,pgfplots,calculator}

\newcommand\polygon[5][]
{\pgfmathsetmacro{\angle}{360/#2}
\pgfmathsetmacro{\a}{(#2-1)/2}
\FLOOR{\a}{\sol}
\pgfmathsetmacro{\startangle}{-90 + \angle/2}
\foreach \i in {1,2,...,#2}
{\pgfmathsetmacro{\x}{\startangle + \angle*\i}
\draw[#5] (\x:#3) --  (\x + \angle:#3) -- cycle;
\tkzDefPoint(\x:#3){A_#2}
\tkzDrawPoint(A_#2)}
\foreach [count=\i] \j in {#4}{
\pgfmathsetmacro{\x}{\startangle - \angle*(\i+\sol)}
\node[anchor=center] at (\x:#3+.3) {$\j$};}}

\begin{document}
\begin{tikzpicture}
\draw[line width=0.3mm] (0,0) circle (1);
\polygon[]{6}{1}{A,B,\varGamma,\varDelta,E,Z}{line width=0.3mm,fill=red}

\end{tikzpicture}
\end{document}

I don't know if the structure of the command is not allowing any other option.

  • In \draw[#5] (\x:#3) -- (\x + \angle:#3) -- cycle; you have nothing to fill (this is just a segment drawn twice), so fill=red is useless here. It is not clear what you want to fill. Do you want (0,0) in place of (cycle) here? – Kpym Apr 1 at 9:38
  • I want to fill the area inside the polygon and I thought that it can be done trough \draw command. – mac Apr 1 at 9:51
4

I am wondering why you don't follow approaches like this

\documentclass{article}
\usepackage{amsmath,tikz}
\usetikzlibrary{shapes}

\newcommand\polygon[5][]{%
    \node[draw,#5,regular polygon,regular polygon sides=#2,minimum size=2*#3cm] (p) {};
    \foreach \i[count=\j] in {#4} {
        \path (p.center)--(p.corner \j) node[pos=1.2] {$\i$};
    }
}

\begin{document}
\begin{tikzpicture}
\draw[line width=0.3mm] (0,0) circle (1);
\polygon[]{6}{1}{A,B,\varGamma,\varDelta,E,Z}{line width=0.3mm,fill=red}
\end{tikzpicture}
\end{document}

enter image description here

However, I think you should not define a macro for this.


If you want to follow your way, Kpym has given you the right way: use (0,0) instead of cycle. However, one has to do a bit more. Also, there will be a very thin white line.

\documentclass{article}
\usepackage{amsmath,tikz,tkz-euclide,pgfplots,calculator}

\newcommand\polygon[5][]{%
    \pgfmathsetmacro{\angle}{360/#2}
    \pgfmathsetmacro{\a}{(#2-1)/2}
    \FLOOR{\a}{\sol}
    \pgfmathsetmacro{\startangle}{-90 + \angle/2}
    \foreach \i in {1,2,...,#2} {
        \pgfmathsetmacro{\x}{\startangle + \angle*\i}
        \fill[#5] (\x:#3) --  (\x + \angle:#3) -- (0,0);
        \draw[#5] (\x:#3) --  (\x + \angle:#3);
        \tkzDefPoint(\x:#3){A_#2}
        \tkzDrawPoint(A_#2)
    }
    \foreach [count=\i] \j in {#4} {
        \pgfmathsetmacro{\x}{\startangle - \angle*(\i+\sol)}
        \node[anchor=center] at (\x:#3+.3) {$\j$};
    }
    \foreach \i in {1,2,...,#2} {
        \pgfmathsetmacro{\x}{\startangle + \angle*\i}
        \tkzDefPoint(\x:#3){A_#2}
        \tkzDrawPoint(A_#2)
    }
}

\begin{document}
\begin{tikzpicture}
\draw[line width=0.3mm] (0,0) circle (1);
\polygon[]{6}{1}{A,B,\varGamma,\varDelta,E,Z}{line width=0.3mm,fill=red}

\end{tikzpicture}
\end{document}

enter image description here

  • 1
    The first sulotion is neat and simple. I had no idea that there was a ready to use option for regular polygons. Also I realised, as Kpym mentioned, that each time I was drawing the same segment twice, so there was no polygon to fill. – mac Apr 1 at 11:25
  • My upvoted surely. Can you put, please, in first plane the gray bullet near \Gamma? There is a strange closure of the exagon. – Sebastiano Apr 1 at 11:37
  • @Sebastiano I will add in a while. However, I don't see anything strange. – user156344 Apr 1 at 11:38
  • @JouleV In the 2nd picture (2nd code). – Sebastiano Apr 1 at 11:40
  • 1
    Ah, ok, I will find some ways to solve. Anyway that way is not recommended clearly. – user156344 Apr 1 at 11:41

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.