-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderer.java
More file actions
135 lines (117 loc) · 4.19 KB
/
Renderer.java
File metadata and controls
135 lines (117 loc) · 4.19 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package mandeljava;
import java.awt.image.BufferedImage;
import java.util.concurrent.ArrayBlockingQueue;
public class Renderer implements Runnable{
private int id;
private Ranges currentRanges;
private Factors currentFactors;
private int totalIterations, width, height, totalColors;
private int[] colorTable;
private BufferedImage QI;
private ArrayBlockingQueue<ProgressMessage> q;
private boolean isJulia;
private Complex juliaCenter;
Renderer(int id, Ranges r, int w, int h, int ti, int tc, int[] ct, BufferedImage qi, ArrayBlockingQueue<ProgressMessage> q, boolean j, Complex jc){
this.setValues(id, r, w, h, ti, tc, ct, qi, q, j, jc);
}
private void setValues(int id, Ranges r, int w, int h, int ti, int tc, int[] ct, BufferedImage qi, ArrayBlockingQueue<ProgressMessage> q, boolean j, Complex jc){
this.id = id;
this.currentRanges = r;
this.width = w;
this.height = h;
this.totalIterations = ti;
this.totalColors = tc;
this.colorTable = ct;
this.QI = qi;
this.q = q;
this.isJulia = j;
this.juliaCenter = jc;
}
public void renderFrame() {
currentFactors = calculateFactors(currentRanges, width, height);
long startTime = System.currentTimeMillis();
for (int i = 0; i < width; ++i) {
for (int j = 0; j < height; ++j) {
Complex iteratee = Main.translatePixelToComplex(i, j, currentFactors);
boolean outOfBounds = false;
Complex z = new Complex(0.0d, 0.0d);
int completedIterations = 0;
for (int k = 0; k < totalIterations; k++) {
Complex result = iterate(z, iteratee);
completedIterations++;
if (result.abs() > 2.0d) {
outOfBounds = true;
break;
}
z = result;
}
if (outOfBounds) {
QI.setRGB(i, j, colorTable[(completedIterations - 1) % totalColors]);
} else {
QI.setRGB(i, j, 0x000000);
}
}
try{
this.q.add(new ProgressMessage(ProgressMessage.TYPE_PROGRESS, id, null, i));
}catch(Exception e){
}
}
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
System.out.println("Rendering took " + elapsedTime);
this.q.add(new ProgressMessage(ProgressMessage.TYPE_DONE, id, QI, width));
}
public void renderJulia() {
currentFactors = calculateFactors(currentRanges, width, height);
long startTime = System.currentTimeMillis();
for (int i = 0; i < width; ++i) {
for (int j = 0; j < height; ++j) {
Complex z = Main.translatePixelToComplex(i, j, currentFactors);
boolean outOfBounds = false;
Complex c = juliaCenter;
int completedIterations = 0;
for (int k = 0; k < totalIterations; k++) {
Complex result = iterate(z, c);
completedIterations++;
if (result.abs() > 2.0d) {
outOfBounds = true;
break;
}
z = result;
}
if (outOfBounds) {
QI.setRGB(i, j, colorTable[(completedIterations - 1) % totalColors]);
} else {
QI.setRGB(i, j, 0x000000);
}
}
try{
this.q.add(new ProgressMessage(ProgressMessage.TYPE_PROGRESS, id, null, i));
}catch(Exception e){
}
}
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
System.out.println("Rendering took " + elapsedTime);
this.q.add(new ProgressMessage(ProgressMessage.TYPE_DONE, id, QI, width));
}
private static Complex iterate(Complex z, Complex c) {
return z.times(z).plus(c);
}
private static Factors calculateFactors(Ranges ranges, int width, int height) {
Factors factors = new Factors();
factors.xFactor = width / (ranges.maxRangeX - ranges.minRangeX);
factors.xScale = ((factors.xFactor * (ranges.maxRangeX + ranges.minRangeX)) - width) / 2;
factors.yFactor = (-1 * height) / (ranges.maxRangeY - ranges.minRangeY);
factors.yScale = ((factors.yFactor * (ranges.maxRangeY + ranges.minRangeY)) - height) / 2;
return factors;
}
@Override
public void run() {
if(this.isJulia){
this.renderJulia();
}else{
this.renderFrame();
}
}
}