## Cellular automata

Are quite fun and very intriguing.

What and how.

Generally you represent cells on a grid, and you “evolve” them using simple rules, based on how many neighbors they have.

In the simple version I so far have toyed around with. A cell is either dead or alive.

In one dimension, there are a ruleset and notation called the wolfram rules. Where you use a integer to represent a rule. Each cells faith is based on its own state and the state to its two adjacent neighbors. Here is a table from wikipedia

Rule 30 cellular automaton

current pattern | 111 | 110 | 101 | 100 | 011 | 010 | 001 | 000 |

new state for center cell | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 0 |

Its rule 30 because 11110 is the binary representation of 30. This notation makes implementation quite easy. I implemented it this way :

- Create an image, fill with black
- Start with initializing the bottom row, put in cells of a different color at random
- for the row above calculate for each position :

`int shift =0;`

if (img( (i>0) ? i-1 : img.dimx()-1 , j, 0,0)==c[0])

shift +=4;

if (img(i, j, 0)==c[0])

shift +=2;

if (img((i+1)%img.dimx(), j, 0)==c[0])

shift +=1;

- basically its if left neighbor is alive shift = 4, itself is 2 and right neighbor 1. I wrap neighborhoods so its topological like a cylinder.
- Heres the smart thing about wolfram rules, we now have the state of the neighborhood represented as an integer. And if we shift the rule with this integer, the lowest bit will represent the state of the current cell. (rule<<shift)&1.
- continue through all rows from top to bottom.

This will generate some interesting patterns, rule 30 is a lot a triangles. And they have some interesting properties. I make them because I find them visually appealing, but the do have other merits. Look at the wikipedia entry for more

Technicalities : I made a small program in c++ using CImg. CImg is easy to use, and its quick to setup up a disply and handle events. (eg lefttclik =use same rule, initializ again, rightclik = next rule)

And though one dimensional cellular automata’s are intriguing and “fun”. They quickly get boring to look at, so I went for two dimensions. More about that in a later post.