The last view days were full of work and so I enjoyed the few free hours yesterday. And because I had no time for coding and blogging, I decided to implement a problem that a friend introduced to me last weekend - Langton’s Ant.
Langton’s Ant is a very simple problem invented by Chris Langton in 1986. It basically covers a set of rules which together determine the behaviour of an ant that colors the ground while moving. Even though the rules used are very simple, the resulting behavior of the ant is very complex. The most interesting is, that the ant’s movement results in a recurring structure after a couple of steps (about 10000). The idea of this kind of simulation has been extended in many different ways. The most popular extended version of Langton’s Ant are Turmites which basically extend the coloring rule of Langton’s Ant.
The idea of traditional Langton’s Ant is very simple. The ant moves on a grid. If it stands on a white field, it colors it black, turns left by 90 degrees and moves one step forward. If it currently stands on a black field, it colors it white, turns right by 90 degrees and moves one step forward.
Since this problem is so simple and much fun, I decided to implement it. And of course the language of my choice was Monty’s Python. You see, I am currently working with a whole zoo.
To complete this zoo I would like to introduce another animal to you - the Ninja Turtle. Turtle is a very easy to use python library that perfectly fits the needs when implementing drawing problems. Turtle offers a little turtle that could programmatically be moved forward and backward, turned around by an arbitrary angle and draw in different colors. The cool thing about turtle is that the drawing process could be animated, so you could watch Langton’s ant moving around.
The code of this ‘short’ program you could find below. I think it is completely self explaining, so I will just cover the usage of the program and not the code itself.
In order to use this program we should take a short look at the parameters accepted by it. You could specify the number of iterations (steps) the ant should do, the size of the world (grid) the ant could move around in, the size of a single cell (blockSize) and an output file in postscript format, the result should be written to.
So if you want to create a world with 300x300 cells of a size of 3x3 pixel, the ant should do 15000 steps and the resulting image should be stored in the file langton.eps, you could use the following command:
I hope you enjoyed this short introduction to Langton’s Ant and the example of how to use the turtle graphics library. I will present another example using the turtle library in a few days when I finished my Lindenmeyer program. The Lindenmeyer systems are another class of problems that were introduced and developed in 1968 by Aristid Lindenmayer. The systems are based on a set of rules, which allow the user to draw arbitrary structures by directing an ant or turtle. A simple example that were first described by the German mathematician David Hilbert in 1891, you could see on the right side. Here you see an screenshot of a simple prototype, which will be extended to allow the drawing of more complex Lindenmeyer systems.
So stay tuned and until next time, happy coding.