Tikz 中的超晶格

Tikz 中的超晶格

我正在尝试弄清楚如何在 tikz 上绘制两个以相对扭曲角度堆叠在一起的(六边形)格子——请参阅随附的示例。我知道如何使用 for 循环绘制格子,但我对如何绘制另一个按给定角度旋转的格子有点困惑。提前致谢!

......................................... 在此处输入图片描述 在此处输入图片描述

答案1

此类效果可以通过非线性变换实现。如果您提供处方的更多详细信息,则可以进行相应调整。在这个版本中,我使用了\flagtransrformation来自这个答案,格点取自这个答案

\documentclass[tikz,border=3.14mm]{standalone}
\usepgfmodule{nonlineartransformations} 
\makeatletter
\def\latticetilt{%
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
%\typeout{old\space x=\pgf@xa\space old \space y=\pgf@ya}%
\pgfmathsetmacro{\myx}{\pgf@xa+3*sin(\pgf@ya*1.8)}%
\pgf@x=\myx pt%
\pgfmathsetmacro{\myy}{\pgf@ya+3*sin(\pgf@xa*1.8)}%
%\typeout{at\space x=\the\pgf@xa:\space new\space y=\myy}%
\pgf@y=\myy pt}
\makeatother
\begin{document}
\begin{tikzpicture}[hexa lattice/.style={insert path={% https://tex.stackexchange.com/a/6025/121799
foreach \i in {\the\numexpr-#1-1\relax,...,#1} 
   {foreach \j in {\the\numexpr-#1*2\relax,...,\the\numexpr#1*2\relax} {
  foreach \a in {0,120,-120} { ({3*\i},{2*sin(60)*\j}) -- +(\a:1)}
  foreach \a in {0,120,-120} { ({3*\i+3*cos(60)},{2*sin(60)*\j+sin(60)}) --
  +(\a:1)}}}}},hexa lattice/.default=3]
  \clip (-5,-5) rectangle (5,5);
  \draw[scale=0.3,hexa lattice=5];  
  \begin{scope}
  \pgftransformnonlinear{\latticetilt}
  \draw[blue,scale=0.3,hexa lattice=5];
  \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

这是将参数存储在 pgf 键中的变体。

\documentclass[tikz,border=3.14mm]{standalone}
\usepgfmodule{nonlineartransformations} 
\makeatletter
\def\latticetilt{%
\pgf@xa=\pgf@x%
\pgf@ya=\pgf@y%
%\typeout{old\space x=\pgf@xa\space old \space y=\pgf@ya}%
\pgfmathsetmacro{\myx}{\pgf@xa+\pgfkeysvalueof{/tikz/lattice/amplitude}*sin((\pgf@ya/\pgfkeysvalueof{/tikz/lattice/spacing})*360/\pgfkeysvalueof{/tikz/lattice/superlattice period})}%
\pgf@x=\myx pt%
\pgfmathsetmacro{\myy}{\pgf@ya+\pgfkeysvalueof{/tikz/lattice/amplitude}*sin((\pgf@xa/\pgfkeysvalueof{/tikz/lattice/spacing})*360/\pgfkeysvalueof{/tikz/lattice/superlattice period})}%
%\typeout{at\space x=\the\pgf@xa:\space new\space y=\myy}%
\pgf@y=\myy pt}
\makeatother
\begin{document}
\begin{tikzpicture}[hexa lattice/.style={insert path={% https://tex.stackexchange.com/a/6025/121799
foreach \i in {\the\numexpr-#1-1\relax,...,#1} 
   {foreach \j in {\the\numexpr-#1*2\relax,...,\the\numexpr#1*2\relax} {
  foreach \a in {0,120,-120} { 
  ({\pgfkeysvalueof{/tikz/lattice/spacing}*3*\i},{\pgfkeysvalueof{/tikz/lattice/spacing}*2*sin(60)*\j}) 
  -- +(\a:\pgfkeysvalueof{/tikz/lattice/spacing})}
  foreach \a in {0,120,-120} { 
  ({\pgfkeysvalueof{/tikz/lattice/spacing}*(3*\i+3*cos(60))},
  {\pgfkeysvalueof{/tikz/lattice/spacing}*(2*sin(60)*\j+sin(60))}) --
  +(\a:\pgfkeysvalueof{/tikz/lattice/spacing})}}}}},hexa lattice/.default=3,
  lattice/.cd,spacing/.initial=1,superlattice
  period/.initial=30,amplitude/.initial=3]
  \clip (-5,-5) rectangle (5,5);
  \draw[lattice/spacing=0.3cm,hexa lattice=5];  
  \begin{scope}
  \pgftransformnonlinear{\latticetilt}
  \draw[blue,lattice/spacing=0.3cm,hexa lattice=5];
  \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

这个问题很有趣,因为绘制六角形网格的其他方法在这里并不适用。

\documentclass[border=9,tikz]{standalone}

\begin{document}

\tikz{
    \draw(-5,-5)rectangle(5,5);
    \begin{pgfinterruptboundingbox}
        \draw[x=10.3923pt,y=18pt,dash pattern={on 6pt off 12pt},cyan]
            foreach\j in{0,120,240}{
                [rotate=\j]
                foreach\i in{-10,...,9}{
                    (\i,-10)--(\i,10)(\i+.5,-10.5)--(\i+.5,10.5)
                }
            }
        ;
        \draw[x=10.3923pt,y=18pt,dash pattern={on 6pt off 12pt},rotate=5]
            foreach\j in{0,120,240}{
                [rotate=\j]
                foreach\i in{-10,...,9}{
                    (\i,-10)--(\i,9.5)(\i+.5,-9.5)--(\i+.5,10)
                }
            }
        ;
    \end{pgfinterruptboundingbox}
}


\end{document}

相关内容