-
Notifications
You must be signed in to change notification settings - Fork 14
/
sierpiński carpet.py
90 lines (54 loc) · 2.73 KB
/
sierpiński carpet.py
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
81
82
83
84
85
86
87
88
89
# coding: utf-8
# In[1]:
#fractal is one of the interesting topics in geometry
#it is usually described by a recursive function
#voila,here we are!
import matplotlib.pyplot as plt
# In[2]:
coordinates=[(0,0),(3,0),(3,-3),(0,-3)]
# In[3]:
def sierpiński_carpet(coordinates,lvl,colour='k'):
#stop recursion
if lvl==0:
return
#unpack coordinates
#coordinates follow clockwise order
topleft=coordinates[0];topright=coordinates[1]
bottomright=coordinates[2];bottomleft=coordinates[3]
#create new coordinates
topleft_new=(topleft[0]+(topright[0]-topleft[0])/3,
bottomleft[1]+(topleft[1]-bottomleft[1])/3*2)
topright_new=(topleft[0]+(topright[0]-topleft[0])/3*2,
bottomleft[1]+(topleft[1]-bottomleft[1])/3*2)
bottomleft_new=(topleft[0]+(topright[0]-topleft[0])/3,
bottomleft[1]+(topleft[1]-bottomleft[1])/3)
bottomright_new=(topleft[0]+(topright[0]-topleft[0])/3*2,
bottomleft[1]+(topleft[1]-bottomleft[1])/3)
coordinates_new=[topleft_new,topright_new,bottomright_new,bottomleft_new]
#viz new coordinates
for i in range(len(coordinates_new)):
plt.plot([coordinates_new[i][0],coordinates_new[i-1][0]],
[coordinates_new[i][1],coordinates_new[i-1][1]],c=colour)
#recursive plot sub carpets following clockwise order
sierpiński_carpet([topleft,(topleft_new[0],topleft[1]),
topleft_new,(topleft[0],topleft_new[1])],lvl-1)
sierpiński_carpet([(topleft_new[0],topleft[1]),(topright_new[0],topright[1]),
topright_new,topleft_new],lvl-1)
sierpiński_carpet([(topright_new[0],topright[1]),topright,
(topright[0],topright_new[1]),topright_new],lvl-1)
sierpiński_carpet([topright_new,(topright[0],topright_new[1]),
(bottomright[0],bottomright_new[1]),bottomright_new],lvl-1)
sierpiński_carpet([bottomright_new,(bottomright[0],bottomright_new[1]),
bottomright,(bottomright_new[0],bottomright[1]),],lvl-1)
sierpiński_carpet([bottomleft_new,bottomright_new,
(bottomright_new[0],bottomright[1]),
(bottomleft_new[0],bottomleft[1])],lvl-1)
sierpiński_carpet([(topleft[0],bottomleft_new[1]),bottomleft_new,
(bottomleft_new[0],bottomleft[1]),bottomleft],lvl-1)
sierpiński_carpet([(topleft[0],topleft_new[1]),topleft_new,
bottomleft_new,(topleft[0],bottomleft_new[1])],lvl-1)
# In[4]:
sierpiński_carpet(coordinates,4)
# In[5]:
#you can check turtle version at the following link
# https://www.geeksforgeeks.org/python-sierpinski-carpet/