DEVELOPMENT ENVIRONMENT

~alex/mandelbrot-set-gen

797e30a9b3b99cce8c6da581457fa599c359b26f — Alejandro Laguna a month ago bf35da7
feat: show values on text
3 files changed, 28 insertions(+), 6 deletions(-)

M include/mandelbrot.h
M src/main.c
M src/math.c
M include/mandelbrot.h => include/mandelbrot.h +1 -1
@@ 21,11 21,11 @@ typedef struct position_s {
// math.c
int mandelbrot_iterations(double cr, double ci, int max_iter);
void calculate_points(position_t* pos);
void mandelbrot_at_point(double cr, double ci, int* iter_out, double* zr_out, double* zi_out);

// draw.c
// TODO: color struct or something
void get_color(int iter, int max_iter, int* r, int* g, int* b);
void draw_points(SDL_Surface* surface);


#endif

M src/main.c => src/main.c +9 -5
@@ 101,11 101,15 @@ int main(void) {
                }
                float mx, my;
                SDL_GetMouseState(&mx, &my);
                int ix = (int)floor(mx);
                int iy = (int)floor(my);
                int iter = points[iy * WINDOW_WIDTH + ix];
                snprintf(debug_text, sizeof(debug_text), "x: %d  y: %d  iterations: %d", ix, iy,
                         iter);
                double cr =
                    position.x_min + (position.x_max - position.x_min) * mx / (WINDOW_WIDTH - 1);
                double ci =
                    position.y_min + (position.y_max - position.y_min) * my / (WINDOW_HEIGHT - 1);
                int iter;
                double zr, zi;
                mandelbrot_at_point(cr, ci, &iter, &zr, &zi);
                snprintf(debug_text, sizeof(debug_text),
                         "c = %.6f %+.6fi | z = %.6f %+.6fi | iteration %d", cr, ci, zr, zi, iter);
            }

            if (e.type == SDL_EVENT_MOUSE_MOTION) {

M src/math.c => src/math.c +18 -0
@@ 35,6 35,24 @@ int mandelbrot_iterations(double cr, double ci, int max_iter) {

extern int points[WINDOW_WIDTH * WINDOW_HEIGHT];

void mandelbrot_at_point(double cr, double ci, int* iter_out, double* zr_out, double* zi_out) {
    double zr = 0.0, zi = 0.0;
    int iter = 0;
    while (iter < 100) {
        double zr2 = zr * zr;
        double zi2 = zi * zi;
        if (zr2 + zi2 > 4.0) {
            break;
        }
        zi = 2.0 * zr * zi + ci;
        zr = zr2 - zi2 + cr;
        iter++;
    }
    *iter_out = iter;
    *zr_out = zr;
    *zi_out = zi;
}

void calculate_points(position_t* pos) {
    int max_iter = 100;
    for (int py = 0; py < WINDOW_HEIGHT; py++) {