diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-09-22 11:44:30 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-09-22 11:44:30 +0200 |
| commit | c5008a4e62fb83eb71f5f94f622c01f2d8fe8b6b (patch) | |
| tree | 02d1f84aca29c95e999156848a86d61c292b7208 /mandelbrot.c | |
| parent | 5efde1e3e65af769cb629d55f0a4dd4f87caebe9 (diff) | |
| download | mandelbrot-c5008a4e62fb83eb71f5f94f622c01f2d8fe8b6b.tar.gz mandelbrot-c5008a4e62fb83eb71f5f94f622c01f2d8fe8b6b.tar.bz2 mandelbrot-c5008a4e62fb83eb71f5f94f622c01f2d8fe8b6b.zip | |
Supersampling
- Random supersampling (prettier but quite slow)
- WIP: bette color gradient
Diffstat (limited to 'mandelbrot.c')
| -rw-r--r-- | mandelbrot.c | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/mandelbrot.c b/mandelbrot.c index 1112575..7017a54 100644 --- a/mandelbrot.c +++ b/mandelbrot.c @@ -1,4 +1,5 @@ #include <stdio.h> +#include <math.h> #include <pthread.h> #include "header.h" @@ -9,6 +10,7 @@ #define AXIS_DIV 46.0 #define REAL_AXIS_STEP ((PRINT_REAL_HI - PRINT_REAL_LO) / AXIS_DIV) #define IMAG_AXIS_STEP ((PRINT_IMAG_HI - PRINT_IMAG_LO) / AXIS_DIV) +#define SAMPLES 1 #define IN_CHAR '*' #define OUT_CHAR ' ' @@ -37,6 +39,16 @@ int mandelbrot_in_set(double ca, double cb) return n; } + +int mandelbrot_in_set_super_sampled(double ca, double cb, double real_step, double imag_step) +{ + int total = 0; + for (int i = 0; i < SAMPLES; i++) + total += mandelbrot_in_set(ca - (double_rand() * real_step) / 2, + cb - (double_rand() * imag_step) / 2); + return total / SAMPLES; +} + void *mandelbrot_pixels(double real_lo, double real_hi, double imag_lo, double imag_hi, int width, int height, Color *palette) { @@ -55,6 +67,7 @@ void *mandelbrot_pixels(double real_lo, double real_hi, double imag_lo, args->real_hi = real_hi; args->width = width; args->imag = map_range((double)y, 0, height, imag_lo, imag_hi); + args->imag_step = (imag_hi - imag_lo) / (0.5 + (height - 1)); args->palette = palette; args->row = pixels + PIXELS_CHANELS * width * y; pthread_create(&threads[y], NULL, pixel_row, args); @@ -68,13 +81,28 @@ void *mandelbrot_pixels(double real_lo, double real_hi, double imag_lo, static void *pixel_row(void *void_args) { ThreadArgs *args = (ThreadArgs*)void_args; + int iterations; + double real_step = (args->real_hi - args->real_lo) / (0.5 + (args->width - 1)); for (int x = 0; x < args->width; x++) { double a = map_range((double)x, 0, args->width, args->real_lo, args->real_hi); - Color color = args->palette[mandelbrot_in_set(a, args->imag)]; - args->row[x * PIXELS_CHANELS] = color.rgb.r; - args->row[x * PIXELS_CHANELS + 1] = color.rgb.g; - args->row[x * PIXELS_CHANELS + 2] = color.rgb.b; + if (SAMPLES == 1) + iterations = mandelbrot_in_set(a, args->imag); + else + { + iterations = mandelbrot_in_set_super_sampled( + a, args->imag, real_step, args->imag_step); + } + + /* double logBase = 1.0 / log(2.0); */ + /* double logHalfBase = log(0.5) * logBase; */ + /* int color_index = (int)(5 + iterations - logHalfBase */ + /* - log(log(a * a + args->imag * args->imag)) * logBase); */ + Color color = args->palette[iterations]; + int row_index = x * PIXELS_CHANELS; + args->row[row_index] = color.rgb.r; + args->row[row_index + 1] = color.rgb.g; + args->row[row_index + 2] = color.rgb.b; } free(args); return NULL; |
