我正在尝试在 TikZ 中制作 2 阶自由群的凯莱图。我希望获得的结果如下所示:
但不需要箭头。我读过TikZ 中自由群的凯莱图并试图弄清楚 Lindemayer 系统,因为大多数答案都是这样。然而,这超出了我的理解范围,我也搞不清楚如何使用不同的颜色(这是我追求的最重要的功能)。
由于我不知道如何调整 Lindemayer 系统答案以获得不同的颜色,因此给出一个最小的工作示例没有什么好处,因为我只是复制了链接问题中的一个答案,并试图找出代码以及如何添加颜色。任何帮助都将不胜感激。
编辑我也尝试手动做一些工作然后旋转整体:我手动定义了右侧的节点,然后尝试使用旋转宏,但这不起作用,我可以包括这个:
\begin{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (2,0);
\coordinate (C) at (3,0);
\coordinate (D) at (3.5, 0);
\coordinate (E) at (2,1);
\coordinate (F) at (2,1.5);
\coordinate (G) at (2,-1);
\coordinate (H) at (2,-1.5);
\coordinate (I) at (1.5, -1);
\coordinate (J) at (2.5, -1);
\coordinate (K) at (1.5, 1);
\coordinate (L) at (2.5, 1);
\coordinate (M) at (3, 0.5);
\coordinate (N) at (3,-0.5);
\foreach \angle \in {0,90,180,-90}
\begin{scope}[rotate=\angle]
\foreach \point in {A,B,..., N}
\fill [black] (\point) circle (2pt);
\end{scope}
\end{tikzpicture}
\end{document}
但这不起作用,我只收到了我最初定义的节点......(正如你所看到的,我对 TikZ 很陌生,我尝试在 PGF 手册中查找范围环境以查看我做错了什么,但我想我并不真正了解这个环境)
答案1
图书馆lindenmayersystems
似乎是个不错的选择。TikZ 中自由群的凯莱图需要注意的是,要根据象限改变样式的颜色:
\documentclass[tikz,border=5]{standalone}
\usepackage{etoolbox}
\usetikzlibrary{lindenmayersystems,arrows.meta}
\newcount\quadrant
\pgfdeclarelindenmayersystem{cayley}{
\rule{A -> B [ R [A] [+A] [-A] ]}
\symbol{R}{ \pgflsystemstep=0.5\pgflsystemstep }
\symbol{-}{
\pgfmathsetcount\quadrant{Mod(\quadrant+1,4)}
\tikzset{rotate=90}
}
\symbol{+}{
\pgfmathsetcount\quadrant{Mod(\quadrant-1,4)}
\tikzset{rotate=-90}
}
\symbol{B}{
\ifnumodd{\quadrant}{%true
\tikzset{dot-cayley/.append style={draw=red}}
}{%false
\tikzset{dot-cayley/.append style={draw=blue}}
}
\draw [dot-cayley] (0,0) -- (\pgflsystemstep,0)
node [font=\footnotesize, midway,
anchor={270-mod(\the\quadrant,2)*90}, inner sep=.5ex]
{};
\tikzset{xshift=\pgflsystemstep}
}
}
\tikzset{
dot/.tip={Circle[sep=-1.5pt,length=3pt]}, cayley/.tip={dot[]}
}
\begin{document}
\begin{tikzpicture}
\draw l-system [l-system={cayley, axiom=[A] [+A] [-A] [++A], step=5cm, order=4}];
\end{tikzpicture}
\end{document}
结果:
片段末尾有黑点:
\documentclass[tikz,border=5]{standalone}
\usepackage{etoolbox}
\usetikzlibrary{lindenmayersystems,arrows.meta}
\newcount\quadrant
\pgfdeclarelindenmayersystem{cayley}{
\rule{A -> B [ R [A] [+A] [-A] ]}
\symbol{R}{ \pgflsystemstep=0.5\pgflsystemstep }
\symbol{-}{
\pgfmathsetcount\quadrant{Mod(\quadrant+1,4)}
\tikzset{rotate=90}
}
\symbol{+}{
\pgfmathsetcount\quadrant{Mod(\quadrant-1,4)}
\tikzset{rotate=-90}
}
\symbol{B}{
\ifnumodd{\quadrant}{%true
\tikzset{dot-cayley-color/.style={draw=red}}
}{%false
\tikzset{dot-cayley-color/.style={draw=blue}}
}
\draw [dot-cayley,dot-cayley-color] (0,0) -- (\pgflsystemstep,0)
node [font=\footnotesize, midway,
anchor={270-mod(\the\quadrant,2)*90}, inner sep=.5ex]
{};
\tikzset{xshift=\pgflsystemstep}
}
}
\tikzset{
dot/.tip={Circle[sep=-1.5pt,length=3pt,color=black]}, cayley/.tip={dot[color=black]}
}
\begin{document}
\begin{tikzpicture}
\draw l-system [l-system={cayley, axiom=[A] [+A] [-A] [++A], step=5cm, order=4}];
\end{tikzpicture}
\end{document}
结果:
答案2
这是一个强力示例。这个想法很简单,您可以轻松调整以满足您的需求。
\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[line width=1pt,scale=0.5]
\pgfmathsetmacro{\Radius}{4}
\foreach \X in {0,...,3}
{\coordinate (X\X) at ({\X*90}:\Radius);
\ifodd\X
\draw[blue](0,0) -- (X\X);
\else
\draw[red](0,0) -- (X\X);
\fi
\foreach \Y in {1,2,3}
{
\pgfmathtruncatemacro{\sumOne}{\X+\Y}
\ifodd\sumOne
\draw[blue] (X\X) -- ++({(\X+\Y-2)*90}:{\Radius/2.5}) coordinate (X\X-\Y);
\else
\draw[red] (X\X) -- ++({(\X+\Y-2)*90}:{\Radius/2.5}) coordinate (X\X-\Y);
\fi
\foreach \Z in {1,2,3}
{
\pgfmathtruncatemacro{\sumTwo}{\X+\Y+\Z}
\ifodd\sumTwo
\draw[blue] (X\X-\Y) -- ++({(\X+\Y+\Z-4)*90}:{\Radius/2.5^2}) coordinate (X\X-\Y-\Z);
\else
\draw[red] (X\X-\Y) -- ++({(\X+\Y+\Z-4)*90}:{\Radius/2.5^2}) coordinate (X\X-\Y-\Z);
\fi
\foreach \V in {1,2,3}
{
\pgfmathtruncatemacro{\sumThree}{\X+\Y+\Z+\V}
\ifodd\sumThree
\draw[blue] (X\X-\Y-\Z) -- ++({(\X+\Y+\Z+\V-2)*90}:{\Radius/2.5^3}) coordinate
(X\X-\Y-\Z-\V);
\else
\draw[red] (X\X-\Y-\Z) -- ++({(\X+\Y+\Z+\V-2)*90}:{\Radius/2.5^3}) coordinate
(X\X-\Y-\Z-\V);
\fi
}
}
}
}
\end{tikzpicture}
\end{document}