我有以下代码:
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \n in {1,...,16}
\fill (90-\n*22.5:2cm) coordinate (v\n) circle[radius=.5mm]
++(90-\n*22.5:10pt) node {\n};
\foreach \m/\n in {1/2,1/3,1/4,1/5,1/6,1/7,1/8,1/9,1/10,1/11,1/12,1/13,1/14,1/15,1/16}
\draw (v\n)--(v\m);
\end{tikzpicture}
\end{document}
对于 16 个顶点,代码运行得很好。但现在我想将顶点数量增加一倍。我想将顶点数量设为 32。不幸的是,如果顶点数量超过 16 个,顶点就会开始重叠。
有什么方法可以修复它并增加顶点的数量但保留结构的整体形状?
答案1
您的代码无法工作,因为您的循环考虑了和(360/16) = 22.5
中的度数。如果您想获得更灵活的功能,您应该执行类似 的操作,并使用任意数字。(90-\n*22.5:2cm)
(90-\n*22.5:10pt)
360/n
n
PGF/TikZ 为您提供了\pgfmathsetmacro
数学计算器评估功能,因此您可以根据需要的模式进行除法和乘法。另一个注意事项是,如果您需要指定两个或更多命令,请用括号括起来重复这些命令:
\documentclass[tikz]{standalone}
\newcommand{\MyLines}[1]{%
\begin{tikzpicture}
%Note N is arbitrary
\foreach \n in {1,...,#1}
%\Division is, well, what its name means
%Note \Division is part of the loop so multiplying by \n is possible
{\pgfmathsetmacro\Division{360/#1}
%These values pass through 360/n to 360
\pgfmathsetmacro\Divisions{(360*\n)/#1}
\fill (90-\Divisions:2cm) coordinate (v\n) circle[radius=.5mm]
++(90-\Divisions:10pt) node {\n};}
\foreach \n in {1,...,#1}
{\draw (v\n)--(v1);}
\end{tikzpicture}
}
\begin{document}
\MyLines{32}
\end{document}
尝试其他值并查看结果。
编辑
如果您确实想在文档中看到它,请参阅以下示例:
\documentclass{article}
\usepackage[paperwidth=9cm,paperheight=9cm,margin=1em]{geometry}
\usepackage[spanish]{babel} %Proud of my mother tongue
\usepackage{tikz}
\usepackage{amsmath,amsthm}
\pgfdeclarelayer{nice}
\pgfsetlayers{nice,main}
\newcommand{\MyLines}[1]{%
\begin{tikzpicture}
%Note N is arbitrary
\foreach \n in {1,...,#1}
%\Division is, well, what its name means
%Note \Division is part of the loop so multiplying by \n is possible
{\pgfmathsetmacro\Division{360/#1}
%These values pass through 360/n to 360
\pgfmathsetmacro\Divisions{(360*\n)/#1}
\draw[fill=white] (90-\Divisions:3cm) coordinate (v\n) circle[radius=.7em] node {\n};}
% ++(90-\Divisions:10pt) node {\n};}
\begin{pgfonlayer}{nice}
\foreach \n in {1,...,#1}
{\draw (v\n)--(v1);}
\end{pgfonlayer}
\end{tikzpicture}
}
\begin{document}
\begin{proof}
Hola
\begin{center}
\MyLines{32}
\end{center}
\end{proof}
\end{document}
给您的建议:PGF/TikZ 文档。