#include <generators/mandelbrotGen.hh> #include <complex> using std::complex; using std::clamp; namespace ty::gen { mandelbrotGen::mandelbrotGen(const int& w, const int& h) : fracGen(w, h) { } mandelbrotGen::~mandelbrotGen() { } void mandelbrotGen::runGen(vector<unsigned char>& image) { maxIter = clamp(maxIter, iterMinLimit, iterMaxLimit); auto [xRange, yRange] = scaleRange(); double xMin = realCenter - xRange; double xMax = realCenter + xRange; double yMin = imagCenter - yRange; double yMax = imagCenter + yRange; double scaleX = (xMax - xMin) / width; double scaleY = (yMax - yMin) / height; for (int py = 0; py < height; ++py) { #pragma omp parallel for schedule(dynamic, 16) for (int px = 0; px < width; ++px) { double real = xMin + static_cast<double>(px) * scaleX; double imag = yMin + static_cast<double>(py) * scaleY; complex<double> c(real, imag); complex<double> z(0.0, 0.0); int iteration = 0; for (; abs(z) <= 2.0 && iteration < maxIter; ++iteration) { z = z * z + c; } unsigned char r = 0, g = 0, b = 0; if (iteration < maxIter) { double t = static_cast<double>(iteration) / maxIter; r = static_cast<unsigned char>(9 * (1 - t) * t * t * t * 255); g = static_cast<unsigned char>(15 * (1 - t) * (1 - t) * t * t * 255); b = static_cast<unsigned char>(8.5 * (1 - t) * (1 - t) * (1 - t) * t * 255); } int idx = (py * width + px) * 4; image[idx] = r; image[idx + 1] = g; image[idx + 2] = b; image[idx + 3] = 255; } } } } // ty::gen