xkcd “Self-Description” solved with python+pygame
Wednesday, January 13th, 2010Well, something about this xkcd comic drew me in …
The 3rd panel reminded me of a study I did in college on iterated function system fractals. These fractals are known for creating ferns, trees, and the Sierpinski triangle. There are a number of ways of creating these fractals, but one of them is called (something like) the reducing photo copier method. Where you can start with ANY starting image on your photo copier, and just shrink the images, make a few copies, position them on the copier, and repeat. Eventually your image converges towards the fern / triangle / whatever if you are precise. (This can be done with a ‘Real Life’ photo copier for simple IFS like the Sierpinski triangle.)
So as a way to avoid work that I’m supposed to be doing this evening, I figured I’d see how “accurate” this comic was. Here’s my pygame code that checks it out. If you run main.py it will start with an image (the xkcd logo) and each time you press a key it will do an iteration of re-creating the comic. You can see it flashing between the current iteration and the original strip. After about 4-5 iterations the images converge (pretty closely.)
The most interesting bit was trying different starting images and seeing how long it took to converge. Starting with all black it takes 6 iterations. Starting with all white takes 5 iterations. And starting with the xkcd logo takes 4 iterations. The logo probably goes the quickest because it has some black and white in it (like this image) so it converges faster. (You can edit main.py towards the bottom page to try these different starting points.)
So .. there ya go! Enjoy!
-Phil