如何绘制一条路径来形成正五边形?

如何绘制一条路径来形成正五边形?

我已经有一个代码,可以按照以下方式绘制正六边形

\begin{tikzpicture}[every node/.style={draw,shape=circle,fill=blue}]
\path (0,0) node (p0) {}
(-1.5,-1) node (p1) {}
(-1.5,-2.5) node (p2) {}
(1.5,-2.5) node (p3) {}
(1.5,-1) node (p4) {}
(0,-3.5) node (p5) { };
\draw (p0) -- (p1)
(p0) -- (p1)
(p0) -- (p2)
(p0) -- (p3)
(p0) -- (p4)
(p0) -- (p5)
(p1) -- (p2)
(p1) -- (p3)
(p1) -- (p4)
(p1) -- (p5)
(p2) -- (p3)
(p2) -- (p4)
(p2) -- (p5)
(p3) -- (p4)
(p3) -- (p5)
(p4) -- (p5);
\end{tikzpicture}

但现在我需要移除其中一个点,并以相同的方式绘制一个正五边形。所以我需要移除 (P5) 并重新校准所有其他点(更改它们的坐标)但我不需要对代码进行太多更改。

有没有办法通过稍微改变我的代码来实现这一点?

答案1

您可以使用 shapes.geometric 库提供的几何形状

\usetikzlibrary{shapes.geometric}

\begin{tikzpicture}[mystyle/.style={draw,shape=circle,fill=blue}]
\def\ngon{5}
\node[regular polygon,regular polygon sides=\ngon,minimum size=3cm] (p) {};
\foreach\x in {1,...,\ngon}{\node[mystyle] (p\x) at (p.corner \x){};}
\foreach\x in {1,...,\numexpr\ngon-1\relax}{
  \foreach\y in {\x,...,\ngon}{
    \draw (p\x) -- (p\y);
  }
}
\end{tikzpicture}

对于 ngon 为 9

在此处输入图片描述

答案2

一个简短的 tikz 代码,无需使用库。

\documentclass[border=7mm]{standalone}
\usepackage{tikz}
\begin{document}
\foreach \n in {3,...,7}
  \tikz\foreach \i in {1,...,\n}
    \fill (\i*360/\n:1) coordinate (n\i) circle(2 pt)
      \ifnum \i>1 foreach \j in {\i,...,1}{(n\i) edge (n\j)} \fi;
\end{document}

在此处输入图片描述

答案3

为了好玩,使用一个简短的代码就pst-poly 可以得到所需的结果:

\documentclass[svgnames]{standalone}

\usepackage{pst-poly}
\usepackage{auto-pst-pdf}
\begin{document}

\psset{unit=3.5cm, dimen=middle, linejoin=1, dotsize=12pt}
\begin{pspicture}(-1,-1)(1,1)
    \providecommand{\PstPolygonNode}{\psdots[dotsize=12pt, linecolor=SteelBlue](1;\INode)
    }
    \rput(0,0){\PstPentagon[PolyName=A, linecolor=LightSteelBlue, linewidth=1.2pt] }
    \rput(0,0){\PstPentagon[PolyName=A, PolyOffset=2] }
\end{pspicture}

\end{document}

在此处输入图片描述

答案4

以下是版本元帖子进行比较。有点像循环练习。

prologues := 3;
outputtemplate := "%j%c.eps";
vardef polygon(expr n) = for t=0 step 360/n until 359: right rotated t -- endfor cycle enddef;
beginfig(1);
path p; 
for n=3 upto 7:
  p := polygon(n) scaled 20 shifted (n*48,0);
  for i=1 upto length p:
    for j=i+1 upto length p:
      draw point i of p -- point j of p;
    endfor
  endfor
  for i=1 upto length p:
    fill fullcircle scaled 3 shifted point i of p withcolor .67 red;
  endfor
endfor
endfig;
end

在此处输入图片描述

相关内容