-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelbrot.pde
397 lines (341 loc) · 9.78 KB
/
mandelbrot.pde
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
//
// mandelbrot.pde
// author: Zack Fleischman
//
// This dumps the mandelbrot set visualization to the window.
//
import java.util.Stack;
import java.awt.Color;
/////////////////////
// Parameters
// Color mode
boolean smoothColors = true;
// Number of images to write
boolean writeImagesMode = false;
int maxImages = 1050;
// Image Size
int imageWidth = 640;
int imageHeight = 480;
//int imageWidth = 1080;
//int imageHeight = 720;
// Iterations to escape
int currentMaxIter = 2000;
int maxIter = 50;
// Which window we are currently using.
MandelbrotWindow window = getCanonicalMandelbrotWindow();
// Zooming Params
double zoomSpeed = 0.1;
double currentZoom = 1.0;
double zoomFactor = 1.03;
// Interesting points to zoom in on
double[][] zoomPoints = new double[][]{
{-1.164129365976551, 0.22585500062515362},
{-1.1351871105851659, 0.2679162115997563},
{-1.238279742917958, -0.11372601742547422},
{-0.7259921381684972, 0.24004692460561433},
{-0.9895494202241311, 0.27757489483067976},
{ -0.75, 0.1 }, // Seahorse Valley
{ 0.275, 0.0 } // Elephant Valley
};
// Which point index we are zooming in on
int currentZoomPointIdx = 0;
double xCenterOffset = 0.0;
double yCenterOffset = 0.0;
double offsetChangeSpeed = 0.05;
float a,b,c,d = 0.0;
color[] colorTable;
// Save current frame when drawing rectangles
PImage currImage; // Source image
//
// Program starts here.
//
void setup() {
// Build Color Table
colorTable = new color[currentMaxIter];
for (int i=0; i<colorTable.length; ++i) {
colorTable[i] = getRandomColor();
}
// Set the size of the canvas to the image size
size(imageWidth, imageHeight);
currImage = new PImage(width, height);
// Setup the initial window
updateWindow();
// Only draw once.
if (!writeImagesMode) {
window = getCanonicalMandelbrotWindow();
noLoop();
}
}
//
// Called after setup() finishes.
//
int frameNum = 1;
void draw() {
long dt = getTimeDelta();
if (frameNum % 20 == 0) {
System.out.println("FPS: " + (1.0 / nanoToSeconds(dt)));
}
if (!dontUpdateMandelbrot) {
background(0);
// Draw pixels
loadPixels();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
color c = getMandelbrotColorForWindow((double)x/width, (double)y/height, window);
setPixel(x, y, c);
}
}
// Save the current image to a buffer
currImage.loadPixels();
currImage.pixels = pixels;
currImage.updatePixels();
updatePixels();
} else {
image(currImage,0,0);
}
// Draw rectangle from mouse
stroke(127, 0, 0);
strokeWeight(3);
noFill();
rect(a, b, c, d);
// Update parameter states
updateParameters();
if (writeImagesMode) {
// Write image
String fileName = "frames/mandelbrot_" + frameNum + ".jpg";
System.out.println(fileName);
save(fileName);
frameNum++;
if (frameNum == 2) {
for (int i=0; i<60; ++i) {
fileName = "frames/mandelbrot_" + frameNum + ".jpg";
System.out.println(fileName);
save(fileName);
frameNum++;
}
}
if (frameNum > maxImages) {
noLoop();
}
}
// Reset this flag
dontUpdateMandelbrot = false;
}
void mousePressed() {
a=mouseX;
b=mouseY;
}
void mouseReleased(){
c=mouseX-a;
d=c*(height/(float)width);
updateWindowWithRect();
a=0.0;
b=0.0;
c=0.0;
d=0.0;
redraw();
}
Stack<MandelbrotWindow> oldWindows = new Stack<MandelbrotWindow>();
void updateWindowWithRect() {
double newMinX = window.minX + ((window.maxX - window.minX) * (a / (double)width));
double newMaxX = window.minX + ((window.maxX - window.minX) * ((a+c) / (double)width));
double newMinY = window.minY + ((window.maxY - window.minY) * (b / (double)height));
double newMaxY = window.minY + ((window.maxY - window.minY) * ((b+d) / (double)height));
oldWindows.push(window);
window = new MandelbrotWindow(newMinX, newMaxX, newMinY, newMaxY);
System.out.println("Window Center: (" + ((newMaxX+newMinX)/2.0) + ", " + ((newMaxY+newMinY)/2.0) + ")");
}
boolean dontUpdateMandelbrot = false;
void mouseDragged() {
c=mouseX-a;
d=c*(height/(float)width);
dontUpdateMandelbrot = true;
redraw();
}
void keyPressed() {
updateCenterOffset();
if (key == ' ') {
currentZoom += zoomSpeed;
zoomSpeed *= zoomFactor;
redraw();
}
if (key == 'u') {
currentMaxIter += 100;
redraw();
}
if (key == 'y') {
currentMaxIter -= 100;
redraw();
}
if (key == 'q') {
System.exit(0);
}
if (key == 'a') {
if (oldWindows.size() > 0) {
window = oldWindows.pop();
}
redraw();
}
if (key == 'b') {
zoomSpeed /= zoomFactor;
currentZoom -= zoomSpeed;
redraw();
}
}
///////////////// Classes /////////////////////
public class MandelbrotWindow {
public double minX, maxX;
public double minY, maxY;
public MandelbrotWindow() {
minX = -2.5;
maxX = 1.0;
minY = -1.0;
maxY = 1.0;
}
public MandelbrotWindow(double minX, double maxX, double minY, double maxY) {
this.minX = minX;
this.maxX = maxX;
this.minY = minY;
this.maxY = maxY;
}
}
/////////////////// Helpers ///////////////////
// Conversion to seconds from nano
double nanoToSeconds(long nano) {
return nano / 1000000000.0;
}
// Get the time elapsed since the last time this was called
long lastTime = 0L;
long getTimeDelta() {
long currentTime = System.nanoTime();
long dt = currentTime - lastTime;
if (lastTime == 0L) {
dt = 0L;
}
lastTime = currentTime;
return dt;
}
// Adjust the offset of the center.
void updateCenterOffset() {
switch (key) {
case 'j':
yCenterOffset += offsetChangeSpeed;
redraw();
dumpCenter();
break;
case 'k':
yCenterOffset -= offsetChangeSpeed;
redraw();
dumpCenter();
break;
case 'h':
xCenterOffset -= offsetChangeSpeed;
redraw();
dumpCenter();
break;
case 'l':
xCenterOffset += offsetChangeSpeed;
redraw();
dumpCenter();
break;
case '0':
offsetChangeSpeed *= 1.5;
System.out.println("Offset Change Speed: " + offsetChangeSpeed);
break;
case '9':
offsetChangeSpeed /= 1.5;
System.out.println("Offset Change Speed: " + offsetChangeSpeed);
break;
}
}
// Print the current center point to the screen
void dumpCenter() {
double cX = zoomPoints[currentZoomPointIdx][0];
double cY = zoomPoints[currentZoomPointIdx][1];
System.out.println("Center Point: (" + (cX + xCenterOffset) + ", " + (cY + yCenterOffset) + ")");
}
// Set Pixel Color
void setPixel(int x, int y, color c) {
// Use the formula to find the 1D location
int loc = x + y * width;
pixels[loc] = c;
}
// Update the program parameters.
void updateParameters() {
if (writeImagesMode) {
// Window
updateWindow();
// Iterations
//currentMaxIter++;
//currentMaxIter %= maxIter;
//currentMaxIter+=5;
//currentMaxIter = 1000;
// Zooming
currentZoom += zoomSpeed;
zoomSpeed *= zoomFactor;
//if (currentZoom > maxZoom) {
// currentZoom = 1.0;
//}
}
}
// Update the window to account for zoom and positioning
void updateWindow() {
double cX = zoomPoints[currentZoomPointIdx][0];
double cY = zoomPoints[currentZoomPointIdx][1];
cX += xCenterOffset;
cY += yCenterOffset;
window = getMandelbrotWindow(cX, cY, currentZoom);
}
// Get canonical Mandelbrot Set Window
MandelbrotWindow getCanonicalMandelbrotWindow() {
return getMandelbrotWindow(-2.5 + (3.5/2.0), 0.0, 1.0);
}
// Get a mandelbrot set window from a center point and a zoom value
MandelbrotWindow getMandelbrotWindow(double cX, double cY, double zoom) {
final double defaultWidth = 3.5;
final double defaultHeight = 2.0;
double windowWidth = defaultWidth / zoom;
double windowHeight = defaultHeight / zoom;
double minX = cX - (windowWidth/2.0);
double maxX = cX + (windowWidth/2.0);
double minY = cY - (windowHeight/2.0);
double maxY = cY + (windowHeight/2.0);
return new MandelbrotWindow(minX, maxX, minY, maxY);
}
// Get Mandelbrot color for image window
color getMandelbrotColorForWindow(double x, double y, MandelbrotWindow w) {
double x0 = w.minX + (x * (w.maxX - w.minX));
double y0 = w.minY + (y * (w.maxY - w.minY));
return getMandelbrotColor(x0, y0);
}
// Get Mandelbrot color for C(x0, y0)
color getMandelbrotColor(double x0, double y0) {
double x = 0.0;
double y = 0.0;
int iteration = 0;
double xTemp = 0.0;
while (x*x + y*y < 4 && iteration < currentMaxIter)
{
xTemp = x*x - y*y + x0;
y = 2*x*y + y0;
x = xTemp;
iteration++;
}
return getColor(iteration);
}
// Return a color for the iteration.
color getColor(int iteration) {
color c = color(0);
if (iteration < currentMaxIter) {
if (smoothColors) {
Color javaColor = Color.getHSBColor((float) iteration / (float) currentMaxIter, 0.85f, 1.0f);
c = color(javaColor.getRed(), javaColor.getGreen(), javaColor.getBlue());
} else {
c = colorTable[iteration % colorTable.length];
}
}
return c;
}
color getRandomColor() {
return color(random(255), random(255), random(255));
}