aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-08-27 19:04:34 +0200
committerCharles <sircharlesaze@gmail.com>2019-08-27 19:04:34 +0200
commitce5cf1b60dae81540b2db366b1408a961f771dcc (patch)
tree25de8dc9727deb188f57fe01c2cf7575d5fbb4bc
parent7c48d434f0a68ac47ebe1bd66daa2c86842979c7 (diff)
downloadmandelbrot_cpu-ce5cf1b60dae81540b2db366b1408a961f771dcc.tar.gz
mandelbrot_cpu-ce5cf1b60dae81540b2db366b1408a961f771dcc.tar.bz2
mandelbrot_cpu-ce5cf1b60dae81540b2db366b1408a961f771dcc.zip
Set visualization
Loop throught each pixel and color it black if its in the set, white otherwise. This is eavily inspired by the coding train video on the subject. (https://www.youtube.com/watch?v=6z7GQewK-Ks)
-rw-r--r--Makefile2
-rw-r--r--graphics.c24
-rw-r--r--header.h15
-rw-r--r--helper.c10
-rw-r--r--mandelbrot.c25
5 files changed, 55 insertions, 21 deletions
diff --git a/Makefile b/Makefile
index 03be06e..dc19c48 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ CCFLAGS = -Wall -Wextra
LDFLAGS = -lm $(shell sdl2-config --libs --cflags)
HEADER = header.h
-SRC = main.c graphics.c mandelbrot.c
+SRC = main.c graphics.c mandelbrot.c helper.c
OBJ = $(SRC:.c=.o)
RM = rm -f
diff --git a/graphics.c b/graphics.c
index e9e12ff..1e7470f 100644
--- a/graphics.c
+++ b/graphics.c
@@ -4,6 +4,7 @@
#define WINDOW_X 20
#define WINDOW_Y 20
+static void update(GState *state);
static void event_handler(GState *state);
static void destroy_state(GState *state);
static void error_exit_state(GState *state, const char *msg);
@@ -17,7 +18,7 @@ GState *graphics_init(GConf *conf)
if (SDL_Init(SDL_INIT_VIDEO) < 0)
error_exit("unable to init SDL");
state->window = SDL_CreateWindow(WINDOW_TITLE, WINDOW_X, WINDOW_Y,
- conf->window_width, conf->window_height, 0);
+ WINDOW_W, WINDOW_H, 0);
if (state->window == NULL)
error_exit("unable to create window");
state->renderer = SDL_CreateRenderer(state->window, -1, 0);
@@ -38,9 +39,28 @@ void graphics_run(GState *state)
while (state->running)
{
event_handler(state);
+ update(state);
+ SDL_Delay(3);
+ }
+}
- SDL_Delay(10);
+static void update(GState *state)
+{
+ SDL_SetRenderDrawColor(state->renderer, 155, 155, 155, SDL_ALPHA_OPAQUE);
+ SDL_RenderClear(state->renderer);
+ for (int x = 0; x < WINDOW_W; x++)
+ {
+ for (int y = 0; y < WINDOW_H; y++)
+ {
+ if (mandelbrot_in_set(map_range((double)x, 0, WINDOW_W, LO, HI)
+ + map_range((double)y, 0, WINDOW_H, LO, HI) * I))
+ {
+ SDL_SetRenderDrawColor(state->renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
+ SDL_RenderDrawPoint(state->renderer, x, y);
+ }
+ }
}
+ SDL_RenderPresent(state->renderer);
}
static void event_handler(GState *state)
diff --git a/header.h b/header.h
index 855cff3..97e3de6 100644
--- a/header.h
+++ b/header.h
@@ -5,6 +5,18 @@
# include <complex.h>
# include <SDL2/SDL.h>
+# define WINDOW_W 500
+# define WINDOW_H 500
+
+# define LO -1.5
+# define HI 1.5
+# define AXIS_DIV 46.0
+# define AXIS_STEP ((HI - LO) / AXIS_DIV)
+
+# define MAX_ITERATION 50
+# define _INFINITY 15
+
+
typedef struct
{
SDL_Window *window;
@@ -27,4 +39,7 @@ GState *graphics_init(GConf *conf);
void graphics_quit(GState *state);
void graphics_run(GState *state);
+// helper.c
+double map_range(double x, double src_lo, double src_hi, double dest_lo, double dest_hi);
+
#endif
diff --git a/helper.c b/helper.c
new file mode 100644
index 0000000..9920c5d
--- /dev/null
+++ b/helper.c
@@ -0,0 +1,10 @@
+#include <math.h>
+#include "header.h"
+
+double map_range(double x, double src_lo, double src_hi, double dest_lo, double dest_hi)
+{
+ double src_len = fabs(src_hi - src_lo);
+ double dest_len = fabs(dest_hi - dest_lo);
+
+ return (x - src_lo) / src_len * dest_len + dest_lo;
+}
diff --git a/mandelbrot.c b/mandelbrot.c
index 053ce70..c5f5148 100644
--- a/mandelbrot.c
+++ b/mandelbrot.c
@@ -1,28 +1,11 @@
#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
+#include "header.h"
#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;
@@ -52,3 +35,9 @@ void mandelbrot_print(void)
}
}
+/*
+#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))); }
+*/