-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSierpinski.py
More file actions
61 lines (49 loc) · 1.26 KB
/
Copy pathSierpinski.py
File metadata and controls
61 lines (49 loc) · 1.26 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
from numpy import *
from random import randint
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# our equilateral triangle vertices
v1 = [0,0]
v2 = [1,0]
v3 = [0.5,sqrt(3)/2]
# initial values
xdata, ydata = [], []
fig, ax = plt.subplots()
line, = ax.plot([], [],'m.',markersize=3)
ax.set_ylim(0, sqrt(3)/2)
ax.set_xlim(0, 1)
#ax.grid()
def midpoint(point1, point2):
return [(point1[0] + point2[0])/2, (point1[1] + point2[1])/2]
def data_gen():
x = data_gen.x
y = data_gen.y
count = data_gen.count
curr_point = [x,y]
while count < 1000:
count+=1
print(count)
print(curr_point)
val = randint(0,2)
print(val)
if val == 0:
curr_point = midpoint(curr_point, v1)
if val == 1:
curr_point = midpoint(curr_point, v2)
if val == 2:
curr_point = midpoint(curr_point, v3)
x=curr_point[0]
y=curr_point[1]
yield x,y
data_gen.x = 0
data_gen.y = 0
data_gen.count = 0
def run(data):
x,y=data
xdata.append(x)
ydata.append(y)
line.set_data(xdata,ydata)
return line,
ani = animation.FuncAnimation(fig, run, data_gen, blit=False, interval=10, repeat=False)
# set blit=False in Mac
plt.show()