Skip to content

Instantly share code, notes, and snippets.

@choas
Created June 15, 2020 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save choas/2f4876f5392980ef742a46dde3ba9901 to your computer and use it in GitHub Desktop.
Save choas/2f4876f5392980ef742a46dde3ba9901 to your computer and use it in GitHub Desktop.
Mandelbrot Set calculation with complex.h
#include <stdio.h>
#include <complex.h>
int main(void)
{
double xl = -2.0;
double xu = 0.5;
double yl = -1.1;
double yu = 1.1;
int reps = 20;
int width = 40;
int height = 25;
double xinc = (xu - xl) / width;
double yinc = (yu - yl) / height;
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width; i++)
{
double nreal = xl + i * xinc;
double nimg = yl + j * yinc;
double complex c = nreal + nimg * I;
double complex z = 0 + 0 * I;
char out = ' ';
for (int k = 1; k <= reps; k++)
{
z = cpow(z, 2) + c;
if (cabs(z) > 2.0)
{
out = 'A' + (k / 2);
break;
}
}
printf("%c", out);
}
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment