答案1
这是 Ti钾Z 解决方案,使用
- 节点数为常数,用 定义
\newcommand
。这确保你不会覆盖任何现有命令。 - 形式为的极坐标
(angle:radius)
,因此我们可以对所有所需角度进行 for 循环,以创建一个漂亮的圆圈。 \pgfsetmacro
进行计算并将结果保存在变量中(例如\angle
)- PGF
ifthenelse
处理第一个和最后一个值(本例中为 1 和 8) - 再次,PGF
ifthenelse
检查我们不必绘制的连接,即对角线连接以及与下一个和上一个节点的连接。
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\newcommand{\numnodes}{8}
\foreach \ii in {1, ..., \numnodes}{
\pgfmathsetmacro{\angle}{90 - (\ii-1)*360/\numnodes}
\node[circle, draw, fill=blue!40] (x\ii) at (\angle:2cm) {\ii};
}
\foreach \ii in {1, ..., \numnodes}{
\pgfmathsetmacro{\prev}{int(ifthenelse(\ii==1, \numnodes, \ii-1))}
\pgfmathsetmacro{\next}{int(ifthenelse(\ii==\numnodes, 1, \ii+1))}
\draw (x\ii) -- (0,0) (x\prev) -- (x\ii) -- (x\next);
\foreach \jj in {\ii, ..., \numnodes}{
\pgfmathsetmacro{\drawoptions}{ifthenelse(
\jj==(\ii+\numnodes/2) || \jj==\prev || \jj==\next,
"none", "gray")}
\path[draw=\drawoptions, thin, dashed] (x\ii) -- (x\jj);
}
}
\end{tikzpicture}
\end{document}
答案2
这是一个解决方案pstricks
(并且auto-pst-pdf
可以用其进行编译pdflatex
):
\documentclass[border=3pt, svgnames]{standalone}%
\usepackage{pgffor, etoolbox}
\usepackage{pst-poly, auto-pst-pdf}
\begin{document}
\everypsbox{\footnotesize}
\begin{pspicture}
\providecommand{\PstPolygonNode}{%
\rput{*0}(1;\INode){\circlenode[fillstyle=solid, fillcolor=LightSteelBlue!60, linecolor=LightSteelBlue, framesep=1pt]{C\the\multidocount}{\the\multidocount}}}
\psset{ linewidth=0.6pt}
\rput{67.5}(0,0){\PstOctogon[unit=2, PolyRotation=90]}
\foreach \x in {1,2,3,4,5,6}{\foreach \y in {3,4,5,6,7,8}{\ifnumequal{\x}{\y}{\relax}{\ifnumequal{\y-\x}{4}{\ncline{C\x}{C\y}}{\psset{linestyle=dashed, dash=3pt 3pt, linewidth=0.6pt, linecolor=LightSteelBlue}\ncline{C\x}{C\y}}}}}
\rput{67.5}(0,0){\PstOctogon[unit=2, PolyRotation=90]}
\end{pspicture}
\end{document}
答案3
只是稍微采用了我在链接中的回答(如何绘制路径以形成正五边形)在我的评论中给出了以下问题:
\documentclass[border=3mm,tikz]{standalone}
\begin{document}
\begin{tikzpicture}[
every node/.style={draw=blue,shape=circle,fill=blue!50,inner sep=1pt,
minimum size=5mm,font=\small, text=white}
]
%%%% variable data data
\def\numpoly{8}%number of nodes
\def\startangle{90}%angle of the first node
\def\pradious{22mm}
%------- calculations positions angles
\pgfmathparse{int(\startangle+360/\numpoly)}%
\let\nextangle=\pgfmathresult
\pgfmathparse{int(\startangle-360/\numpoly+360)}%
\let\endangle=\pgfmathresult
%--- regular polygon nodes
\foreach \i [count=\ii from 1] in {\startangle,\nextangle,...,\endangle}
{
\path (\i:\pradious) node (p\ii) {\ii};
}
%--- nodes interconnections
\foreach \x in {1,...,\numpoly}
\foreach \y in {\x,...,\numpoly}
\draw[dashed,blue] (p\y) -- (p\x);
\foreach \i [count=\ii] in {2,...,\numpoly, 1}
\draw[semithick, red] (p\ii) -- (p\i);
\foreach \i [count=\ii] in {2,...,\numpoly, 1}
{
\draw[semithick, red] (p\ii) -- (p\i);
\draw[semithick, red] (0,0) -- (p\ii);
}
\end{tikzpicture}
\end{document}