aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--makefile29
-rw-r--r--src/mandelbrot.c79
2 files changed, 85 insertions, 23 deletions
diff --git a/makefile b/makefile
index 1cbae10..83ba870 100644
--- a/makefile
+++ b/makefile
@@ -3,23 +3,30 @@ CPPFLAGS :=
CFLAGS := -Wall
LDFLAGS :=
-all:
+SRC_DIR := src
+BUILD_DIR := build
+OBJ_DIR := $(BUILD_DIR)/objects
+
+TARGET := serial-fractal
+SRCS := $(wildcard $(SRC_DIR)/*.c)
+OBJS := $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.o, $(SRCS))
+
+
+.PHONY: all presentation analysis clean test
+
+all: $(BUILD_DIR)/$(TARGET)
##############
# Programs #
##############
-SRCS := mandelbrot.c plotting.c
-OBJS := $(SRC:.c=.o)
-OBJS_DIR := build/objs
-
-build/mandelbrot: $(addprefix $(OBJS_DIR)/, $(OBJS))
+build/mandelbrot: $(OBJ_DIR)/serial-fractal.o $(OBJ_DIR)/grids.o
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
-$(OBJS_DIR)/%.o: src/%.c $(OBJS_DIR)
- $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
+$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
+ $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
-$(OBJS_DIR):
+$(OBJ_DIR):
mkdir -p $@
################
@@ -44,6 +51,4 @@ analysis: analysis/analysis.html
analysis/analysis.html: analysis/analysis.Rmd # TODO: add compile command
clean:
- rm -rf presentation/presentation.html analysis/analysis.html
-
-.PHONY: all presentation analysis clean
+ rm -rf presentation/presentation.html analysis/analysis.html $(OBJS)
diff --git a/src/mandelbrot.c b/src/mandelbrot.c
index 63db30d..f8e65f7 100644
--- a/src/mandelbrot.c
+++ b/src/mandelbrot.c
@@ -2,16 +2,18 @@
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
-#include "plotting.h"
+#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 esacpe algorithm
+ * This is an implementation the escape algorithm
*/
-size_t mandelbrot(double complex z0, size_t max_iterations) {
- double complex z = 0.0 + 0.0*I;
+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) {
@@ -27,8 +29,8 @@ size_t mandelbrot(double complex z0, size_t max_iterations) {
* This is implementation closely matches mandelbrot
* Note, only positive integer powers are supported
*/
-size_t multibrot(double complex z0, size_t max_iterations, uintmax_t d){
- double complex z = 0.0 + 0.0*I;
+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){
@@ -42,10 +44,65 @@ size_t multibrot(double complex z0, size_t max_iterations, uintmax_t d){
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;
-int main(const int argc, const char *argv[]) {
- double complex z = 1.0 + 0.0*I;
- size_t result = mandelbrot(z, 1000);
- printf("Input: %f+%fI\n", creal(z), cimag(z));
- printf("result: %zu\n", result);
+ //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");
+ }
}