答案1
- 使用极坐标
(angle:distance)
。对于五边形,角度相差 360/5 = 72 度。由于一个节点应该位于 90 度(北),因此从那里计算角度为 18、90、162、234 和 306 度。 - 查找
\foreach
循环以及中的\draw
和命令\node
TikZ 手册。
\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\begin{document}
\newcommand\size{1}% distance of nodes from center
\begin{tikzpicture}
\draw[thick,red] (18:\size) \foreach \a in {90,162,234,306} { -- (\a:\size) } -- cycle;
\draw[thick,blue] (18:\size) \foreach \a in {162,306,90,234} { -- (\a:\size) } -- cycle;
\foreach \a in {18,90,162,234,306} { \node[black,fill=black,circle,inner sep=2pt] at (\a:\size){}; }
\end{tikzpicture}
\end{document}
答案2
使用 MetaPost 非常简单:
\documentclass{standalone}
\usepackage[latex,shellescape]{gmp}
\begin{document}
\begin{mpost}[name=nice]
numeric u; u := 3cm;
for i = 1 upto 5:
for j = 1 upto i:
if i <> j:
draw (u*dir (72i+18)) -- (u*dir(72j+18)) withcolor
if (abs(i-j) = 1) or (abs(i-j) = 4):
red
else:
blue
fi
withpen currentpen scaled 4;
fi
endfor
endfor
for i = 1 upto 5: drawdot (u*dir (72i+18)) withpen currentpen scaled 20; endfor
\end{mpost}%
\usempost{nice}%
\end{document}
答案3
另一种方法(用于练习),使用regular polygon
形状和双环:
\documentclass[tikz, margin=3.141592]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[
dot/.style = {circle, fill=black, inner sep=2pt, outer sep=0pt}
]
\node (n) [regular polygon,
regular polygon sides=5, minimum size=22mm,
draw=red, thick] {};
\foreach \i in {1,2,...,5}
{
\foreach \j [evaluate=\j as \k using int(\i+\j)] in {2,3}
{
\ifnum\k<6
\draw[thick,blue] (n.corner \i) -- (n.corner \k);
\fi
}
\node [dot] at (n.corner \i) {};
}
\end{tikzpicture}
\end{document}
答案4
可以使用该graphs
库来生成图表。
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{graphs,graphs.standard}
\begin{document}
\begin{tikzpicture}
\graph [nodes={circle,inner sep=1pt,fill},radius=1.75cm,
clockwise, empty nodes,n=5] {
subgraph I_n [clique,edge=blue];
subgraph C_n [edge={red,semithick}]; ,
};
\end{tikzpicture}
\end{document}