Skip to content

Instantly share code, notes, and snippets.

@nrdmn
Created July 7, 2017 11:23
Show Gist options
  • Save nrdmn/fc39275f9b412f35328cb1dc62d5d5b6 to your computer and use it in GitHub Desktop.
Save nrdmn/fc39275f9b412f35328cb1dc62d5d5b6 to your computer and use it in GitHub Desktop.
mandelbrot
#include <math.h>
#include <stdio.h>
#ifndef EXAMPLE
#define EXAMPLE 0
#endif
#ifndef ACCURATE
#define ACCURATE 1
#endif
#if EXAMPLE == 0
#define X_START -2.05
#define X_END 1.05
#define Y_START 1.2
#define Y_END -1.2
#define X_STEP 0.02
#define Y_STEP 0.03
const unsigned int LIMITS[] = {7, 10, 14, 40};
#elif EXAMPLE == 1
#define X_START -1.5
#define X_END 0.1
#define Y_START 0.8
#define Y_END -0.2
#define X_STEP 0.01
#define Y_STEP 0.015
const unsigned int LIMITS[] = {10, 18, 25, 70};
#elif EXAMPLE == 2
#define X_START -1.1
#define X_END -0.4
#define Y_START 0.5
#define Y_END 0
#define X_STEP 0.004
#define Y_STEP 0.006
const unsigned int LIMITS[] = {15, 22, 30, 200};
#elif EXAMPLE == 3
#define X_START -0.9
#define X_END -0.65
#define Y_START 0.3
#define Y_END 0.15
#define X_STEP 0.0015
#define Y_STEP 0.00175
const unsigned int LIMITS[] = {25, 40, 70, 700};
#elif EXAMPLE == 4
#define X_START -0.86
#define X_END -0.75
#define Y_START 0.23
#define Y_END 0.15
#define X_STEP 0.0006
#define Y_STEP 0.0009
const unsigned int LIMITS[] = {35, 50, 90, 1000};
#elif EXAMPLE == 5
#define X_START -0.835
#define X_END -0.802
#define Y_START 0.205
#define Y_END 0.17
#define X_STEP 0.0002
#define Y_STEP 0.0003
const unsigned int LIMITS[] = {40, 70, 120, 1500};
#elif EXAMPLE == 6
#define X_START -0.8175
#define X_END -0.811
#define Y_START 0.186
#define Y_END 0.181
#define X_STEP 0.00004
#define Y_STEP 0.00006
const unsigned int LIMITS[] = {100, 180, 250, 1500};
#elif EXAMPLE == 7
#define X_START -0.815
#define X_END -0.8127
#define Y_START 0.1844
#define Y_END 0.1825
#define X_STEP 0.000015
#define Y_STEP 0.0000175
const unsigned int LIMITS[] = {150, 250, 400, 2000};
#elif EXAMPLE == 8
#define X_START -0.8142
#define X_END -0.8134
#define Y_START 0.1837
#define Y_END 0.1832
#define X_STEP 0.000004
#define Y_STEP 0.000006
const unsigned int LIMITS[] = {300, 450, 600, 3000};
#elif EXAMPLE == 9
#define X_START -0.81387
#define X_END -0.81371
#define Y_START 0.18357
#define Y_END 0.18345
#define X_STEP 0.000001
#define Y_STEP 0.0000015
const unsigned int LIMITS[] = {700, 1000, 1600, 7000};
#elif EXAMPLE == 10
#define X_START -0.813818
#define X_END -0.81377
#define Y_START 0.18351
#define Y_END 0.183465
#define X_STEP 0.0000003
#define Y_STEP 0.000000475
const unsigned int LIMITS[] = {900, 1200, 2600, 20000};
#elif EXAMPLE == 11
#define X_START -0.813799
#define X_END -0.813795
#define Y_START 0.183482
#define Y_END 0.183471
#define X_STEP 0.00000004
#define Y_STEP 0.00000006
const unsigned int LIMITS[] = {3000, 5000, 10000, 30000};
#elif EXAMPLE == 12
#define X_START -0.8137988
#define X_END -0.8137977
#define Y_START 0.1834835
#define Y_END 0.1834828
#define X_STEP 0.000000006
#define Y_STEP 0.000000009
const unsigned int LIMITS[] = {5000, 7500, 20000, 50000};
#endif
const char LIMITCHARS[sizeof(LIMITS)/sizeof(unsigned int)+1] = {' ', '.', ':', '+', '#'};
unsigned int mandelbrotf(const float x, const float y)
{
unsigned int i = 0;
float tmp;
float re = 0;
float im = 0;
while (fabsf(re) < 2 && fabsf(im) < 2 && i < LIMITS[sizeof(LIMITS)/sizeof(unsigned int)-1]) {
tmp = re*re - im*im + x;
im = 2*re*im + y;
re = tmp;
i++;
}
return i;
}
unsigned int mandelbrot(const double x, const double y)
{
unsigned int i = 0;
double tmp;
double re = 0;
double im = 0;
while (fabs(re) < 2 && fabs(im) < 2 && i < LIMITS[sizeof(LIMITS)/sizeof(unsigned int)-1]) {
tmp = re*re - im*im + x;
im = 2*re*im + y;
re = tmp;
i++;
}
return i;
}
int main()
{
unsigned int m;
size_t i;
double x, y;
for (y = Y_START; y > Y_END; y-=Y_STEP) {
for (x = X_START; x < X_END; x+=X_STEP) {
#if ACCURATE == 0
m = mandelbrotf(x, y);
#else
m = mandelbrot(x, y);
#endif
for (i = 0; LIMITS[i] <= m; i++);
putchar(LIMITCHARS[i]);
}
putchar('\n');
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment