 Index Books FAQ Contact About Rod   # Title: Draw a randomly colored Sierpinski octagon in C# This example draws a randomly colored Sierpinski octagon. It's basically the same as the example Draw a randomly colored Sierpinski pentagon in C# except it draws an octagon instead of a pentagon. See that example for most of the details.

There are two main differences between that example and this one. First, the new example uses the following code to set its scale factor.

// Scale factor for moving to smaller octagons. private float size_scale = (float)(1.0 / (2.0 * (1 + Math.Cos(Math.PI / 180 * 45))));

The program adjusts the radius of the smaller octagons by this scale factor to make them fit properly. (This scale needs to change to different kinds of polygons.)

The second change is in the method that generates the points that make up the polygons. The following code shows the new version, which creates an octagon.

// Find the octagon's corners. private PointF[] GetOctagonPoints(PointF center, float radius) { PointF[] points = new PointF; double theta = -Math.PI / 2.0; double dtheta = 2.0 * Math.PI / 8.0; for (int i = 0; i < 8; i++) { points[i] = new PointF( center.X + (float)(radius * Math.Cos(theta)), center.Y + (float)(radius * Math.Sin(theta))); theta += dtheta; } return points; }

This method simply loops the variable theta through the angles at which the octagon's vertices lie and uses Math.Sin and Math.Cos to calculate the locations of the vertices.

The rest of the program is similar to the previous post.