Skip to content

Instantly share code, notes, and snippets.

@cosinekitty
Created February 25, 2020 01:29
Show Gist options
  • Save cosinekitty/98f9e971aee54acd5d5eac86b2ae6f73 to your computer and use it in GitHub Desktop.
Save cosinekitty/98f9e971aee54acd5d5eac86b2ae6f73 to your computer and use it in GitHub Desktop.
C++ function for generating a series of Mandelbrot zoom PNG files.
static int GenerateZoomFrames(const char *outdir, int numframes, double xcenter, double ycenter, double zoom)
{
try
{
// Create a video frame buffer with 720p resolution (1280x720).
const int width = 1280;
const int height = 720;
VideoFrame frame(width, height);
const int limit = 16000;
double multiplier = pow(zoom, 1.0 / (numframes - 1.0));
double denom = 1.0;
for (int f = 0; f < numframes; ++f)
{
// Calculate the real and imaginary range of values for each frame.
// The zoom is exponential.
// On the first frame, the scale is such that the smaller dimension (height) spans 4 units
// from the bottom of the frame to the top.
// On the last frame, the scale is that number of units divided by 'zoom'.
double ver_span = 4.0 / denom;
double hor_span = ver_span * (width - 1.0) / (height - 1.0);
double ci_top = ycenter + ver_span/2.0;
double ci_delta = ver_span / (height - 1.0);
double cr_left = xcenter - hor_span/2.0;
double cr_delta = hor_span / (width - 1.0);
for (int x=0; x < width; ++x)
{
double cr = cr_left + x*cr_delta;
for (int y=0; y < height; ++y)
{
double ci = ci_top - y*ci_delta;
int count = Mandelbrot(cr, ci, limit);
PixelColor color = Palette(count, limit);
frame.SetPixel(x, y, color);
}
}
// Create the output PNG filename in the format "outdir/frame_12345.png".
char number[20];
snprintf(number, sizeof(number), "%05d", f);
std::string filename = std::string(outdir) + "/frame_" + number + ".png";
// Save the video frame as a PNG file.
int error = frame.SavePng(filename.c_str());
if (error)
return error;
printf("Wrote %s\n", filename.c_str());
// Increase the zoom magnification for the next frame.
denom *= multiplier;
}
return 0;
}
catch (const char *message)
{
fprintf(stderr, "EXCEPTION: %s\n", message);
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment