-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_life.py
More file actions
33 lines (25 loc) · 834 Bytes
/
example_life.py
File metadata and controls
33 lines (25 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import cv2
import jax.numpy as np
import numpy as onp
from cellular_automaton import CellularAutomaton2D
from neighborhood import moore
if __name__ == "__main__":
# cells are [ bool ] vectors
width = 1900
height = 1200
grid = onp.round(onp.random.random((height, width, 1)))
grid = np.array(grid, dtype="bool")
def life_rule(neighbors):
center = neighbors[len(neighbors) // 2]
alive = np.sum(neighbors) - center
return np.where(
alive == 3,
np.array([1]),
np.where(alive == 2, np.array([center]), np.array([0])),
)[0]
ca = CellularAutomaton2D(rule=life_rule, neighborhood=moore(1), grid=grid)
ca.step()
while True:
cv2.imshow("Life", onp.array(ca.grid, dtype="float32"))
ca.step(1)
cv2.waitKey(20)