% --- start of displayed preamble in the book ---

vardef hilbertPath(expr s, n) =
 save pa,pb,pc,A,B,C,shift,hilb,h;
 path pa,pb,pc,hilb,h; pair A,B,C,shift;
 if n=0:  hilb=origin;
 else:    shift=(s, 0);
   h=hilbertPath(s, n-1);
   pa=h reflectedabout(origin,(1, -1));
   A=point length(pa) of pa;
   pb=h shifted (A+shift);
   B=point 0 of pb;
   hilb=pa & A--B & pb;
   C=B shifted ((A+.5shift) rotated -90);
   pc=reverse hilb reflectedabout(C,C+right);
   hilb:=hilb & point length(hilb) of
         hilb--point 0 of pc  & pc;
 fi
 hilb
enddef;
% --- end of displayed preamble in the book ---
defaultfont:="ptmr8r";
warningcheck:=0;
beginfig(1)
side=5cm; n=5;
onestep=side/(2**n);
draw hilbertPath(onestep, n);
draw unitsquare scaled side
    shifted (-.5onestep,.5onestep-side);
endfig;
end;