-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fractals.py
88 lines (73 loc) · 2.26 KB
/
Fractals.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 2 12:44:25 2019
@author: DannySwift
"""
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
def animate(i, angle, ax, angles, mid_xs, mid_ys, lines):
bp = 3**i #branchpoints
xs = mid_xs[-bp:]
ys = mid_ys[-bp:]
bpangles = angles[-bp:]
if i==0:
lines.append(ax.plot([],[],lw=1, color='g')[0])
lines[0].set_data([0,0], [0,1])
for k in range(bp):
lines.append(ax.plot([],[],lw=1, color='g')[0])
lines.append(ax.plot([],[],lw=1, color='g')[0])
x, y = xs[k], ys[k]
a = bpangles[k]
a1 = a + angle
a2 = a - angle
newx1, newy1 = x + np.cos(a1) / 2**i, y + np.sin(a1) / 2**i
newx2, newy2 = x + np.cos(a2) / 2**i, y + np.sin(a2) / 2**i
lines[-1].set_data([x, newx1], [y, newy1])
lines[-2].set_data([x, newx2], [y, newy2])
mid_xs.append(x + (np.cos(a) / 2**i) / 2)
mid_xs.append((x + newx1) / 2)
mid_xs.append((x + newx2) / 2)
mid_ys.append(y + (np.sin(a) / 2**i) / 2)
mid_ys.append((y + newy1) / 2)
mid_ys.append((y + newy2) / 2)
angles.append(a)
angles.append(a1)
angles.append(a2)
return lines
def make_gif(deg_angle):
if deg_angle == None:
angle = np.radians(45)
else:
angle = np.radians(deg_angle)
iterations=7
start_angle = np.radians(90)
angles = [start_angle]
mid_xs = [0]
mid_ys = [0]
lines=[]
fig, ax = plt.subplots(frameon=False)
fig.set_facecolor('xkcd:sky')
ax.set_facecolor('xkcd:sky')
ax.set_xticks([])
ax.set_yticks([])
plt.xlim(-1, 1)
plt.ylim(-0.7, 1.3)
images = []
for i in range(iterations):
canvas = plt.get_current_fig_manager().canvas
canvas.draw()
pil_image = Image.frombytes('RGB', canvas.get_width_height(), canvas.tostring_rgb())
images.append(pil_image)
animate(i, angle, ax, angles, mid_xs, mid_ys, lines)
gif_filepath = '{}.gif'.format(deg_angle)
images[0].save(
gif_filepath,
save_all=True,
format='GIF',
append_images=images[1:],
loop=0,
duration=1000
)
return(gif_filepath)