Треугольник Коха

 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
from math import *
from tkinter import *
root = Tk()
canv = Canvas(root, width=1000, height=1000, bg='lightgreen')
def kox(x1,y1,x2,y2,k):
if k == 0:
canv.create_line(x1, y1, x2, y2, width = 3)
return
x3 = x1 + int((x2 - x1)/3)
y3 = y1 + int((y2 - y1)/3)
x4 = x1 + int(2*(x2 - x1)/3)
y4 = y1 + int(2*(y2 - y1)/3)
x5 = x1 + int((x2-x1)/2 + (y2-y1)/3)
y5 = y1 + int((y2-y1)/2 - (x2-x1)/3)
kox(x1,y1,x3,y3,k-1)
kox(x3,y3,x5,y5,k-1)
kox(x5,y5,x4,y4,k-1)
kox(x4,y4,x2,y2,k-1)
kox(400, 300, 800, 300, 5)
kox(800, 300, 600, 646, 5)
kox(600, 646, 400, 300, 5)
canv.pack()
root.mainloop()