aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-08-28 08:56:31 +0200
committerCharles <sircharlesaze@gmail.com>2019-08-28 08:56:31 +0200
commit0a808a0f355390c68a5ca6f93c10758107c67700 (patch)
treeded488ba4737d0ca4d599b76046d0d726145032f
parent003e9e628b3e7b516d3d70d78b6ba97bca69c813 (diff)
downloadmandelbrot-0a808a0f355390c68a5ca6f93c10758107c67700.tar.gz
mandelbrot-0a808a0f355390c68a5ca6f93c10758107c67700.tar.bz2
mandelbrot-0a808a0f355390c68a5ca6f93c10758107c67700.zip
State stores the window size and the domain where to look the set
-rw-r--r--graphics.c32
-rw-r--r--header.h26
-rw-r--r--main.c6
-rw-r--r--mandelbrot.c8
4 files changed, 43 insertions, 29 deletions
diff --git a/graphics.c b/graphics.c
index e36fee0..dd146d2 100644
--- a/graphics.c
+++ b/graphics.c
@@ -1,8 +1,17 @@
+#include <stdbool.h>
+#include <complex.h>
+#include <SDL2/SDL.h>
#include "header.h"
#define WINDOW_TITLE "Mandelbrot"
#define WINDOW_X 20
#define WINDOW_Y 20
+#define WINDOW_W 500
+#define WINDOW_H 500
+#define REFRESH_RATE 3
+
+#define BRIGHTNESS_LO 0
+#define BRIGHTNESS_HI 20
static void update(GState *state);
static void event_handler(GState *state);
@@ -10,7 +19,7 @@ static void destroy_state(GState *state);
static void error_exit_state(GState *state, const char *msg);
static void error_exit(const char *msg);
-GState *graphics_init(GConf *conf)
+GState *graphics_init(void)
{
GState *state = (GState*)malloc(sizeof(GState));
if (state == NULL)
@@ -25,6 +34,12 @@ GState *graphics_init(GConf *conf)
if (state->renderer == NULL)
error_exit_state(state, "unable to create renderer");
state->running = true;
+ state->window_w = WINDOW_W;
+ state->window_h = WINDOW_H;
+ state->real_lo = REAL_LO;
+ state->real_hi = REAL_HI;
+ state->imag_lo = IMAG_LO;
+ state->imag_hi = IMAG_HI;
return state;
}
@@ -40,24 +55,27 @@ void graphics_run(GState *state)
{
event_handler(state);
update(state);
- SDL_Delay(3);
+ SDL_Delay(REFRESH_RATE);
}
}
static void update(GState *state)
{
+ double a, b, brightness;
SDL_SetRenderDrawColor(state->renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(state->renderer);
- for (int x = 0; x < WINDOW_W; x++)
+ for (int x = 0; x < state->window_w; x++)
{
- for (int y = 0; y < WINDOW_H; y++)
+ for (int y = 0; y < state->window_h; y++)
{
- double complex mapped_z = map_range((double)x, 0, WINDOW_W, LO, HI)
- + map_range((double)y, 0, WINDOW_H, LO, HI) * I;
+ a = map_range((double)x, 0, state->window_w, state->real_lo, state->real_hi);
+ b = map_range((double)y, 0, state->window_h, state->imag_lo, state->imag_hi);
+ double complex mapped_z = a + b * I;
int steps = mandelbrot_in_set(mapped_z);
if (steps == -1)
continue;
- double brightness = map_range(steps, LO, HI, 0, 20);
+ brightness = map_range(steps, state->real_lo, state->real_hi,
+ BRIGHTNESS_LO, BRIGHTNESS_HI);
SDL_SetRenderDrawColor(state->renderer, brightness, brightness,
brightness, SDL_ALPHA_OPAQUE);
SDL_RenderDrawPoint(state->renderer, x, y);
diff --git a/header.h b/header.h
index 8873077..3f86f46 100644
--- a/header.h
+++ b/header.h
@@ -5,37 +5,33 @@
# 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 REAL_LO -2.0
+# define REAL_HI 2.0
+# define IMAG_LO -2.0
+# define IMAG_HI 2.0
# define MAX_ITERATION 30
# define _INFINITY 4
-
typedef struct
{
SDL_Window *window;
SDL_Renderer *renderer;
bool running;
+ int window_w;
+ int window_h;
+ double real_lo;
+ double real_hi;
+ double imag_lo;
+ double imag_hi;
} GState;
-typedef struct
-{
- int window_width;
- int window_height;
-} GConf;
-
// mandelbrot.c
int mandelbrot_in_set(double _Complex c);
void mandelbrot_print(void);
// graphics.c
-GState *graphics_init(GConf *conf);
+GState *graphics_init(void);
void graphics_quit(GState *state);
void graphics_run(GState *state);
diff --git a/main.c b/main.c
index 494ee2c..7757243 100644
--- a/main.c
+++ b/main.c
@@ -3,11 +3,7 @@
int main(void)
{
/* mandelbrot_print(); */
- GConf gconf = {
- .window_width = 300,
- .window_height = 300
- };
- GState *gstate = graphics_init(&gconf);
+ GState *gstate = graphics_init();
graphics_run(gstate);
graphics_quit(gstate);
return 0;
diff --git a/mandelbrot.c b/mandelbrot.c
index 48d893d..fcbb765 100644
--- a/mandelbrot.c
+++ b/mandelbrot.c
@@ -3,6 +3,10 @@
#include <complex.h>
#include "header.h"
+#define AXIS_DIV 46.0
+#define REAL_AXIS_STEP ((REAL_HI - REAL_LO) / AXIS_DIV)
+#define IMAG_AXIS_STEP ((IMAG_HI - IMAG_LO) / AXIS_DIV)
+
#define IN_CHAR '*'
#define OUT_CHAR ' '
@@ -21,9 +25,9 @@ int mandelbrot_in_set(double complex c)
void mandelbrot_print(void)
{
- for (double i = LO; i < HI; i += AXIS_STEP)
+ for (double i = IMAG_LO; i < IMAG_HI; i += IMAG_AXIS_STEP)
{
- for (double r = LO; r < HI; r += AXIS_STEP)
+ for (double r = REAL_LO; r < REAL_HI; r += REAL_AXIS_STEP)
{
if (mandelbrot_in_set(r + i * I))
putchar(IN_CHAR);