

Task
Produce a graphical or ASCII-art representation of a Hilbert curve of at least order 3.

def Order=5, Size=15;   \length of line segment
int Dir, X, Y;
 
proc GoFwd;
[case Dir&3 of
 0: X:= X+Size;
 1: Y:= Y+Size;
 2: X:= X-Size;
 3: Y:= Y-Size
other   [];
Line(X, Y, \white\7);
];
 
proc Hilbert(Lev, Ang);
int  Lev, Ang;
[if Lev then
    [Dir:= Dir+Ang;
    Hilbert(Lev-1, -Ang);
    GoFwd;
    Dir:= Dir-Ang;
    Hilbert(Lev-1, Ang);
    GoFwd;
    Hilbert(Lev-1, Ang);
    Dir:= Dir-Ang;
    GoFwd;
    Hilbert(Lev-1, -Ang);
    Dir:= Dir+Ang;
    ];
];
 
[SetVid($12);   \640x480 graphics
Dir:= 0;  X:= 0;  Y:= 0;
Move(X, Y);
Hilbert(Order, 1);
]