Skip to content

Instantly share code, notes, and snippets.

@neuro-sys
Created February 20, 2014 10:20
Show Gist options
  • Save neuro-sys/9110614 to your computer and use it in GitHub Desktop.
Save neuro-sys/9110614 to your computer and use it in GitHub Desktop.
console mandelbrot zoomer
#include <stdio.h>
#include <complex.h>
#include <math.h>
double tx = -0.1;
double ty = 0.651;
void mandelbrot_draw(const unsigned int width, const unsigned int height)
{
#define MAX_ITER 50
unsigned int x, y;
const char *magnitude = " .*oO@@@@@";
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
double x_t = ((double) x / width * 2.0) - 1.5; /* [-2.5, 1] */
double y_t = ((double) y / height * 1.5) - .5; /* [ -1, 1] */
double _Complex z = 0. + 0.*_Complex_I;
double _Complex c = x_t/tx + y_t/ty*_Complex_I;
unsigned int n = 0;
while ( (cabs(z) < 2.0) && n++ != MAX_ITER ) {
z = z * z + c;
}
n = ((double) n / MAX_ITER) * 9; /* [0, 9] */
putchar(magnitude[n]);
}
printf("\n");
}
#undef MAX_ITER
}
int main(int argc, char *argv[])
{
while (1) {
mandelbrot_draw(70, 22);
tx += 0.01;
ty += 0.01;
system("cls");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment