-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathifs.py
More file actions
61 lines (48 loc) · 1.62 KB
/
ifs.py
File metadata and controls
61 lines (48 loc) · 1.62 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
import sys, random
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def f_split(matr):
matr = matr.split('\n')
matr = [c.split(' ') for c in matr]
matr = [[float(c) for c in r if c != ''] for r in matr]
matr = [x for x in matr if x != []]
return matr
f = open(sys.argv[1], 'r', encoding='utf8')
matr = f.read()
f.close()
matr = f_split(matr)
fig, ax = plt.subplots()
if sys.argv[1] == "sierpinsky.txt":
fig.suptitle('Ковер Серпинского')
elif sys.argv[1] == "tree.txt":
fig.suptitle('Дерево')
elif sys.argv[1] == "fern.txt":
fig.suptitle('Папоротник Барнсли')
x, y = [0], [0]
ln, = plt.plot(x, y, 'bo', markersize=1)
def init():
ax.set_xlim(-0.2, 1.2)
ax.set_ylim(-0.2, 1)
return ln,
def animate(j):
l = len(matr[0])
r = random.random()
values = []
for j in range(l):
cur_sum = sum(matr[0][:j])
values.append(cur_sum)
all_values = values + [1]
for i in range(1, l + 1):
if r < all_values[i] and r > all_values[i - 1]:
index = i - 1
cx = matr[1 + index]
cy = matr[1 + index + l]
x.append((x[-1]*cx[0] + y[-1]*cx[1] + cx[2]) )
y.append((x[-1]*cy[0] + y[-1]*cy[1] + cy[2]) )
ln.set_data(x, y)
return ln,
ani = FuncAnimation(fig, animate, init_func=init, interval = 0.001)
plt.show()
#python3 ifs.py "fern.txt"
#python3 ifs.py "tree.txt"
#python3 ifs.py "sierpinsky.txt"