-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMandelbrotFast.java
More file actions
71 lines (54 loc) · 1.94 KB
/
MandelbrotFast.java
File metadata and controls
71 lines (54 loc) · 1.94 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
/**
* Faster implementation of Mandelbrot set with less detail.
*/
public class MandelbrotFast {
private static final int WIDTH = 960;
private static final int HEIGHT = 540;
private static final int MAX_ITER = 200; // Lower = faster
// Complex plane bounds
private static final double X_MIN = -2.5;
private static final double X_MAX = 1.0;
private static final double Y_MIN = -1.0;
private static final double Y_MAX = 1.0;
// Size of the block to draw instead of 1 pixel, higher = faster
private static final int BLOCK = 4;
private static double mapX(int px) {
return X_MIN + px * (X_MAX - X_MIN) / WIDTH;
}
private static double mapY(int py) {
return Y_MIN + py * (Y_MAX - Y_MIN) / HEIGHT;
}
private static int mandelbrotIterations(double cr, double ci) {
double zr = 0, zi = 0;
int count = 0;
while (count < MAX_ITER) {
double zr2 = zr * zr - zi * zi;
double zi2 = 2 * zr * zi;
zr = zr2 + cr;
zi = zi2 + ci;
if (zr * zr + zi * zi > 4.0)
break;
count++;
}
return count;
}
public static void main(String[] args) {
StdDraw.setTitle("Fast Mandelbrot");
StdDraw.setCanvasSize(WIDTH, HEIGHT);
StdDraw.setXscale(0, WIDTH);
StdDraw.setYscale(0, HEIGHT);
for (int px = 0; px < WIDTH; px += BLOCK) {
for (int py = 0; py < HEIGHT; py += BLOCK) {
double cr = mapX(px);
double ci = mapY(py);
int iter = mandelbrotIterations(cr, ci);
if (iter == MAX_ITER)
StdDraw.setPenColor(StdDraw.BLACK);
else
StdDraw.setPenColor(StdDraw.WHITE);
// Draw a filled block instead of a pixel
StdDraw.filledRectangle(px, py, BLOCK / 2.0, BLOCK / 2.0);
}
}
}
}