正多边形:奇数索引顶点之间的边

正多边形:奇数索引顶点之间的边

我想绘制一个正多边形,其中奇数索引的连续顶点之间有一条边:例如,有 8 个顶点,我想要一条路径1 -- 3 -- 5 -- 7 -- 1。要使用大量顶点执行此操作,我希望自动执行此操作,而不是手写完整路径。

使用这里的一些答案(特别是这个),我得到了以下解决方案(这里有 16 个顶点):

\documentclass[tikz]{standalone}

\usetikzlibrary{shapes.geometric,calc}

\begin{document}
\begin{tikzpicture}
\node (pol) [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);
}
\end{tikzpicture}
\end{document}

问题是我得到的是路径3 -- 5 -- 7 -- 1加边,3 -- 7而不是我想要的路径。在我看来(我可能错了!)\lastn在循环开始时没有正确更新。

您知道哪里出了问题吗?您有其他方法可以实现相同的目标吗?

答案1

正如您在这里看到的,您的代码和 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}

在此处输入图片描述

答案2

也许是偶数个多边形的替代方案(奇数也是可能的,但有点繁琐)?

\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}

在此处输入图片描述

答案3

使用 MetaPost 完成,插入 LuaLaTeX 程序中。在下面的代码中, 负责odd_subpolygon所有工作,有两个参数可以随意调整:n主正多边形的边数及其半径(以厘米为单位)。作为示例,以下代码分别使用、和r三次应用此宏。n=6n=8n=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}

在此处输入图片描述

答案4

PSTricks 解决方案使用pst-poly包裹:

\documentclass{article}

\usepackage{multido}
\usepackage{pst-poly}
\usepackage{xfp}

\newcommand*\hori[2]{\fpeval{#1+#2+0.05}}
\newcommand*\verti[2]{\fpeval{#1+#2+0.1}}

\def\polygon[#1]#2#3{%
\begin{pspicture}(-\hori{#1}{#2},-\verti{#1}{#2})(\hori{#1}{#2},\verti{#1}{#2})
  \rput(0,0){\PstPolygon[unit = #2, PolyNbSides = #3]}
  \rput(0,0){\PstPolygon[unit = #2, PolyNbSides = \fpeval{2*#3}, linecolor = red]}
  \multido{\i = 1+1}{\fpeval{2*#3}}{%
    \rput(\fpeval{(#1+#2)*cos(pi/#3*(\i-1))},\fpeval{(#1+#2)*sin(pi/#3*(\i-1))}){$\i$}}
\end{pspicture}}

\begin{document}

\polygon[0.25]{2}{8}

\end{document}

输出

该图是使用通用宏绘制的

\polygon[distance between outer polygon and numbers]
        {<radius of the circumcircle>}
        {<number of sides of the inner polygon>}

相关内容