你好我有以下代码:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[help lines, color=gray!30] (0,0) grid (12,12);
\draw[->] (1,6)--(11,6);
\draw[->] (6,1)--(6,11);
\draw (6,6) circle [radius=4cm];
\end{tikzpicture}
\end{document}
输出:
我的问题是如何使用 \pgfmathsetmacro 命令设置 N 的值(这样多边形的边数就可以轻松改变)来向该图片添加一个有 N 边的多边形(角位于圆上)。
答案1
以下是一种方法:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw[help lines, color=gray!30] (0,0) grid (12,12);
\draw[->] (1,6)--(11,6);
\draw[->] (6,1)--(6,11);
\draw (6,6) circle [radius=4cm];
\pgfmathsetmacro{\n}{5}
\foreach \i in {0, ..., \n} {
\draw ($(\i*360/\n:4) + (6, 6)$) -- ($(\i*360/\n+360/\n:4) + (6, 6)$);
}
\end{tikzpicture}
\end{document}
其工作原理是围绕圆圈移动,以正确的角度绘制直线段,以形成所需的多边形。
答案2
使用shapes.geometric
库,您可以声明具有任意边数的正多边形。如果您使用某个 来声明它们minimum size
,则此尺寸是相应外接圆的直径。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
\draw[help lines, color=gray!30] (0,0) grid (12,12);
\draw[->] (1,6)--(11,6);
\draw[->] (6,1)--(6,11);
\draw (6,6) circle [radius=4cm];
\node[regular polygon, regular polygon sides=7, minimum size=8cm, draw=red, shape border rotate=30] at (6,6) {};
\node[regular polygon, regular polygon sides=4, minimum size=8cm, draw=blue, shape border rotate=45] at (6,6) {};
\node[regular polygon, regular polygon sides=5, minimum size=8cm, draw=green, shape border rotate=25] at (6,6) {};
\end{tikzpicture}
\end{document}