概要
wslでmanimやってみた。
manim見つけたので、やってみた。
参考にしたページ
サンプルコード
from manim import *
from collections import deque
class TableBFS(Scene):
def construct(self):
m, n = map(int, input().split())
g = [list(map(int, input().split())) for _ in range(n)]
table = MathTable([[str(cell) for cell in row] for row in g], include_outer_lines = True).scale(0.8)
self.play(Create(table))
self.wait(1)
cnt_text = Text("Count: 0").next_to(table, UP)
self.play(Write(cnt_text))
dij = [(1, 0), (-1, 0), (0, 1), (0, -1)]
visited = set()
cnt = 0
for i in range(n):
for j in range(m):
if g[i][j] == 0:
self.play(table.get_cell((i + 1, j + 1)).animate.set_fill(GRAY, opacity = 0.5))
continue
if (i, j) in visited:
continue
que = deque([(i, j)])
visited.add((i, j))
while que:
pi, pj = que.popleft()
self.play(table.get_cell((pi + 1, pj + 1)).animate.set_fill(RED, opacity = 0.5))
self.wait(0.1)
for di, dj in dij:
if 0 <= (ni := pi + di) < n and 0 <= (nj := pj + dj) < m:
if g[ni][nj] == 0 or (ni, nj) in visited:
continue
que.append((ni, nj))
visited.add((ni, nj))
cnt += 1
new_cnt_text = Text(f"Count: {cnt}").next_to(table, UP)
self.play(ReplacementTransform(cnt_text, new_cnt_text))
cnt_text = new_cnt_text
成果物
以上。