I want to illustrate the iterations of a cellular automaton picture-by-picture in some slides. For those not familiar with the topic, imagine a two-dimensional grid of square cells where each square in the grid can have a "state" (which will be represented by its color). Every iteration or timestep, the state of a cell/square changes depending on its surroundings and current state – its color should change in the next picture.
It seems obvious to use TikZ since it directly supports drawing of grids, but the part where I struggle is when thinking of a solution to minimize the effort when coloring every single square, every single iteration – the cells (or "squares") change state with every picture, so I would essentially have to color every square manually for each iteration. That seems like a lot of nasty, unmantainable code will be unavoidable; I essentially know how to solve my problem but wanted to ask if there is (hopefully) a smarter way of drawing/doing this (I'm hoping for some \foreach
magic and such.)
Phrasing concrete questions:
- Is there, maybe, a package for drawing cellular automatons already?
- If not, is there a smarter way to use TikZ to draw many iterations of a cellular automaton than to manually draw the grid and color it by hand?
- If those last two questions are too localized/hard: Are there other "control structure" commands similar to
\foreach
that I could use to draw using some "rules", so that I don't have to color each square by hand?
Update 1
After considering the situation to the best of my knowledge, I want to try an implementation with the matrix
command in TikZ. I will define a matrix of empty, minimum-sized and square cells, and change the style of the nodes according to the slide number. This might very well be a suboptimal approach, but since the pgf-magic happening in the answer by Mark Wibrow is still almost undecipherable to me, I think this the best way forward.
Desired Automaton Behavior
In simple terms: a cell is a square in the grid, and connected to its four orthogonal neighbors (up, down, right left). At each point in time, it is either excited, resting, or in refractory phase. For each iteration, the following happens:
- A cell that is resting goes into the excited state if any of its neighbors are excited themselves.
- An excited cell goes into the refractory phase.
- A cell in refractory phase goes into the resting state, regardless of the states of its neighbors.
Here is an image of what the model looks like (n is the time, a dark cell is excited, a striped one is refractory):
I realize my problem is highly localized, and don't mind if I need to adapt potential answers to fit my purposes if they describe a general way the problem could be tackled; what I am trying to learn is elegant ways of bending TikZ to one's will, so those answers are very much welcome.
As of now, I am "painting" the cells by applying the respective style
to the node
in the matrix. My real matrix is of course bigger than 3
Below is my MWE:
\documentclass[ngerman,compress]{beamer}
\setbeamercovered{invisible}
% Packages
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
\begin{frame}{The cellular automaton}
\center
\begin{tikzpicture}
[help lines/.style={draw=black},
every node/.style={help lines,rectangle,minimum size=3mm},
cellular automaton/.style={draw=none,row sep=0mm,column sep=0mm, ampersand replacement=\&},
rst/.style={fill=white,help lines},
exc/.style={fill=blue!70,help lines}
ref/.style={fill=blue!30!white,help lines]
\matrix[cellular automaton] {
\node[rst] {};\& \node[rst] {};\& \node[rst] {}; \\
\node[rst] {};\& \node[exc] {};\& \node[rst] {}; \\
\node[rst] {};\& \node[rst] {};\& \node[rst] {}; \\
};
\end{tikzpicture}
\end{frame}
\end{document}
Best Answer
EDIT: Added LuaTeX version
Probably not what is exactly wanted. Also somewhat crude, and slow. Using
LuaTeX
orPSTricks
would be very good choice to speed this up. But I'm old fashioned and stuck in my ways, so...And here's how it could be done in
LuaTeX
:The result is the same as before.