我想画一个由许多小等边三角形组成的六边形图,并能够标记图中的特定顶点。具体来说,像这样:
但理想情况下是旋转的,使得其中一侧位于顶部而不是顶点。
有人能给我指明正确的方向吗?
答案1
我尝试在 TikZ 的精神下寻找另一种不需要复杂宏的方法。
第一部分:方法
首先我定义顶点。其中一些位于圆形上并定义六边形。圆形或六边形的数字从 0 到 3。0 是中心。然后在每个六边形上,我放置顶点,第一个顶点的数字从 0 到 5;第二个顶点的数字从 0 到 11;第三个顶点的数字从 0 到 17。第一次,我使用节点,但对于最终的绘图,我将使用坐标。顶点由h;i
h 定义,表示六边形,i 定义,表示索引。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,arrows}
\begin{document}
\begin{tikzpicture}
%%% define vertices with coordinates
\node (00) at (0,0) {00};
\foreach \c in {1,...,3}{%
\foreach \i in {0,...,5}{%
\pgfmathtruncatemacro\j{\c*\i}
% little trick \c*\i gives 0,1,2,3,4,5 for this first circle
% 0,2,4 etc.. for the second one and 0,3,6 for the third one.
% the for the second hexagon, i define the midpoints ith indices : 1,3,5,..
% and for the third hexagon , two points betweens 3;0 and 3;3 etc...
% pgfmathtruncatemacro is used because we can't accept 3;2.0 for a name
\node[circle,minimum width=4pt,inner sep=0pt] (\c;\j) at (60*\i:\c){\c;\j};
} }
% now on the second hexagon we need to place between each corners, a midpoint
% to finish we need to change 12 in 0 so I use mod
\foreach \i in {0,2,...,10}{%
% perhaps foreach now gives some possibilities to avoid \pgfmathtruncatemacro
\pgfmathtruncatemacro\j{mod(\i+2,12)}%
\pgfmathtruncatemacro\k{\i+1}
% midpoint I use the same method further
\node (2\k) at ($(2;\i)!.5!(2;\j)$) {2;\k} ; }
% now on the third hexagon we need to place between each corners, two points
% to finish we need to change 18 in 0 so I use mod
% ($(3;\i)!1/3!(3;\j)$) is a barycenter coeff 1 and 2
\foreach \i in {0,3,...,15}{%
\pgfmathtruncatemacro\j{mod(\i+3,18)}
\pgfmathtruncatemacro\k{\i+1}
\pgfmathtruncatemacro\l{\i+2}
\node (3\k) at ($(3;\i)!1/3!(3;\j)$) {3;\k} ;
\node (3\l) at ($(3;\i)!2/3!(3;\j)$) {3;\l} ;
}
\end{tikzpicture}
\end{document}
我得到了这张图片。您可以看到节点的名称。
第二部分:绘制图形
我用坐标改变节点
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,arrows}
\begin{document}
\begin{tikzpicture}
%%% define vertices with coordinates
\coordinate (0;0) at (0,0);
\foreach \c in {1,...,3}{%
\foreach \i in {0,...,5}{%
\pgfmathtruncatemacro\j{\c*\i}
\coordinate (\c;\j) at (60*\i:\c);
} }
\foreach \i in {0,2,...,10}{%
\pgfmathtruncatemacro\j{mod(\i+2,12)}
\pgfmathtruncatemacro\k{\i+1}
\coordinate (2;\k) at ($(2;\i)!.5!(2;\j)$) ;}
\foreach \i in {0,3,...,15}{%
\pgfmathtruncatemacro\j{mod(\i+3,18)}
\pgfmathtruncatemacro\k{\i+1}
\pgfmathtruncatemacro\l{\i+2}
\coordinate (3;\k) at ($(3;\i)!1/3!(3;\j)$) ;
\coordinate (3;\l) at ($(3;\i)!2/3!(3;\j)$) ;
}
%%%%%%%%% draw lines %%%%%%%%
\foreach \i in {0,...,6}{%
\pgfmathtruncatemacro\k{\i}
\pgfmathtruncatemacro\l{15-\i}
\draw[thin,gray] (3;\k)--(3;\l);
\pgfmathtruncatemacro\k{9-\i}
\pgfmathtruncatemacro\l{mod(12+\i,18)}
\draw[thin,gray] (3;\k)--(3;\l);
\pgfmathtruncatemacro\k{12-\i}
\pgfmathtruncatemacro\l{mod(15+\i,18)}
\draw[thin,gray] (3;\k)--(3;\l);}
%%%%%%%%% some specific lines %%%%%%%%%%
\foreach \i in {0,2,...,10} {
\pgfmathtruncatemacro\j{mod(\i+2,12)}
\draw[thick,dashed] (2;\i)--(2;\j);}
%%%%%%%%% draw points %%%%%%%%
\fill [gray] (0;0) circle (2pt);
\foreach \c in {1,...,3}{%
\pgfmathtruncatemacro\k{\c*6-1}
\foreach \i in {0,...,\k}{%
\fill [gray] (\c;\i) circle (2pt);}}
%%%%%%%%% some specific points %%%%%%%%%%
\foreach \n in {0,3,...,15}{%
\draw (3;\n) circle (4pt);}
\foreach \n in {1,3,...,11}{%
\draw (2;\n) circle (4pt);}
%%%%%%%%%% arrows %%%%%%%%%%%%
\draw[->,red,thick,shorten >=4pt,shorten <=2pt](0;0)--(2;3);
\draw[->,red,thick,shorten >=4pt,shorten <=2pt](0;0)--(2;1);
\end{tikzpicture}
\end{document}
结果 :
答案2
抱歉,这不是 tkz-berge 代码的一个很好的示例,但是 tkz-graph 和 tkz-berge 有很多可能性,我将其做得最简单,没有太好的想法。
您需要找到更好的名称。要查看顶点的名称,您可以将 Art 更改为 Classic
\GraphInit[vstyle=Classic]
\documentclass[]{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tkz-berge}
\begin{document}
\begin{tikzpicture}
\GraphInit[vstyle=Art]
\SetGraphUnit{2}
\Vertex{A}
\grCycle[prefix=a,rotation=30,RA=6]{6}
{\SetUpEdge[style={ultra thick},color=red]
\grCycle[prefix=b,RA=3.464]{6} }
\SetVertexArt[MinSize = 4pt]
\grCycle[prefix=c,rotation=30,RA=2]{6}
\grCycle[prefix=d,rotation=30,RA=4]{6}
\NO(d2){e2} \NO(b2){e3}
\NO(b1){e4} \NO(d0){e5}
\SO(d3){e6} \SO(b4){e7}
\SO(b5){e8} \SO(d5){e9}
\NO(a3){f1} \NO(f1){f2}
\NO(a5){f3} \NO(f3){f4}
\Edges(a2,d2,c2,A,c5,d5,a5)
\Edges(a3,d3,c3,A,c0,d0,a0)
\Edges(a4,d4,c4,A,c1,d1,a1)
\Edges(f2,d2,e2,b2,c1,b1,e4,d1)
\Edges(d1,e3,b2,c2,b3,f1)
\Edges(d0,f4,b0,c5,b5,e8,d4,e7,b4,c3,b3,f2)
\Edges(d0,e5,b1,c0,b0,f3,d5,e9,b5,c4,b4,e6,d3,f1)
\SetUpEdge[style={->,ultra thick,double},color=blue]
\Edges(A,b0) \Edges(A,b1)
\end{tikzpicture}
\end{document}
我忘了旋转:
\begin{tikzpicture}[rotate=30] ....
开始此代码的更好的想法是:
\begin{tikzpicture}
\GraphInit[vstyle=Art]
\SetVertexArt[MinSize = 4pt]
\SetGraphUnit{2}
\grCycle[prefix=V,RA=4]{6}
\foreach \n in {0,...,5}{%
\begin{scope}[shift=(V\n)]
\grCycle[prefix=V\n,RA=2]{6}
\end{scope}}
\end{tikzpicture}
版本 3
\begin{tikzpicture}
\foreach \n in {0,...,3}
{\begin{scope}[shift={({\n*sqrt(3)},\n)},rotate=90]
\pgfmathsetmacro\order{7-\n}
\grEmptyPath[prefix=\n,RA=2]{\order}
\end{scope} }
\foreach \n in {-1,...,-3}
{ \begin{scope}[shift={({\n*sqrt(3)},-\n)},rotate=90]
\pgfmathsetmacro\order{7+\n}
\grEmptyPath[prefix=\n,RA=2]{\order}
\end{scope} }
\end{tikzpicture}
答案3
下载geogebra并绘制图形。然后您可以将其导出为 TikZ 或 PSTricks 并在 LaTeX 中使用它。
答案4
我建议你使用我的解决方案。它有点棘手,可能不是网络上最漂亮的 TikZ/TeX 代码,但它运行良好,并且与原始图片完全相同。
编辑:只是清理了一下代码
下面是代码:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,intersections,arrows}
\begin{document}
%%%%%%%% INPUT %%%%%%%
\def\edge{1.2} %
\def\N{4} %
\def\LabelsOn{0} %
\def\IntRadius{0.05} %
\def\ExtRadius{0.08} %
%%%%%%%%%%%%%%%%%%%%%%
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\begin{tikzpicture}[> = latex,
shorten > = \IntRadius cm,
shorten < = \IntRadius cm]
\pgfmathtruncatemacro{\cond}{mod(\N,2)}
\pgfmathtruncatemacro{\condI}{mod(\N,3)}
\pgfmathsetmacro{\cosine}{cos(30)}
\def\ClipOn#1{
\foreach[count=\t] \ang in {0,60,...,360}{
\coordinate (P\t) at (\ang:#1);
}
\clip (P1)--(P2)--(P3)--(P4)--(P5)--(P6)--cycle;
}
\ifnum\cond=0%pari
\gdef\cond{0}
\else
\gdef\cond{1}
\fi
\ClipOn{\N*\edge+\ExtRadius/\cosine+0.02}
\foreach[count=\t] \row in {-\N,...,\N}{
\ifnum\cond=0
\def\translate{0}
\else
\def\translate{\edge/2}
\fi
\ifnum\condI=0 %divisibile per 3
\gdef\circles{3}
\else
\pgfmathtruncatemacro{\temp}{\condI-1}
\global\let\circles\temp
\fi
\begin{pgfonlayer}{background}
\ClipOn{\N*\edge}
\draw[lightgray] (-\N*\edge,\row*\edge*\cosine)--++(2*\N*\edge,0);
\end{pgfonlayer}
\foreach \column in {-\N,...,\N}{
\coordinate (\row/\column) at (\column*\edge+\translate,\row*\edge*\cosine);
\ifnum0=\row
\begin{pgfonlayer}{background}
\ClipOn{\N*\edge}
\foreach \deltaAng in {60,120}{
\draw[lightgray] ($(\row/\column)+(180+\deltaAng:\N*\edge)$)--
($(\row/\column)+(\deltaAng:\N*\edge)$);
}
\end{pgfonlayer}
\fi
\ifnum\LabelsOn=1
\node[above] at (\row/\column){\row/\column};
\fi
\fill (\row/\column)circle[radius=\IntRadius];
\ifnum\circles=3
\ifnum\cond=0
\draw (\row/\column)circle[radius=\ExtRadius];
\else
\draw ($(\row/\column)+(-2*\edge,0)$)circle[radius=\ExtRadius];
\fi
\pgfmathtruncatemacro{\temp}{\circles-2}
\else
\pgfmathtruncatemacro{\temp}{\circles+1}
\fi
\global\let\circles\temp
}
\ifnum\cond=0
\gdef\cond{1}
\else
\gdef\cond{0}
\fi
}
\draw[line width=1pt] (P1)--(P2)--(P3)--(P4)--(P5)--(P6)--cycle;
\draw[dashed,line width=1pt] (2/1)--(2/-1)--(0/-2)--(-2/-1)--(-2/1)--(0/2)--cycle;
\draw[->,red,line width=1.1pt](0/0)--(1/1)--(0/2);
\draw[->,red,line width=1.1pt](0/0)--(2/0);
\end{tikzpicture}
\end{document}
结果如下:
输入为:
- \edge = 三角形边长
- \N = 半径上的节点数减去中心节点数
- \LabelsOn = 如果设置为 1,则显示所有节点名称。绘制箭头和六边形很有用。请参阅下图了解我的意思。
我可以添加圆半径输入等等。
如果有人有改进代码的想法,欢迎您。
希望能帮助到你。
阿宗