diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-08-27 15:30:51 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-08-27 15:30:51 +0200 |
| commit | 60adb6e2f051ab72fb66541a8f48ef195317d403 (patch) | |
| tree | d9cf1c7c689a5871cb2c434958787c5b75fd7e10 /mandelbrot.c | |
| download | mandelbrot_cpu-60adb6e2f051ab72fb66541a8f48ef195317d403.tar.gz mandelbrot_cpu-60adb6e2f051ab72fb66541a8f48ef195317d403.tar.bz2 mandelbrot_cpu-60adb6e2f051ab72fb66541a8f48ef195317d403.zip | |
Initial commit
Compute if a number is in Mandelbrot set.
Print it to the terminal.
Diffstat (limited to 'mandelbrot.c')
| -rw-r--r-- | mandelbrot.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/mandelbrot.c b/mandelbrot.c new file mode 100644 index 0000000..053ce70 --- /dev/null +++ b/mandelbrot.c @@ -0,0 +1,54 @@ +#include <stdio.h> +#include <math.h> +#include <complex.h> + +#define LO -1.7 +#define HI 1.7 +#define AXIS_DIV 46.0 +#define AXIS_STEP ((HI - LO) / AXIS_DIV) + +#define MAX_ITERATION 1000 +#define _INFINITY 1000 + +#define IN_CHAR '*' +#define OUT_CHAR ' ' + +/* +#define SQUARE(x) (pow((x), 2)) +#define SQUARE_CML(z) (SQUARE(creal(z)) - SQUARE(cimag(z)) + 2 * creal(z) * cimag(z) * I) + +double magnitude(double complex z) +{ + return sqrt(SQUARE(creal(z)) + SQUARE(cimag(z))); +} +*/ + +double mandelbrot_in_set(double complex c) +{ + int i; + double complex z = 0; + for (i = 0; i < MAX_ITERATION; i++) + { + z = cpow(z, 2) + c; + if (cabs(z) > _INFINITY) + return 0; + } + return 1; +} + +void mandelbrot_print(void) +{ + for (double i = LO; i < HI; i += AXIS_STEP) + { + for (double r = LO; r < HI; r += AXIS_STEP) + { + if (mandelbrot_in_set(r + i * I)) + putchar(IN_CHAR); + else + putchar(OUT_CHAR); + putchar(' '); + } + putchar('\n'); + } +} + |
