-
Notifications
You must be signed in to change notification settings - Fork 14
/
cellular-automata.lisp
25 lines (22 loc) · 970 Bytes
/
cellular-automata.lisp
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
(in-package :academy)
(defparameter *rule-patterns*
'((nil nil nil) (nil nil t) (nil t nil) (nil t t) (t nil nil) (t nil t) (t t nil) (t t t)))
(defun pick-rules (rule)
(loop for row in *rule-patterns*
for index from 0
when (logbitp index rule)
collect row))
(defun cellular-automata (&optional (rule 30) (size 64))
"Draw an elementary cellular automata."
(let ((rules (pick-rules rule)))
(let ((bitmap (make-bitmap size size)))
(setf (aref bitmap 0 (floor size 2)) t)
(loop for y from 1 to (1- size)
do (loop for x from 1 to (- size 2)
do (when (find (list
(aref bitmap (1- y) (1- x))
(aref bitmap (1- y) x)
(aref bitmap (1- y) (1+ x)))
rules :test #'equalp)
(setf (aref bitmap y x) t))))
(draw bitmap))))