我试图在 tikz 中表示二面体群 (D_{2n}) 在正 n 边形上的作用,特别是 D_10 在五边形上的作用。我希望通过对节点进行标记和着色来表示它如何响应反射和旋转。
我对 tikz 还很陌生(在以前的工作中我只需要 tikz-cd),所以进展缓慢。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[help lines/.style={blue!30,very thin}, thicc/.style={very thick}]
\draw[help lines] (-3.9,-3.9) grid (3.9,3.9);
\node[thicc] (1) at (450:2) [circle,draw,red,fill=red!50,label=right:$1$];
\node[thicc] (2) at (378:2) [circle,draw,blue,fill=blue!50,label=right:$2$];
\node[thicc] (3) at (306:2) [circle,draw,green,fill=green!30,label=right:$3$];
\node[thicc] (4) at (234:2) [circle,draw,orange,fill=red!30!yellow!30,label=right:$4$];
\node[thicc] (5) at (162:2) [circle,draw,purple,fill=red!50!blue,label=right:$5$];
\draw[thicc] (1) -- (2) -- (3) -- (4) -- (5) -- (1) -- cycle;
\end{tikzpicture}
\end{document}
我的目标是每个节点都有不同的颜色和标签(标签 1-5),然后重新绘制相同的形状,但反映在通过(比如说)顶点 2 的线上(可能用虚线表示对称线),即对应于排列 (13)(45) 和一些旋转,比如说 (13524)。
这是最好的方法吗?我尝试了 \foreach,但不确定如何正确使用语法来获取每个节点的不同颜色等。我还想将节点缩放得更大,并让标签出现在圆圈之外(而不是在某个固定方向上)。
我的做法正确吗?
答案1
为此,将五边形存储在 pic 中,然后对其进行变换可能更有意义。为了让标签始终直立,可能需要使用transform shape=false
。将颜色存储在列表中并使用 foreach 循环可能也更方便。当然,相对于旋转轴的反射是一系列操作
rotate, reflect, inverse rotate
这是代码。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[help lines/.style={blue!30,very thin},
pics/pentagon/.style={code={
\node[regular polygon,regular polygon sides=5,minimum size=4cm,draw]
(5gon){};
\foreach \X in {1,...,5}
{\pgfmathsetmacro{\myfillcolor}{{\LstFillCols}[mod(\X-1,5)]}
\pgfmathsetmacro{\mydrawcolor}{{\LstDrawCols}[mod(\X-1,5)]}
\path (5gon.center) -- (5gon.corner \X)
node[circle,draw=\mydrawcolor,red,fill=\myfillcolor]{}
node[pos=1.2,transform shape=false](\X){\X};}}}]
thicc/.style={very thick}]
\edef\LstFillCols{"red!50","blue!50","green!30","red!30!yellow!30","red!50!blue"}
\edef\LstDrawCols{"red","blue","green","orange","purple"}
\draw[help lines] (-3.9,-3.9) grid (3.9,3.9);
\pic{pentagon};
\begin{scope}[xshift=8cm]
\draw[help lines] (-3.9,-3.9) grid (3.9,3.9);
\begin{scope}[rotate=72,xscale=-1,rotate=-72,transform shape]
\draw[dashed] (162:3.9) -- (162+180:3.9); % symmetry axis
\pic{pentagon};
\end{scope}
\end{scope}
\end{tikzpicture}
\end{document}
这种方法的优点是它可以用于任意 n。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}[help lines/.style={blue!30,very thin},
pics/ngon/.style={code={\tikzset{ngon/.cd,#1}
\node[regular polygon,regular polygon sides=\pgfkeysvalueof{/tikz/ngon/n},
minimum size=4cm,draw,ngon/border] (\pgfkeysvalueof{/tikz/ngon/name}){};
\foreach \X in {1,...,\pgfkeysvalueof{/tikz/ngon/n}}
{\pgfmathsetmacro{\myfillcolor}{{\LstFillCols}[mod(\X-1,5)]}
\pgfmathsetmacro{\mydrawcolor}{{\LstDrawCols}[mod(\X-1,5)]}
\path (\pgfkeysvalueof{/tikz/ngon/name}.center) -- (\pgfkeysvalueof{/tikz/ngon/name}.corner \X)
node[circle,draw=\mydrawcolor,fill=\myfillcolor,ngon/nodes]{}
node[pos=\pgfkeysvalueof{/tikz/ngon/label pos},transform shape=false](\X){\X};
}
}},flip about/.style={/utils/exec=\pgfmathsetmacro{\posangle}{%
-1*iseven(\pgfkeysvalueof{/tikz/ngon/n})*180/\pgfkeysvalueof{/tikz/ngon/n}+%
(#1-1)*360/\pgfkeysvalueof{/tikz/ngon/n}},
rotate=\posangle,xscale=-1,rotate=-1*\posangle},
ngon/.cd,n/.initial=5,border/.style={very thick},nodes/.style={very thick},
name/.initial={ngon},label pos/.initial=1.2,
angle of/.code 2 args=\pgfmathsetmacro{#2}{%
-1*iseven(\pgfkeysvalueof{/tikz/ngon/n})*180/\pgfkeysvalueof{/tikz/ngon/n}+%
(#1-1)*360/\pgfkeysvalueof{/tikz/ngon/n}}]
\edef\LstFillCols{"red!50","blue!50","green!30","red!30!yellow!30","red!50!blue"}
\edef\LstDrawCols{"red","blue","green","orange","purple"}
\draw[help lines] (-3.9,-3.9) grid (3.9,3.9);
\pic{ngon={n=5}};
\begin{scope}[xshift=8cm]
\draw[help lines] (-3.9,-3.9) grid (3.9,3.9);
\begin{scope}[ngon/n=5,flip about=2,transform shape]
\pgfkeys{tikz/ngon/angle of={2}{\myangle}}
\draw[dashed] (\myangle+90:3.9) -- (\myangle+270:3.9); % symmetry axis
\pic{ngon};
\end{scope}
\end{scope}
% second example
\begin{scope}[yshift=-8cm]
\draw[help lines] (-3.9,-3.9) grid (3.9,3.9);
\pic{ngon={n=8}};
\begin{scope}[xshift=8cm]
\draw[help lines] (-3.9,-3.9) grid (3.9,3.9);
\begin{scope}[ngon/n=10,flip about=3,transform shape]
\pgfkeys{tikz/ngon/angle of={3}{\myangle}}
\draw[dashed] (\myangle+90:3.9) -- (\myangle+270:3.9); % symmetry axis
\pic{ngon};
\end{scope}
\end{scope}
\end{scope}
% third example
% second example
\begin{scope}[yshift=-16cm,ngon/.cd,n=5,
nodes/.style={inner sep=2ex},label pos=1.4]
\draw[help lines] (-3.9,-3.9) grid (3.9,3.9);
\pgfkeys{tikz/ngon/angle of={1}{\myangleA}}
\pgfkeys{tikz/ngon/angle of={2}{\myangleB}}
\pic{ngon};
\draw[semithick,-latex] (90+\myangleA:3.5)
arc(90+\myangleA:90+\myangleB:3.5);
\begin{scope}[xshift=8cm]
\draw[help lines] (-3.9,-3.9) grid (3.9,3.9);
\begin{scope}[flip about=2,transform shape]
\pgfkeys{tikz/ngon/angle of={2}{\myangle}}
\draw[dashed] (\myangle+90:3.9) -- (\myangle+270:3.9); % symmetry axis
\pic{ngon};
\end{scope}
\end{scope}
\end{scope}
\end{tikzpicture}
\end{document}
在此代码中,有一个代码ngon/n
可确定正多边形的顶点数。(还有其他键可确定线条、节点和名称的样式。)还有一个操作,它flip about=m
就是上面提到的旋转、反射和逆旋转序列,它围绕穿过其第 1 个m
角和中心的轴反射多边形。为方便起见,还有一个键ngon/angle of
可计算第 1 个角的角度m
(Ti钾Z 具有不同的约定,取决于角的数量是偶数还是奇数)。