TransWikia.com

Tikz: Lebesgue curve / Z curve

TeX - LaTeX Asked on January 5, 2021

I tried to draw a Z curve (Lebesgue curve) by using this example for Hilbert curves. But I don’t see why it does not work.

documentclass[12pt]{article}

usepackage{tikz}
usepackage{ifthen}

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

newdimenHilbertLastX
newdimenHilbertLastY
newcounter{HilbertOrder}

defDrawToNext#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]
defHilbert[#1,#2,#3,#4,#5,#6,#7,#8] {
  ifnumvalue{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
}

defhilbert((#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}

I get this result which is obviously wrong: enter image description here

Any suggestions how to make it work? Thank you!

One Answer

Here's a way using the lindenmayersystems library. Orders above 8 will take ages:

documentclass[tikz,border=5]{standalone}
usetikzlibrary{lindenmayersystems}
pgfdeclarelindenmayersystem{z-curve}{
symbol{I}{pgftransformscale{+0.25pgflsystemstep}}
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}

enter image description here

Correct answer by Mark Wibrow on January 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP