-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsierpinski.py
More file actions
executable file
·80 lines (72 loc) · 2.67 KB
/
Copy pathsierpinski.py
File metadata and controls
executable file
·80 lines (72 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python3
# El triangulo de sierpinski
from tkinter import *
class Sierpinski(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.grid(sticky = N + S + E + W)
self.crearControles()
def crearControles(self):
top = self.winfo_toplevel()
top.rowconfigure(0, weight = 1)
top.columnconfigure(0, weight = 1)
self.columnconfigure(0, weight = 1)
self.columnconfigure(1, weight = 1)
self.rowconfigure(0, weight = 1)
self.lbTriangulo = Label(self, text = "Triángulo")
self.lbTriangulo.grid(row = 0, column = 0)
self.txTriangulo = Entry(self)
self.txTriangulo.grid(row = 0, column = 1)
self.txTriangulo.insert(INSERT, "((150, 0), (0, 300), (300, 300))")
self.rowconfigure(1, weight = 1)
self.lbEtapa = Label(self, text = "Etapa")
self.lbEtapa.grid(row = 1, column = 0)
self.txEtapa = Entry(self)
self.txEtapa.grid(row = 1, column = 1)
self.txEtapa.insert(INSERT, "3")
self.rowconfigure(2, weight = 1)
self.btDibujar = Button(self, text = "Dibujar !!",
command = self.hacerDibujo)
self.btDibujar.grid(row = 2, column = 1)
self.rowconfigure(3, weight = 1)
self.cvDibujo = Canvas(self, height = 300, width = 300)
self.cvDibujo.grid(row = 3, column = 0, columnspan = 2)
def hacerDibujo(self):
tgls = []
# borra todo lo que haya en el canvas
objetos = self.cvDibujo.find_all()
for objeto in objetos:
self.cvDibujo.delete(objeto)
# trae los datos del formulario
tgls.append(eval(self.txTriangulo.get()))
etapas = int(self.txEtapa.get())
# subdivide la cantidad de veces necesario
# print(tgls)
while etapas > 0:
nuevo = []
for tgl in tgls:
nuevo.extend(subdividir(tgl))
tgls = nuevo
# print(tgls)
etapas -= 1
# haz el dibujo
for tgl in tgls:
self.cvDibujo.create_polygon(tgl[0][0], tgl[0][1],
tgl[1][0], tgl[1][1],
tgl[2][0], tgl[2][1])
def subdividir(tgl):
a = tgl[0]
b = tgl[1]
c = tgl[2]
ab = puntoMedio(a, b)
bc = puntoMedio(b, c)
ca = puntoMedio(c, a)
return [(a, ab, ca), (ab, b, bc), (ca, bc, c)]
def puntoMedio(a, b):
x = (a[0] + b[0]) // 2
y = (a[1] + b[1]) // 2
# print("Punto medio {}, {}, {}, {}".format(a, b, x, y))
return x, y
app = Sierpinski()
app.master.title("Triangulo de Sierpinski")
app.mainloop()