Created
August 7, 2017 19:29
-
-
Save PurpleMyst/b480c8ad2fd8827bf57204b9acec3ba3 to your computer and use it in GitHub Desktop.
A visualization of the mandelbrot set written in C99.
This file contains 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 <stdint.h> | |
#include <complex.h> | |
#define MAX_ITERATIONS 255 | |
#define REAL_START -2 | |
#define REAL_END 1 | |
#define IMAG_START -1 | |
#define IMAG_END 1 | |
#define IMAGE_WIDTH 1920 | |
#define IMAGE_HEIGHT 1080 | |
static uint_fast8_t iterations(const double x, const double y) { | |
uint_fast8_t n = 0; | |
double complex c = (REAL_START + (x / IMAGE_WIDTH) * (REAL_END - REAL_START)) + | |
(IMAG_START + (y / IMAGE_HEIGHT) * (IMAG_END - IMAG_START)) * I; | |
for (double complex z = 0; cabs(z) <= 2 && n < MAX_ITERATIONS; z = z * z + c, ++n); | |
return n; | |
} | |
int main(void) { | |
printf("P1\n"); | |
printf("%d %d\n", IMAGE_WIDTH, IMAGE_HEIGHT); | |
for (double y = 0; y < IMAGE_HEIGHT; ++y) { | |
for (double x = 0; x < IMAGE_WIDTH; ++x) { | |
const uint_fast8_t n = iterations(x, y); | |
if (x > 0) printf(" "); | |
printf("%d", n == MAX_ITERATIONS); | |
} | |
printf("\n"); | |
} | |
return 0; | |
} |
This file contains 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 <stdint.h> | |
#include <complex.h> | |
#define MAX_ITERATIONS 80 | |
#define REAL_START -2 | |
#define REAL_END 1 | |
#define IMAG_START -1 | |
#define IMAG_END 1 | |
#define IMAGE_WIDTH 1920 | |
#define IMAGE_HEIGHT 1080 | |
static uint_fast8_t iterations(const double x, const double y) { | |
uint_fast8_t n = 0; | |
double complex c = (REAL_START + (x / IMAGE_WIDTH) * (REAL_END - REAL_START)) + | |
(IMAG_START + (y / IMAGE_HEIGHT) * (IMAG_END - IMAG_START)) * I; | |
for (double complex z = 0; cabs(z) <= 2 && n < MAX_ITERATIONS; z = z * z + c, ++n); | |
return n; | |
} | |
int main(void) { | |
printf("P2\n"); | |
printf("%d %d\n", IMAGE_WIDTH, IMAGE_HEIGHT); | |
printf("255\n"); | |
for (double y = 0; y < IMAGE_HEIGHT; ++y) { | |
for (double x = 0; x < IMAGE_WIDTH; ++x) { | |
const uint_fast8_t n = iterations(x, y); | |
const uint_fast8_t color = 255 - (n * 255 / MAX_ITERATIONS); | |
if (x > 0) printf(" "); | |
printf("%d", color); | |
} | |
printf("\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment