- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathMandelbrot.java
188 lines (165 loc) · 7.74 KB
/
Mandelbrot.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
packagecom.thealgorithms.others;
importjava.awt.Color;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjavax.imageio.ImageIO;
/**
* The Mandelbrot set is the set of complex numbers "c" for which the series
* "z_(n+1) = z_n * z_n + c" does not diverge, i.e. remains bounded. Thus, a
* complex number "c" is a member of the Mandelbrot set if, when starting with
* "z_0 = 0" and applying the iteration repeatedly, the absolute value of "z_n"
* remains bounded for all "n > 0". Complex numbers can be written as "a + b*i":
* "a" is the real component, usually drawn on the x-axis, and "b*i" is the
* imaginary component, usually drawn on the y-axis. Most visualizations of the
* Mandelbrot set use a color-coding to indicate after how many steps in the
* series the numbers outside the set cross the divergence threshold. Images of
* the Mandelbrot set exhibit an elaborate and infinitely complicated boundary
* that reveals progressively ever-finer recursive detail at increasing
* magnifications, making the boundary of the Mandelbrot set a fractal curve.
* (description adapted from https://en.wikipedia.org/wiki/Mandelbrot_set ) (see
* also https://en.wikipedia.org/wiki/Plotting_algorithms_for_the_Mandelbrot_set
* )
*/
publicfinalclassMandelbrot {
privateMandelbrot() {
}
publicstaticvoidmain(String[] args) {
// Test black and white
BufferedImageblackAndWhiteImage = getImage(800, 600, -0.6, 0, 3.2, 50, false);
// Pixel outside the Mandelbrot set should be white.
assertblackAndWhiteImage.getRGB(0, 0) == newColor(255, 255, 255).getRGB();
// Pixel inside the Mandelbrot set should be black.
assertblackAndWhiteImage.getRGB(400, 300) == newColor(0, 0, 0).getRGB();
// Test color-coding
BufferedImagecoloredImage = getImage(800, 600, -0.6, 0, 3.2, 50, true);
// Pixel distant to the Mandelbrot set should be red.
assertcoloredImage.getRGB(0, 0) == newColor(255, 0, 0).getRGB();
// Pixel inside the Mandelbrot set should be black.
assertcoloredImage.getRGB(400, 300) == newColor(0, 0, 0).getRGB();
// Save image
try {
ImageIO.write(coloredImage, "png", newFile("Mandelbrot.png"));
} catch (IOExceptione) {
e.printStackTrace();
}
}
/**
* Method to generate the image of the Mandelbrot set. Two types of
* coordinates are used: image-coordinates that refer to the pixels and
* figure-coordinates that refer to the complex numbers inside and outside
* the Mandelbrot set. The figure-coordinates in the arguments of this
* method determine which section of the Mandelbrot set is viewed. The main
* area of the Mandelbrot set is roughly between "-1.5 < x < 0.5" and "-1 <
* y < 1" in the figure-coordinates.
*
* @param imageWidth The width of the rendered image.
* @param imageHeight The height of the rendered image.
* @param figureCenterX The x-coordinate of the center of the figure.
* @param figureCenterY The y-coordinate of the center of the figure.
* @param figureWidth The width of the figure.
* @param maxStep Maximum number of steps to check for divergent behavior.
* @param useDistanceColorCoding Render in color or black and white.
* @return The image of the rendered Mandelbrot set.
*/
publicstaticBufferedImagegetImage(intimageWidth, intimageHeight, doublefigureCenterX, doublefigureCenterY, doublefigureWidth, intmaxStep, booleanuseDistanceColorCoding) {
if (imageWidth <= 0) {
thrownewIllegalArgumentException("imageWidth should be greater than zero");
}
if (imageHeight <= 0) {
thrownewIllegalArgumentException("imageHeight should be greater than zero");
}
if (maxStep <= 0) {
thrownewIllegalArgumentException("maxStep should be greater than zero");
}
BufferedImageimage = newBufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
doublefigureHeight = figureWidth / imageWidth * imageHeight;
// loop through the image-coordinates
for (intimageX = 0; imageX < imageWidth; imageX++) {
for (intimageY = 0; imageY < imageHeight; imageY++) {
// determine the figure-coordinates based on the image-coordinates
doublefigureX = figureCenterX + ((double) imageX / imageWidth - 0.5) * figureWidth;
doublefigureY = figureCenterY + ((double) imageY / imageHeight - 0.5) * figureHeight;
doubledistance = getDistance(figureX, figureY, maxStep);
// color the corresponding pixel based on the selected coloring-function
image.setRGB(imageX, imageY, useDistanceColorCoding ? colorCodedColorMap(distance).getRGB() : blackAndWhiteColorMap(distance).getRGB());
}
}
returnimage;
}
/**
* Black and white color-coding that ignores the relative distance. The
* Mandelbrot set is black, everything else is white.
*
* @param distance Distance until divergence threshold
* @return The color corresponding to the distance.
*/
privatestaticColorblackAndWhiteColorMap(doubledistance) {
returndistance >= 1 ? newColor(0, 0, 0) : newColor(255, 255, 255);
}
/**
* Color-coding taking the relative distance into account. The Mandelbrot
* set is black.
*
* @param distance Distance until divergence threshold.
* @return The color corresponding to the distance.
*/
privatestaticColorcolorCodedColorMap(doubledistance) {
if (distance >= 1) {
returnnewColor(0, 0, 0);
} else {
// simplified transformation of HSV to RGB
// distance determines hue
doublehue = 360 * distance;
doublesaturation = 1;
doubleval = 255;
inthi = (int) (Math.floor(hue / 60)) % 6;
doublef = hue / 60 - Math.floor(hue / 60);
intv = (int) val;
intp = 0;
intq = (int) (val * (1 - f * saturation));
intt = (int) (val * (1 - (1 - f) * saturation));
switch (hi) {
case0:
returnnewColor(v, t, p);
case1:
returnnewColor(q, v, p);
case2:
returnnewColor(p, v, t);
case3:
returnnewColor(p, q, v);
case4:
returnnewColor(t, p, v);
default:
returnnewColor(v, p, q);
}
}
}
/**
* Return the relative distance (ratio of steps taken to maxStep) after
* which the complex number constituted by this x-y-pair diverges. Members
* of the Mandelbrot set do not diverge so their distance is 1.
*
* @param figureX The x-coordinate within the figure.
* @param figureX The y-coordinate within the figure.
* @param maxStep Maximum number of steps to check for divergent behavior.
* @return The relative distance as the ratio of steps taken to maxStep.
*/
privatestaticdoublegetDistance(doublefigureX, doublefigureY, intmaxStep) {
doublea = figureX;
doubleb = figureY;
intcurrentStep = 0;
for (intstep = 0; step < maxStep; step++) {
currentStep = step;
doubleaNew = a * a - b * b + figureX;
b = 2 * a * b + figureY;
a = aNew;
// divergence happens for all complex number with an absolute value
// greater than 4 (= divergence threshold)
if (a * a + b * b > 4) {
break;
}
}
return (double) currentStep / (maxStep - 1);
}
}