Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

CS 351 Assignment #2: Fractals
Justin Russell & Adam Lowenstein


In this assignment, we created and altered a number of images. We started by defining the appropriate type structures and functions in C. These programs allowed us to produce and edit different types of images. Some of the images that we produced were generated from scratch, like the fractals created through the use of Mandelbrot and Julia sets. The program that we created to generate these images tested each pixel to see whether it was in the zero attractor (i.e., it was eventually going to zero) or the infinity attractor. The picture to the right was generated using the Julia set with an iteration limit of 100, meaning that if the number of iterations exceeded 100, the pixel was assumed to be going to infinity.

The other part of this assignment involved making a number of modifications to a photograph taken in front of a blue screen. These modifications included changing the background color, replacing the background color with a photograph of the same size, and replacing the background with a photograph of a different size. These operations are described in greater detail in the sections below.


Before we could begin generating images of the Mandelbrot and Julia sets, we needed to create the Image type structure in C. To do so, we created a function called Image_init() that takes two integer parameters, rows and columns, and allocates the corresponding space for an image. In Image_mandelbrot(), we used Image_init() to implement the Mandelbrot algorithm provided in the lab notes. By altering the variables and constants within the algorithm, we were able to create a number of different representations of the Mandelbrot and Julia sets.

In the second part of the lab, we took a picture of Adam that was taken in front of a blue screen replaced the background. First, we wrote a program that turned to black all of the blue pixels above a certain threshold of "blueness," so the image was set against a black background. Then, we replaced the black background and imported an image of the Wu-Tang Clan (R.I.P. ODB) as our new background

How we solved the the task
As mentioned earlier, the algorithm used to generate the Mandelbrot and Julia sets was based on the pseudocode provided in the lab notes. Our implementation of that algorithm is shown below:Image *Image_julia(float x0, float y0, float x1, float y1, int rows, float cx, float cy) {
Image *im;
int cols = (x1 -x0) * rows / (y1 - y0);
int i, j, n;
float x, y, zx, zy, scols, srows;
scols = (x1 - x0) / cols;
srows = (y1 - y0) / rows;
im = Image_init(rows, cols);

for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
x = (scols * j + x0);
y = (-srows * i + y1);
for(n = 0; n < 100; n++) {
zx = x*x - y*y - cx;
zy = 2 * x * y - cy;
x = zx;
y = zy;
if( sqrt(x*x + y*y) > 2)

Unknown macro: { break; }

if(n < 100)

Unknown macro: { im -> data[i*cols + j].r = 0; im -> data[i*cols + j].g = 0; im -> data[i*cols + j].b = rand()%256; }


Unknown macro: { im -> data[i*cols + j].r = 0; im -> data[i*cols + j].g = rand()%256; im -> data[i*cols + j].b = 0; }




Using this algorithm in conjunction with another Mandelbrot.c file (to generate the image and specify its dimensions), we created the following images:

  • No labels