1

I'm doing the Mandelbrot set in c as a school project.<br>

This is my code:

int mandelbrot(int x, int y, t_data *data)
{
    double complex  c;
    double complex  z;
    double          cx;
    double          cy;
    int             i;

    cx = map(x, 0, WIDTH, data->min,  data->max);
    cy = map(y, 0, HEIGHT, data->min ,  data->max);
    c = cx + cy * I;
    z = 0;
    i = 0;
    while (i < data->max_iteration)
    {
        z = z * z + c;
        if (creal(z) * creal(z) + cimag(z) * cimag(z) > 4)
            break;
        i++;
    }
    
    return (get_color(i, *data));
}

I used the map() function to convert the window coordinates to the complex coordinates and I used it to get the cursor coordinates in the complex space too:

double map(double value, double screen_min, double screen_max
                       , double image_min, double image_max)
{
    return ((value - screen_min ) \
            * (image_max - image_min) \
            / (screen_max - screen_min) \
            + image_min);
}

the data is a struct that has the variables I need for the mand:

typedef struct mouse
{
    double          cursor_cx; // cursor x coordinates in the complex plane
    double          cursor_cy; // cursor x coordinates in the complex plane
    double          min; // the minimum edge of the mand initialized to -2
    double          max; // initialized to 2
    int         max_iteration; // initialized to 100
    mlx_t           *mlx;  // the window I guess
    mlx_image_t     *g_img; // the img I'll draw my pixels to
}   t_data;

My problem now is moving the mandelbrot around and zooming in and out.<br>

1st: I managed to zoom in and out by changing the min and max:

void    zoom_hook(double xdelta, double ydelta, void *param)
{
        int32_t cursor_x = get_cursor_x();
    int32_t cursor_y = get_cursor_x();

    float   step;

    step = 0.1;
    t_data *data = param;
    (void)xdelta;
    if (ydelta > 0)
    {
        data->min += step;
        data->max -= step;
    }
    else if (ydelta < 0)
    {
        data->min -= step;
        data->max += step;
    }
}

but when I zoom in a few times the mand get's inverted and starts zooming out even tho I'm zooming in<br> that's because min is moving from -2 to 0 then if I zoom more then that from 0 to 2 and vise versa for max so I need a better way of zooming in, I'm expecting to be able to zoom in forever as long as it is within the limits of the computer. but right now my mand get's inverted before reaching that limit.

and more than that I can get the cursor x and y positions so I want to zoom in into those coordinates.

2nd: I need to move my `mand` and this is what I tried but it doesn't work It's zooming in and out but in diagonal way:

void    key_hook(void *param)
{
    t_data  *data;
    float   step;

    step = 0.1;
    data = param;
    if (mlx_is_key_down(data->mlx, MLX_KEY_ESCAPE))
        mlx_close_window(data->mlx);
    if (mlx_is_key_down(data->mlx, MLX_KEY_RIGHT))
        data->min -= step;
    if (mlx_is_key_down(data->mlx, MLX_KEY_LEFT))
        data->min += step;
    if (mlx_is_key_down(data->mlx, MLX_KEY_UP))
        data->max += step;
    if (mlx_is_key_down(data->mlx, MLX_KEY_DOWN))
        data->max -= step;
}

I'm expecting it to move to the directions right, left, up and down.<br> So I need some helpt to move and zoom my mandelbrot set.

Thanks in advance.

  • Welcome to SO. What values do `min` and `max` hold at start and when you get problems? If you want to move around, don't you need to adjust `min` and `max` at the same time? Also, as you only seem to have one `min` and one `max` value, what does moving left/right and up/down mean? – Gerhardh Dec 12 '22 at 10:45
  • I already commented my struct with some values to initialize it. min will hold -2 and max hold 2. when I get the problem that's already explained. when I zoom in min and max will get close to 0 but after they reach 0 they get inverted because min was negative now it's getting bigger till it reachs 2 vice versa for max it was 2 now it's reaching -2 – Bradass_FaLLaG Dec 12 '22 at 11:19
  • 1
    You could simply reduce your step according to your range. `step = 0.1; while (max-min < 2*step) step /= 10;` This scales down if the limits get close to each other. – Gerhardh Dec 12 '22 at 11:31

0 Answers0