DEPARTMENT OF COMPUTING

CS 3005: Programming in C++

Complex Fractal Plane

Introduction

In a previous assignment, we created a number grid, that allows the user to store integer numbers at each location in a rectangular, regular grid. We then added the a color table that allows the user to assign a color to each of the integers stored in the grid, allowing a way for users to create images from numbers.

The next series of assignments will add ways for users to insert numbers into the number grid using algorithms. Most of these algorithms work better for floating point numbers, instead of integer numbers.

In this assignment, we’ll add a translation between continuous number plane (x,y) coordinates and discrete grid (row, column) coordinates.

Then, we’ll create a simple algorithm to calculate numbers using the number plane, and store the numbers in the grid.

Mapping between a regular grid and a number plane

Our end goal is to have a rectangular image with pixel colors assigned based on a mathematical function. The function will take plane coordinates (x,y) as input, and return a number for the number grid. That number will be used to lookup a color in the color table, to set a pixel color in an image.

We make the grid with dimensions of height by width. The rows of the grid are numbered 0 through height-1 and the columns are numbered 0 through width-1. For each of these grid locations, we want to calculate a point x,y that represents the center of the pixel in the number plane.

If two grid locations are horizontal neighbors (for example, column differs by 1, and row is the same), then the plane coordinates of the two points will differ by a fixed amount in the x dimension. Similarly, if two grid locations are vertical neighbors (for example, row differs by 1, and column is the same), then the plane coordinates of the two points will differ by a fixed amount in the y dimension.

Depending on the mathematical function we use to calculate numbers for our grid, we may want the part of the number plane that is mapped to our grid to be different. We will choose minimum and maximum values for x and y that correspond to the centers of the left and right columns, and the bottom and top rows. (Minimum x is left; maximum x is right; minimum y is bottom; maximum y is top.)

For example, we may want to have our image look at the region of the plane from -1.5 to 0.5 in the x axis and -0.3 to 1.2 in the y axis. For this example, we’ll make a grid that is 4 locations wide and 3 locations high. (Very small, but a good example of the calculations.)

Let’s calculate the x coordinates of our image. First, the left most column (0) will have x = -1.5, because that is the left side of the region of the image. Next, the right most column (3) will have x = 0.5, the right side of the region. What are the x coordinates of columns 1 and 2? They need to be regularly spaced. There are width-1 = 3 gaps between our columns. Draw a picture to convince yourself that width-1 is the correct number of gaps. We will use the term delta to indication the change in a coordinate over a gap.

Since there are 3 gaps to travel from -1.5 to 0.5, how big is each gap? The generic formula for this is delta_x = (max_x - min_x) / (width - 1).

delta_x = ( 0.5 - ( -1.5 ) ) / ( 4 - 1 ) = 2.0 / 3 = 0.666666

With the delta between columns calculated, we can easily calculate the x coordinate for any column. The generic formula for this calculation is x = min_x + column * delta_x.

x_column_0 = min_x + 0 * delta_x = -1.5
x_column_1 = min_x + 1 * delta_x = -0.833333
x_column_2 = min_x + 2 * delta_x = -0.166666
x_column_3 = min_x + 3 * delta_x =  0.5

Using these two formulas, we can calculate the x coordinate of any column.

A similar discussion for calculating the y coordinate of any row yields these results.

delta_y = ( 1.2 - ( -0.3 ) ) / ( 3 - 1 ) = 1.5 / 2 = 0.75
y_row_0 = max_y - 0 * delta_y =  1.2
y_row_1 = max_y - 1 * delta_y =  0.45
y_row_2 = max_y - 2 * delta_y = -0.3

Now, for every grid location, we can calculate the point in the plane (x0,y0), from the (column,row) of the pixel. For each grid location, these values are used in our mathematical function to calculate a number to store in the grid.

Assignment

In this assignment you will create a class to map between number plane and grid coordinates. It will inherit from NumberGrid for the height, width, max value, and number storage.

You will also extend the ppm_menu program to add a few new commands.

The new commands required are:

Programming Requirements

The following files must be updated or created and stored in the src directory of your repository.

Updates to NumberGrid.{h,cpp}

Updated Methods:

Additional Methods:

Create ComplexFractal.{h,cpp}

This class will handle all of the translations between plane coordinates (x,y) and number grid coordinates (column, row). This class inherits publicly from NumberGrid.

Data Members:

Methods:

Update image_menu.h and image_drawing.cpp

The follow functions must be declared and implemented.

Update Functions in controllers.cpp

Table of New Commands

Command Name Function Name Description
fractal-plane-size setFractalPlaneSize Set the dimensions of the grid in the complex plane.
fractal-calculate calculateFractal Calculate the escape values for the fractal.

Update Makefile

The following commands should work correctly.

Additional Documentation

Show Off Your Work

To receive credit for this assignment, you must

Additionally, the program must build, run and give correct output.

Extra Challenges (Not Required)

Last Updated 03/27/2024