L-System > Curva a C (di Lévy)

Vedi la discussione


import math
import turtle
#-----------------------------------------------
WIDTH  = 800            # Dimensione finestra
HEIGHT = 500            # ...
LIVELLI= 14             # Numero livelli
#-----------------------------------------------
W      =-WIDTH*0.2      # Posizione iniziale
H      =-HEIGHT*0.25    # ...
DIR    = 0              # Direzione iniziale
ANGOLO = 45             # Angolo a sinistra
#-----------------------  Passo della tartaruga
sqrt2  = math.sqrt(2)
SIZE   = WIDTH*0.4 / sqrt2**(LIVELLI-1)
#-----------------------------------------------
R      = 'F'
REGOLE = {'F':'+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