[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Draw a randomly colored Sierpinski octagon in C#

[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[8]; 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.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.