-
Notifications
You must be signed in to change notification settings - Fork 14
/
sierpiński triangle.py
59 lines (36 loc) · 1.48 KB
/
sierpiński triangle.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
# 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]:
def sierpiński_triangle(coordinates,lvl,colour='k'):
#stop recursion
if lvl==0:
return
#unpack coordinates
#coordinates have to follow the order of left,mid,right
left=coordinates[0];mid=coordinates[1];right=coordinates[2]
#compute mid point for each line
left_new=((mid[0]-left[0])/2+left[0],(mid[1]-left[1])/2+left[1])
mid_new=((right[0]-left[0])/2+left[0],(right[1]-left[1])/2+left[1])
right_new=((right[0]-mid[0])/2+mid[0],(mid[1]-right[1])/2+right[1])
#create new coordinates
coordinates_new=[left_new,mid_new,right_new]
#viz coordinates
for i in coordinates:
for j in coordinates:
if i!=j:
plt.plot([i[0],j[0]],[i[1],j[1]],c=colour)
#recursive plot sub triangles
sierpiński_triangle([left,left_new,mid_new],lvl-1)
sierpiński_triangle([left_new,mid,right_new],lvl-1)
sierpiński_triangle([mid_new,right_new,right],lvl-1)
# In[3]:
sierpiński_triangle([(0,0),(0.5,1),(1,0)],4)
# In[4]:
#you can check turtle version at the following link
# https://runestone.academy/runestone/books/published/pythonds/Recursion/pythondsSierpinskiTriangle.html
#you can check print version at the following link
# https://www.geeksforgeeks.org/sierpinski-triangle/