#include #include #include int escape_iterations(double re, double im, int max_iterations) { double xre = 0.0, xim = 0.0, rtemp, xre2 = 0.0, xim2 = 0.0; int i = max_iterations; do { --i; rtemp = xre2 - xim2 + re; xim = 2*xre*xim + im; xre = rtemp; xre2 = rtemp*rtemp; xim2 = xim*xim; } while ( (xre2 + xim2 < 4.0f) && i); return max_iterations-i; } void mandelbrot(std::ostream& os, int w, int h, double startre, double startim, double endre, double endim, int max_iterations) { int r, c, iter; double zre, zim; double stepre = (endre-startre)/w, stepim = (endim-startim) / h; int x, y = h; os << "P5 " << w << " " << h << " " << max_iterations << "\n"; for (zim = startim; y; --y) { x = w; for (zre = startre; x; x--) { os << (unsigned char)escape_iterations(zre, zim, max_iterations); zre += stepre; } zim += stepim; } } int main(int argc, char const *argv[]) { std::ofstream ofs("test3.pgm", std::ios::out | std::ios::binary); auto start = std::chrono::steady_clock::now(); mandelbrot(ofs, 10000, 10000, -2.0, -2.0, 2.0, 2.0, 255); auto end = std::chrono::steady_clock::now(); std::cout << "Took " << std::chrono::duration_cast(end - start).count() << "ms" << std::endl; return 0; }