Draw a colored fractal curve generated by prime numbers in C#

colored fractal curve generated by prime numbers

This example shows how to draw a colored fractal curve generated by prime numbers. See the example Draw a fractal curve generated by prime numbers in C# for the basic algorithm.

This example assigns colors to line segments based on the number of times the curve visits each point. Points that are visited only once get red segments. Points that are visited many times get blue segments.

This example is very similar to the previous one, so see that example for most of the code. The difference is that this example needs to keep track of the number of times it visits each point.

Before the program starts, you don’t know what points it will eventually visit, so you can’t build an array or some other simple data structure to keep track of the counts. (Actually you could, but you would need to restrict the points that the algorithm would track. The example Draw a fractal generated by prime numbers in C# takes that approach.)

To store point hit counts, this example uses the following statement to create a Dictionary<Point, int>.

Dictionary<Point, int> hits = new Dictionary<Point, int>();

When the program visits a point, it uses the following code to increment that point’s hit count.

points[i] = current_point;
int count = 0;
if (hits.ContainsKey(current_point))
    count = hits[current_point];
hits[current_point] = count + 1;

The previous example used a Graphics object’s DrawLines method to draw lines connecting all of the points with a single red pen. This example uses different colors for each point, so it needs to use a different method.

First, the program needs to find the largest hit count for any point. The following code does that.

// Get the largest hit count.
int max_hit = hits.Values.ToArray().Max();

This code gets the values from the hits Dictionary. It converts the result (which is a list) into an array, and calls the array’s Max LINQ extension method.

The following code shows how the program draws the line segments.

using (Pen thin_pen = new Pen(Color.Black, 0))
    // Draw the lines.
    for (int i = 1; i < num_points; i++)
        // Set the pen color.
        thin_pen.Color =
            MapRainbowColor(hits[points[i]], 1, max_hit);
        gr.DrawLine(thin_pen, points[i - 1], points[i]);

    // Draw the axes.
    thin_pen.Color = Color.Blue;
    gr.DrawLine(thin_pen, xmin, 0, xmax, 0);
    gr.DrawLine(thin_pen, 0, ymin, 0, ymax);

For each point (after the first one), the program gets the point’s hit count. It uses the MapRainbowColor method to map the hit count to a color (see the post Map numeric values to colors in a rainbow in C# for details). The code then sets its pen’s color to the resulting color and draws the segment from the previous point to this one.

The rest of the program is similar to the previous one. Download the example for additional details.

Download Example   Follow me on Twitter   RSS feed

About RodStephens

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

Leave a Reply

