Regular polygon: edges between vertices of odd index

Maybe an alternative for even-number-of-size polygons (odd is also possible but a little more tedious)?

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
\def\mycorner{18}
\node (pol) [minimum size=\textwidth,regular polygon, 
             rotate=90,regular polygon sides=\mycorner] at (0,0) {}; 
\foreach \n in {1, 2, ..., \mycorner} {
    \node[anchor=\n*(360/\mycorner)] at (pol.corner \n) {\n};
}
\foreach \n [evaluate={\modn = int(Mod(\n+2,\mycorner));}] in {1,3,...,\mycorner} {
    \path[draw] (pol.corner \n) -- (pol.corner \modn);
}
\end{tikzpicture}
\end{document}

enter image description here


As you can see here, there's no problem with your code and TiKZ 3.0

\documentclass[tikz]{standalone}

\usetikzlibrary{shapes.geometric,calc}

\begin{document}
\begin{tikzpicture}
\node (pol) [draw=red,minimum size=\textwidth,regular polygon, rotate=90,regular polygon sides=8] at (0,0) {}; 
\foreach \n in {1, 2, ..., 8} {
    \node[anchor=\n*(360/8)] at (pol.corner \n) {\n};
}
\foreach \n [remember=\n as \lastn (initially 7)] in {1, 3, ...,  7} {
    \path[draw] (pol.corner \lastn) -- (pol.corner \n);
}

\begin{scope}[yshift=13cm]
\node (pol) [draw=red,minimum size=\textwidth,regular polygon, rotate=90,regular polygon sides=18] at (0,0) {}; 
\foreach \n in {1, 2, ..., 18} {
    \node[anchor=\n*(360/18)] at (pol.corner \n) {\n};
}
\foreach \n [remember=\n as \lastn (initially 17)] in {1, 3, ...,  17} {
    \path[draw] (pol.corner \lastn) -- (pol.corner \n);
}
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here


Done with MetaPost, inserted in a LuaLaTeX program. In the following code the odd_subpolygon takes care of all the job, with two parameters to be adjusted at will: the number n of sides of the main regular polygon and its radius r (in centimeters). As examples, the following code applies this macro thrice, respectively with n=6, n=8 and n=16.

\documentclass{article}
\usepackage{luamplib}
  \mplibsetformat{metafun}
  \mplibtextextlabel{enable}
  \everymplib{verbatimtex \leavevmode etex;
    % Macro producing a unit regular polygon
    vardef unit_regpoly(expr n) =
      save angl; angl := 360/n; right for i = 1 upto n-1: -- dir(i*angl) endfor -- cycle
    enddef;
    % Macro drawing a n-sided regular polygon of radius r and its subpolygon
    vardef odd_subpolygon(expr n, r) =
      clearxy; save polygon; path polygon; 
      polygon = unit_regpoly(n) scaled r; draw polygon withcolor red;
      % polygon and labels
      for i = 1 upto n: 
        z[i] = point i-1 of polygon; freelabel(decimal i, z[i], origin);
      endfor;
      % Subpolygon
      draw z1 for i = 3 step 2 until n: -- z[i] endfor -- cycle;
    enddef;
    r = 2cm; % radius parameter
    beginfig(0);}
  \everyendmplib{endfig;}
\begin{document} % Three examples, with n = 5, 8 and 16 respectively
\begin{center}
  \begin{mplibcode} odd_subpolygon(6, r); \end{mplibcode}
  \qquad
  \begin{mplibcode} odd_subpolygon(8, r); \end{mplibcode}
  \par\bigskip
  \begin{mplibcode} odd_subpolygon(16, r); \end{mplibcode}
\end{center}
\end{document}

enter image description here

Tags:

Tikz Pgf