-
Notifications
You must be signed in to change notification settings - Fork 0
/
serpinski_triangle.py
53 lines (44 loc) · 1.38 KB
/
serpinski_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
"""
A script to generate an animated gif of the Sierpinski triangle.
author: Fabrizio Musacchio (fabriziomusacchio.com)
date: Aug 2, 2021
"""
# %% IMPORTS
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# %% SIERPINSKI TRIANGLE
def sierpinski_triangle(ax, p1, p2, p3, depth=0):
if depth == 0:
ax.fill([p1[0], p2[0], p3[0]], [p1[1], p2[1], p3[1]], 'k')
else:
p12 = (p1 + p2) / 2
p23 = (p2 + p3) / 2
p31 = (p3 + p1) / 2
sierpinski_triangle(ax, p1, p12, p31, depth - 1)
sierpinski_triangle(ax, p12, p2, p23, depth - 1)
sierpinski_triangle(ax, p31, p23, p3, depth - 1)
def update(frame):
ax.clear()
ax.set_aspect('equal')
ax.axis('off')
depth = frame + 1
sierpinski_triangle(ax, p1, p2, p3, depth)
ax.set_xlim(-0.1, 1.1)
ax.set_ylim(-0.1, 1.1)
plt.tight_layout()
# initialize the plot:
fig, ax = plt.subplots()
ax.set_aspect('equal')
ax.axis('off')
# define the vertices of the initial triangle:
p1 = np.array([0, 0])
p2 = np.array([0.5, np.sqrt(3) / 2])
p3 = np.array([1, 0])
# set the number of animation frames (depth levels):
num_frames = 11
# create and save the animation:
anim = FuncAnimation(fig, update, frames=num_frames, interval=500)
anim.save('images/sierpinski_triangle_animation.gif', writer='imagemagick')
plt.show()
# %% END