From e5969b04f811cf2d03ddfeb25129e6f2e9cef798 Mon Sep 17 00:00:00 2001 From: Tyrel Souza Date: Fri, 9 Dec 2022 18:03:06 -0500 Subject: [PATCH] animation library --- .gitignore | 1 + 2021/python/anim.py | 49 +++++++++++++++++++++++++++++++++++++++++ 2021/python/day09.py | 1 + 2022/python/anim.py | 50 ++++++++++++++++++------------------------ 2022/python/day09.py | 18 +++++++++++---- 2022/samples/day09.txt | 8 +++++++ 6 files changed, 94 insertions(+), 33 deletions(-) create mode 100644 2021/python/anim.py diff --git a/.gitignore b/.gitignore index 2a005df..eb99fdc 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/2021/python/anim.py b/2021/python/anim.py new file mode 100644 index 0000000..041e43f --- /dev/null +++ b/2021/python/anim.py @@ -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 diff --git a/2021/python/day09.py b/2021/python/day09.py index bfb2873..dad9f09 100644 --- a/2021/python/day09.py +++ b/2021/python/day09.py @@ -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[:] diff --git a/2022/python/anim.py b/2022/python/anim.py index 041e43f..65d6694 100644 --- a/2022/python/anim.py +++ b/2022/python/anim.py @@ -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) diff --git a/2022/python/day09.py b/2022/python/day09.py index 993687f..6e62fe0 100644 --- a/2022/python/day09.py +++ b/2022/python/day09.py @@ -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) diff --git a/2022/samples/day09.txt b/2022/samples/day09.txt index 9874df2..80d0f82 100644 --- a/2022/samples/day09.txt +++ b/2022/samples/day09.txt @@ -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