Posts Tagged ‘ chaos

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.

Now has its own page on the blog.