我想使用 Lindenmayer 系统在 LaTeX 中绘制 Z 曲线TikZ
。但我没能将曲线置于每个要点的中心。此外,我找不到任何优雅的方法使所有曲线具有相同的大小。
以下是我得到的结果:
这是我的代码:
\documentclass[a4paper,11pt]{article}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{lindenmayersystems}
\pgfdeclarelindenmayersystem{z-curve}{
\symbol{I}{\pgftransformscale{+0.25\pgflsystemstep}}
\symbol{p}{%
\pgfpathmoveto{\pgfqpoint{1pt}{3pt}}\pgfpathlineto{\pgfqpoint{3pt}{3pt}}%
\pgfpathlineto{\pgfqpoint{1pt}{1pt}}\pgfpathlineto{\pgfqpoint{3pt}{1pt}}%
}
\symbol{q}{%
\pgfpathlineto{\pgfqpoint{1pt}{3pt}}\pgfpathlineto{\pgfqpoint{3pt}{3pt}}%
\pgfpathlineto{\pgfqpoint{1pt}{1pt}}\pgfpathlineto{\pgfqpoint{3pt}{1pt}}%
}
\symbol{S}{\pgftransformscale{+0.5pt}}
\symbol{A}{\pgftransformshift{\pgfqpoint{0pt}{4pt}}}
\symbol{B}{\pgftransformshift{\pgfqpoint{4pt}{4pt}}}
\symbol{C}{\pgftransformshift{\pgfqpoint{4pt}{0pt}}}
\symbol{[}{\bgroup}
\symbol{]}{\egroup}
\rule{Z -> Ip}
\rule{p -> S[Ap][Bq][q][Cq]}
\rule{q -> S[Aq][Bq][q][Cq]}
}
\begin{figure}[ht]
\centering
\subcaptionbox{}
{\begin{tikzpicture}[scale=2]
\draw [l-system={z-curve, step=1cm, axiom=Z, order=1}] l-system;
\end{tikzpicture}}
\hspace{1cm}
\subcaptionbox{}
{\begin{tikzpicture}[scale=2]
\draw [l-system={z-curve, step=1cm, axiom=Z, order=2}] l-system;
\end{tikzpicture}}
\hspace{1cm}
\subcaptionbox{}
{\begin{tikzpicture}[scale=2]
\draw [l-system={z-curve, step=1cm, axiom=Z, order=3}] l-system;
\end{tikzpicture}}
\hspace{1cm}
\subcaptionbox{}
{\begin{tikzpicture}[scale=2]
\draw [l-system={z-curve, step=1cm, axiom=Z, order=4}] l-system;
\end{tikzpicture}}
\end{figure}
\end{document}
有什么建议我应该改变什么才能得到我想要的?
答案1
1. 设法将曲线定位在每个要点的中心:
我改变了符号 p 和 q(水平域:0pt
改为2pt
;而不是改为1pt
)3pt
,以实现水平居中。垂直方向也从 开始,0pt
而不是1pt
。使用 ,\draw(<x>,<y>)...
您可以稍后任意放置曲线。例如使用\draw(0,0)
。
\symbol{p}{%
\pgfpathmoveto{\pgfqpoint{0pt}{2pt}}\pgfpathlineto{\pgfqpoint{2pt}{2pt}}%
\pgfpathlineto{\pgfqpoint{0pt}{0pt}}\pgfpathlineto{\pgfqpoint{2pt}{0pt}}%
}
\symbol{q}{%
\pgfpathlineto{\pgfqpoint{0pt}{2pt}}\pgfpathlineto{\pgfqpoint{2pt}{2pt}}%
\pgfpathlineto{\pgfqpoint{0pt}{0pt}}\pgfpathlineto{\pgfqpoint{2pt}{0pt}}%
}
2. 所有曲线尺寸相同:
与\resizebox{<horizontal size>}{<vertical size>}{...}
(参见:4.3.2 缩放至所需大小,手册第 8 页 [“‘graphics’ 包中的包”]),您可以设置 的水平和垂直尺寸tikzpicture
。如果您希望tikzpicture
按比例缩放 ,您可以指定其中一个尺寸并输入!
另一个尺寸。
\resizebox{!}{2cm}{
\begin{tikzpicture}
\draw[black](0,0) l-system [l-system={z-curve, step=1cm, axiom=Z, order=1}];
\end{tikzpicture}
}
3.注意事项
我使用\subfloat
并减少了包的导入,因为 TikZ 会graphicx
自动加载。
结果:
梅威瑟:
\documentclass[a4paper,11pt]{article}
\usepackage{tikz}
\usepackage{subfig}
\begin{document}
\usetikzlibrary{lindenmayersystems}
\pgfdeclarelindenmayersystem{z-curve}{
\symbol{I}{\pgftransformscale{+0.25\pgflsystemstep}}
\symbol{p}{%
\pgfpathmoveto{\pgfqpoint{0pt}{2pt}}\pgfpathlineto{\pgfqpoint{2pt}{2pt}}%
\pgfpathlineto{\pgfqpoint{0pt}{0pt}}\pgfpathlineto{\pgfqpoint{2pt}{0pt}}%
}
\symbol{q}{%
\pgfpathlineto{\pgfqpoint{0pt}{2pt}}\pgfpathlineto{\pgfqpoint{2pt}{2pt}}%
\pgfpathlineto{\pgfqpoint{0pt}{0pt}}\pgfpathlineto{\pgfqpoint{2pt}{0pt}}%
}
\symbol{S}{\pgftransformscale{+0.5pt}}
\symbol{A}{\pgftransformshift{\pgfqpoint{0pt}{4pt}}}
\symbol{B}{\pgftransformshift{\pgfqpoint{4pt}{4pt}}}
\symbol{C}{\pgftransformshift{\pgfqpoint{4pt}{0pt}}}
\symbol{[}{\bgroup}
\symbol{]}{\egroup}
\rule{Z -> Ip}
\rule{p -> S[Ap][Bq][q][Cq]}
\rule{q -> S[Aq][Bq][q][Cq]}
}
\begin{figure}[h!]
\centering
\subfloat[]{\label{fig:a}
\resizebox{!}{2cm}{
\begin{tikzpicture}
\draw[black](0,0) l-system [l-system={z-curve, step=1cm, axiom=Z, order=1}];
\end{tikzpicture}
}
}
\hfill
\subfloat[]{\label{fig:a}
\resizebox{!}{2cm}{
\begin{tikzpicture}
\draw [red](0,0) l-system [l-system={z-curve, step=1cm, axiom=Z, order=2}];
\end{tikzpicture}
}
}
\hfill
\subfloat[]{\label{fig:a}
\resizebox{!}{2cm}{
\begin{tikzpicture}
\draw [blue](0,0) l-system [l-system={z-curve, step=1cm, axiom=Z, order=3}];
\end{tikzpicture}
}
}
\hfill
\subfloat[]{\label{fig:a}
\resizebox{!}{2cm}{
\begin{tikzpicture}
\draw [green](0,0) l-system [l-system={z-curve, step=1cm, axiom=Z, order=4}];
\end{tikzpicture}
}
}
\hfill
\caption{Z-Curve using TikZ and the Lindenmayer system}\label{fig:LabelFigOne}
\end{figure}
\end{document}