The Chaos Game

After getting the book “INTRODUCING Fractal Geometry“, I thought it’d make a good learning project to tackle “The Chaos Game“, as coined by the mathematician Michael Barnsley.  Seemed simple enough to grasp, and would be fun to code up in Python\PyGame.

What attracted me to it was that while it was a seemingly simple problem, a few things outside my realm of knowledge were needed:  Find a random point inside of a triangle.  After a bit of web searching, I ran across the post on Wolfram Mathworld describing ‘Triangle Point Picking‘.  It shows how to find points based on a quadrilateral (2x triangle).  Using that code,  and a Python specific post called “Deciding if a Point is Inside a Polygon“, I was able to combine them with the game rules to achieve success, the Sierpinski triangle!

While the end result is nothing special to look at, Sierpinski triangles have been around for a long time, it was the fun of actually making the whole thing work through Python that kept me at it on Sunday afternoon 😉

  • Python source code HERE.
    • Its expecting to use this Vec2D class.
  • .zip with Windows executable HERE.

    • xuxana
    • September 30th, 2012 7:27am

    (I’m colombian and my englhis is bad :p )thank You, but the module vec2D have a error, i dont know that is No module named geometry.vec2d…

  1. I’m importing the PyGame 2DVectorClass, which you can find here:
    Give that a shot.

  1. No trackbacks yet.