Fibonacci Word – Zoom

import turtle
#-----------------------------------------------
WIDTH   = 800  # Dimensione finestra
HEIGHT  = 800  # ...
ANGOLO  = 90   # Angolo specifico della figura
#-----------------------------------------------
LIVELLI = 19   # Numero livelli
DIR     = 90   # Direzione iniziale
#-----------------------------------------------
R      = ['0']
REGOLE = {'0':'01','1':'0'}
#-----------------------------------------------
def trasforma(R):
    R2=''
    for x in R:
        R2 += REGOLE[x]
    return R2
#-----------------------------------------------
def prepara():
    global dX, dY
    SIZE = 1
    turtle.setup(width=WIDTH, height=HEIGHT)
    turtle.bgcolor('pink')
    turtle.hideturtle()
    turtle.pencolor("red")
    turtle.pensize(20-LIVELLI)
    turtle.penup()
    turtle.setposition(0, 0)
    turtle.setheading(DIR)
    turtle.title("L-System: Fibonacci Word, livello="
                 +str(LIVELLI))
    turtle.tracer(0)
    minX=minY=maxX=maxY=0
    EVEN = False
    for m in R:
        turtle.forward(SIZE)        
        if(m == '0'):
            if(EVEN):
                turtle.left(ANGOLO)
            else:
                turtle.right(ANGOLO)
        EVEN = not EVEN

        x, y = turtle.position()
        if(x < minX): minX = x
        if(y < minY): minY = y
        if(x > maxX): maxX = x
        if(y > maxY): maxY = y
    turtle.update()
    dX = (maxX-minX)
    dY = (maxY-minY)

def disegna():
    if(dX >= dY):
        dXY  = dX
    else:
        dXY = dY
    SIZE = WIDTH*0.90/dXY    
    W = -SIZE*dX/2
    H = -SIZE*dY/2
    turtle.setposition(W, H)
    turtle.setheading(DIR)
    turtle.pendown()
    turtle.tracer(0)
    EVEN = False
    for m in R:
        turtle.forward(SIZE)        
        if(m == '0'):
            if(EVEN):
                turtle.left(ANGOLO)
            else:
                turtle.right(ANGOLO)
        EVEN = not EVEN
    turtle.update()
#-----------------------------------------------
for j in range(LIVELLI-1):
    R = trasforma(R)
print(len(R), R)
prepara()
disegna()

Lascia un commento