Skip to content

Instantly share code, notes, and snippets.

@neuro-sys
Last active December 11, 2015 20:18
Show Gist options
  • Select an option

  • Save neuro-sys/4654176 to your computer and use it in GitHub Desktop.

Select an option

Save neuro-sys/4654176 to your computer and use it in GitHub Desktop.
Mandelbrot ascii draw.
#include <stdio.h>
#include <complex.h>
/**
The Mandelbrot set is the set of values of c in the complex
plane for which the orbit of 0 under iteration of the complex quadratic polynomial
zn+1 = zn2 + c remains bounded. That is, a complex number c is part of the
Mandelbrot set if, when starting with z0 = 0 and applying the iteration repeatedly,
the absolute value of zn remains bounded however large n gets.
*/
void mandelbrot_draw(const unsigned int width, const unsigned int height)
{
#define MAX_ITER 50
unsigned int x, y;
const char *mandelbrot = " -o8";
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
double x_t = ((double) x / width * 3.5) - 2.5; /* [-2.5, 1] */
double y_t = ((double) y / height * 2.) - 1.; /* [ -1, 1] */
double _Complex z = 0. + 0.*_Complex_I;
double _Complex c = x_t + y_t*_Complex_I;
unsigned int n = 0;
while ( (cabs(z) < 2.0) && n++ != MAX_ITER ) {
z = z * z + c;
}
n = ((double) n / MAX_ITER) * 3; /* [0, 9] */
putchar(mandelbrot[n]);
}
printf("\n");
}
#undef MAX_ITER
}
int main(int argc, char *argv[])
{
mandelbrot_draw(70, 22);
return 0;
}
@neuro-sys

Copy link
Copy Markdown
Author
                                         .....@
                                        ...Oo...
                                      ...o@@@OO..
                                  .......o@@@@*......
                                ..*O@.o@@@@@@@@@OO...*.
                              .....@@@@@@@@@@@@@@@@@@o.
                     ...    ....*@o@@@@@@@@@@@@@@@@@@...
                   ..*..........@@@@@@@@@@@@@@@@@@@@@@O*
                   ...*@oo@o@..*@@@@@@@@@@@@@@@@@@@@@@*.
                  ...*O@@@@@@@@o@@@@@@@@@@@@@@@@@@@@@@*.
            ......***@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@..
       @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*...
            ......***@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@..
                  ...*O@@@@@@@@o@@@@@@@@@@@@@@@@@@@@@@*.
                   ...*@oo@o@..*@@@@@@@@@@@@@@@@@@@@@@*.
                   ..*..........@@@@@@@@@@@@@@@@@@@@@@O*
                     ...    ....*@o@@@@@@@@@@@@@@@@@@...
                              .....@@@@@@@@@@@@@@@@@@o.
                                ..*O@.o@@@@@@@@@OO...*.
                                  .......o@@@@*......
                                      ...o@@@OO..
                                        ...Oo...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment