Chaos Game

This is a cool little thing that I recently found by accident.

What's happening here is I create a triangle with vertices `a`,`b`,`c`. I'm choosing vertex `a` for my starting point and selecting a second vertex at random and then plot a point halfway between the 2. For example, if we define vertex `a` as (1,1) and vertex `b` as (0,0), our point between the 2 will be (0.5, 0.5). We then set (0.5, 0.5) as our 'game point' which will be used to help calculate the next point. Next select another vertex at random. Let's say we get vertex `c` (0,2)...we'll need to plot a point halfway between our current game point and point `c`. So:

x = Math.round( (0 + 0.5)/2 ) = .25;
y = Math.round( (2 + 0.5)/2 ) = 1.25;

Our new game point is (.25,1.25) and we'll select another vertex at random, plot the halfway point, and so on. Do this several thousand times and the result is a fractal.

This obviously works well with triangles, but can easily be applied to any polygon. I've also seen variations such as not permitting the same vertex to be randomly selected twice consecutively and not allowing adjacent vertices to be selected consecutively. There are several examples of this here, and a more in-depth explanation of what the Chaos Game is and how it works on Wikipedia.

I've written this example in javascript partly for the sake of speed and simplicity and partly because I almost never get to write anything in plain javascript anymore. There's nothing very exciting about any of the code so I won't step through it here, but you can pull a copy from bitbucket if you're interested.