Curva di Koch – 2 *

Una variazione della curva di Koch.

import math
import turtle
#-----------------------------------------------
WIDTH  = 800             # Dimensione finestra
HEIGHT = 400             # ...
LIVELLI= 5               # Numero livelli
#-----------------------------------------------
W      =-WIDTH *0.45      # Posizione iniziale
H      =-HEIGHT*0.4       # ...
DIR    = 0                # Direzione iniziale
#-----------------------------------------------
ANGOLO = 120               # Angolo a sinistra
coseno = math.cos(math.radians(ANGOLO))
SIZE   = WIDTH*0.90/(3**(LIVELLI-1))
#-----------------------------------------------
R      = 'F'
REGOLE = {'F':'FF+F+F+FF','+':'+', '-':'-'}
#-----------------------------------------------
def trasforma(R):
    R2=''
    for x in R:
        R2 += REGOLE[x]
    return R2
#-----------------------------------------------
def prepara():
    turtle.setup(width=WIDTH, height=HEIGHT)
    turtle.bgcolor('pink')
    turtle.hideturtle()
    turtle.pencolor("red")
    turtle.pensize(1)
    turtle.penup()
    turtle.shape('turtle')
    turtle.setposition(W, H)
    turtle.setheading(DIR)
    turtle.title("L-System: curva di Koch 2*, livello="
                 + str(LIVELLI))
    turtle.pendown()
#-----------------------------------------------
def disegna(R):
    turtle.tracer(0)
    for x in R:
        if(x == 'F'):
            turtle.forward(SIZE)
        elif(x == '+'):
            turtle.left(ANGOLO)
        elif(x == '-'):
            turtle.right(ANGOLO)
    turtle.update()
#-----------------------------------------------
for j in range(LIVELLI-1):
    R=trasforma(R)
 
print(len(R))
print(R)
prepara()
disegna(R)

Lascia un commento