# The Math Behind Glory Math 1

March 20th, 2010 Glory Math 1 is an example of an asynchronous, two-dimensional cellular automaton (CA). If you poke around Wikipedia, you can find some pretty good information on what exactly that means, but in a nutshell, it's a rectangular grid of squares, each containing a number (which represents a color), and each square in the grid constantly updates itself based on a set of rules that take into account that square's neighbors (the squares around it).

## The Rule

The rule dictating the behavior of the cellular automaton is really simple: each generation (step in time), each square takes on the value of the average of its 4 neighbors' values (in the cardinal directions, not the diagonals). That's it.

## But, It's Asynchronous

A synchronous CA computes each cell in a generation simultaneously, meaning that only the cells from a previous generation are taken into account when calculating the next generation. Glory Math 1 does things slightly different: it calculates each cell's value in-place in the big 2D array of numbers, meaning that once the first cell is calculated, that new value is used when calculating the second cell's value, and so on. This fact is what makes it an asynchronous CA.

The Wikipedia article basically says that most of the interesting nature of CA's breaks down when you make them asynchronous, but I think that some of the craziness that Glory Math 1 generates as it progresses through generations is precisely because it's asynchronous. Of course, I designed the whole thing through experimentation, so it's hard for me to say for sure.

## The Colors

As you might expect, since the CA just averages out numbers over time, what you eventually end up with is a uniform grid of value. They start out different (based on your touches and shakes), but in the end, they're all the same.

If you were to map these values from, say, black to white (0 being black and the maximum being white), what you would see is a blurring effect. You'd start with a black field, get white dots where you touched, and it would all average out to gray. That's boring.

Half the fun going on in Glory Math 1 is the color mapping. Instead of doing a linear map from one color to another, I experimented a lot and ended up with a mapping that can generate a wide range of colors with even a small range in numeric values. So, 0 may be black, and 1 may be a slightly lighter version of black, but 2 is some kind of orange. And this is why, even as the grid of values approaches near-uniformity, you still get a lot of color action. I'm not going to give the mapping function here, but it's basically the result of playing with bitwise operations.

## The Rest

There's a little bit of fun stuff going on with how touches work in Glory Math 1 as well, but I'm going to save that for now. I hope this teaches somebody something, or maybe inspires.

And if you haven't tried Glory Math 1 yet, well you should check it out.