[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 recursive overlapping snowflake fractal in C#

[Draw a recursive overlapping snowflake fractal in C#] [Draw a recursive overlapping snowflake fractal in C#] [Draw a recursive overlapping snowflake fractal in C#]

The example Draw a recursive snowflake fractal in C# explains how to use an iterator and a generator to draw a snowflake fractal. This example uses the same technique with the initiator and generator shown in the second and third pictures above to create an overlapping snowflake fractal.

The code that draws the curve is the same as before. The only difference between this example and the previous one is the following code, which defines the initiator and generator.

// Coordinates of the points in the initiator. private List<PointF> Initiator; // Angles and distances for the generator. private float ScaleFactor; private List<float> GeneratorDTheta; private void btnGo_Click(object sender, EventArgs e) { this.Cursor = Cursors.WaitCursor; Application.DoEvents(); // Define an initiator and generator. Initiator = new List<PointF>(); float sqrt_3 = (float)Math.Sqrt(3.0); float side1 = (picCanvas.ClientSize.Height - 10f) / 2f; float side2 = (picCanvas.ClientSize.Width - 10f) / 2f / sqrt_3 * 2f; float side = Math.Min(side1, side2); float height = side * sqrt_3 / 2f; float y1 = (picCanvas.ClientSize.Height - 2 * side) / 2; float y2 = y1 + side / 2; float y3 = y2 + side; float y4 = y1 + 2 * side; float x1 = (picCanvas.ClientSize.Width - 2 * height) / 2; float x2 = x1 + height; float x3 = x2 + height; Initiator.Add(new PointF(x1, y2)); Initiator.Add(new PointF(x2, y1)); Initiator.Add(new PointF(x3, y2)); Initiator.Add(new PointF(x3, y3)); Initiator.Add(new PointF(x2, y4)); Initiator.Add(new PointF(x1, y3)); Initiator.Add(new PointF(x1, y2)); //// Initiator for drawing a single generator. //Initiator = new List<PointF>(); //Initiator.Add(new PointF(5, // picCanvas.ClientSize.Height / 3)); //Initiator.Add(new PointF( // picCanvas.ClientSize.Width - 5, // picCanvas.ClientSize.Height / 3)); ScaleFactor = (float)(1.0 / 3.0); GeneratorDTheta = new List<float>(); float pi_over_3 = (float)(Math.PI / 3f); GeneratorDTheta.Add(0); GeneratorDTheta.Add(pi_over_3); GeneratorDTheta.Add(pi_over_3); GeneratorDTheta.Add(-2 * pi_over_3); GeneratorDTheta.Add(-pi_over_3); GeneratorDTheta.Add(-pi_over_3); GeneratorDTheta.Add(2 * pi_over_3); // Get the parameters. int depth = int.Parse(txtDepth.Text); Bitmap bm = new Bitmap( picCanvas.ClientSize.Width, picCanvas.ClientSize.Height); picCanvas.Image = bm; // Draw the snowflake. using (Graphics gr = Graphics.FromImage(bm)) { gr.SmoothingMode = SmoothingMode.AntiAlias; DrawSnowflake(gr, depth); } this.Cursor = Cursors.Default; }

This code initializes the Initiator and GeneratorDTheta lists. It then calls the DrawSnowflake method to draw the fractal.

Note that the code includes a commented out section that makes the initiator include only a single line segment. That code makes the program draw the single generator shown in the third picture at the top of the post.

The rest of this initialization code is confusing but straightforward. It simply performs the calculations needed to perform the initializations for the overlapping snowflake fractal.

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

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