Skip to content

Instantly share code, notes, and snippets.

@manu354
Created April 11, 2018 09:04
Show Gist options
  • Select an option

  • Save manu354/b141ef74a92fee9ff1389537aeb3bfea to your computer and use it in GitHub Desktop.

Select an option

Save manu354/b141ef74a92fee9ff1389537aeb3bfea to your computer and use it in GitHub Desktop.
mandelbrot.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#define MAX 256
#define PIXELS 512*512
#define ZOOM 6
#define CENTER_Y 0
#define CENTER_X 1
typedef unsigned short bits16;
int escapeSteps(double x, double y);
int getValues() ;
int main(int argc, char *argv[]) {
getValues();
return EXIT_SUCCESS;
}
int escapeSteps(double x, double y) { //Where x = real component, and y = complex component e.g 5 + 6i --> x= 5 y= 6
bits16 i = 0;
const double real = x;
const double imaginary = y;
int iterate() {
// printf("\n XY: %f %f %f\n ", x , y, imaginary);
const double oldX = x;
x = x * x - y * y + real;
y = 2 * oldX * y + imaginary;
// printf("\n RI: %f \n ",y);
return 0;
}
int steps = 0;
while (i < MAX) {
i++;
steps++;
const double distance = sqrt(x * x + y * y);
// printf("\n Distance: %f %f %f %f\n ", distance, x, y, real);
if (distance > 2) {
// printf("\n Distance: %f %f %f %f\n ", distance, x, y, real);
steps = i;
i = MAX;
// printf("\n Called %d \n ", steps);
}
iterate();
}
// printf("\n Steps: %d, \n ", steps);
return steps;
}
int getValues() {
double real = 1/(512*ZOOM) - CENTER_X;
double imaginary = 1/(512*ZOOM) - CENTER_Y;
int i = 0;
int values[PIXELS];
while(i < PIXELS) {
real += 1/(512*ZOOM);
imaginary += 1/(512*ZOOM);
values[i] = escapeSteps(real, imaginary);
printf("%d", values[i]);
i++;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment