-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmandelbrot.cpp
489 lines (449 loc) · 10.1 KB
/
mandelbrot.cpp
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <fstream>
# include <cmath>
# include <ctime>
# include <cstring>
using namespace std;
int main ( );
bool ppma_write ( string file_out_name, int xsize, int ysize, int *r,
int *g, int *b );
bool ppma_write_data ( ofstream &file_out, int xsize, int ysize, int *r,
int *g, int *b );
bool ppma_write_header ( ofstream &file_out, string file_out_name, int xsize,
int ysize, int rgb_max );
void timestamp ( );
//****************************************************************************80
int main ( )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for MANDELBROT.
//
// Discussion:
//
// MANDELBROT computes an image of the Mandelbrot set.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 22 July 2010
//
// Author:
//
// John Burkardt
//
// Local Parameters:
//
// Local, integer COUNT_MAX, the maximum number of iterations taken
// for a particular pixel.
//
{
int *b;
int c;
int c_max;
int *count;
int count_max = 400;
string filename = "mandelbrot.ppm";
int *g;
int i;
bool ierror;
int j;
int k;
int n = 501;
int *r;
double x;
double x_max = 1.25;
double x_min = - 2.25;
double x1;
double x2;
double y;
double y_max = 1.75;
double y_min = - 1.75;
double y1;
double y2;
timestamp ( );
cout << "\n";
cout << "MANDELBROT\n";
cout << " C++ version\n";
cout << "\n";
cout << " Create an ASCII PPM image of the Mandelbrot set.\n";
cout << "\n";
cout << " For each point C = X + i*Y\n";
cout << " with X range [" << x_min << "," << x_max << "]\n";
cout << " and Y range [" << y_min << "," << y_max << "]\n";
cout << " carry out " << count_max << " iterations of the map\n";
cout << " Z(n+1) = Z(n)^2 + C.\n";
cout << " If the iterates stay bounded (norm less than 2)\n";
cout << " then C is taken to be a member of the set.\n";
cout << "\n";
cout << " An ASCII PPM image of the set is created using\n";
cout << " N = " << n << " pixels in the X direction and\n";
cout << " N = " << n << " pixels in the Y direction.\n";
//
// Carry out the iteration for each pixel, determining COUNT.
//
count = new int[n*n];
for ( i = 0; i < n; i++ )
{
for ( j = 0; j < n; j++ )
{
x = ( ( double ) ( j ) * x_max
+ ( double ) ( n - j - 1 ) * x_min )
/ ( double ) ( n - 1 );
y = ( ( double ) ( i ) * y_max
+ ( double ) ( n - i - 1 ) * y_min )
/ ( double ) ( n - 1 );
count[i+j*n] = 0;
x1 = x;
y1 = y;
for ( k = 1; k <= count_max; k++ )
{
x2 = x1 * x1 - y1 * y1 + x;
y2 = 2 * x1 * y1 + y;
if ( x2 < -2.0 || 2.0 < x2 || y2 < -2.0 || 2.0 < y2 )
{
count[i+j*n] = k;
break;
}
x1 = x2;
y1 = y2;
}
}
}
//
// Determine the coloring of each pixel.
//
c_max = 0;
for ( j = 0; j < n; j++ )
{
for ( i = 0; i < n; i++ )
{
if ( c_max < count[i+j*n] )
{
c_max = count[i+j*n];
}
}
}
//
// Set the image data.
//
r = new int[n*n];
g = new int[n*n];
b = new int[n*n];
for ( i = 0; i < n; i++ )
{
for ( j = 0; j < n; j++ )
{
if ( count[i+j*n] % 2 == 1 )
{
r[i+j*n] = 255;
g[i+j*n] = 255;
b[i+j*n] = 255;
}
else
{
c = ( int ) ( 255.0 * sqrt ( sqrt ( sqrt (
( ( double ) ( count[i+j*n] ) / ( double ) ( c_max ) ) ) ) ) );
r[i+j*n] = 3 * c / 5;
g[i+j*n] = 3 * c / 5;
b[i+j*n] = c;
}
}
}
//
// Write an image file.
//
ierror = ppma_write ( filename, n, n, r, g, b );
cout << "\n";
cout << " ASCII PPM image data stored in \"" << filename << "\".\n";
delete [] b;
delete [] count;
delete [] g;
delete [] r;
//
// Terminate.
//
cout << "\n";
cout << "MANDELBROT\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
return 0;
}
//****************************************************************************80
bool ppma_write ( string file_out_name, int xsize, int ysize, int *r,
int *g, int *b )
//****************************************************************************80
//
// Purpose:
//
// PPMA_WRITE writes the header and data for an ASCII portable pixel map file.
//
// Example:
//
// P3
// # feep.ppm
// 4 4
// 15
// 0 0 0 0 0 0 0 0 0 15 0 15
// 0 0 0 0 15 7 0 0 0 0 0 0
// 0 0 0 0 0 0 0 15 7 0 0 0
// 15 0 15 0 0 0 0 0 0 0 0 0
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 28 February 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, string FILE_OUT_NAME, the name of the file to contain the ASCII
// portable pixel map data.
//
// Input, int XSIZE, YSIZE, the number of rows and columns of data.
//
// Input, int *R, *G, *B, the arrays of XSIZE by YSIZE data values.
//
// Output, bool PPMA_WRITE, is
// true, if an error was detected, or
// false, if the file was written.
//
{
int *b_index;
bool error;
ofstream file_out;
int *g_index;
int i;
int j;
int *r_index;
int rgb_max;
//
// Open the output file.
//
file_out.open ( file_out_name.c_str ( ) );
if ( !file_out )
{
cout << "\n";
cout << "PPMA_WRITE - Fatal error!\n";
cout << " Cannot open the output file \"" << file_out_name << "\".\n";
return true;
}
//
// Compute the maximum.
//
rgb_max = 0;
r_index = r;
g_index = g;
b_index = b;
for ( j = 0; j < ysize; j++ )
{
for ( i = 0; i < xsize; i++ )
{
if ( rgb_max < *r_index )
{
rgb_max = *r_index;
}
r_index = r_index + 1;
if ( rgb_max < *g_index )
{
rgb_max = *g_index;
}
g_index = g_index + 1;
if ( rgb_max < *b_index )
{
rgb_max = *b_index;
}
b_index = b_index + 1;
}
}
//
// Write the header.
//
error = ppma_write_header ( file_out, file_out_name, xsize, ysize, rgb_max );
if ( error )
{
cout << "\n";
cout << "PPMA_WRITE - Fatal error!\n";
cout << " PPMA_WRITE_HEADER failed.\n";
return true;
}
//
// Write the data.
//
error = ppma_write_data ( file_out, xsize, ysize, r, g, b );
if ( error )
{
cout << "\n";
cout << "PPMA_WRITE - Fatal error!\n";
cout << " PPMA_WRITE_DATA failed.\n";
return true;
}
//
// Close the file.
//
file_out.close ( );
return false;
}
//****************************************************************************80
bool ppma_write_data ( ofstream &file_out, int xsize, int ysize, int *r,
int *g, int *b )
//****************************************************************************80
//
// Purpose:
//
// PPMA_WRITE_DATA writes the data for an ASCII portable pixel map file.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 28 February 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, ofstream &FILE_OUT, a pointer to the file to contain the ASCII
// portable pixel map data.
//
// Input, int XSIZE, YSIZE, the number of rows and columns of data.
//
// Input, int *R, *G, *B, the arrays of XSIZE by YSIZE data values.
//
// Output, bool PPMA_WRITE_DATA, is
// true, if an error was detected, or
// false, if the data was written.
//
{
int *b_index;
int *g_index;
int i;
int j;
int *r_index;
int rgb_num;
r_index = r;
g_index = g;
b_index = b;
rgb_num = 0;
for ( j = 0; j < ysize; j++ )
{
for ( i = 0; i < xsize; i++ )
{
file_out << *r_index << " " << *g_index << " " << *b_index;
rgb_num = rgb_num + 3;
r_index = r_index + 1;
g_index = g_index + 1;
b_index = b_index + 1;
if ( rgb_num % 12 == 0 || i == xsize - 1 || rgb_num == 3 * xsize * ysize )
{
file_out << "\n";
}
else
{
file_out << " ";
}
}
}
return false;
}
//****************************************************************************80
bool ppma_write_header ( ofstream &file_out, string file_out_name, int xsize,
int ysize, int rgb_max )
//****************************************************************************80
//
// Purpose:
//
// PPMA_WRITE_HEADER writes the header of an ASCII portable pixel map file.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 28 February 2003
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, ofstream &FILE_OUT, a pointer to the file to contain the ASCII
// portable pixel map data.
//
// Input, string FILE_OUT_NAME, the name of the file.
//
// Input, int XSIZE, YSIZE, the number of rows and columns of data.
//
// Input, int RGB_MAX, the maximum RGB value.
//
// Output, bool PPMA_WRITE_HEADER, is
// true, if an error was detected, or
// false, if the header was written.
//
{
file_out << "P3\n";
file_out << "# " << file_out_name << " created by PPMA_WRITE.C.\n";
file_out << xsize << " " << ysize << "\n";
file_out << rgb_max << "\n";
return false;
}
//****************************************************************************80
void timestamp ( )
//****************************************************************************80
//
// Purpose:
//
// TIMESTAMP prints the current YMDHMS date as a time stamp.
//
// Example:
//
// 31 May 2001 09:45:54 AM
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 08 July 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// None
//
{
# define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
const struct std::tm *tm_ptr;
size_t len;
std::time_t now;
now = std::time ( NULL );
tm_ptr = std::localtime ( &now );
len = std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr );
std::cout << time_buffer << "\n";
return;
# undef TIME_SIZE
}