

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

_Title "Hilbert Curve"
Dim Shared As Integer sw, sh, wide, cell
 
wide = 128: cell = 4
sw = wide * cell + cell
sh = sw
 
Screen _NewImage(sw, sh, 8)
Cls , 15: Color 0
PSet (wide * cell, wide * cell)
 
Call Hilbert(0, 0, wide, 0, 0)
 
Sleep
System
 
Sub Hilbert (x As Integer, y As Integer, lg As Integer, p As Integer, q As Integer)
    Dim As Integer iL, iX, iY
    iL = lg: iX = x: iY = y
    If iL = 1 Then
        Line -((wide - iX) * cell, (wide - iY) * cell)
        Exit Sub
    End If
    iL = iL \ 2
    Call Hilbert(iX + p * iL, iY + p * iL, iL, p, 1 - q)
    Call Hilbert(iX + q * iL, iY + (1 - q) * iL, iL, p, q)
    Call Hilbert(iX + (1 - p) * iL, iY + (1 - p) * iL, iL, p, q)
    Call Hilbert(iX + (1 - q) * iL, iY + q * iL, iL, 1 - p, q)
End Sub