
.NET 1.1+The Chaos Game and the Sierpinski Triangle (2)
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.
Class-Level Scoped Variables
During the production of the image there are three items that will be used by various methods. The first is a pseudo-random number generator, which will be used to determine the direction of travel during each iteration of the chaos game. Secondly, we need to hold the positions of the three vertices of the equilateral triangle. We can hold these in a small array of Point structures. Finally, another Point structure will hold the current position.
To declare the variables for these items, add the following class-level scoped variables to the class:
Random _randomiser = new Random();
private Point[] _points;
private Point _currentLocation;
Determining the Triangle Size
The size of the triangle should be as large as possible to enable the maximum detail to be displayed. However, we do want to ensure that the triangle is equilateral and is positioned centrally within the form's client area. To determine the largest possible size, we must consider the ratio between the height and width of any equilateral triangle. Assuming the base of the triangle is aligned horizontally, the width will be the same as the length of one of the sides. The height will be the width multiplied by half of the square root of three.
We can add a method to the form to calculate the ratio by adding the following code:
private double HeightWidthRatio()
{
return Math.Sqrt(3) / 2;
}
With the ratio readily available, we can determine the maximum size of the triangle by comparing the full width of the form's client area and the its height divided by the ratio. The smaller of these two values is the greatest size. The following method should be added to the form's class to calculate the triangle's size. Note that the height is reduced by two before being returned. This will allow for a small border around the edge of the fractal.
private int SideLength()
{
int height = (int)Math.Min(
(double)ClientSize.Width, ClientSize.Height / HeightWidthRatio());
return (int)height - 2;
}
Setting the Vertex Locations
The next method will calculate the positions of each of the three attractors, each being located at a vertex of the triangle. These values will be based upon the size of the triangle and the midpoint of the form's client area. The midpoint is calculated first by using half of the height and width of the form. We then add the top vertex, which will be point zero, whose X co-ordinate is vertically aligned with the midpoint and whose Y co-ordinate is half of the height of the triangle above the midpoint.
The two lower vertices are positioned at half of the triangle's height below the centre of the form and half of its width to either side of this point. These are points one and two within the array.
private void SetPointLocations(int sideLength)
{
Point midPoint = new Point(ClientSize.Width / 2, ClientSize.Height / 2);
_points = new Point[3];
_points[0] = new Point(midPoint.X,
midPoint.Y - (int)(sideLength * HeightWidthRatio() / 2));
_points[1] = new Point(midPoint.X - sideLength / 2,
midPoint.Y + (int)(sideLength * HeightWidthRatio() / 2));
_points[2] = new Point(midPoint.X + sideLength / 2,
midPoint.Y + (int)(sideLength * HeightWidthRatio() / 2));
}
17 May 2009