Last active
December 11, 2015 20:18
-
-
Save neuro-sys/4654176 to your computer and use it in GitHub Desktop.
Mandelbrot ascii draw.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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
commented
Jan 28, 2013
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment