Skip to content

Commit

Permalink
dynamic grid resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
jameslawlor committed Mar 30, 2024
1 parent ccaf357 commit b9e5bb7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
9 changes: 6 additions & 3 deletions src/pyturmite/constants.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
CANVAS_SIZE = 64
N_STEPS = 8000
CANVAS_SIZE = 8
N_STEPS = 6000
PLOT_MODE = "animate"
CMAP = "Oranges"
RULESET = "RLLR"
RULESET = "RL"
ANIMATION_INTERVAL = 1
PADDING_SIZE = 2 # expand in each direction by this amount of cells
SAVE_ANIMATION = False
FRAME_SKIP = 2
16 changes: 15 additions & 1 deletion src/pyturmite/turmites.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
import matplotlib.pylab as plt
from pyturmite.constants import CMAP, RULESET
from pyturmite.constants import CMAP, RULESET, PADDING_SIZE

"""
The colors are modified in a cyclic fashion.
Expand Down Expand Up @@ -101,3 +101,17 @@ def update(self):
self.turn(colour)
self.change_colour(self.x, self.y)
self.move()

# expand canvas/grid if we moved outside it
if (
(self.x >= self.grid.shape[0]) or
(self.x < 0) or
(self.y >= self.grid.shape[1]) or
(self.y < 0)
):
self.expand_grid()

def expand_grid(self):
self.grid = np.pad(self.grid, PADDING_SIZE)
self.x += PADDING_SIZE
self.y += PADDING_SIZE
23 changes: 10 additions & 13 deletions src/pyturmite/utils/plotters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import numpy as np
from matplotlib.animation import FuncAnimation
from functools import partial
from pyturmite.constants import ANIMATION_INTERVAL
from pyturmite.constants import ANIMATION_INTERVAL, SAVE_ANIMATION, FRAME_SKIP


class Plotter:
Expand Down Expand Up @@ -48,7 +48,7 @@ def animate(self, turmite, n_steps):
)
plt.axis("off")

def update(step, turmite, skip=5):
def update(step, turmite, skip=FRAME_SKIP):
for _ in range(skip):
turmite.update()
data = np.array(turmite.grid)
Expand All @@ -68,15 +68,12 @@ def update(step, turmite, skip=5):
repeat=False,
)

# writer = PillowWriter(
# fps=30,
# )
if SAVE_ANIMATION:
anim.save(
"example.mp4",
writer="ffmpeg",
fps=120,
progress_callback=lambda i, n: print(f"Saving frame {i}/{n}"),
)

anim.save(
"example.mp4",
writer="ffmpeg",
fps=120,
progress_callback=lambda i, n: print(f"Saving frame {i}/{n}"),
)

# plt.show()
plt.show()

0 comments on commit b9e5bb7

Please sign in to comment.