
.NET 1.1+The Chaos Game and the Sierpinski Triangle
The chaos game is a method for generating fractal images by plotting randomised points inside a polygon according to a simple set of rules. In this article we will use the chaos game to generate the Sierpinski triangle using C# and Windows Forms.
The Chaos Game
The chaos game is a mathematical term for a method of generating fractal imagery. The game provides a simple set of rules that can produce surprisingly complex figures. The algorithm specifies a starting condition with a set of defined points, or attractors. The set of attractors can be thought of as a series of vertices of a polygon. A starting point within the polygon is selected and plotted within the image.
Once the start condition is known, a repetitive process is followed to generate the fractal. Firstly a random vertex in the polygon is selected. Next, the current position, or starting position for the first iteration, is connected to the attractor's position with an imaginary, straight line. The current position is moved along this line by a fraction of the distance between the two points and the new position is plotted in the figure.
The Sierpinski Triangle
The Sierpinski triangle is a fractal that was first described by the Polish mathematician, Wacław Sierpiński. When displayed, it appears as a triangle divided into four sections, each a triangle half of the height and width of the original. The central triangle is inverted and can be thought of as a hole in the image. Each of the three outer triangles is a smaller version of the entire figure, with its own central hole. This pattern repeats infinitely to give an image similar to that shown below:

There are various methods in which the Sierpinski triangle can be produced. One of these is using the chaos game with the three attractors positioned at the three vertices of an equilateral triangle. When moving the current position towards an attractor, the amount moved is always half of the distance between the current point and the selected vertex. The steps to create the fractal are therefore:
- Define the three points.
- Select a starting position.
- Randomly select one of the three attractors.
- Move the current position halfway towards the selected attractor position and plot a point.
- Return to step 3.
Drawing the Sierpinski Triangle
In the remainder of this article we will create a C# program to generate the Sierpinski triangle. The program we will develop is that one that was used to generate the image above.
Creating the Project
To begin, create a Windows Forms project. We will use the default form to display the triangle, drawing directly onto the form's surface. Open the default form and change its BackColor property to white. We will start the process of generating the fractal in response to a button click, so add a button to the form and apply a suitable label to it using the Text property.
17 May 2009