我有以下代码:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw[help lines, color=gray!20] (0,0) grid (12,12);
\draw[->] (1,6)--(11,6);
\draw[->] (6,1)--(6,11);
\draw (6,6) circle [radius=4cm];
\pgfmathsetmacro{\n}{6}
\foreach \i in {0, ..., \n} {
\draw[line width=1] ($(\i*360/\n:4) + (6, 6)$) -- ($(\i*360/\n+360/\n:4) + (6, 6)$);
}
\end{tikzpicture}
\end{document}
其结果为:
请注意,我可以通过改变 n 轻松改变多边形的大小(边数)。
我的问题是,如何在多边形的角上添加红点?我想添加一条线,如果我改变 n,该线的角度也会相应改变。它应该看起来像这样:
谢谢 !
答案1
有专门处理此类事物的图书馆,具体来说angles
和shapes.geometric
。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{angles,calc,shapes.geometric}
\begin{document}
\begin{tikzpicture}
\draw[help lines, color=gray!20] (0,0) grid (12,12);
\draw[->] (1,6)--(11,6);
\draw[->] (6,1)--(6,11);
\pgfmathtruncatemacro{\myn}{6}
\draw (6,6) circle [radius=4cm]
node[regular polygon,regular polygon sides=\myn,minimum size=8cm,draw,line
width=1pt] (P){}
foreach \x in {1,...,\myn} {(P.corner \x) node[circle,fill=red,inner sep=1.5pt]
(P\x){}}
pic[draw,fill=green!20,angle radius=1.5cm,pic text={$\alpha=60^\circ$},
angle eccentricity=1]{angle=P6--P--P1}
(P6) -- (P.center) -- (P1);
\end{tikzpicture}
\end{document}
答案2
这是我的方法。
我添加了一些评论以便更好地澄清。
\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw[help lines, color=gray!20] (0,0) grid (12,12);
\draw[->] (1,6)--(11,6);
\draw[->] (6,1)--(6,11);
\coordinate (center) at (6,6); % <- define center of the plot
\draw (center) circle [radius=4cm];
\pgfmathsetmacro{\n}{6}
\foreach \i in {0, ..., \n} {
\draw[line width=1] ($(\i*360/\n:4) + (6, 6)$) -- ($(\i*360/\n+360/\n:4) + (6, 6)$) node (P-1-\i) {}; % <- define node for each point of the polygon
}
\draw (center) --( P-1-6.center); % <- draw line from the center to the desired point
\filldraw[fill=green!20,draw=green!50!black] (center) --++ (1cm,0) arc (0:60:1cm) -- cycle; % <- draw angle
\node[above right=5pt] at (center.center) {$ \alpha=60.0 $}; % <- label the angle
\foreach \i in {0, ..., \n} {
\filldraw[red] (P-1-\i) circle[radius=2pt]; % <- draw red dots
}
\end{tikzpicture}
\end{document}