Generative Art Using Cellular Automaton

The entirety of this post is largely a rehash of this post. They go over the algorithm and post some really cool images. I thought a good way to get into generative art would be to code it up in Julia, so that is what I did. Directly from the linked blog:

The basic rule is you fill an image with random colors and set an update probability. For each pixel see if a random value is less than the probability. If it is then change the pixel to the color of one of its neighbor cells (this can be the 4 N,S,E,W Von-Neumann neighbors or all 8 closest cells in the Moore neighborhood). Griffeath’s original uses a 50/50 chance of each cell being changed and uses the Von-Neumann neighborhood.

The code is pretty simple in Julia, especially with the Images package. Originally I thought about using no external packages / dependencies. It's not so bad if you use a PPM format, but there is so much redundant data and no compression, so I decided against it. The code is below, imgc is a matrix of random pixels and chance is a number between 0 and 1- I chose 0.5f. It doesn't matter so much, but I iterated over the rows and then the columns, but Julia is column major so I should have switched that. It would be interesting to see the performance inhancements.

 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
26
27
function iterate_stepping(imgc, chance)
    for i in 1:m
        for j in 1:n
            if rand() < chance
                neighbor = rand(nn)
                # 1 is north
                if neighbor == 1
                    new_j = getsub(n, j-1)
                    imgc[i, j] = imgc[i, new_j]
                # 2 is south
                elseif neighbor == 2
                    new_j = getsub(n, j+1)
                    imgc[i, j] = imgc[i, new_j]
                # 3 is east
                elseif neighbor == 3
                    new_i = getsub(m, i+1)
                    imgc[i, j] = imgc[new_i, j]
                # 4 is west
                elseif neighbor == 4
                    new_i = getsub(m, i-1)
                    imgc[i, j] = imgc[new_i, j]
                end
            end
        end
    end
    return imgc
end

If you iterate over a 1000x1500 matrix 1000 times, you get something like the image below. You can also click the image to open an mp4 of the animation. (Note: it is not optimized for web browsing!)