0% found this document useful (0 votes)
75 views

Assignment 2

This document summarizes a programming assignment to create a Mandelbrot fractal using complex numbers in Java. It describes dividing the problem into calculating points and displaying the fractal. A Complex class is defined to represent complex numbers since Java does not have a native datatype. The Complex class contains methods for arithmetic operations. A Mandelbrot class uses Complex to calculate values for each point and return a color. A FractalWindow class displays the fractal by looping through points and coloring pixels. The program was tested using a Test class and achieved the intended output of displaying the Mandelbrot fractal.

Uploaded by

Ana Roman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Assignment 2

This document summarizes a programming assignment to create a Mandelbrot fractal using complex numbers in Java. It describes dividing the problem into calculating points and displaying the fractal. A Complex class is defined to represent complex numbers since Java does not have a native datatype. The Complex class contains methods for arithmetic operations. A Mandelbrot class uses Complex to calculate values for each point and return a color. A FractalWindow class displays the fractal by looping through points and coloring pixels. The program was tested using a Test class and achieved the intended output of displaying the Mandelbrot fractal.

Uploaded by

Ana Roman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Object Oriented Programming

Programming report
assignment 1
A.J. ten Caat Second Author
s2477637 second authors s number
May 11, 2015

Problem description

This first assignment is about the Mandelbrot-fractal. Our task is to draw one using complex numbers, so
there is no input and the output is a Mandelbrot-fractal.

Problem analysis

This problem can be divided into two smaller problems: calculating the points of the fractal and displaying
them. The students are already given a class called FractalWindow which solves the handles the displaying,
leaving us with the first. For this task, we need to use complex numbers, so we had to find a way to define
complex numbers, since java does not have a datatype for the complex numbers by default.

Program design

To define complex numbers, we create a new class called Complex. This class contains fields for the real
and imaginary part and several methods to define addition, multiplication, squaring and calculating the
norm of complex numbers. These are then used in the class Mandelbrot to create an object containing the
right greyscale for each coordinate. A Mandelbrot object is created in the main class, which is then used in
the creation of a FractalWindow object, which draws the fractal in a separate window.

Evaluation of the program

To test if the Complex class worked as it should be we created a class Test (see appendix) which created
some complex numbers and performed certain mathematical operations on them using the Complex class.
Since there was no input and the output was the Mandelbrot fractal, we can assume that our program works
as it should. Most of the design was outlined in the syllabus, and since we followed those outlines, the end
result is indeed as it was designed.

Conclusions

Our program solves the problem in an efficient way. Since the steps we needed to perform to create the
program were already told in the syllabus, thinking of a solution to the problem was quite easy. The
difficult part of this first assignment was understanding to workflow of object oriented programming and
implemented these steps in such a programming language. So what we learned from this assignment were
some of the basics of java, like creating a class with fields, constructors, etc.
1

Appendix: program text


Listing 1: Fractal.java

1
2
3
4
5
6
7
8
9

public class Fractal {


public static void main(String args[]){
Mandelbrot m = new Mandelbrot();
new FractalWindow(m);
}
}
Listing 2: Complex.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

public class Complex {


public double real;
public double imaginary;

public Complex(){
real = 0;
imaginary = 0;
}
public Complex(double x){
real = x;
imaginary = 0;
}
public Complex(double x, double y){
real = x;
imaginary = y;
}
public void SetReal(double x){
this.real=x;
}
public void SetImaginary(double y){
this.imaginary=y;
}
public double getReal(){
return this.real;
}

public double getImaginary(){


return this.imaginary;
}
2

39
40
41
42
43
44

public String toString(){


return String.format("%f + %fi", real,imaginary);
}
public void add(double x){
this.real+=x;

45
46
47
48
49
50

}
public void add(Complex x){
this.real+=x.real;
this.imaginary+=x.imaginary;
}

51
52
53
54
55
56

public void multiply(double x){


this.real*=x;
this.imaginary*=x;
}

57
58
59
60
61

public void
double a
double b
double c
double d

62
63
64
65
66
67

this.real = a*c-b*d;
this.imaginary = a*d+b*c;
}

