Sometimes I get the urge to create something…anything. Generating fractals from code is a good exercise in those situations and this time I decided to go with the simple Sierpinski Triangle.

Play with the two values in the demo below and hit the “Draw” button to redraw the triangle.

This movie requires Flash Player 9

Here’s a quick explanation on the logic:

For every triangle, three more triangles that are half the size of the original are generated. These are placed adjacent to each side of the original triangle. Keep repeating for every triangle created and eventually you end up with something similar to the example above.