2012-12-25 22:47:25 +01:00
|
|
|
import turtle,math
|
|
|
|
|
|
|
|
def impostazioni():
|
|
|
|
turtle.title("Frattali")
|
|
|
|
turtle.bgcolor("#000")
|
|
|
|
turtle.hideturtle()
|
|
|
|
|
|
|
|
class frattale(turtle.Pen):
|
|
|
|
|
2013-03-23 14:16:46 +01:00
|
|
|
def __init__(self, colore = "yellow", velocità=0, riempi=True):
|
2012-12-25 22:47:25 +01:00
|
|
|
super(frattale,self).__init__()
|
|
|
|
self.colore = colore
|
|
|
|
self.riempi = riempi
|
|
|
|
self.velocità = velocità
|
|
|
|
self.speed(self.velocità)
|
|
|
|
self.color(self.colore)
|
|
|
|
self.hideturtle()
|
|
|
|
|
|
|
|
def posizione(self, x, y):
|
|
|
|
self.pu()
|
|
|
|
self.setpos(x, y)
|
|
|
|
self.pd()
|
|
|
|
|
|
|
|
class koch(frattale):
|
|
|
|
|
|
|
|
def disegna(self, l=3, s=250, n=4, p=1):
|
2013-03-23 14:16:46 +01:00
|
|
|
spigolo = 2 * s * math.sin(math.pi/l)
|
2012-12-25 22:47:25 +01:00
|
|
|
self.posizione(s,0)
|
|
|
|
if self.riempi == True: self.begin_fill()
|
2013-03-23 14:16:46 +01:00
|
|
|
self.rt(180 - (90 * (l-2) / l))
|
2012-12-25 22:47:25 +01:00
|
|
|
for i in range(l):
|
|
|
|
self.curva(spigolo, n, p)
|
|
|
|
self.rt(360 / l)
|
2013-03-23 14:16:46 +01:00
|
|
|
self.lt(180 - (90 * (l-2) / l))
|
2012-12-25 22:47:25 +01:00
|
|
|
if self.riempi == True: self.end_fill()
|
|
|
|
self.posizione(0,0)
|
|
|
|
|
|
|
|
def curva(self, s, n, p):
|
|
|
|
if n < 1:
|
|
|
|
self.fd(s)
|
|
|
|
return
|
2013-03-23 14:16:46 +01:00
|
|
|
self.curva(s/3, n-1, p)
|
|
|
|
self.lt(60*p)
|
|
|
|
self.curva(s/3, n-1, p)
|
|
|
|
self.rt(120*p)
|
|
|
|
self.curva(s/3, n-1, p)
|
2012-12-25 22:47:25 +01:00
|
|
|
self.lt(60 * p)
|
2013-03-23 14:16:46 +01:00
|
|
|
self.curva(s/3, n-1, p)
|
2012-12-25 22:47:25 +01:00
|
|
|
|
|
|
|
class hilbert(frattale):
|
|
|
|
|
2013-03-23 14:16:46 +01:00
|
|
|
def disegna(self, s=4, n=6, p=1):
|
2012-12-25 22:47:25 +01:00
|
|
|
self.posizione(-33*s*p,-33*s*p)
|
|
|
|
if self.riempi == True: self.begin_fill()
|
|
|
|
self.curva(s, n, p)
|
|
|
|
if self.riempi == True: self.end_fill()
|
|
|
|
self.posizione(0,0)
|
|
|
|
|
|
|
|
def curva(self, s, n, p):
|
|
|
|
if n == 0:
|
|
|
|
return
|
2013-03-23 14:16:46 +01:00
|
|
|
self.lt(p*90)
|
|
|
|
self.curva(s, n-1, -p)
|
2012-12-25 22:47:25 +01:00
|
|
|
self.fd(s)
|
2013-03-23 14:16:46 +01:00
|
|
|
self.rt(p*90)
|
|
|
|
self.curva(s, n-1, p)
|
2012-12-25 22:47:25 +01:00
|
|
|
self.fd(s)
|
2013-03-23 14:16:46 +01:00
|
|
|
self.curva(s, n-1, p)
|
|
|
|
self.rt(p*90)
|
2012-12-25 22:47:25 +01:00
|
|
|
self.fd(s)
|
2013-03-23 14:16:46 +01:00
|
|
|
self.curva(s, n-1, -p)
|
|
|
|
self.lt(p*90)
|
2012-12-25 22:47:25 +01:00
|
|
|
|
|
|
|
class sierpinski(frattale):
|
|
|
|
|
2013-03-23 14:16:46 +01:00
|
|
|
def disegna(self, n, s=400, x=-200, y=-150):
|
2012-12-25 22:47:25 +01:00
|
|
|
self.posizione(x, y)
|
|
|
|
if n==1:
|
|
|
|
if self.riempi == True: self.begin_fill()
|
|
|
|
for i in range(3):
|
|
|
|
self.fd(s)
|
|
|
|
self.lt(120)
|
|
|
|
if self.riempi == True: self.end_fill()
|
|
|
|
else:
|
|
|
|
sierpinski.disegna(self, n-1, s/2, x, y)
|
|
|
|
sierpinski.disegna(self, n-1, s/2, x+s/2, y)
|
|
|
|
sierpinski.disegna(self, n-1, s/2, x+s/4, y+(s*((3)**0.5))/4)
|