绘制八边形,中间边缘有颜色节点

绘制八边形,中间边缘有颜色节点

我想在 TikZ 中画三幅图,但我不知道该怎么做。

通缉

答案1

暂无评论!

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

\begin{document}
  \begin{tikzpicture}
    \begin{scope}
    \node[draw,thick,minimum size=4cm,name=O,regular polygon,regular polygon sides=8] {};
    \foreach \x in {1,2,...,8}{
     \node[fill=red,circle,draw,inner sep=1pt] at (O.side \x) {};
     \node[fill,circle,draw,inner sep=1pt] at (O.corner \x) {};     
    }
    \node[fill=green,circle,draw,inner sep=1pt] at (O.center) {};
    \end{scope}
    \begin{scope}[xshift=5cm]
      \node[draw,thick,minimum size=4cm,name=O,regular polygon,regular polygon sides=4] {};
    \foreach \x in {1,2,...,4}{
     \node[fill=red,circle,draw,inner sep=1pt] at (O.side \x) {};
     \node[fill,circle,draw,inner sep=1pt] at (O.corner \x) {};
    }
    \node[fill=green,circle,draw,inner sep=1pt] at (O.center) {};
    \end{scope}
    \begin{scope}[xshift=9cm]
      \node[draw,thick,minimum size=2cm,name=O,regular polygon,regular polygon sides=4,rotate=40] {};
    \foreach \x in {1,2,...,4}{     
     \node[fill=red,circle,draw,inner sep=1pt] at (O.corner \x) {};
    }
    \node[fill=green,circle,draw,inner sep=1pt] at (O.center) {};
    \node[fill,circle,draw,inner sep=1pt] (t) at ([shift={(-8mm,2cm)}]O.corner 1) {};
    \foreach \x in {2,...,4}{
     \draw (O.corner \x) -- (t);
    }
    \draw[dashed] (O.corner 1) -- (t);
    \end{scope}
  \end{tikzpicture}
\end{document}

在此处输入图片描述

这是 Svend Tveskæg 的简单版本:

