[Tex/LaTex] How to draw star in TikZ background

backgroundstikz-pgf

I draw shapes on background via pgfonlayer

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}

\begin{document}
 \begin{tikzpicture}
  \begin{pgfonlayer}{background}
   \draw[rounded corners,fill=red] (1,1) rectangle (2,2);
   \draw[fill=blue] (0,0) circle (.8cm);
  \end{pgfonlayer}
 \end{tikzpicture}
\end{document}

I know the values (coordinates) needed to draw rectangle or circle, but I have no idea what are the parameters needed to draw a star.

We define the starting coordinate; for rectangle we give the second coordinate two draw rectangle between two points, for circle, we give the radius to draw circle around the starting point. What we need to give to draw star around the starting point?

\draw[fill=green] (0,0) star ????;

PS. Sorry, I know this is a basic typical question, but I had a long struggle with it.

Best Answer

Here you have a macro for drawing stars as well as "n-grams". meaning stars that result when you connect the diagonals of a regular polygon:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{shapes,backgrounds}

\newcommand{\tstar}[5]{% inner radius, outer radius, tips, rot angle, options
\pgfmathsetmacro{\starangle}{360/#3}
\draw[#5] (#4:#1)
\foreach \x in {1,...,#3}
{ -- (#4+\x*\starangle-\starangle/2:#2) -- (#4+\x*\starangle:#1)
}
-- cycle;
}

\newcommand{\ngram}[4]{% outer radius, tips, rot angle, options
\pgfmathsetmacro{\starangle}{360/#2}
\pgfmathsetmacro{\innerradius}{#1*sin(90-\starangle)/sin(90+\starangle/2)}
\tstar{\innerradius}{#1}{#2}{#3}{#4}
}

\begin{document}

\begin{tikzpicture}
 \tstar{2}{4}{7}{10}{thick,fill=blue}
\end{tikzpicture}
\begin{tikzpicture}
 \tstar{0.5}{3}{20}{0}{thick,fill=green}
\end{tikzpicture}

\begin{tikzpicture}
 \ngram{4}{5}{45}{thick,fill=red}
\end{tikzpicture}
\begin{tikzpicture}
 \ngram{4}{7}{0}{thick,fill=yellow}
\end{tikzpicture}

\end{document}

enter image description here