-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJuliaSet.java
More file actions
67 lines (50 loc) · 1.76 KB
/
JuliaSet.java
File metadata and controls
67 lines (50 loc) · 1.76 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
/**
* Julia set implementation.
*/
public class JuliaSet {
private static final int WIDTH = 960;
private static final int HEIGHT = 540;
private static final int MAX_ITER = 200;
// Complex constant c (choose different values for different styles)
private static final double C_RE = -0.7;
private static final double C_IM = 0.27015;
private static final double X_MIN = -2;
private static final double X_MAX = 2;
private static final double Y_MIN = -1.2;
private static final double Y_MAX = 1.2;
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 juliaIter(double zr, double zi) {
int count = 0;
while (count < MAX_ITER) {
double zr2 = zr * zr - zi * zi;
double zi2 = 2 * zr * zi;
zr = zr2 + C_RE;
zi = zi2 + C_IM;
if (zr * zr + zi * zi > 4.0)
break;
count++;
}
return count;
}
public static void main(String[] args) {
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 zr = mapX(px);
double zi = mapY(py);
int iter = juliaIter(zr, zi);
if (iter == MAX_ITER) StdDraw.setPenColor(StdDraw.BLACK);
else StdDraw.setPenColor(StdDraw.WHITE);
StdDraw.filledRectangle(px, py, BLOCK / 2.0, BLOCK / 2.0);
}
}
}
}