Skip to content

Instantly share code, notes, and snippets.

@DataKinds
Created January 15, 2015 05:55
Show Gist options
  • Save DataKinds/5a3e5e124db80c9ff398 to your computer and use it in GitHub Desktop.
Save DataKinds/5a3e5e124db80c9ff398 to your computer and use it in GitHub Desktop.
hacklebrot
#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
#include <complex>
#include <algorithm>
int main(int argc, std::string argv[]) {
if (argc != 8) {
std::cout << "help: mandelbrot <iterations> <left x> <x step> <right x> <bottom y> <y step> <top y>" << "\n";
std::cout << std::to_string(argc) << "\n";
exit(1);
}
long double xLeft = std::stold(argv[2]);
long double xStep = std::stold(argv[3]);
long double xRight = std::stold(argv[4]);
long double yBottom = std::stold(argv[5]);
long double yStep = std::stold(argv[6]);
long double yTop = std::stold(argv[7]);
long xPixels = (long)std::floor((xRight - xLeft) / xStep);
long yPixels = (long)std::floor((yTop - yBottom) / yStep);
int iterations = std::stoi(argv[1]);
int maxDepth = iterations;
std::vector<int> mandelVector;
long iter;
for (long double imaginary = yTop; imaginary > yBottom; imaginary -= yStep) {
for (long double real = xLeft; real < xRight - (xStep/2); real += xStep) { //FIX THIS xRight - (xStep/2) PLEASE IT'S SO BROKEN IT'S JUST TO COMPENSATE FOR FLOATING POINT ERRORS
std::complex<long double> lastComplex = std::complex<long double>(0.0, 0.0);
std::complex<long double> thisComplex = std::complex<long double>(real, imaginary);
std::complex<long double> coordComplex = thisComplex;
std::complex<long double> tempComplex;
for (iter = 0; iter < iterations; iter++) {
tempComplex = thisComplex;
thisComplex = lastComplex * lastComplex + coordComplex;
lastComplex = tempComplex;
if (std::abs(thisComplex) >= 2) {
break;
}
}
mandelVector.push_back(iter);
}
}
std::string mandelStringHeader = "P2\n" + std::to_string(xPixels) + " " + std::to_string(yPixels) + "\n" + std::to_string(maxDepth) + "\n";
std::string mandelString = "";
for (auto pix : mandelVector) {
mandelString += std::to_string((int)std::floor(pix)) + " ";
}
mandelString = mandelStringHeader + mandelString;
std::ofstream mandelImage;
mandelImage.open("mcplusplus.pgm");
mandelImage << mandelString;
mandelImage.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment