Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def rewrite(axiom, rules, times=1):
- result = "";
- for i in range(times):
- for c in axiom:
- if c in rules:
- result += rules[c]
- else:
- result += c
- axiom = result
- return result
- 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):
- print(f"\tInput:\n{path}\n")
- newPath = ""
- for c in path:
- if c == 'F' or c == '+' or c == '-':
- newPath += c
- path = newPath
- newPath = ""
- print(f"\tRemoved variables:\n{path}\n")
- for i in range(0, len(path)):
- c = path[i]
- print(f"{i} : {c}")
- if c == 'F':
- newPath += c
- elif c == '+':
- if i+1 < len(path) and path[i+1] == '-':
- continue
- else:
- newPath += c
- elif c == '-':
- if i+1 < len(path) and path[i+1] == '+':
- continue
- else:
- newPath += c
- path = newPath
- if path[len(path)-1] == '-' or path[len(path)-1] == '+':
- path = path[0:len(path)-1]
- print(f"\tRemoved negated rotations:\n{path}\n")
- return path
- newPath = ""
- for c in path:
- if c == 'F' or c == '+' or c == '-':
- newPath += c
- path = newPath
- newPath = ""
- for i in range(0, len(path)):
- c = path[i]
- if c == 'F':
- newPath += c
- elif c == '+':
- if i+1 < len(path) and path[i+1] == '-':
- i += 1
- else:
- newPath += c
- elif c == '-':
- if i+1 < len(path) and path[i+1] == '+':
- i += 1
- else:
- newPath += c
- path = newPath
- if path[len(path)-1] == '-' or path[len(path)-1] == '+':
- path = path[0,len(path)-2]
- return path
- def turn(ang):
- t.seth(t.heading()+ang)
- def drawPath(path):
- for c in path:
- 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