-
Notifications
You must be signed in to change notification settings - Fork 1
/
GlynnTree.java
139 lines (125 loc) · 3.95 KB
/
GlynnTree.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package io.qiro.logo;
import java.awt.image.BufferedImage;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.imageio.ImageIO;
import org.apache.commons.math3.complex.Complex;
public class GlynnTree {
private static double julia(Complex zz, int maxIteration) {
int i = 0;
Complex z = zz;
while (i < maxIteration && z.abs() < 1.0) {
z = z.pow(1.5).subtract(0.2);
i += 1;
}
return i;
}
private static BufferedImage generate(
double xcenter,
double xrange,
double ycenter,
double yrange,
int maxIteration,
double whiteThreshold,
int size
) {
BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
double xmin = xcenter - xrange / 2;
double xmax = xcenter + xrange / 2;
double ymin = ycenter - yrange / 2;
double ymax = ycenter + yrange / 2;
double[][] buffer = new double[size][size];
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
double x = (double) i / size * xrange + xmin;
double y = ymax - (double) j / size * yrange;
double z = julia(new Complex(-x, y), maxIteration);
buffer[i][j] = z;
min = Math.min(min, z);
max = Math.max(max, z);
}
}
double range = max - min;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
double z = buffer[i][j];
z = (z - min) / range;
if (z < whiteThreshold) {
image.setRGB(size - j - 1, size - i - 1, new Color(255, 255, 255, 0).getRGB());
} else {
z = Math.pow(z, 5);
int rgb = (int) (255 * z);
image.setRGB(size - j - 1, size - i - 1, new Color(rgb, rgb, rgb).getRGB());
}
}
}
return image;
}
public static void show(
double xcenter,
double xrange,
double ycenter,
double yrange,
int maxIteration,
double whiteThreshold,
int size
) throws IOException {
BufferedImage image = generate(
xcenter, xrange,
ycenter, yrange,
maxIteration,
whiteThreshold,
size);
JFrame f = new JFrame("Julia Example") {
@Override
public void paint(java.awt.Graphics g) {
g.drawImage(image, 0, 0, null);
}
};
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(size, size);
f.repaint();
f.setVisible(true);
}
public static void generateImage(
double xcenter,
double xrange,
double ycenter,
double yrange,
int maxIteration,
double whiteThreshold,
int size,
File output
) throws IOException {
BufferedImage image = generate(
xcenter, xrange,
ycenter, yrange,
maxIteration,
whiteThreshold,
size);
ImageIO.write(image, "PNG", output);
}
public static void main(String[] args) throws IOException {
double xcenter = 0.54;
double xrange = 0.35;
double ycenter = 0.0;
double yrange = 0.4;
int maxIteration = 105;
double whiteThreshold = 0.35;
int size = 4096;
// GlynnTree.show(xcenter, xrange, ycenter, yrange, maxIteration, whiteThreshold, size);
File output = new File("qiro-logo_" + size + "x" + size + ".png");
GlynnTree.generateImage(
xcenter, xrange,
ycenter, yrange,
maxIteration,
whiteThreshold,
size,
output
);
}
}