68
69
70
71
72
73

public void square(){


this.multiply(this); //compute the
//square of the real number
}
public double norm(){

74
75

return Math.sqrt(this.real*this.real + this.imaginary*this.


imaginary);
}

76
77
78
79
80

multiply(Complex x){
= this.real;
= this.imaginary;
= x.real;
= x.imaginary;

}
Listing 3: Mandelbrot.java

1
2
3
4
5
6
7

import java.awt.Color;
public class Mandelbrot {

public int repetitions(Complex c, int max){

Complex d = new Complex();

9
10
11
12
13
14

int i=0;
while (d.norm() < 2 && i<max ){
d.square();
d.add(c);
i+=1;
}
return i;
}

15
16
17
18
19
20

// this is for printing the black and white fractal


/*public Color colorAt(double x, double y){
int a = repetitions( new Complex(x,y), 50);
System.out.println(a);
double b = (double)a / 50 * 255;
return new Color((int)b, (int) b, (int)b );

21
22
23
24
25
26
27
28
29
30
31

}*/
//this will print the color version
public double max(double x, double y){
if(x > y){
return x;
}
return y;
}

32
33
34
35
36
37

public double min(double x, double y){


if(x < y){
return x;
}
return y;
}

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

public Color colorAt(double x, double y){


int a = (repetitions( new Complex(x,y), 50));
double q = (double)a / 50;
int r = (int) (255 * max(min(3 * q - 1, 3 - 3 * q),0));
int g = (int) (255 * max(min(3 * q, 2 - 3 * q),0));
int b = (int) (255 * max(1 - 3 * q,0));
return new Color(r, g, b);
}

}
Listing 4: FractalWindow.java

1
2
3
4

import javax.swing.*;
import java.awt.*;
4

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

import java.awt.image.*;
/**
*
* @author OOP 2015 TA team.
*/
public class FractalWindow extends JFrame {
private FractalPanel panel;
/**
* Constructor of the class. This will create the main window.
* @param fractal
*/
public FractalWindow(Mandelbrot fractal) {
super("Mandelbrot fractal"); // Add title.
panel = new FractalPanel(fractal); //Create panel.
add(panel);

23
24
25

setDefaultCloseOperation(EXIT_ON_CLOSE); //What happens when the


frame closes

26

setSize(panel.getWidth() + 32, panel.getHeight() + 32); //Set


frame size
setVisible(true); //make it visible

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

}
/**
* This class will create the windows panel.
*/
private class FractalPanel extends JPanel {
private final int canvasWidth = 512, canvasHeight = 512; //
Assign values for width and height.
private final double xMin = -2, xMax = 1, yMin = -1.5, yMax =
1.5;
private BufferedImage image;
/**
* Panel constructor.
* @param f
*/
public FractalPanel(Mandelbrot f) {
setSize(canvasWidth, canvasHeight); //Set panel size
image = new BufferedImage(canvasWidth, canvasHeight,
BufferedImage.TYPE_INT_RGB); //Create the image
for (int i = 0; i < canvasHeight; i++) { //Loop throught
every pixel
for (int j = 0; j < canvasWidth; j++) {
double x = xMin + j * (xMax - xMin) / canvasWidth;
5

double y = yMin + (canvasHeight - i) * (yMax - yMin) /


canvasHeight;
Color c = f.colorAt(x, y);
image.setRGB(j, i, c.getRGB()); //Color the pixel

53
54
55
56
57

}
}
}
/**
* Draw on the panel.
* @param g
*/
public void paint(Graphics g) {
super.paint(g);
g.drawImage(image, 0, 0, null);
}

58
59
60
61
62
63
64
65
66
67
68

}
}
Listing 5: Test.java

1
2
3
4
5
6
7
8
9
10

public class Test {


public static void main(String args[]){
Complex a = new Complex(10,10);
Complex b = new Complex(2.7,20);

System.out.println(a.getReal());
System.out.println(b.getImaginary());

11
12
13
14
15
16
17
18
19

a.add(new Complex(50,50));
System.out.println(a.toString());
System.out.println(a.norm());
}
}

Output

You might also like