From 70b7e36de4a9db760c254b8f22d91ea4e6ddc9d8 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Tue, 9 Jun 2015 22:11:04 +1200 Subject: Made it easier to add new algos, added tricorn algo, burning ship lattice ship algo --- .gitignore | 4 ++++ Makefile | 22 +++++++++++++++------- README.md | 3 ++- algorithms.h | 8 ++++++++ algorithms/burning-ship-lattice.c | 31 +++++++++++++++++++++++++++++++ algorithms/burning-ship.c | 31 +++++++++++++++++++++++++++++++ algorithms/mandelbrot.c | 31 +++++++++++++++++++++++++++++++ algorithms/tricorn.c | 31 +++++++++++++++++++++++++++++++ bship.c | 28 ---------------------------- fractal-gen.c | 23 ++++++++++++++++++----- fractal-gen.h | 4 +--- mbrot.c | 28 ---------------------------- 12 files changed, 172 insertions(+), 72 deletions(-) create mode 100644 .gitignore create mode 100644 algorithms.h create mode 100644 algorithms/burning-ship-lattice.c create mode 100644 algorithms/burning-ship.c create mode 100644 algorithms/mandelbrot.c create mode 100644 algorithms/tricorn.c delete mode 100644 bship.c delete mode 100644 mbrot.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..076de85 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.o +*-gen +*.pgm +*.png diff --git a/Makefile b/Makefile index 2155733..0162825 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,28 @@ all: fractal-gen symlinks symlinks: fractal-gen - ln -sf $< mbrot-gen - ln -sf $< bship-gen + ln -sf $< mandelbrot-gen + ln -sf $< burning-ship-gen + ln -sf $< burning-ship-lattice-gen + ln -sf $< tricorn-gen + +fractal-gen: fractal-gen.o \ + algorithms/mandelbrot.o \ + algorithms/burning-ship.o \ + algorithms/burning-ship-lattice.o \ + algorithms/tricorn.o -fractal-gen: fractal-gen.o mbrot.o bship.o $(CC) -o $@ $^ -lm -lpthread %.o: %.c $(CC) -c -o $@ $< -Wall -Wextra -Werror - .PHONY: all clean symlinks clean: rm fractal-gen \ - mbrot-gen \ - bship-gen \ - *.o \ + mandelbrot-gen \ + burning-ship-gen \ + burning-ship-lattice-gen \ + tricorn-gen \ + **/*.o \ -f diff --git a/README.md b/README.md index c0b8cc5..29337bc 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,9 @@ This is a tiny program which will output a binary [PGM](https://wikipedia.org/wi Syntax ------ - ./mbrot-gen [threads] > output.pgm + ./mandelbrot-gen [threads] > output.pgm +Also check out the other algorithms like `tricorn-gen` and `burning-ship-gen`. You might then want to consider using a tool such as ImageMagick to then convert the (large) output file into something more sane like a PNG. For the cliché set you'll want to keep the exponent at 2. For more info on the exponent, read through [Wikipedia's fine article](http://wikipedia.org/wiki/Mandelbrot_set). diff --git a/algorithms.h b/algorithms.h new file mode 100644 index 0000000..d8d2935 --- /dev/null +++ b/algorithms.h @@ -0,0 +1,8 @@ +#ifndef GENERATORS_H +#define GENERATORS_H + +void *generate_mandelbrot_section(void *section); +void *generate_burning_ship_section(void *section); +void *generate_tricorn_section(void *section); + +#endif diff --git a/algorithms/burning-ship-lattice.c b/algorithms/burning-ship-lattice.c new file mode 100644 index 0000000..cf80632 --- /dev/null +++ b/algorithms/burning-ship-lattice.c @@ -0,0 +1,31 @@ +#include "../fractal-gen.h" + +void *generate_burning_ship_lattice_section(void *section) +{ + data_section *d = (data_section*)section; + unsigned int x,y,i; + int idx = 0; + double a,b; + double complex z,c; + double size_units = 0.09f; + double top = -0.082f; + double left = -1.8f; + + for (y = d->core, b = (d->core*(size_units/d->size)+top); y < d->size; b+=((d->cores*size_units)/d->size), y+=d->cores) + { + for (x = 0, a = left; x < d->size; a+=(size_units/d->size), x++) + { + z = 0; + c = a+I*b; + for (i = 0; i < d->iterat; i++) + { + if (cabsf(z) >= 2) + break; + + z = cpow( cabsf(crealf(z)) + I*cabsf(cimagf(z)) , d->power) + c; + } + d->data[idx++] = (255*i)/d->iterat; + } + } + return NULL; +} diff --git a/algorithms/burning-ship.c b/algorithms/burning-ship.c new file mode 100644 index 0000000..bfbd0a7 --- /dev/null +++ b/algorithms/burning-ship.c @@ -0,0 +1,31 @@ +#include "../fractal-gen.h" + +void *generate_burning_ship_section(void *section) +{ + data_section *d = (data_section*)section; + unsigned int x,y,i; + int idx = 0; + double a,b; + double complex z,c; + double size_units = 3.5f; + double top = -2.2f; + double left = -2.2f; + + for (y = d->core, b = (d->core*(size_units/d->size)+top); y < d->size; b+=((d->cores*size_units)/d->size), y+=d->cores) + { + for (x = 0, a = left; x < d->size; a+=(size_units/d->size), x++) + { + z = 0; + c = a+I*b; + for (i = 0; i < d->iterat; i++) + { + if (cabsf(z) >= 2) + break; + + z = cpow( cabsf(crealf(z)) + I*cabsf(cimagf(z)) , d->power) + c; + } + d->data[idx++] = (255*i)/d->iterat; + } + } + return NULL; +} diff --git a/algorithms/mandelbrot.c b/algorithms/mandelbrot.c new file mode 100644 index 0000000..7f8a613 --- /dev/null +++ b/algorithms/mandelbrot.c @@ -0,0 +1,31 @@ +#include "../fractal-gen.h" + +void *generate_mandelbrot_section(void *section) +{ + data_section *d = (data_section*)section; + unsigned int x,y,i; + int idx = 0; + double a,b; + double complex z,c; + double size_units = 3.5f; + double top = -1.75f; + double left = -2.5f; + + for (y = d->core, b = (d->core*(size_units/d->size)+top); y < d->size; b+=((d->cores*size_units)/d->size), y+=d->cores) + { + for (x = 0, a = left; x < d->size; a+=(size_units/d->size), x++) + { + z = 0; + c = a+I*b; + for (i = 0; i < d->iterat; i++) + { + if (cabsf(z) >= 2) + break; + + z = cpow(z , d->power) + c; + } + d->data[idx++] = (255*i)/d->iterat; + } + } + return NULL; +} diff --git a/algorithms/tricorn.c b/algorithms/tricorn.c new file mode 100644 index 0000000..eddc4d6 --- /dev/null +++ b/algorithms/tricorn.c @@ -0,0 +1,31 @@ +#include "../fractal-gen.h" + +void *generate_tricorn_section(void *section) +{ + data_section *d = (data_section*)section; + unsigned int x,y,i; + int idx = 0; + double a,b; + double complex z,c; + double size_units = 4.f; + double top = -2.f; + double left = -2.3f; + + for (y = d->core, b = (d->core*(size_units/d->size)+top); y < d->size; b+=((d->cores*size_units)/d->size), y+=d->cores) + { + for (x = 0, a = left; x < d->size; a+=(size_units/d->size), x++) + { + z = 0; + c = a+I*b; + for (i = 0; i < d->iterat; i++) + { + if (cabsf(z) >= 2) + break; + + z = cpow(conj(z) , d->power) + c; + } + d->data[idx++] = (255*i)/d->iterat; + } + } + return NULL; +} diff --git a/bship.c b/bship.c deleted file mode 100644 index 1d16ae2..0000000 --- a/bship.c +++ /dev/null @@ -1,28 +0,0 @@ -#include "fractal-gen.h" - -void *generate_bship_section(void *section) -{ - data_section *d = (data_section*)section; - unsigned int x,y,i; - int idx = 0; - double a,b; - double complex z,c; - - for (y = d->core, b = (d->core*(3.5f/d->size)-1.75f); y < d->size; b+=((d->cores*3.5f)/d->size), y+=d->cores) - { - for (x = 0, a = -2.5f; x < d->size; a+=(3.5f/d->size), x++) - { - z = 0; - c = a+I*b; - for (i = 0; i < d->iterat; i++) - { - if (cabsf(z) >= 2) - break; - - z = cpow( cabsf(crealf(z)) + I*cabsf(cimagf(z)) , d->power) + c; - } - d->data[idx++] = (255*i)/d->iterat; - } - } - return NULL; -} diff --git a/fractal-gen.c b/fractal-gen.c index 6ce9867..10803d9 100644 --- a/fractal-gen.c +++ b/fractal-gen.c @@ -1,5 +1,11 @@ #include "fractal-gen.h" +struct section_generator +{ + char *executable_name; + void *(*generator)(void *); +}; + int main(int argc, char **argv) { unsigned int size, iterat, cores, i, x, y; @@ -8,14 +14,21 @@ int main(int argc, char **argv) data_section* sections; void *(*generator)(void *); + struct section_generator generators[] = { + { "mandelbrot-gen" , &generate_mandelbrot_section }, + { "burning-ship-gen" , &generate_burning_ship_section }, + { "tricorn-gen" , &generate_tricorn_section } + }; + // Select correct generator for the fractal type bname = basename(argv[0]); - if (strcmp(bname, "mbrot-gen") == 0) + generator = NULL; + for (i = 0; i < sizeof(generators)/sizeof(struct section_generator); i++) + if (strcmp(bname, generators[i].executable_name) == 0) + generator = generators[i].generator; + + if (generator == NULL) { - generator = &generate_mbrot_section; - } else if (strcmp(bname, "bship-gen") == 0) { - generator = &generate_bship_section; - } else { fprintf(stderr, "Don't call this directly, call a symlink to me\n"); return EXIT_FAILURE; } diff --git a/fractal-gen.h b/fractal-gen.h index e710606..914c65e 100644 --- a/fractal-gen.h +++ b/fractal-gen.h @@ -22,8 +22,6 @@ typedef struct pthread_t thread; } data_section; -void *generate_mbrot_section(void *d); -void *generate_bship_section(void *d); - +#include "algorithms.h" #endif diff --git a/mbrot.c b/mbrot.c deleted file mode 100644 index 6722b55..0000000 --- a/mbrot.c +++ /dev/null @@ -1,28 +0,0 @@ -#include "fractal-gen.h" - -void *generate_mbrot_section(void *section) -{ - data_section *d = (data_section*)section; - unsigned int x,y,i; - int idx = 0; - double a,b; - double complex z,c; - - for (y = d->core, b = (d->core*(3.5f/d->size)-1.75f); y < d->size; b+=((d->cores*3.5f)/d->size), y+=d->cores) - { - for (x = 0, a = -2.5f; x < d->size; a+=(3.5f/d->size), x++) - { - z = 0; - c = a+I*b; - for (i = 0; i < d->iterat; i++) - { - if (cabsf(z) >= 2) - break; - - z = cpow(z, d->power)+c; - } - d->data[idx++] = (255*i)/d->iterat; - } - } - return NULL; -} -- cgit v1.1