-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelbrotset.cpp
93 lines (68 loc) · 3.46 KB
/
mandelbrotset.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
//============================================================================
// Name : mandelbrotset.cpp
// Author : anematode
// Version : 1.0.0
// Copyright : N/A
// Description : Generation of a Mandelbrot set through iterative application of a function onto the complex numbers.
//============================================================================
#include <iostream>
#include <fstream>
#include <complex>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
/** Customizable variables **/
const int w = 2048; // Width, in pixels, of the output image
const int h = 2048; // Height, in pixels, of the output image
const float re_min = -2.5; // Real value of the left edge of the image
const float re_max = 1.5; // Real value of the right edge of the image
const float im_min = -2; // Imaginary value of the bottom edge of the image
const float im_max = 2; // Imaginary value of the top edge of the image
const int iterations = 255; // Number of iterations required to assume boundedness under iteration
const int escaperadius = 2; // Critical radius determining non-boundedness under iteration
// Filename to be saved to (.pgm format). Please modify for the program to work.
string filename = "/Users/anematode/Desktop/mandelbrot.pgm";
int main() {
clock_t begin = clock(); // Begin clock
const float colordecrement = 250.0/iterations; // Change in pixel darkness after each iteration
const int radiussquared = escaperadius * escaperadius;
ofstream image; // Image output stream
image.open(filename,ios_base::app); // Open output stream
image << "P2\n# Fractal image\n" <<
to_string(w) << ' ' << to_string(h) // Write header data
<< "\n255\n";
float xdelta = (re_max-re_min)/w; // Difference between consecutive pixels in a row
float ydelta = (im_max-im_min)/h; // Difference between consecutive pixels in a column
int row = 0; // Current row number
for (double y = im_min; y < im_max; y += ydelta) { // For all imaginary positions y...
row += 1; // Increment row number
if (row % 100 == 0) { // Status report every 100 rows
cout << "Completed row #" << row << ". "
<< to_string(100*(y-im_min)/(im_max-im_min))
<< "\% done with image." << endl;
}
for (double x = re_min; x < re_max; x += xdelta) { // For all real positions x...
complex<double> z(x,y); // Construct a complex number z = x + yi
complex<double> k;
k = z; // Construct a complex number k = z
float n = 255.0; // Stores grayscale value of current pixel (255 = white, 0 = black)
while (z.real()*z.real() + z.imag()*z.imag() < radiussquared
&& n >= 5) { // While complex number is within the escape radius and insufficient iterations have passed...
/** The following assignment of z is the central iterative function, which continually assigns z = f(z).
* Since the function is f(z) = z*z + k, where k is the value of z before any iteration, this program
* will produce the Mandelbrot set.
*/
z = z*z+k;
n -= colordecrement; // Decrement n by the needed grayscale value
}
image << to_string((int) n) << endl; // Write pixel to file
}
image << '\n'; // End this row
}
clock_t end = clock(); // End clock
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
cout << "Computed " << w*h << " pixels in " << elapsed_secs << " seconds (CPU time)!" << endl;
image.close(); // Close the image
}