forked from doingmathwithpython/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsierpinski.py
More file actions
71 lines (62 loc) · 1.51 KB
/
sierpinski.py
File metadata and controls
71 lines (62 loc) · 1.51 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
'''
sierpinski.py
Draw Sierpinski Triangle
'''
import random
import matplotlib.pyplot as plt
def transformation_1(p):
x = p[0]
y = p[1]
x1 = 0.5*x
y1 = 0.5*y
return x1, y1
def transformation_2(p):
x = p[0]
y = p[1]
x1 = 0.5*x + 0.5
y1 = 0.5*y + 0.5
return x1, y1
def transformation_3(p):
x = p[0]
y = p[1]
x1 = 0.5*x + 1
y1 = 0.5*y
return x1, y1
def get_index(probability):
r = random.random()
c_probability = 0
sum_probability = []
for p in probability:
c_probability += p
sum_probability.append(c_probability)
for item, sp in enumerate(sum_probability):
if r <= sp:
return item
return len(probability)-1
def transform(p):
# list of transformation functions
transformations = [transformation_1, transformation_2, transformation_3]
probability = [1/3, 1/3, 1/3]
# pick a random transformation function and call it
tindex = get_index(probability)
t = transformations[tindex]
x, y = t(p)
return x, y
def draw_sierpinski(n):
# We start with (0, 0)
x = [0]
y = [0]
x1, y1 = 0, 0
for i in range(n):
x1, y1 = transform((x1, y1))
x.append(x1)
y.append(y1)
return x, y
if __name__ == '__main__':
n = int(input('Enter the desired number of points'
'in the Sierpinski Triangle: '))
x, y = draw_sierpinski(n)
# Plot the points
plt.plot(x, y, 'o')
plt.title('Sierpinski with {0} points'.format(n))
plt.show()