[pstricks] Hilbert Curve with TeX and PSTricks
eugene.ressler at frontiernet.net
eugene.ressler at frontiernet.net
Sat Nov 13 05:55:57 CET 2004
Have been trying to learn something about the exotic inner workings of
TeX and so wrote the code below as an experiment. Perhaps someone else
will get the same smile out of it that I do.
Even better, the good TeX hackers in the group might say how it can be
better done. (I know this would be easy in Postscript. The idea was to
learn about recursive macro expansion.)
This draws a 64x64 curve (n=6). At n=7, MikTeX dies with an out of main
memory error (2 million words)!
Cheers,
Gene
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Draw Hilbert curve in TeX with PSTricks
% Gene Ressler 2004
%
% This blows out 2m words memory at n=7 !
%
\documentclass{article}
\oddsidemargin=0in
\textwidth=6.5in
\usepackage{pstricks}
\newcount\n % recursion depth
\newcount\x\newcount\y % pen location
\newcount\xo\newcount\yo % old pen location
\newcount\dx\newcount\dy % next step to take
\newcount\t % temp for swap
%
% swap step deltas
\def\swp{\t=\dx\dx=\dy\dy=\t}
% turn so step is pi/2 left of where it was
\def\L{\swp\multiply\dx by-1}
% turn so step is pi/2 right of where it was
\def\R{\swp\multiply\dy by-1}
% take a step with pen down
\def\S{%
\xo=\x \yo=\y %
\advance\x by\dx%
\advance\y by\dy%
\ln{\the\xo}{\the\yo}{\the\x}{\the\y}}
% draw a line from old to new pen position
\def\ln#1#2#3#4{\qline(#1,#2)(#3,#4)}
% a compact version of the hilbert recursion!
% procedure h(order, angle) is
% begin
% if order > 0 then
% turn(angle)
% h(order - 1, -angle)
% step
% turn(-angle)
% h(order - 1, angle)
% step
% h(order - 1, angle)
% turn(-angle)
% step
% h(order - 1, -angle)
% turn(angle)
% end if
% end
%
% represent angle, which is +-90 degrees,
% as a [dx,dy] vector instead
\def\h#1#2{\ifnum\n=0\relax\else%
\advance\n by-1%
#1\h#2#1\S#2\h#1#2\S\h#1#2#2\S\h#2#1#1%
\advance\n by1\fi}
\begin{document}
% set up and draw
\psset{unit=2mm}
\begin{center}
{\bf\LARGE Hilbert Curve}\\[6ex]
\fbox{\begin{pspicture}(0,0)(63,63)
\x=0 \y=0
\dx=1 \dy=0
\n=6 \h\L\R
\end{pspicture}}
\end{center}
\end{document}
More information about the PSTricks
mailing list