From 5cba5bc63de403c779fd4fa762216280d4396444 Mon Sep 17 00:00:00 2001 From: JP Appel Date: Sat, 20 Apr 2024 02:43:17 -0400 Subject: reorganized src files --- src/fractals.c | 73 +++++++++++++++++++++++++++++ src/fractals.h | 9 ++++ src/grids.h | 4 +- src/mandelbrot.c | 125 -------------------------------------------------- src/plotting.h | 4 +- src/serial-fractals.c | 55 ++++++++++++++++++++++ 6 files changed, 141 insertions(+), 129 deletions(-) create mode 100644 src/fractals.c create mode 100644 src/fractals.h delete mode 100644 src/mandelbrot.c create mode 100644 src/serial-fractals.c (limited to 'src') diff --git a/src/fractals.c b/src/fractals.c new file mode 100644 index 0000000..1e27b9c --- /dev/null +++ b/src/fractals.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include + +#include "grids.h" +#include "fractals.h" + + +/* + * Converts a grid point into an complex number + */ +double complex lattice_to_complex(const size_t index, const size_t x_res, const size_t y_res) { + const double x_min = -2.0; + const double x_max = 2.0; + const double y_min = -2.0; + const double y_max = 2.0; + + const double x_step = (x_max - x_min) / (double)x_res; + const double y_step = (y_max - y_min) / (double)y_res; + + const size_t x_index = index % x_res; + const size_t y_index = index / x_res; + + const double x = x_min + x_index * x_step; + const double y = y_min + y_index * y_step; + + return x + y * I; +} + + +int main(const int argc, char *argv[]) { + //default values + size_t iterations = 1000; + size_t x_res = 100; + size_t y_res = 100; + + //parse command line arguments + int opt; + while((opt =getopt(argc, argv, "i:x:y:")) != -1){ + switch(opt){ + case 'i': + iterations = strtoull(optarg, NULL, 10); + break; + case 'x': + x_res = strtoull(optarg, NULL, 10); + break; + case 'y': + y_res = strtoull(optarg, NULL, 10); + break; + default: + fprintf(stderr, "Usage: %s [-i iterations] [-x x_res] [-y y_res]\n", argv[0]); + return 1; + } + } + + + grid_t* grid = create_grid(x_res, y_res); + if(!grid) return 1; + + + const size_t size = grid->size; + size_t* data = grid->data; + for(size_t i = 0; i < size;i++){ + data[i] = multibrot(lattice_to_complex(i, x_res, y_res), iterations, 3); + } + + for(size_t i = 0; i < size; i++){ + printf("%zu%s", data[i], (i % x_res == x_res - 1) ? "\n" : "\t"); + } +} diff --git a/src/fractals.h b/src/fractals.h new file mode 100644 index 0000000..60d9013 --- /dev/null +++ b/src/fractals.h @@ -0,0 +1,9 @@ +#pragma once + +#include +#include +#include + +size_t mandelbrot(const double complex z0, const size_t max_iterations); +size_t multibrot(const double complex z0, const size_t max_iterations, const uintmax_t d); +size_t julia(const double R, const double complex z0, const double complex c, const size_t max_iterations); diff --git a/src/grids.h b/src/grids.h index 8de6375..f1581b3 100644 --- a/src/grids.h +++ b/src/grids.h @@ -1,8 +1,8 @@ +#pragma once + #include #include -#pragma once - typedef struct { size_t x; size_t y; diff --git a/src/mandelbrot.c b/src/mandelbrot.c deleted file mode 100644 index ee2585f..0000000 --- a/src/mandelbrot.c +++ /dev/null @@ -1,125 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include "grids.h" - -/* - * Computes the number of iterations it takes for a point z0 to diverge - * if the return value is equal to max_iterations, the point lies within the mandelbrot set - * This is an implementation the escape algorithm - */ -size_t mandelbrot(const double complex z0, const size_t max_iterations) { - double complex z = z0; - size_t iteration = 0; - - while (cabs(z) <= 2 && iteration < max_iterations) { - z = z * z + z0; - iteration++; - } - return iteration; -} - -/* - * Computes the number of iterations it takes for a point z0 to diverge - * if the return value is equal to max_iterations, the point lies within the multibrot set - * This is implementation closely matches mandelbrot - * Note, only positive integer powers are supported - */ -size_t multibrot(const double complex z0, const size_t max_iterations, const uintmax_t d){ - double complex z = z0; - double complex ztemp; - size_t iteration = 0; - while(cabs(z) <= 2 && iteration < max_iterations){ - ztemp = z; - for(size_t i = 0; i < d; i ++){ - ztemp *= ztemp; - } - z = ztemp + z0; - iteration++; - } - return iteration; -} - -/* - * Computes ????? for a julia set - * implementation of https://en.wikipedia.org/wiki/Julia_set#Pseudocode - */ -size_t julia(const double R, const double complex z0, const double complex c, const size_t max_iterations){ - //FIXME: I'm notsure if this is currently implemented correctly - if(R*R - R >= cabs(z0)) return 0; - double complex z = z0; - - size_t iteration = 0; - while(cabs(z) < R && iteration < max_iterations){ - z = z * z + c; - iteration++; - } - return iteration; -} - -/* - * Converts a grid point into an complex number - */ -double complex lattice_to_complex(const size_t index, const size_t x_res, const size_t y_res) { - const double x_min = -2.0; - const double x_max = 2.0; - const double y_min = -2.0; - const double y_max = 2.0; - - const double x_step = (x_max - x_min) / (double)x_res; - const double y_step = (y_max - y_min) / (double)y_res; - - const size_t x_index = index % x_res; - const size_t y_index = index / x_res; - - const double x = x_min + x_index * x_step; - const double y = y_min + y_index * y_step; - - return x + y * I; -} - - -int main(const int argc, char *argv[]) { - //default values - size_t iterations = 1000; - size_t x_res = 100; - size_t y_res = 100; - - //parse command line arguments - int opt; - while((opt =getopt(argc, argv, "i:x:y:")) != -1){ - switch(opt){ - case 'i': - iterations = strtoull(optarg, NULL, 10); - break; - case 'x': - x_res = strtoull(optarg, NULL, 10); - break; - case 'y': - y_res = strtoull(optarg, NULL, 10); - break; - default: - fprintf(stderr, "Usage: %s [-i iterations] [-x x_res] [-y y_res]\n", argv[0]); - return 1; - } - } - - - grid_t* grid = create_grid(x_res, y_res); - if(!grid) return 1; - - - const size_t size = grid->size; - size_t* data = grid->data; - for(size_t i = 0; i < size;i++){ - data[i] = multibrot(lattice_to_complex(i, x_res, y_res), iterations, 3); - } - - for(size_t i = 0; i < size; i++){ - printf("%zu%s", data[i], (i % x_res == x_res - 1) ? "\n" : "\t"); - } -} diff --git a/src/plotting.h b/src/plotting.h index d94c7bc..19d669b 100644 --- a/src/plotting.h +++ b/src/plotting.h @@ -1,7 +1,7 @@ -#include - #pragma once +#include + typedef struct { char red; char green; diff --git a/src/serial-fractals.c b/src/serial-fractals.c new file mode 100644 index 0000000..c5b9c4e --- /dev/null +++ b/src/serial-fractals.c @@ -0,0 +1,55 @@ +#include "fractals.h" +/* + * Computes the number of iterations it takes for a point z0 to diverge + * if the return value is equal to max_iterations, the point lies within the mandelbrot set + * This is an implementation the escape algorithm + */ +size_t mandelbrot(const double complex z0, const size_t max_iterations) { + double complex z = z0; + size_t iteration = 0; + + while (cabs(z) <= 2 && iteration < max_iterations) { + z = z * z + z0; + iteration++; + } + return iteration; +} + +/* + * Computes the number of iterations it takes for a point z0 to diverge + * if the return value is equal to max_iterations, the point lies within the multibrot set + * This is implementation closely matches mandelbrot + * Note, only positive integer powers are supported + */ +size_t multibrot(const double complex z0, const size_t max_iterations, const uintmax_t d){ + double complex z = z0; + double complex ztemp; + size_t iteration = 0; + while(cabs(z) <= 2 && iteration < max_iterations){ + ztemp = z; + for(size_t i = 0; i < d; i ++){ + ztemp *= ztemp; + } + z = ztemp + z0; + iteration++; + } + return iteration; +} + +/* + * Computes ????? for a julia set + * implementation of https://en.wikipedia.org/wiki/Julia_set#Pseudocode + */ +size_t julia(const double R, const double complex z0, const double complex c, const size_t max_iterations){ + //FIXME: I'm notsure if this is currently implemented correctly + if(R*R - R >= cabs(z0)) return 0; + double complex z = z0; + + size_t iteration = 0; + while(cabs(z) < R && iteration < max_iterations){ + z = z * z + c; + iteration++; + } + return iteration; +} + -- cgit v1.2.3