So I’ve started on Stephen Wolfram’s epic tome, ’A new Kind of Science’. I have known about Cellular Automata for some time.. I remember playing around with them in late High School, using BASIC. I went to the computer lab and got all the computers rendering different rules, producing different patterns. The machines were so slow it took like five minutes for a single screen to be rendered.
So it is with some sense of familiarity and nostalgia that I look on these wedge shaped fractal patterns again.. and with great interest that I’m reading Mr Wolfram’s eloquent expositions of how they may be the key to understanding the universe.
Basically they work by evaluating each pixel in an image, and applying some rule which takes into account the values of surrounding pixels, and then changes the color of the pixel according to the rule. In the book, the simplest examples at the start just consider the pixel above the current one, and the pixels to the left and right. You can also think of this as applying the rule on the first row, placing the result in the second row, and then applying the rule to the second row and placing the result in the third, and so on. A rule may be something such as ‘if the pixels to the top-left and top-right of the pixel are black, make the pixel black’.
So after reading a bit, I felt inspired to code up some of the examples. After doing a few of them I got to thinking that there must be a way to generalize them so that a single program could generate them. I was also thinking that binary/boolean arithmetic had to be helpful here somehow. The verbal descriptions seemed helpful at first, but were quite tricky to implement, requiring lots of if-then cases. After a while I had worked out a nice system of constructing the rule as an array of outcomes (either black or white). The array has 8 items – as there are 8 possible permutations of the 3 preceding rows pixels (the current pixel and its neighbours, in the previous row.) If you visualize the index as a binary number, with the 1’s as black pixels and the 0’s as white, then you can translate the visual representation of the rules in the books (3 cells – the condition , or match, with the resulting pixel below) into simple numbers. This code probably is clearer than my explanation:
//initial condition --> result rule = WHITE; //000 -> white rule = BLACK; //001 -> black rule = BLACK; //010 -> black rule = WHITE; //011 -> black rule = BLACK; //100 -> black rule = WHITE; //101 -> black rule = WHITE; //110 -> white rule = WHITE; //111 -> white
After that insight I was able to create an application that lets you toggle the rules (click to toggle the result). The rules are also numbered from 0-255, so you can enter the number in the input field, or click the next / previous buttons.
flash 10 required
As always, you can right-click and zoom in to see details…