-
Notifications
You must be signed in to change notification settings - Fork 22
/
MandelbrotRenderer.java
118 lines (100 loc) · 3.7 KB
/
MandelbrotRenderer.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package mandelbrot;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
/** MandelbrotRenderer.java
* The thread class that will do the work of making images of the Mandelbrot set.
* Much of the computation is or is based on Dr. Matt Lang's code.
* @author Eric Mosher, Dr. Matt Lang
*/
public class MandelbrotRenderer implements Runnable {
// The size of all images
private int width = 1920;
private int height = 1080;
// The number of maximum iterations
private int maxIterations;
// Divergence radius
private double radius = 2;
private double divergence = radius * radius;
// Offsets between image[0][0] and center point
private int xOffset = -(width - 1)/2;
private int yOffset = (height - 1)/2;
private int black = 0;
private MbrotParameter[] mbrots;
private ColorScheme color;
/**
* This class needs a list of parameters to iterate over, this is taken as
* an array of MbrotParameters.
* @param imgs the images to produce
* @param color the color scheme to use
*/
public MandelbrotRenderer(MbrotParameter[] imgs, ColorScheme color,
int maxIterations) {
mbrots = imgs;
this.color = color;
this.maxIterations = maxIterations;
}
/**
* This class will be used as a thread, so we must use run() to do our work.
*/
public void run() {
int iteration, point;
double a, b, aOld, x, y;
int[] colorPalette = new int[maxIterations];
// Fill the color palette
for (int i = 0; i < maxIterations; i++) {
colorPalette[i] = color.getColor(i);
}
// For each element in the list of MbrotParameters...
for (MbrotParameter currMbrot : mbrots){
// Do the work to create each image
// Create a new image
BufferedImage img = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
File out = new File(currMbrot.fName);
// For each pixel
for (int r = 0; r < height; r++)
{
for (int c = 0; c < width; c++)
{
// Calculate the point the pixel actually represents
x = currMbrot.xCenter + (xOffset + c)
/ currMbrot.resolution;
y = currMbrot.yCenter + (yOffset - r)
/ currMbrot.resolution;
iteration = 0;
a = x;
b = y;
// While the point is within the divergence radius and
// the iteration bound
while (a*a + b*b <= divergence && iteration < maxIterations)
{
// Calculate the sequence
aOld = a;
a = a*a - b*b + x;
b = 2*aOld*b + y;
iteration ++;
}
/*
* If the point looks as if it will diverge, color it black.
* Otherwise, give it a color from our color palette.
*/
if (iteration == maxIterations)
point = black;
else {
point = colorPalette[iteration];
}
// Set the color of the pixel
img.setRGB(c, r, point);
}
}
// Write the image
try {
ImageIO.write(img, "png", out);
}
catch (Exception e) {
System.out.println("Oops.");
}
}
}
}