Description
Problem: Mandelbrot Set
The Mandelbrot set was first discovered by Pierre Fatou and Gaston Julia. Benoit Mandelbrot was among the first
to use a computer to visualize the Mandelbrot set, so it was named in his honor. Perhaps the most eloquent
description of the set was given by musician Jonathan Coulton, better known these days for his song “Still Alive”.
In his song “Mandelbrot Set”, Coulton sings: “Pick a point called z in the complex plane, and let z1 be z
2 + z, and z2
is z1
2 + z, and z3 is z2
2 + z, and so on. If the series of zs will always stay close to z and never trend away, that point is
in the Mandelbrot set.” Your goal with this assignment is to essentially repeat Benoit Mandelbrot’s contribution to
mathematics by writing a program to visualize the Mandelbrot set.
In case your math is a little rusty, a complex number is a number in the form a + bi, where i is the imaginary
number, equal to the square root of -1, and a and b are real numbers. So, in multiplication i
2 = -1. The number a is
referred to as the real part, and bi is the imaginary part. Complex numbers can be plotted in a two dimensional plane
in much the same way as ordered pairs are plotted in middle school algebra. The horizontal dimension of the
complex plane is the real dimension (i.e. a), and the vertical dimension is the imaginary dimension (i.e. b). The
magnitude of a complex number is simply its distance from the origin, calculated using the Pythagorean Theorem.
Figuring out exactly which complex numbers are in the Mandelbrot set and which aren’t can get a little hairy. Some
numbers are easily shown to be inside and some are easily shown to be outside, but other numbers are a little trickier
to figure out. This is where computers come in. To show that a given number is outside the Mandelbrot set, you
can just calculate the series of zs and if the magnitude of any number in the sequence is bigger than 2, the number is
not in the Mandelbrot set, and you can stop computing the sequence. For numbers that are in the Mandelbrot set, the
sequence will go on forever without reaching a magnitude of 2, so you need to give up at some point and resign
yourself to the fact that the number may be in the Mandelbrot set. The most spectacular images of the Mandelbrot
set come not from plotting the numbers inside the set, but from plotting numbers that are just outside the set and
assigning them colors based on how long it takes for the sequence to reach a magnitude greater than 2.
Program Requirements
Implement a class MandelbrotSet that offers visualization of the Mandelbrot set described above. The program
must have
1. A window with a Canvas (or JPanel) for displaying the Mandelbrot set visualization.
2. Text fields for the following four data entries: real center, imaginary center, view size and maximum
sequence length.
3. A button that can be clicked to plot the Mandelbrot set.
The GUI components should be laid out neatly in the window. When the button is clicked, your program should go
through every point on the canvas, treat the point as a complex number, and use the procedure described above to
determine whether or not the point is in the Mandelbrot set. If the sequence escapes (magnitude gets bigger than 2),
then draw the point on the canvas in a color based on the number of steps taken to escape. If the sequence doesn’t
escape, then draw the canvas point in a fixed color. Converting a point on the canvas to a complex number is
covered in the “Useful Formulas” section.
The four text fields in your window whose values indicate the real and imaginary coordinates represented by the
center of the canvas, the level to which the view should be zoomed, and the maximum length a sequence can reach
before giving up and deciding that a point didn’t escape (i.e. may be in the Mandelbrot set). Clicking the canvas
should fill in the coordinate text fields with the complex number corresponding to the clicked point on the
canvas, and generate a new image with this new center. If the enter key is pressed on any of the text fields, your
program should retrieve the typed in values and generate a new image with these parameters (Hint: adding an
ActionListener to a text field will capture when the user presses the enter key). Good default values for the text
fields are: real center = -0.4, imaginary center = 0, view size = 1.1, and maximum sequence length = 50.
Your program must include a ComplexNumber class with methods for squaring the number, addition, and getting
the point’s magnitude. Additionally, there should be a constructor that takes in two doubles indicating the values of
the real and imaginary parts of the number being created. ComplexNumber objects should be immutable, meaning
that once the object is created, its value must not change.
Useful Formulas
Squaring a complex number:
If z is the complex number a + bi, then z
2 = (a2 – b2
) + (2ab)i.
Magnitude of a complex number:
If z is the complex number a + bi, then z’s magnitude is:
2 2
z = a + b
Adding complex numbers:
If z = a + bi and y = c + di, then z + y = (a + c) + (b + d)i.
Mapping a canvas point to a complex number:
Let a0 and b0 be the real and imaginary coordinates corresponding to the center of the canvas, respectively. Let r be
the view size. Let the canvas be a square with side length w. Let (x, y) be coordinates of a point on the canvas. The
coordinates (x, y) correspond to the complex number z = a + bi where:
( ) ( )
w
ry b b r
w
rx
a = a0 − r + 2 = 0 + − 2
Documentation
Your code must include:
1. Header comments for each source file, with the following information: file name, your name, course
number and date.
2. Inline comments throughout the code explaining its logic and design.
3. Javadoc comments for your ComplexNumber class, and each of the public members in the class.
You also need to turn in Javadoc HTML files for your ComplexNumber class.
Deliverables
Create a folder Mandelbrot. Put your source codes (ComplexNumber.java and MandelbrotSet.java)
inside this folder and put your Javadoc HTML files inside a sub-folder doc. Then, zip the folder Mandelbrot into
file Mandelbrot.zip and submit it through WebCourses by the due date and time indicated. You must send your
file as an attachment using the “Add Attachments” button. Assignments that are typed into the submission box will
not be accepted. Submission of .class files instead of .java files will result in a score of 0. Make sure you submit
your source files and not your compiled class files.
Restrictions
1. Your program must compile using Java 6.0 or later.
Execution and Grading Instructions (This is for TAs)
Points Total: 100
1. Create a new folder for the submissions.
2. For each student, download student’s source codes into the folder.
3. Review each of the source files and check for comments.
4. Check the Javadoc files for the ComplexNumber class.
5. Compile the program using “javac MandelbrotSet.java”
6. Run the program using “java MandelbrotSet”
Points Breakdown
Execution: 60
Code: 20
Documentation: 20