我正在尝试左侧的多边形,并使用下面的代码生成了右侧的图表。我要求一种解决方案来对可以实现“任何”规则 a 边多边形的顶点进行编号(我希望只需更改即可\pgfmathtruncatemacro\a{12}
获得所需结果)。
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, calc}
\begin{document}
\begin{tikzpicture}[line cap=round]
\def\r{80pt} %radius of circumscribed circle
\pgfmathtruncatemacro\a{12} % number of polygon vertices
\foreach \i in {1, 2, ..., \a} {
\coordinate (p\i) at ({-\r*cos(\i*(360/\a) + (90-360/\a)},{\r*sin(\i*(360/\a) + (90-360/\a))});
\draw[fill=black] (p\i) circle (1pt);
\node[shift={(0,0)}] at (p\i) {\i};
}
%Polygon Edges
\pgfmathtruncatemacro\b{\a-1} %used to iterate till \a-1
\foreach \i in {1, 2, ..., \b} {
\pgfmathsetmacro{\j}{\i+1}
\draw[thick,black] (p\i) -- (p\j);
}
\draw[thick,black] (p\a) -- (p1);
\end{tikzpicture}
\end{document}
答案1
您的代码中已经有了解决方案:
只需定义半径\def\rn{90pt}
然后,这个:\node[shift={(0,0)}] at (p\i) {\i};
变成:
\node at (
{-\rn*cos(\i*(360/\a) + (90-360/\a)},
{+\rn*sin(\i*(360/\a) + (90-360/\a))}
){\i};
答案2
TikZ 具有极坐标:(<angle>:<radius>)
。(从技术上讲,它允许X和是半徑。
这些数字是带有明确锚点的标签,因为 TikZ 会将锚点对齐到八个罗盘方向之一。由于我circle
为这些标签使用了形状,因此正确的锚点是可以的,并且节点不会与多边形相交。
该\tikzpolygon
宏使用四个参数:
- 命名空间的可选参数
/tikz/polygon
用于调整多边形实例的预定义样式之一, - 相位,即第一个顶点所在的位置,
- 半径和
- 顶点的数量——这必须是整数,不能是公式。
这将始终顺时针绘制多边形。在此配置下,很难更改此方向。我们需要另一个参数,将计算角度的公式中的 转换-
为 a 。+
该graphs
库及其扩展graphs.standard
提供了subgraph C_n
,实际上它要容易得多,因为你已经有了半径、相位、顶点数量的关键接口和direction – 不过,我还没有用它来实现\tikzpolygon
宏。它使用与之前相同的参数,并相应地设置相应的键。
只需正确放置标签,工作量就和以前一样大。
代码(纯 TikZ)
\documentclass[tikz]{standalone}
\tikzset{
polygon/.cd,
dot/.style={
shape=circle, color=black, draw, fill, inner sep=+0pt, minimum size=+2pt},
path/.style={draw},
label/.style={shape=circle, inner sep=+.05em}}
\newcommand*\tikzpolygon[4][]{% #2 = phase, #3 = radius, #4 = number of vertices
\tikz[line cap=round,polygon/.cd,#1]
\path[polygon/path]
foreach[
count = \j from 0,
evaluate = \j as \angle using {#2-360/(#4)*\j}
] \i in {1,...,#4}{
node[
polygon/dot,
label = {[polygon/label, anchor=\angle+180]\angle:$\i$}
] (pg-\i) at (\angle:{#3}) {}
}
plot[sharp cycle, samples at = {1,...,#4}] (pg-\x.center);%
}
\begin{document}
\tikzpolygon{90}{80pt}{12}
\tikzpolygon[path/.append style={fill=gray}]{180}{1cm}{17}
\end{document}
输出(纯 Tikz)
代码(graphs
库)
\documentclass[tikz]{standalone}
\usetikzlibrary{graphs.standard}
\tikzset{
polygon/.cd,
dot/.style={
shape=circle, color=black, fill, draw, inner sep=+0pt, minimum size=+2pt},
path/.style={draw},
label/.style={shape=circle, inner sep=+.05em}}
\newcommand*\tikzpolygon[4][]{% #2 = phase, #3 = radius, #4 = number of vertices
\tikz[polygon/.cd,#1]
\graph[
edges = {polygon/path},
phase = {#2},
radius = {#3},
clockwise = {#4},
n = {#4},
nodes={
/tikz/polygon/dot, as=,
/utils/exec=\pgfmathsetmacro\angle{#2-360/(#4)*(\tikzgraphnodename-1)},
label/.expanded={[polygon/label, anchor=\angle+180]\angle:$\tikzgraphnodename$}},
]{subgraph C_n};%
}
\begin{document}
\tikzpolygon{90}{80pt}{12}
\tikzpolygon[path/.append style={green, thick}]{180}{1cm}{17}
\end{document}
输出(graphs
库)
答案3
像这样修改一下你的程序:
% draw vertices and numbers
\def\s{88pt}
\foreach \i in {1, 2, ..., \a} {
\coordinate (p\i) at ({-\r*cos(\i*(360/\a) + (90-360/\a)},{\r*sin(\i*(360/\a) + (90-360/\a))});
\coordinate (q\i) at ({-\s*cos(\i*(360/\a) + (90-360/\a)},{\s*sin(\i*(360/\a) + (90-360/\a))});
\draw[fill=black] (p\i) circle (1pt);
\node[shift={(0,0)}] at (q\i) {\i};
}
输出: