Advertisement
Mangus875

turtle_fractal.py

Jan 8th, 2024
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. def rewrite(axiom, rules, times=1):
  2.     result = "";
  3.     for i in range(times):
  4.         for c in axiom:
  5.             if c in rules:
  6.                 result += rules[c]
  7.             else:
  8.                 result += c
  9.         axiom = result
  10.     return result
  11.  
  12. import turtle
  13. t = turtle.Turtle()
  14. t.speed(5)
  15. t.width(3)
  16. t.ht()
  17. t.tracer(50)
  18.  
  19. def hilbertSize(i):
  20.     return 2**i - 1
  21.  
  22. hilbIters = 5
  23. lineSeg = (t.window_width()-50) / hilbertSize(hilbIters)
  24. offset = hilbertSize(hilbIters)*lineSeg/2
  25. t.pu()
  26. t.goto(-offset, offset)
  27. t.pd()
  28.  
  29. def hilbert(iteration):
  30.     path = "A"
  31.     newPath = ""
  32.     while iteration > 0:
  33.         for c in path:
  34.             if c == 'A':
  35.                 newPath += "+BF-AFA-FB+"
  36.             elif c == 'B':
  37.                 newPath += "-AF+BFB+FA-"
  38.             else:
  39.                 newPath += c
  40.         path = newPath
  41.         newPath = ""
  42.         iteration -= 1
  43.     return path
  44.  
  45. def cleanPath(path):
  46.     print(f"\tInput:\n{path}\n")
  47.     newPath = ""
  48.     for c in path:
  49.         if c == 'F' or c == '+' or c == '-':
  50.             newPath += c
  51.    
  52.     path = newPath
  53.     newPath = ""
  54.     print(f"\tRemoved variables:\n{path}\n")
  55.     for i in range(0, len(path)):
  56.         c = path[i]
  57.         print(f"{i} : {c}")
  58.         if c == 'F':
  59.             newPath += c
  60.         elif c == '+':
  61.             if i+1 < len(path) and path[i+1] == '-':
  62.                 continue
  63.             else:
  64.                 newPath += c
  65.         elif c == '-':
  66.             if i+1 < len(path) and path[i+1] == '+':
  67.                 continue
  68.             else:
  69.                 newPath += c
  70.                
  71.     path = newPath
  72.    
  73.     if path[len(path)-1] == '-' or path[len(path)-1] == '+':
  74.         path = path[0:len(path)-1]
  75.    
  76.     print(f"\tRemoved negated rotations:\n{path}\n")
  77.     return path
  78.     newPath = ""
  79.     for c in path:
  80.         if c == 'F' or c == '+' or c == '-':
  81.             newPath += c
  82.    
  83.     path = newPath
  84.     newPath = ""
  85.     for i in range(0, len(path)):
  86.         c = path[i]
  87.         if c == 'F':
  88.             newPath += c
  89.         elif c == '+':
  90.             if i+1 < len(path) and path[i+1] == '-':
  91.                 i += 1
  92.             else:
  93.                 newPath += c
  94.         elif c == '-':
  95.             if i+1 < len(path) and path[i+1] == '+':
  96.                 i += 1
  97.             else:
  98.                 newPath += c
  99.                
  100.     path = newPath
  101.     if path[len(path)-1] == '-' or path[len(path)-1] == '+':
  102.         path = path[0,len(path)-2]
  103.    
  104.     return path
  105.  
  106. def turn(ang):
  107.     t.seth(t.heading()+ang)
  108.  
  109. def drawPath(path):
  110.     for c in path:
  111.         if c == 'F':
  112.             t.fd(lineSeg)
  113.         elif c == '+':
  114.             turn(-90)
  115.         elif c == '-':
  116.             turn(90)
  117.     t.tracer(1)
  118.  
  119.  
  120. path = cleanPath(hilbert(hilbIters))
  121. drawPath(path)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement