Tikz:勒贝格曲线/Z 曲线

Tikz:勒贝格曲线/Z 曲线

我试着画一个Z 曲线(勒贝格曲线)通过使用希尔伯特曲线的例子。但我不明白为什么它不起作用。

\documentclass[12pt]{article}

\usepackage{tikz}
\usepackage{ifthen}

\usepackage[position=top,labelformat=empty]{subfig}
\usepackage{verbatim}

\newdimen\HilbertLastX
\newdimen\HilbertLastY
\newcounter{HilbertOrder}

\def\DrawToNext#1#2{%
   \advance \HilbertLastX by #1
   \advance \HilbertLastY by #2
   \pgfpathlineto{\pgfqpoint{\HilbertLastX}{\HilbertLastY}}
}

% \Hilbert[right_x,right_y,left_x,left_x,up_x,up_y,down_x,down_y]
\def\Hilbert[#1,#2,#3,#4,#5,#6,#7,#8] {
  \ifnum\value{HilbertOrder} > 0%
     \addtocounter{HilbertOrder}{-1}
     \Hilbert[#5,#6,#7,#8,#1,#2,#3,#4]
     \Hilbert[#1,#2,#3,#4,#5,#6,#7,#8]
     \DrawToNext {#1} {#5}
     \Hilbert[#1,#2,#3,#4,#5,#6,#7,#8]
     \DrawToNext {#3} {#3}
     \Hilbert[#7,#8,#5,#6,#3,#4,#1,#2]
     \DrawToNext {#1} {#5}
     \addtocounter{HilbertOrder}{1}
  \fi
}

\def\hilbert((#1,#2),#3){%
   \advance \HilbertLastX by #1
   \advance \HilbertLastY by #2
   \pgfpathmoveto{\pgfqpoint{\HilbertLastX}{\HilbertLastY}}
   \setcounter{HilbertOrder}{#3}
   \Hilbert[1mm,0mm,-1mm,0mm,0mm,1mm,0mm,-1mm]
   \pgfusepath{stroke}%
}

\begin{document}
\begin{figure}%
    \centering
    \subfloat[$n=1$]{\tikz[scale=18] \hilbert((0mm,0mm),1);}~~
    \subfloat[$n=2$]{\tikz[scale=6] \hilbert((0mm,0mm),2);}~~
    \subfloat[$n=3$]{\tikz[scale=2.6] \hilbert((0mm,0mm),3);}~~
    \subfloat[$n=4$]{\tikz[scale=1.2] \hilbert((0mm,0mm),4);}~~
    \subfloat[$n=5$]{\tikz[scale=0.58] \hilbert((0mm,0mm),5);}%
\end{figure}%

\end{document}

我得到了这个显然是错误的结果:在此处输入图片描述

有什么建议可以让它发挥作用吗?谢谢!

答案1

这是使用该库的方法lindenmayersystems。上面的命令8将需要年龄

\documentclass[tikz,border=5]{standalone}
\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{document}
\foreach \i in {1,...,9}{
\begin{tikzpicture}[line join=round]
\draw [draw=red] (0,0) rectangle (5,5) (2.5, 5) node [above] {order: \i};
\draw [l-system={z-curve, step=5cm, axiom=Z, order=\i}] l-system;
\end{tikzpicture}}
\end{document}

在此处输入图片描述

相关内容