Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- t = turtle.Turtle()
- t.speed(5)
- t.width(3)
- t.ht()
- t.tracer(50)
- def hilbertSize(i):
- return 2**i - 1
- hilbIters = 5
- lineSeg = (t.window_width()-50) / hilbertSize(hilbIters)
- offset = hilbertSize(hilbIters)*lineSeg/2
- t.pu()
- t.goto(-offset, offset)
- t.pd()
- def hilbert(iteration):
- path = "A"
- newPath = ""
- while iteration > 0:
- for c in path:
- if c == 'A':
- newPath += "+BF-AFA-FB+"
- elif c == 'B':
- newPath += "-AF+BFB+FA-"
- else:
- newPath += c
- path = newPath
- newPath = ""
- iteration -= 1
- return path
- def cleanPath(path):
- newPath = ""
- for c in path:
- if c == 'F' or c == '+' or c == '-':
- newPath += c
- return newPath
- def turn(ang):
- t.seth(t.heading()+ang)
- def drawPath(path):
- for c in path:
- #c = c.upper()
- if c == 'F':
- t.fd(lineSeg)
- elif c == '+':
- turn(-90)
- elif c == '-':
- turn(90)
- t.tracer(1)
- path = cleanPath(hilbert(hilbIters))
- drawPath(path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement