L-System > Curva di Hilbert

Vedi la discussione

import math
import turtle
#-----------------------------------------------
WIDTH  = 800       # Dimensione finestra
HEIGHT = 800       # ...
LIVELLI= 8         # Numero livelli
#-----------------------------------------------
W      =-WIDTH*0.45      # Posizione iniziale
H      =-HEIGHT*0.45     # ...
DIR    = 0               # Direzione iniziale
ANGOLO = 90              # Angolo a sinistra
#------------------------  Passo della tartaruga
SIZE   = WIDTH*0.9 / (2**(LIVELLI-1)-1)
#-----------------------------------------------
R      = 'A'
REGOLE = {'A':'+BF-AFA-FB+', 'B':'-AF+BFB+FA-',
          'F':'F', '+':'+', '-':'-'}
#-----------------------------------------------
def trasforma(R):
    R2=''
    for x in R:
        R2 += REGOLE[x]
    return R2
#-----------------------------------------------
def prepara():
    turtle.setup(width=WIDTH, height=HEIGHT)
    turtle.title("Livello="+str(LIVELLI))
    turtle.bgcolor('pink')
    turtle.pencolor("red")
    turtle.pensize(2)
    turtle.penup()
    turtle.shape('turtle')
    turtle.speed(0)
    turtle.setposition(W,H)
    turtle.setheading(DIR)
    turtle.pendown()
    turtle.hideturtle()
    turtle.tracer(0)
#-----------------------------------------------
def disegna(R):
    for x in R:
        if(x == '+'):
            turtle.left(ANGOLO)
        elif(x == '-'):
            turtle.right(ANGOLO)
        elif(x == 'F'):
            turtle.forward(SIZE)
#-----------------------------------------------
for j in range(LIVELLI-1):
    R=trasforma(R)

print(len(R), '\t', str().join(R), '\n')
prepara()
disegna(R)
turtle.update()

Lascia un commento