I'm trying to work out how to plot two (hexagonal) lattices stacked on top of each other at a relative twist angle on tikz -- see the attached examples. I know how to plot a lattice using a for loop, but I'm a bit stuck on how to plot another one rotated by a given angle. Thanks in advance!
Asked
Active
Viewed 402 times
4
-
1Maybe related: https://tex.stackexchange.com/questions/49169 – Dr. Manuel Kuehner May 26 '19 at 17:22
-
Welcome to TeX-SE! Since you are saying you can draw the ordinary lattices, it would make a lot of sense if you added them to your question. It is quite conceivable that nonlinear transformations may allow you to tilt the lattice but you will have much better chances if those who are considering trying this out didn't have to start from scratch. – May 26 '19 at 18:02
2 Answers
4
This question is interesting since other methods to draw hexagonal grid does not quite work here.
\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}
Symbol 1
- 36,855
4
Such effects can be achieved with nonlinear transformations. If you provide more detailed information the prescriptions, this can be adjusted accordingly. In this version, I use a slight generalization of the \flagtransrformation from this answer, and the lattice points are taken from this answer.
\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}
This is a variation in which the parameters are stored in pgf keys.
\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}
-
1
-
-
1@manooooh They only shift x by a periodic function of the (unshifted) y coordinate and y by a periodic function of the (unshifted) x coordinate. This preserves the periodicity of the lattice. One can make this much more customizable, of course, if there is a clear prescription on the various periods. – May 26 '19 at 20:38




