% Hilbert curve
%
% Variables affecting L-system drawing
%
% anglestep - how much does a "+" or "-" rotate us by?
% direction - the angle (from horizontal) we start at.
% length - the initial length of a line segment
% start-point - (0,0) is at bottom left
/anglestep 90 def
/direction 90 def
/maxdepth { 6 } def
/length { 4 } def
/start_point { 10 10 } def
% Some defines to let us use more traditional rule descriptions
/START { saveposition } def
/END { restoreposition } def
/* { drawline } def
/F { drawline } def
/@ { 0.25 changelength } def
/+ { turnleft } def
/- { turnright } def
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Rules
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/A { - recurseB F + recurseA F recurseA + F recurseB - } def
/B { + recurseA F - recurseB F recurseB - F recurseA + } def
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% End of rules
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'recurse' is used as part of the rule to indicate that
% X should be repeated at this point. Since we don't
% have infinite memory or time, we only recurse up to maxdepth
/increasedepth { /depth depth 1 add def } def
/decreasedepth { /depth depth 1 sub def } def
/recurse
{
depth maxdepth lt
{ increasedepth X decreasedepth } {} ifelse
} def
/recurseA
{
depth maxdepth lt
{ increasedepth A decreasedepth } {} ifelse
} def
/recurseB
{
depth maxdepth lt
{ increasedepth B decreasedepth } {} ifelse
} def
% Helpers - convert from angle to horizontal/vertical projection.
/angletohorz { cos } def
/angletovert { sin } def % angle of 45 give 0.707
% Actions
/drawline
{
direction angletohorz length mul
direction angletovert length mul
rlineto
} def
/turnright { /direction direction anglestep sub def } def
/turnleft { /direction direction anglestep add def } def
/saveposition { direction length currentpoint } def
/restoreposition { moveto /length exch def /direction exch def } def
/changelength { /length exch length mul def } def
%
% Start of main code
%
/depth { 0 } def
start_point moveto
A
stroke
showpage