Assignment 2
Assignment 2
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.
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
1
2
3
4
5
6
7
8
9
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 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;
}
39
40
41
42
43
44
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
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
74
75
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 {
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
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
}
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
26
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
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
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