Draw the Hilbert curve using slashes
Python, 282
from numpy import*
def r(n):
x=2**n-2;b=3*x/2+1;c=x/2+1;a=zeros((x*2+2,)*2,int);a[x+1,x+1]=1;a[b,x/2]=a[x/2,b]=-1
if n>1:s=r(n-1);a[:x,c:b]=rot90(s,3)*-1;a[c:b,:x]|=rot90(s)*-1;a[c:b,x+2:]|=s;a[x+2:,c:b]|=s
return a
for l in r(input()):print''.join(' /\\'[c] for c in l).rstrip()
This uses a recursive approach to construct the nth order Hilbert curve out of the previous curve. The curves are represented as a 2d numpy array for better slicing and manipulation.
Here are some examples:
$ python hilbert.py
2
/
\/\
/\ \
/ /\/
\ \
\/
$ python hilbert.py
3
\
/\/
/ /\
\/\ \ \
/\ / / /
/ / \/ \/\
\ \/\ /\ \
\/ / / / /\/
/\/ / \ \
\ \/\ \/
\/\ \
/ /\/
\ \
\/
$ python hilbert.py
4
/
\/\
/\ \
/ / /\/
\ \ \ /\
/\/ \/ \ \
/ /\ /\/ /
\/\ \ \ \ \/\
/\ / / \ \/\ \
/ / \/ /\/ / /\/
\ \/\ / /\/ / /\
/\/ / \/\ \ \/\ \ \
/ /\/ /\ / / /\ / / /
\/\ \ / / \/ / / \/ \/\
/\ \ \ \ \/\ \ \/\ /\ \
/ /\/ \/ / /\/ / / / /\/
\ \ /\ /\/ \ /\/ / \ \
\/ \ \ \ /\/ \ \/\ \/
/\/ / / / /\ \/\ \
\ \/ \/\ \ \ / /\/
\/\ /\ / / / \ \
/ / / \/ \/\ \/
\ \ \/\ /\ \
\/ / / / /\/
/\/ / \ \
\ \/\ \/
\/\ \
/ /\/
\ \
\/
Ruby, 247 230 205 characters
r=?D
y=d=0
z=(1..2*x=2**gets.to_i.times{r.gsub!(/\w/){$&<?H?'-H~+D~D+~H-':'+D~-H~H-~D+'}}-1).map{' '*2*x}
r.bytes{|c|c>99?(z[y-=s=-~d/2%2][x-=1-d/2]='/\\'[d%2]
x+=d/2
y+=1-s):d-=c
d%=4}
puts z.map &:rstrip
An ASCII-turtle approach using the Lindenmayer representation (try here).
Big thank you to @Ventero for some more golfing.
Malsys - 234 221 characters
I smell some L-systems here :) Malsys is online L-system interpreter. This is not really serious entry but I felt like this solution is somewhat interesting.
Syntax of Malsys is not really good for golfing since it contains a lot of lengthy keywords but still, it's quite short, readable, and expressive.
lsystem HilbertCurveAscii {
set symbols axiom = R;
set iterations = 5;
set rightAngleSlashMode = true;
interpret F as DrawLine;
interpret + as TurnLeft;
interpret - as TurnRight;
rewrite L to + R F - L F L - F R +;
rewrite R to - L F + R F R + F L -;
}
process all with HexAsciiRenderer;
http://malsys.cz/g/3DcVFMWn
Interpreter: http://malsys.cz/Process
Golfed version:
lsystem H{set symbols axiom=R;set iterations=3;set
rightAngleSlashMode=1;interpret.as DrawLine;interpret+as
TurnLeft;interpret-as TurnRight;rewrite L to+R.-L.L-.R+;rewrite
R to-L.+R.R+.L-;}process H with HexAsciiRenderer;
And how about Ascii hexagonal Gosper curve? :)
____
____ \__ \
\__ \__/ / __
__/ ____ \ \ \
/ __ \__ \ \/
\ \ \__/ / __
\/ ____ \/ /
\__ \__/
__/
http://malsys.cz/g/ae5v5vGB