\documentclass[tikz,border=4mm]{standalone}
\usetikzlibrary{shapes.geometric}
\tikzset{mypolygon/.pic={%
    \begin{scope}
    \node[draw,thick,minimum size=4cm,name=O,regular polygon,regular polygon sides=#1] {};
    \foreach \x in {1,2,...,#1}{
     \node[fill=red,circle,draw,inner sep=1pt] at (O.side \x) {};
     \node[fill,circle,draw,inner sep=1pt] at (O.corner \x) {};
    }
    \node[fill=green,circle,draw,inner sep=1pt] at (O.center) {};
    \end{scope}
  }
}


\begin{document}
  \begin{tikzpicture}
    \pic {mypolygon={8}};
    \pic at (5,0) {mypolygon={4}};
  \end{tikzpicture}
\end{document}

以下是剧透:

\documentclass[tikz,border=4mm]{standalone}
\usetikzlibrary{shapes.geometric}
\tikzset{pics/.cd,
mypolygon/.style args={#1 and #2}{%
        code = {%
    \begin{scope}
    \node[draw,thick,minimum size=#2,name=O,regular polygon,regular polygon sides=#1] {};
    \foreach \x in {1,2,...,#1}{
     \node[fill=red,circle,draw,inner sep=1pt] at (O.side \x) {};
     \node[fill,circle,draw,inner sep=1pt] at (O.corner \x) {};
    }
    \node[fill=green,circle,draw,inner sep=1pt] at (O.center) {};
    \end{scope}
  }
}}


\begin{document}
  \begin{tikzpicture}
    \pic {mypolygon={9 and 4cm}};
    \pic at (5,0) {mypolygon={4 and 6cm}};
  \end{tikzpicture}
\end{document}

答案2

PSTricks 解决方案:

\documentclass{article}

\usepackage{multido}
\usepackage{pstricks}
\usepackage{xfp}

% polygon drawing
\newcommand*\polygon[2]{%
\def\spacing{0.04}% spacing for the boundind box
\begin{pspicture}%
(\fpeval{cos(pi*(1-2*ceil(0.5*#2))/#2)*#1-\spacing},
 \fpeval{sin(pi*(1-2*ceil(0.25*#2))/#2)*#1-\spacing})%
(\fpeval{cos(pi/#2)*#1+\spacing},
 \fpeval{sin(pi*(1-2*ceil(0.75*#2))/#2)*#1+\spacing})
  \psdot[dotstyle = o, fillcolor = green](0,0)
  \multido{\i = 1+1}{#2}{%
     \psdot(\fpeval{cos(pi/#2*(2*\i-1))*#1},\fpeval{sin(pi/#2*(2*\i-1))*#1})
    \psline(\fpeval{cos(pi/#2*(2*\i-1))*#1},\fpeval{sin(pi/#2*(2*\i-1))*#1})%
           (\fpeval{cos(pi/#2*(2*\i+1))*#1},\fpeval{sin(pi/#2*(2*\i+1))*#1})
     \psdot[dotstyle = o, fillcolor = red]%
           (\fpeval{cos(pi/#2)*cos(2*pi/#2*\i)*#1},\fpeval{cos(pi/#2)*sin(2*pi/#2*\i)*#1})}
\end{pspicture}}

\begin{document}

\polygon{2.2}{8}
\enskip
\polygon{2}{4}
\enskip
\begin{pspicture}(-2,-2)(2,5)
  \psdot[dotstyle = o, fillcolor = green](0,0)
  \psdot(-0.7,5)
  \psline[linestyle = dashed](0,2)(-0.7,5)
  \psline(2,0)(-0.7,5)
  \psline(-2,0)(-0.7,5)
  \psline(0,-2)(-0.7,5)
  \pspolygon(2,0)(0,2)(-2,0)(0,-2)
  \psdots[dotstyle = o, fillcolor = red](2,0)(0,2)(-2,0)(0,-2)
\end{pspicture}

\end{document}

输出

请注意,前两个图形是使用通用宏绘制的

\polygon{<radius of the circumcircle>}{<number of sides in the regular polygon>}

通过使用pst-poly软件包,但由于某些(目前我还不知道)原因,该软件包无法在我的任何系统上运行。因此,我使用了一种更麻烦的方法,以便在发布代码之前能够对其进行测试。

答案3

LuaLaTeX 程序中包含的 MetaPost 版本(也没有注释)。

\documentclass{standalone}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}

  vardef regpoly(expr n) =
    save angl; angl = 360/n;
    (right for i = 1 upto n-1: -- dir(i*angl) endfor -- cycle) rotated 0.5angl
  enddef;

  vardef regpoly_withdots(expr n, radius) =
    save polygon; path polygon; 
    polygon = regpoly(n) scaled radius; draw polygon;
    drawoptions(withpen pencircle scaled 3bp);
    drawdot origin withcolor green;
    for i = 0 upto n: 
      drawdot (point i of polygon);
      drawdot 0.5[point i of polygon, point i+1 of polygon] withcolor red;
    endfor;
    drawoptions();
  enddef;

  u = cm; r = 2u;

  beginfig(0);

    regpoly_withdots(8, r);

    draw image(regpoly_withdots(4, r)) shifted (2.5r, 0);

    draw image(
      z = (-.75, 4)*u ;
      path quadri; quadri = regpoly(4) scaled 0.5r rotated 38; draw quadri; 
      draw z -- point 0 of quadri dashed evenly;
      for i = 1 upto 3: draw z -- (point i of quadri) ; endfor ;
      drawoptions(withpen pencircle scaled 3bp);
      drawdot origin withcolor green;
      for i = 0 upto 3: drawdot (point i of quadri) withcolor red; endfor;
      drawdot z; drawoptions();
    ) shifted (4.5r, 0);

  endfig;

\end{mplibcode}
\end{document}

在此处输入图片描述

相关内容