This example shows how to draw a fractal curve generated by prime numbers. It’s a modification of the earlier example Draw a fractal generated by prime numbers in C#.

The basic idea is follow these rules:

- Start at the point (0, 0), corresponding to the prime 7.
- Go to the next prime.
- Take one of the following steps depending on the prime’s form:
- 5n + 1: Move up 1 pixel
- 5n + 2: Move right 1 pixel
- 5n + 3: Move down 1 pixel
- 5n + 4: Move left 1 pixel

- Repeat from step 2 until bored.

The earlier example simply plots the points. This example draws lines between the points. It scales and translates the result so it as big as possible while still fitting on the drawing surface.

The following code shows how the program draws its fractal curve.

// Draw the fractal. private void btnDraw_Click(object sender, EventArgs e) { Cursor = Cursors.WaitCursor; Refresh(); // Generate the points. int num_points = int.Parse(txtNumPoints.Text); Point[] points = new Point[num_points]; Point current_point = new Point(0, 0); int prime = 7; for (long i = 0; i < num_points; i++) { // Find the next prime. while (!IsPrime(prime)) prime += 2; // Use this prime. switch (prime % 5) { case 1: current_point.Y--; break; case 2: current_point.X++; break; case 3: current_point.Y++; break; case 4: current_point.X--; break; } points[i] = current_point; // Move to the next possible prime. prime += 2; } // Draw the points. const int width = 500; Bitmap bm = new Bitmap(width, width); using (Graphics gr = Graphics.FromImage(bm)) { gr.Clear(Color.Black); // Get the largest and smallest coordinates. var x_query = from Point point in points select point.X; int[] xs = x_query.ToArray(); int xmin = xs.Min(); int xmax = xs.Max(); int wid = xmax - xmin; var y_query = from Point point in points select point.Y; int[] ys = y_query.ToArray(); int ymin = ys.Min(); int ymax = ys.Max(); int hgt = ymax - ymin; float scale = width / Math.Max(wid, hgt); // Scale and translate. gr.TranslateTransform(-xmin, -ymin); gr.ScaleTransform(scale, scale, MatrixOrder.Append); using (Pen thin_pen = new Pen(Color.Red, 0)) { // Draw the lines. gr.DrawLines(thin_pen, points); // Draw the axes. thin_pen.Color = Color.Blue; gr.DrawLine(thin_pen, xmin, 0, xmax, 0); gr.DrawLine(thin_pen, 0, ymin, 0, ymax); } } picFractal.Image = bm; picFractal.Visible = true; Cursor = Cursors.Default; }

The program creates an array of `Point`s to hold the points it generates. Next it sets `current_point = (0, 0)` and sets `prime = 7`. It then enters a loop to generate the points.

Inside the loop that generates a point, the program enters another loop. As long as `prime` is not prime, the code adds 2 to it. (The value `prime` starts at 7, so this loop only generates odd numbers.)

When it finds a prime, the program uses the rules to update `current_point`. It saves the new location and continues the outer loop to generate all of the points.

After it finishes generating the points, the program draws lines connecting them. To make the drawing as large as possible, the code first uses LINQ to get the minimum and maximum X and Y coordinates for all of the points. It translates the drawing to move the smallest coordinates to the origin. It then scales the drawing to make the drawing's larger dimension (width or height) cover the drawing's width.

After building the necessary transformations, the program draws lines connecting the points and draws the X and Y axes. The code finishes by displaying the result.

Note that if you use too many values, the program must scale the results down until they're so small you don't really see more than you do with the earlier example.

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