是否可以只旋转形状,而保持内部的锚点和文本保持不变?
\documentclass[12pt,a4paper,preview,multi={tikzpicture},border=0pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\def\hexagonosize{30mm}
\def\desloca{-\pgflinewidth}
\tikzset{%
hexagono/.style={draw,shape=regular polygon,regular polygon sides=6,minimum size=\hexagonosize,inner sep=0pt},
hexagono2/.style={hexagono,rotate=90},
}
\begin{document}
\begin{tikzpicture}
\node[hexagono] (centro) at (0,0) {};
\node[hexagono,anchor=150,yshift=\desloca*sin(330),xshift=\desloca*cos(330)] (A) at (centro.330) {A};
\node[hexagono,anchor=210,yshift=\desloca*sin(30),xshift=\desloca*cos(30)] (B) at (centro.30) {B};
\node[hexagono,anchor=270,yshift=\desloca] (C) at (centro.90) {C};
\node[hexagono,anchor=330,yshift=\desloca*sin(150),xshift=\desloca*cos(150)] (D) at (centro.150) {D};
\node[hexagono,anchor=30,yshift=\desloca*sin(210),xshift=\desloca*cos(210)] (E) at (centro.210) {E};
\node[hexagono,anchor=90,yshift=-\desloca] (F) at (centro.270) {F};
\draw[thick] (A.90) -- (F.45);
\end{tikzpicture}
\begin{tikzpicture}
\node[hexagono2] (centro) at (0,0) {};
\node[hexagono2,anchor=150,yshift=\desloca*sin(330),xshift=\desloca*cos(330)] (A) at (centro.330) {A};
\node[hexagono2,anchor=210,yshift=\desloca*sin(30),xshift=\desloca*cos(30)] (B) at (centro.30) {B};
\node[hexagono2,anchor=270,yshift=\desloca] (C) at (centro.90) {C};
\node[hexagono2,anchor=330,yshift=\desloca*sin(150),xshift=\desloca*cos(150)] (D) at (centro.150) {D};
\node[hexagono2,anchor=30,yshift=\desloca*sin(210),xshift=\desloca*cos(210)] (E) at (centro.210) {E};
\node[hexagono2,anchor=90,yshift=-\desloca] (F) at (centro.270) {F};
\draw[thick] (A.90) -- (F.45);
\end{tikzpicture}
\end{document}
答案1
我将使用shape border rotate
@Claudio Fiandrino 在评论中建议的选项。
首先我们来看看锚点。右图是旋转后的六边形。注意 c1 表示锚点corner 1
,s1 表示锚点side 1
。
代码:
\documentclass[12pt,tikz,border=0pt]{standalone}
\usetikzlibrary{shapes.geometric}
\newcommand\hexagonosize{30mm}
\tikzset{%
hexagono/.style={draw,shape=regular polygon,regular polygon sides=6,minimum size=\hexagonosize,inner sep=0pt,outer sep=0pt},
hexagono2/.style={hexagono,shape border rotate=90},
}
\begin{document}
\begin{tikzpicture}[font=\scriptsize]
\node[hexagono](test){T};
\foreach \i/\l in {1/above,2/left}{
\node[blue,label={[blue]\l:c\i}] at (test.corner \i){x};
\node[blue,label={[blue]\l:s\i}] at (test.side \i){x};
}
\foreach \i/\l in {0/right,300/left}\node[green,label={[green]right:\i}] at (test.\i){x};
\node[green,label={[green]below:south}]at(test.south){x};
%
\begin{scope}[xshift=5cm]
\node[hexagono2](test){T};
\foreach \i/\l in {1/left,2/below left}{
\node[blue,label={[blue]\l:c\i}] at (test.corner \i){x};
\node[blue,label={[blue]\l:s\i}] at (test.side \i){x};
}
\foreach \i/\l in {0/right,300/left}\node[green,label={[green]right:\i}] at (test.\i){x};
\node[green,label={[green]below:south}]at(test.south){x};
\end{scope}
\end{tikzpicture}
\end{document}
shape border rotate
如您所见,如果使用,指南针和角度锚点不会旋转。只有几何锚点(如corner 1
和)side 1
会旋转。
但您也可以看到,必须使用其他角度锚点来将外六边形定位在中心周围:
\documentclass[12pt,tikz,border=0pt]{standalone}
\usetikzlibrary{shapes.geometric}
\newcommand\hexagonosize{30mm}
\tikzset{%
hexagono/.style={draw,shape=regular polygon,regular polygon sides=6,minimum size=\hexagonosize,
inner sep=0pt,outer sep=0pt},
hexagono2/.style={hexagono,shape border rotate=90},
}
\begin{document}
\begin{tikzpicture}
\node[hexagono] (centro) at (0,0) {};
\node[hexagono,anchor=150] (A) at (centro.330) {A};
\node[hexagono,anchor=210] (B) at (centro.30) {B};
\node[hexagono,anchor=270] (C) at (centro.90) {C};
\node[hexagono,anchor=330] (D) at (centro.150) {D};
\node[hexagono,anchor=30] (E) at (centro.210) {E};
\node[hexagono,anchor=90] (F) at (centro.270) {F};
\draw[thick,orange] (A.90) -- (F.45);
\end{tikzpicture}
\begin{tikzpicture}
\node[hexagono2] (centro) at (0,0) {};
\node[hexagono2] (centro) at (0,0) {};
\node[hexagono2,anchor=180] (A) at (centro.0) {A};
\node[hexagono2,anchor=240] (B) at (centro.60) {B};
\node[hexagono2,anchor=300] (C) at (centro.120) {C};
\node[hexagono2,anchor=0] (D) at (centro.180) {D};
\node[hexagono2,anchor=60] (E) at (centro.240) {E};
\node[hexagono2,anchor=120] (F) at (centro.300) {F};
\draw[thick,orange] (A.90) -- (F.45);
\end{tikzpicture}
\end{document}
结果:
请注意,我已经使用了outer sep=0pt
风格hexagono
。
但我建议使用side 1
,...,side 6
锚点来放置六边形。然后可以使用不同的旋转角度:
代码:
\documentclass[12pt,tikz,border=0pt]{standalone}
\usetikzlibrary{shapes.geometric}
\newcommand\hexagonosize{30mm}
\tikzset{%
hexagono/.style={draw,shape=regular polygon,regular polygon sides=6,minimum size=\hexagonosize,
inner sep=0pt,outer sep=0pt,shape border rotate=#1},
hexagono/.default={0},
}
\begin{document}
\foreach \rotangle in {0,45,90,200}{%
\begin{tikzpicture}
\node[hexagono=\rotangle](centro) at (0,0) {};
\foreach[count=\i,evaluate={\j=int(\i<=3?\i+3:\i-3)}] \n in {C,D,E,F,A,B}
\node[hexagono=\rotangle,anchor=side \j](\n)at(centro.side \i){\n};
\draw[orange,thick] (A.90) -- (F.45);
\end{tikzpicture}
}
\end{document}
如果节点A
应位于右侧,则centro
必须手动选择锚定在上的节点centro.side 1
。对于 0° 和 45°,它是C
(节点名称数组中的第 3 个节点 - 参见下面的代码),对于 90°,它是D
(第 4 个节点),对于 200°,它是F
(第 6 个节点)。
代码:
\documentclass[12pt,tikz,border=0pt]{standalone}
\usetikzlibrary{shapes.geometric}
\newcommand\hexagonosize{30mm}
\tikzset{%
hexagono/.style={draw,shape=regular polygon,regular polygon sides=6,minimum size=\hexagonosize,
inner sep=0pt,outer sep=0pt,shape border rotate=#1},
hexagono/.default={0},
}
\newcommand\hexagons{{"A","B","C","D","E","F"}}% pgfarray with the node names
\begin{document}
\foreach \rotangle/\start in {0/3,45/3,90/4,200/6}{%
\begin{tikzpicture}
\node[hexagono=\rotangle](centro) at (0,0) {};
%\node[font=\tiny]at(centro.side 1){x};% to mark the side 1 anchor of centro
\foreach[
evaluate={\j=int(\i<=3?\i+3:\i-3)},
evaluate={\n=\hexagons[int(\start+\i-1<=6?\start+\i-2:\start+\i-8)]},
] \i in {1,...,6}
\node[hexagono=\rotangle,anchor=side \j](\n)at(centro.side \i){\n};
\draw[thick,orange] (A.90) -- (F.45);
\end{tikzpicture}
}
\end{document}
更新:使用角度锚点
也可以以同样的方式使用角度锚点。你只需要知道节点必须定位在节点\posA
的哪个角度锚点上:centro
\documentclass[12pt,tikz,border=0pt]{standalone}
\usetikzlibrary{shapes.geometric}
\newcommand\hexagonosize{30mm}
\tikzset{%
hexagono/.style={draw,shape=regular polygon,regular polygon sides=6,minimum size=\hexagonosize,
inner sep=0pt,outer sep=0pt,shape border rotate=#1},
hexagono/.default={0},
}
\newcommand\hexagons{{"A","B","C","D","E","F"}}% pgfarray with the node names
\begin{document}
\foreach \rotangle/\posA in {0/-30,45/15,90/0,200/-10}{%
\begin{tikzpicture}
\node[hexagono=\rotangle] (centro) at (0,0) {};
\foreach[
evaluate={\pos=\posA+\i*60},
evaluate={\anc=\pos+180},
evaluate={\n=\hexagons[\i]}
]\i in {0,...,5}
\node[hexagono=\rotangle,anchor=\anc] (\n) at (centro.\pos) {\n};
\draw[thick,orange] (A.90) -- (F.45);
\end{tikzpicture}
}
\end{document}
结果与上面相同。但现在你也可以使用 your\desloca
代替outersep=0pt
:
\documentclass[12pt,tikz,border=0pt]{standalone}
\usetikzlibrary{shapes.geometric}
\newcommand\hexagonosize{30mm}
\tikzset{%
hexagono/.style={draw,shape=regular polygon,regular polygon sides=6,minimum size=\hexagonosize,
inner sep=0pt,
%outer sep=0pt,% do not use together with \desloca
shape border rotate=#1},
hexagono/.default={0},
}
\newcommand\desloca{-\pgflinewidth}
\newcommand\hexagons{{"A","B","C","D","E","F"}}% pgfarray with the node names
\begin{document}
\foreach \rotangle/\posA in {0/-30,45/15,90/0,200/-10}{%
\begin{tikzpicture}
\node[hexagono=\rotangle] (centro) at (0,0) {};
\foreach[
evaluate={\pos=\posA+\i*60},
evaluate={\anc=\pos+180},
evaluate={\n=\hexagons[\i]}
]\i in {0,...,5}
\node[hexagono=\rotangle,anchor=\anc,yshift=\desloca*sin(\pos),xshift=\desloca*cos(\pos)] (\n) at (centro.\pos) {\n};
\draw[thick,purple] (A.90) -- (F.45);
\end{tikzpicture}
}
\end{document}
正如您所见,结果仍然相同: