aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--Makefile34
-rw-r--r--README.md1
-rw-r--r--graphics.c0
-rw-r--r--header.h9
-rw-r--r--main.c7
-rw-r--r--mandelbrot.c54
7 files changed, 108 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..e07657e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+a.out
+mandel
+*.o
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..232508f
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,34 @@
+NAME = mandel
+CC = gcc
+CCFLAGS = -Wall -Wextra
+LDFLAGS = -lm # $(shell sdl2-config --libs --cflags)
+
+HEADER = header.h
+SRC = main.c graphics.c mandelbrot.c
+OBJ = $(SRC:.c=.o)
+
+RM = rm -f
+
+.PHONY: all
+all: $(NAME)
+
+$(NAME): $(OBJ) $(HEADER)
+ $(CC) $(LDFLAGS) $(CCFLAGS) -o $@ $(OBJ)
+
+%.o: %.c
+ $(CC) $(LDFLAGS) $(CCFLAGS) -c -o $@ $<
+
+.PHONY: debug
+debug: CCFLAGS += -g
+debug: re
+
+.PHONY: clean
+clean:
+ $(RM) $(OBJ)
+
+.PHONY: fclean
+fclean: clean
+ $(RM) $(NAME)
+
+.PHONY: re
+re: fclean all
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..d32372b
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+# Mandelbrot set visualizer
diff --git a/graphics.c b/graphics.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/graphics.c
diff --git a/header.h b/header.h
new file mode 100644
index 0000000..e46ce7e
--- /dev/null
+++ b/header.h
@@ -0,0 +1,9 @@
+#ifndef HEADER_H
+# define HEADER_H
+
+#include <complex.h>
+
+double mandelbrot_in_set(double complex c);
+void mandelbrot_print(void);
+
+#endif
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..f1fb7af
--- /dev/null
+++ b/main.c
@@ -0,0 +1,7 @@
+#include "header.h"
+
+int main(void)
+{
+ mandelbrot_print();
+ return 0;
+}
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');
+ }
+}
+