# Assignment 9: Mandelbrot Set

Due: Tuesday, April 28th by 11:59 PM

In this assignment, you will create a program that uses random numbers to create a Mandelbrot Set fractal.

## Getting Started

Start the Cygwin Bash Shell and run the following commands:

```cd h:
cd CS101
unzip CS101_Assign9.zip
cd CS101_Assign9
```

Leave the Cygwin Bash Shell open.

Start Visual Studio 2008 (from the Start menu, Programming->All Programs->Visual Studio 2008. Choose File->Open->Project/Solution.... Choose the file

H:\CS101\CS101_Assign9\CS101_Assign9.sln

In the Solution Explorer, double click the file Form1.cs. You should the form in a tab called Form1.cs [Design].

Create a program that draws a Mandelbrot Set and allows the user to zoom into a particular section of the screen. The Mandelbrot Set is formed by iterating the equation:

Z = Z2 + C

where C and Z are complex numbers.

Here is how it works:

The x-axis represents the real portion of C and the y-axis represents the imaginary portion of C. Therefore, each pixel represents a different complex number, depending on where it is on the screen and what values the edges of the screen represent. For example, if your form is 400 pixels wide and 400 pixels high, and it is to represent the values of C = -2 -2i in the upper left corner and C = 2 + 2i in the bottom right corner, then the point in the center of the screen (200, 200) would represent C = 0 + 0i. A point on the left edge 1/2 way between the top and bottom (0, 200) would represent C = -2 + 0i. A point at the bottom center (200, 400) would represent C = 0 + 2i. Each pixel represents a different complex number and you will need to use scaling to figure out what the real and imaginary parts of the complex number should be based on the pixel's location along the x- and y-axes and the maximum and minimum x and y values.

You will loop through each pixel on your form, each time calculating the value of C that the pixel represents, calling a function that iterates the equation Z = Z2 + C, starting with Z = 0 + 0i, and returns the number of iterations required for the magnitude of Z to become greater than 2.0. The returned number of iterations will be used to determine the color of the pixel. If the number of iterations is greater than or equal to 1000, the C value is said to be in the set and is normally colored black.

Here is a screenshot:

Hints

• Use text boxes to allow the user to enter the max and min real and imaginary values. You should then use these plus this.Width and this.Height to calculate the real and imaginary parts of C.

• If Z = a + bi, where a is the real part and b is the imaginary part, then Z2 = (a + bi)(a + bi). Using the FOIL method to multiply, Z2 = a2 + 2abi + b2i2, but i2 = -1. Thus the real part of Z2 is a2 - b2 and the imaginary part of Z2 is 2ab. You should calculate the real part and imaginary parts of each new Z value separately. To add complex numbers, just add the real parts to find the resulting real part and add the imaginary parts to find the resulting imaginary part.

• The magnitude of a complex number is sqrt(real2 + imaginary2).

• It would be good to use a structure called Complex to represent C.

• You will need to create a method that takes the complex number C and calculates the number of iterations of the equation Z = Z2 + C (max. of 1000) until the magnitude of Z is greater than 2.0. For example:

```int MandelIterations(Complex C)
```
• Here is one possible coloring scheme (where n is the number of iterations):

if n = 1, use one color
if 2 n 4, use another color
if 5 n 7 , use another color
if 8 n 9 , use another color
if 10 n 14 , use another color
if 15 n < 1000 , use another color
if n 1000, leave it black - it's in the "set"

For up to 80% credit, create a Mandelbrot Set with a the range of both real and imaginary numbers -2.0 to +2.0

For up to 90% credit, calculate the number of iterations in a separate method.

For up to 100% credit, use a Complex structure and allow the user to enter the max and min real and imaginary values of C that are displayed, thus allowing the user to zoom into a part of the picture. Note: you don't have to support zooming with the mouse. Instead, take the min and max real and imaginary values from the text boxes.

For up to 110% credit, use the FromArgb( ) method to calculate colors based on number of iterations, thus resulting in a smooth transition between colors.

For up to 150% credit, learn about another fractal on your own and write a program to produce it. Suggestions are Newton's Method in the Complex plane or Julia sets.

## Submitting

Before you submit, make sure Visual Studio is closed.

To submit, run the following command from the Cygwin bash shell (making sure that you are in the H:\CS101\CS101_Assign9 folder):

```make submit
```

You will be prompted for your Marmoset username and password, which you should have received by email. Note that your password will not appear on the screen. Important: Make sure that you check the file(s) you submitted to ensure that they are correct. Log into the server using the following URL:

https://camel.ycp.edu:8443/view/course.jsp?coursePK=54

You should see a list of labs and assignments. In the row for assign9, click the link labeled view. You will see a list of your submissions. Download the most recent one (which should be listed first). Verify that it contains the correct files.

You are responsible for making sure that your submission contains the correct file(s).