Draw a recursive overlapping snowflake fractal in C#

[overlapping snowflake fractal] [overlapping snowflake fractal] [overlapping snowflake fractal]

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;

    // 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(-2 * pi_over_3);
    GeneratorDTheta.Add(2 * pi_over_3);

    // Get the parameters.
    int depth = int.Parse(txtDepth.Text);

    Bitmap bm = new Bitmap(
    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 Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in fractals, graphics and tagged , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *