animation library

This commit is contained in:
Tyrel Souza 2022-12-09 18:03:06 -05:00
parent 03ce126e23
commit e5969b04f8
6 changed files with 94 additions and 33 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
gif-*
# Created by https://www.toptal.com/developers/gitignore/api/python
# Edit at https://www.toptal.com/developers/gitignore?templates=python

49
2021/python/anim.py Normal file
View File

@ -0,0 +1,49 @@
from matrix import get_size, pmx
import imageio
import matplotlib.pyplot as plt
import numpy as np
from copy import deepcopy
class Animate:
def __init__(self, mx, day="CHANGEME"):
self.mx= mx
_size = get_size(mx)
self.height = _size[0]
self.width = _size[1]
self.day = day
self.frames = [deepcopy(mx)]
def add_frame(self, frame):
self.frames.append(deepcopy(frame))
def print(self):
for f in self.frames:
pmx(f)
print("-")
def animate(self, save=False, or_frames=False):
last_file = 0
for f_idx, frame in enumerate(self.frames):
current = np.zeros_like(self.mx)
for y, row in enumerate(frame):
for x, col in enumerate(row):
current[y][x] = frame[y][x]
pmx(current)
fig, ax = plt.subplots()
ax.imshow(current, cmap=plt.cm.gray)
ax.axis("off")
if save:
plt.savefig(f"{self.day}/{f_idx:03}.png")
plt.close()
with imageio.get_writer(f"{self.day}/day{self.day}.gif", mode="I") as writer:
names = [f"{self.day}/{x:03}.png" for x in range(len(self.frames))]
for filename in names:
try:
image = imageio.imread(filename)
writer.append_data(image)
except FileNotFoundError:
pass

View File

@ -69,6 +69,7 @@ class Smoke:
return prod(largest)
def animate(self, save=False):
breakpoint()
largest = [x for x in self.by_center if x[0] in self.largest][-3:]
current = np.zeros_like(self.mx)
shuf = self.by_center[:]

View File

@ -2,45 +2,37 @@ from matrix import get_size, pmx
import imageio
import matplotlib.pyplot as plt
import numpy as np
from copy import deepcopy
class Animate:
def __init__(self, mx, day="CHANGEME"):
self.mx= mx
self.mx = mx
self.day = day
_size = get_size(mx)
self.height = _size[0]
self.width = _size[1]
self.day = day
self.frames = [deepcopy(mx)]
self.f_count = -1
def add_frame(self, frame):
self.frames.append(deepcopy(frame))
self.f_count += 1
print(self.f_count)
self.write_frame(frame)
def print(self):
for f in self.frames:
pmx(f)
print("-")
def write_frame(self, frame):
current = np.zeros_like(self.mx)
for y, row in enumerate(frame):
for x, col in enumerate(row):
current[y][x] = frame[y][x]
fig, ax = plt.subplots()
ax.imshow(current, cmap=plt.cm.gray)
ax.axis("off")
_figpath = (f"gif-{self.day}/{self.f_count:05}.png")
plt.savefig(_figpath)
plt.close()
def animate(self, save=False, or_frames=False):
last_file = 0
for f_idx, frame in enumerate(self.frames):
current = np.zeros_like(self.mx)
for y, row in enumerate(frame):
for x, col in enumerate(row):
current[y][x] = frame[y][x]
pmx(current)
fig, ax = plt.subplots()
ax.imshow(current, cmap=plt.cm.gray)
ax.axis("off")
if save:
plt.savefig(f"{self.day}/{f_idx:03}.png")
plt.close()
with imageio.get_writer(f"{self.day}/day{self.day}.gif", mode="I") as writer:
names = [f"{self.day}/{x:03}.png" for x in range(len(self.frames))]
def animate(self, frameskip=1):
with imageio.get_writer(f"gif-{self.day}/day{self.day}.gif", mode="I") as writer:
names = [f"gif-{self.day}/{x:05}.png" for x in range(0,self.f_count, frameskip)]
print(names)
for filename in names:
try:
image = imageio.imread(filename)

View File

@ -1,9 +1,11 @@
from pprint import pprint as pp
from anim import Animate
import shared
import matrix
MATRIX_SIZE = 500
#MATRIX_SIZE = 40 # sample
MATRIX_SIZE = 400
ATRIX_SIZE = 40 # sample
HALF = MATRIX_SIZE // 2
H = [HALF,HALF]
@ -25,6 +27,7 @@ DIRS = {
def part1(steps):
field = matrix.matrix_of_size(MATRIX_SIZE,MATRIX_SIZE)
anim = Animate(field, day="09")
for direction, amount in steps:
# move HEAD
@ -73,14 +76,18 @@ def part1(steps):
T[0] += tY
T[1] += tX
anim.add_frame(field)
field[T[0]][T[1]] = 1 # mark visited tails
print(matrix.sum_matrix(field))
anim.animate()
def part2(steps):
field = matrix.matrix_of_size(MATRIX_SIZE,MATRIX_SIZE)
anim = Animate(field, day="09")
S = [[HALF,HALF],] # HEAD ONLY
_cur_frame = 0
for x in range(9):
S.append([HALF,HALF])
@ -136,15 +143,18 @@ def part2(steps):
S[knot_idx][0] += tY
S[knot_idx][1] += tX
if _cur_frame % 25 == 0:
anim.add_frame(field)
_cur_frame += 1
field[S[-1][0]][S[-1][1]] = 1 # mark visited tails
print(matrix.sum_matrix(field))
anim.animate()
def main():
rows = [x.split(" ") for x in shared.load_rows(9)]
part1(rows)
#part1(rows)
part2(rows)

View File

@ -6,3 +6,11 @@ R 4
D 1
L 5
R 2
R 5
U 8
L 8
D 3
R 17
D 10
L 25
U 20