Find the sketch-a-day archives and tip jar at: https://abav.lugaralgum.com/sketch-a-day
Code for this sketch at: https://github.com/villares/sketch-a-day/tree/main/2026/sketch_2026_02_28 #Processing #Python #py5 #CreativeCoding
@rzeta0 my friend, what do you think about the code using np.roll() (in the linked sketch)?
I'm afraid it would be a bit hard to explain on a short class. So I'll use the simpler inefficient function...
The next big step in efficiency would be using #scipy I think:
def count_live_neighbors(status):
"""Counts the number of neighboring live cells"""
kernel = np.array([
[1, 1, 1],
[1, 0, 1],
[1, 1, 1]
])
return scipy.signal.convolve2d(status, kernel, mode='same', boundary="wrap")
Hi - I few times I have done something like this I have just avoided the "rolling around the edge" issue by just having a larger array and only plotting an internal smaller window.
but your method of adding up the "rolled" method clever and in my opinion simpler than the "kernel" methods that I did once try.
my guess is that some students will prefer the rolling method, and some might prefer the "kernel" convolve2d method.
Also - inefficient doesn't matter too much I think when teaching the main ideas .. .optimisation can come later after the students understand the idea.
Thank you for sharing this - I hadn't considered this use of the roll() method ! Very cool!
looking at your code a second time, I think I would do this
1. explain the simplest (slowest) method so the key idea of neighbour count is understood
2. only then explain some optimisations eg roll() or convolve2d
this is just my opinion
@rzeta0 yeah, I'll do that.
Even then, the wrapping with the % operator is always a bit of a time sink to explain I'm afraid... I might do the "board shrinking" thing subtracting rows and columns from the loop at the borders.
that makes sense to me
you can offer the roll/scipy as something optional for students to look into if they wish