diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-08-28 13:57:11 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-08-28 13:57:11 +0200 |
| commit | df6e7cbaa0edf2df8b5f3929a2eb34fa2aa5a28c (patch) | |
| tree | b27d14df93691892c8e906435b8c4ee9ac46b5bc | |
| parent | ff61da6fb9720106d7869a0e5026810450d8f515 (diff) | |
| download | mandelbrot_cpu-df6e7cbaa0edf2df8b5f3929a2eb34fa2aa5a28c.tar.gz mandelbrot_cpu-df6e7cbaa0edf2df8b5f3929a2eb34fa2aa5a28c.tar.bz2 mandelbrot_cpu-df6e7cbaa0edf2df8b5f3929a2eb34fa2aa5a28c.zip | |
Proportionnal movement and zoom
Zooming and moving is proportionnal to the range we are viewing,
feels more natural than incrementing by a constant value.
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | graphics.c | 53 | ||||
| -rw-r--r-- | header.h | 21 | ||||
| -rw-r--r-- | mandelbrot.c | 12 |
4 files changed, 47 insertions, 43 deletions
@@ -12,10 +12,10 @@ RM = rm -f .PHONY: all all: $(NAME) -$(NAME): $(OBJ) $(HEADER) +$(NAME): $(OBJ) $(CC) $(LDFLAGS) $(CCFLAGS) -o $@ $(OBJ) -%.o: %.c +%.o: %.c $(HEADER) $(CC) $(LDFLAGS) $(CCFLAGS) -c -o $@ $< .PHONY: debug @@ -6,11 +6,15 @@ #define WINDOW_TITLE "Mandelbrot" #define WINDOW_X 20 #define WINDOW_Y 20 -#define WINDOW_W 500 -#define WINDOW_H 500 +#define WINDOW_W 300 +#define WINDOW_H 300 #define REFRESH_RATE 1 -#define MOVE_SIZE 0.1 -#define ZOOM_SIZE 0.1 +#define MOVE_RATIO 10 +#define ZOOM_RATIO 1.1 +#define REAL_LO(state) (state->center.x - state->real_range / 2) +#define REAL_HI(state) (state->center.x + state->real_range / 2) +#define IMAG_LO(state) (state->center.y - state->imag_range / 2) +#define IMAG_HI(state) (state->center.y + state->imag_range / 2) #define RED_MASK 0xFF0000 #define GREEN_MASK 0x00FF00 @@ -19,8 +23,8 @@ #define GREEN(c) ((c & GREEN_MASK) >> 8) #define BLUE(c) (c & BLUE_MASK) #define MERGE_RGB(r, g, b) ((r) << (8 * 2) | (g) << 8 | (b)) -#define PALETTE_START 0x334455 -#define PALETTE_END 0xFFFFFF +#define PALETTE_START 0x000022 +#define PALETTE_END 0xd62f2f #define SET_DRAW_COLOR_RGB(renderer, c) ( \ SDL_SetRenderDrawColor(renderer, RED(c), GREEN(c), BLUE(c), SDL_ALPHA_OPAQUE)) #define IN_SET_COLOR 0x000000 @@ -51,10 +55,10 @@ GState *graphics_init(void) 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; + state->real_range = REAL_RANGE; + state->imag_range = IMAG_RANGE; + state->center.x = CENTER_X; + state->center.y = CENTER_Y; return state; } @@ -83,8 +87,8 @@ static void update(GState *state) { for (int y = 0; y < state->window_h; y++) { - 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); + a = map_range((double)x, 0, state->window_w, REAL_LO(state), REAL_HI(state)); + b = map_range((double)y, 0, state->window_h, IMAG_LO(state), IMAG_HI(state)); double complex mapped_z = a + b * I; int steps = mandelbrot_in_set(mapped_z); if (steps == -1) @@ -111,41 +115,32 @@ static void event_handler(GState *state) { case SDLK_UP: case SDLK_k: - state->imag_lo -= MOVE_SIZE; - state->imag_hi -= MOVE_SIZE; + state->center.y -= state->imag_range / MOVE_RATIO; break; case SDLK_DOWN: case SDLK_j: - state->imag_lo += MOVE_SIZE; - state->imag_hi += MOVE_SIZE; + state->center.y += state->imag_range / MOVE_RATIO; break; case SDLK_LEFT: case SDLK_h: - state->real_lo -= MOVE_SIZE; - state->real_hi -= MOVE_SIZE; + state->center.x -= state->real_range / MOVE_RATIO; break; case SDLK_RIGHT: case SDLK_l: - state->real_lo += MOVE_SIZE; - state->real_hi += MOVE_SIZE; + state->center.x += state->real_range / MOVE_RATIO; break; case SDLK_PLUS: case SDLK_p: - state->real_lo += ZOOM_SIZE; - state->real_hi -= ZOOM_SIZE; - state->imag_lo += ZOOM_SIZE; - state->imag_hi -= ZOOM_SIZE; + state->real_range /= ZOOM_RATIO; + state->imag_range /= ZOOM_RATIO; break; case SDLK_MINUS: case SDLK_m: - state->real_lo -= ZOOM_SIZE; - state->real_hi += ZOOM_SIZE; - state->imag_lo -= ZOOM_SIZE; - state->imag_hi += ZOOM_SIZE; + state->real_range *= ZOOM_RATIO; + state->imag_range *= ZOOM_RATIO; break; case SDLK_q: state->running = false; - break; } } } @@ -5,10 +5,10 @@ # include <complex.h> # include <SDL2/SDL.h> -# define REAL_LO -2.0 -# define REAL_HI 2.0 -# define IMAG_LO -2.0 -# define IMAG_HI 2.0 +# define CENTER_X 0.0 +# define CENTER_Y 0.0 +# define REAL_RANGE 4.0 +# define IMAG_RANGE 4.0 # define MAX_ITERATION 35 # define ESCAPE_VALUE 2 @@ -19,15 +19,20 @@ typedef int Color; typedef struct { + double x; + double y; +} Point; + +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; + Point center; + double real_range; + double imag_range; Color *palette; } GState; diff --git a/mandelbrot.c b/mandelbrot.c index 51d6648..d44b12b 100644 --- a/mandelbrot.c +++ b/mandelbrot.c @@ -3,9 +3,13 @@ #include <complex.h> #include "header.h" +#define PRINT_REAL_LO -2.0 +#define PRINT_REAL_HI 2.0 +#define PRINT_IMAG_LO -2.0 +#define PRINT_IMAG_HI 2.0 #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 REAL_AXIS_STEP ((PRINT_REAL_HI - PRINT_REAL_LO) / AXIS_DIV) +#define IMAG_AXIS_STEP ((PRINT_IMAG_HI - PRINT_IMAG_LO) / AXIS_DIV) #define IN_CHAR '*' #define OUT_CHAR ' ' @@ -25,9 +29,9 @@ int mandelbrot_in_set(double complex c) void mandelbrot_print(void) { - for (double i = IMAG_LO; i < IMAG_HI; i += IMAG_AXIS_STEP) + for (double i = PRINT_IMAG_LO; i < PRINT_IMAG_HI; i += IMAG_AXIS_STEP) { - for (double r = REAL_LO; r < REAL_HI; r += REAL_AXIS_STEP) + for (double r = PRINT_REAL_LO; r < PRINT_REAL_HI; r += REAL_AXIS_STEP) { if (mandelbrot_in_set(r + i * I) == -1) putchar(IN_CHAR); |
