我想在圆周上放置 n 个点,如图 3 所示文件。我会选择要分配给每个顶点和颜色的标签,并且我希望在 TikZ-pgf 中拥有代码。
答案1
这是一个可能的解决方案,主要基于如何在图像尺寸受到限制的情况下,生成圆周上的 n 个点并连接所有点?
该代码的改编涉及如何定制标签和颜色:我开发了两种基本方法
circumference with labels
允许自定义两者;它们应该像在 foreach 循环中一样插入(因为它们是实际上用于 foreach 循环):(label/color
例如1/red,2/blue
:);请注意,对的数量label/color
应该与相同num vertex
;circumference with labels in order
按顺时针顺序插入标签并允许选择颜色。
除了来自其他答案的键之外,还有一个:vertex radius
;它允许增加或减少表示顶点的圆的半径(默认值为1.5pt
)。
代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,shapes.geometric} % required for the polygon shape
\pgfkeys{/tikz/.cd,
num vertex/.initial=4,
num vertex/.get=\vertices,
num vertex/.store in=\vertices,
circle radius/.initial=3,
circle radius/.get=\circleradius,
circle radius/.store in=\circleradius,
shift angle/.initial=0,
shift angle/.get=\shiftangle,
shift angle/.store in=\shiftangle,
at pos/.initial={(0,0)},
at pos/.get=\position,
at pos/.store in=\position,
vertex radius/.initial=1.5pt,
vertex radius/.get=\vertexradius,
vertex radius/.store in=\vertexradius,
}
% that's just an alias for \node
\makeatletter
\def\drawvertices{\tikz@path@overlay{node}}
\makeatother
\pgfkeys{/tikz/circumference with labels/.code={
\pgfmathsetmacro\halfcircleradius{\circleradius/2}
\draw \position circle (\halfcircleradius cm) node[regular polygon, regular polygon sides=\vertices, minimum size=\circleradius cm, draw=none, name={vertex set}] {};
\foreach \textlabel/\circlecolor [count=\x] in {#1}{
\node[draw,circle, inner sep=\vertexradius,black, fill=\circlecolor] at (vertex set.corner \x) {};
\pgfmathparse{\shiftangle-360*(\x-1)/ \vertices}
\node at ($(vertex set)+(\pgfmathresult:\halfcircleradius)$)[label={[font=\small]\pgfmathresult:$\textlabel$}]{};
}
}
}
\pgfkeys{/tikz/circumference with labels in order/.code={
\pgfmathsetmacro\halfcircleradius{\circleradius/2}
\draw \position circle (\halfcircleradius cm) node[regular polygon, regular polygon sides=\vertices, minimum size=\circleradius cm, draw=none, name={vertex set}] {};
\foreach \circlecolor [count=\x] in {#1}{
\node[draw,circle, inner sep=\vertexradius,black, fill=\circlecolor] at (vertex set.corner \x) {};
\pgfmathparse{\shiftangle-360*(\x-1)/ \vertices}
\node at ($(vertex set)+(\pgfmathresult:\halfcircleradius)$)[label={[font=\small]\pgfmathresult:$\x$}]{};
}
}
}
\begin{document}
\begin{tikzpicture}
\drawvertices[at pos={(0,0.75)}, shift angle=45,circumference with labels={2/red,3/blue,1/blue,4/red}] {};
\drawvertices[at pos={(0,-5)}, shift angle=45,circumference with labels in order={red,blue,blue,red}] {};
\drawvertices[num vertex=6,
circle radius=4,
at pos={(5,0.75)},
circumference with labels={
1/white,4/green,5/blue,2/red,3/red,6/white
}] {};
\drawvertices[num vertex=6,
circle radius=4,
at pos={(5,-5)},
circumference with labels in order={
white,green,blue,red,red,white
}] {};
\drawvertices[num vertex=19,
circle radius=6,
vertex radius=3pt,
shift angle=90,
at pos={(2.5,-12)},
circumference with labels in order={
white,white,white,green,blue,red,red,red,red,
orange,orange,orange,violet,violet,violet,violet,violet,white,white
}] {};
\end{tikzpicture}
\end{document}
结果:
答案2
与 Claudio 的回答相比,这是一个穷人的解决方案:)
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def\numofpoints{19}
\def\circpatt{{1,0,0,1,1,0,0,1,0,1,1,0,0,0,1,0,0,1,2,3}} % 1 filled 0 empty
\def\labelpatt{{1,11,2,12,3,13,4,14,5,15,6,16,7,17,8,18,9,19,10}}
\node[circle,draw,minimum width=4cm] (bigc) {};
\foreach \x in {0,...,\numexpr\numofpoints-1\relax}
{
\pgfmathparse{\circpatt[\x]}
\ifnum\pgfmathresult>0\relax\def\mycolor{black}\else\def\mycolor{white}\fi
\node[circle,inner sep=3pt,draw,fill=\mycolor] (n-\x) at (bigc.360/\numofpoints*\x+90) {};
\node (l-\x) at (360/\numofpoints*\x+90:2.5cm) {\pgfmathparse{\labelpatt[\x]}$\pgfmathresult$};
}
\end{tikzpicture}
\end{document}