CS 3005: Programming in C++
Mandelbrot Set
Introduction
The Mandelbrot set is a mathematical set defined from a function, similar to and related to the Julia set. However, the Mandelbrot set is more widely know.
Finding Points in the Mandelbrot Set
For our purposes, this is a good enough definition of the Mandelbrot set for a function (x’, y’) = f(x, y, a, b), where (x, y), (a, b) and (x’, y’) are the coordinates of points in the 2 dimensional plane.
Take a point (a, b). Using x0 = 0, y0 = 0, a, and b as input to f(x, y, a, b), we receive a new point from the function.
x1, y1 = f(x0, y0, a, b)
Repeat that process by using the output of the previous call of the function
as the input to the current call. We could repeat up to n
times:
x2, y2 = f(x1, y1, a, b)
x3, y3 = f(x2, y2, a, b)
x4, y4 = f(x3, y3, a, b)
...
xn-1, yn-1 = f(xn-2, yn-2, a, b)
xn, yn = f(xn-1, yn-1, a, b)
For large enough values of n, the resulting point at xn, yn will fall into one of two categories:
1- The point will still be close to the origin (0,0) of the plane. We define close to mean a distance less than or equal to 2.
2- The point will be far from the origin. We define far to mean a distance greater than 2.
If the point is close to the origin, it is part of the Mandelbrot set for f(x,y,a,b). Otherwise it is not part of the Mandelbrot set. A point that is not part of the Mandelbrot set “escapes” at the first iteration where the new (x,y) point is a distance of more than 2 from the origin. For points that escape, we will want to remember the “escape count”, or which iteration it escaped.
From Functions to Images
So, where do the interesting pictures come from?
We choose an actual function for f(x,y,a,b)
, then color
each point in the plane based on how far it is from
being in the Mandelbrot set for the selected function.
“How far” is the “escape count”.
Actually, there are infinitely many points in the plane, so we can’t do every point. Instead, we select a regular grid of points each representing the points near it in the plane. For each point in the grid:
- Calculate its
a,b
value, from its position in the grid. - Use x0 = 0 and y0 = 0.
- Iterate the application of
f(x,y,a,b)
up ton
times. - If the result becomes far away, remember the iteration number when it first escaped (became far away from the origin). We call this the “escape value”. Points with lower escape values are further from the Mandelbrot set than those with higher escape values.
- If the result doesn’t become far away (escape) within
n
iterations, assume it did not escape and remembern
as the escape value. These points are assumed to be part of the Mandelbrot set forf(x,y,a,b)
.
Now, we have a set of integers (the escape values) that represent
how close each of the points in the grid
are to the Mandelbrot set for f(x,y,a,b)
. Larger numbers
mean they are closer to the Mandelbrot set.
Since the points selected are in a regular grid, we can pair each grid point with a pixel in a rectangular image. We assign the color of pixel based on the escape value of the corresponding grid point. All pixels associated with the same escape value will have the same color.
Defining a Regular Grid
See the description in the Julia set assignment.
Our Function f(x,y,a,b)
For our function f(x,y,a,b)
we will use this definition:
x' = x*x - y*y + a
y' = 2*x*y + b
where, a
and b
are the coordinates of the original point in the plane.
Note this is similar to, but different from the Julia set, where a
and b
where fixed values for an entire image calculation. Here, they depend on
the point whose escape count you are calculating.
Assignment
In this assignment you will create a class to calculate and store a Mandelbrot
set’s escape values. Most of the work has already been done in the
NumberGrid
and ComplexFractal
classes. In this assignment, you will create
a new class MandelbrotSet
class, inheriting from ComplexFractal
and adding a few methods specific to the calculation of Mandelbrot sets.
You will also extend
the ppm_menu
program to add a few new commands.
The new commands required are:
mandelbrot
: Choose to make a Mandelbrot set.
Programming Requirements
The following files must be updated or created and stored in the src
directory
of your repository.
Create MandelbrotSet.{h,cpp}
These files will be used to declare and define the MandelbrotSet
class.
No data members are required.
Note that the NumberGrid
’s maximum number will be used for the MandelbrotSet
’s maximum escape count.
Also note that MandelbrotSet
inherits from ComplexFractal
.
Methods:
MandelbrotSet( );
The default constructor, chain constructs via theComplexFractal
default constructor.MandelbrotSet( const int& height, const int& width, const double& min_x, const double& max_x, const double& min_y, const double& max_y );
Passes arguments on to theComplexFractal
constructor.virtual ~MandelbrotSet( );
Does nothing, empty code block.virtual void calculateNextPoint( const double x0, const double y0, const double& a, const double& b, double& x1, double &y1 ) const;
Calculatesx1
andy1
using the function described above for the Mandelbrot set.int calculatePlaneEscapeCount( const double& a, const double& b ) const;
UsescalculateNextPoint
to find the escape count for the point at (a
,b
). The first iteration from x,y = 0,0 to x,y = a,b doesn’t count as an iteration.virtual int calculateNumber( const int& row, const int& column ) const;
Uses other methods to find the plane coordinate of the pixel atrow
,column
and to calculate the escape count. Returns the count. Ifrow
,column
isn’t valid, then returns-1
.
Update/Add Functions in image_menu.h
and controllers.cpp
Add or update the following function declarations to the header file and implementations to the .cpp
file.
void setMandelbrotFractal( ActionData& action_data );
UsesetGrid()
to setaction_data
’s grid to aMandelbrotSet
object allocated from the heap.void configureMenu( MenuData& menu_data )
add the new actions with the names and descriptions listed below.
Table of New Commands
Command Name | Function Name | Description |
mandelbrot | setMandelbrotFractal | Choose to make a Mandelbrot set. |
Update Makefile
The following commands should work correctly.
make hello
- builds the hello programmake questions_3
- builds the questions_3 programmake ascii_image
- builds the ascii_image programmake image_file
- builds the image_file programmake ppm_menu
- builds the image_file programmake all
- builds all programsmake
- builds all programs (same asmake all
)make clean
- removes all .o files, and all executable programs
Additional Documentation
- C++ Reference
- Examples from class
- Sample Session Input File
- Julia set on Wikipedia
- Mandelbrot set on Wikipedia
Sample PPM Images
Show Off Your Work
To receive credit for this assignment, you must
- use git to add, commit and push your solution to your repository for this class.
- successfully pass all unit tests and acceptance tests
Additionally, the program must build, run and give correct output.
Extra Challenges (Not Required)
- Create classes that inherit from
MandelbrotSet
that have differentcalculateNumber()
functions for calculating values. Add the ability to use them from theimageMenu()
. - Try other ways to modify the plane and parameters that would make it easier to create interesting images.
Last Updated 09/11/2023