Reviews 7

  • STOP adding executable files to the repository
/**
 * @brief A struct to pass arguments to the mandelbrot_thread function
 * 
 */
typedef struct {
    bool **grid;
    int width;
    int height;
    int start_x;
    int start_y;
    double min_x;
    double max_x;
    double min_y; 
    double max_y;
} thread_args;

/**
 * @brief Calculate the mandelbrot set for sub-grid of pixels
 * 
 * @param args The sub-section of the grid to calculate
 * @return void* NULL
 */
void *mandelbrot_thread(void *args);

/**
 * @brief Use multiple threads to calculate the mandelbrot set for a grid of pixels
 * 
 * @param grid The grid of boolean values to store the mandelbrot set
 * @param width The width of the grid
 * @param height The height of the grid
 * @param min_x The minimum x value of the screen
 * @param max_x The maximum x value of the screen
 * @param min_y The minimum y value of the screen
 * @param max_y The maximum y value of the screen
 * @param num_threads The number of threads to use
 */
void mandelbrot_grid(bool **grid, int width, int height, double min_x, double max_x, double min_y, double max_y, int num_threads);
/**
 * @brief Create a grid object
 * 
 * @param width The width of the grid
 * @param height The height of the grid
 * @return The grid created
 */
bool** create_grid(int width, int height) {
    bool **grid = malloc(width * sizeof(bool *));
    if (!grid) {
        perror("Erreur d'allocation de mémoire !\n");
        exit(1);
    }
    for (int i = 0; i < width; i++) {
        grid[i] = calloc(height, sizeof(bool));
        if (!grid[i]) {
            perror("Erreur d'allocation de mémoire !\n");
            exit(1);
        }
    }
    return grid;
}
#include <raylib.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <complex.h>
#include <math.h>

#define MAX_ITERATIONS 100
#define WIDTH 800
#define HEIGHT 450

typedef struct {
    int x;
    int y;
} pos;

double module(double complex z){
    return creal(z)*creal(z) + cimag(z)*cimag(z);
}

bool convergence_pixel(double complex c){
    double complex z = 0;

    int nb_iterations = 0;

    while (nb_iterations < MAX_ITERATIONS && module(z) <= 4){
        z = z*z + c;
        nb_iterations++;
    }
    if (nb_iterations == MAX_ITERATIONS){
        return true;
    }else{
        return false;
    }

}

double complex coordonnees(int x, int y, double abscisse, double ordonnee, double abscisse_offset, double ordonnee_offset){
    double Zx = x * (abscisse/WIDTH) - (abscisse)/2 + abscisse_offset;
    double Zy = - y * ((ordonnee)/HEIGHT) + (ordonnee)/2 + ordonnee_offset;
    double complex z = Zx + I*Zy;
    return z;
}

void calcul_rectangle(double abscisse_init, double ordonnee_init, double abscisse_offset, double ordonnee_offset){
    ClearBackground(RAYWHITE);
    double abscisse = abscisse_init;
    double ordonnee = ordonnee_init;
    for (int i = 0; i < WIDTH; i++){
            for (int j = 0; j < HEIGHT; j++){
                if (convergence_pixel(coordonnees(i, j, abscisse, ordonnee, abscisse_offset, ordonnee_offset))){
                    DrawPixel(i, j, BLACK);
                }else{
                    DrawPixel(i, j, WHITE);
                }
            }
        }
        BeginDrawing();
        
        EndDrawing();
}

void handle_zoom(double* p_abscisse, double* p_ordonnee){
    double zoom = (double)GetMouseWheelMove();
        if (zoom > 0){
            *p_abscisse = *p_abscisse/(double)2;
            *p_ordonnee = *p_ordonnee/(double)2;
        }
        if (zoom < 0){
            *p_abscisse = *p_abscisse*2;
            *p_ordonnee = *p_ordonnee*2;
        }
}

void handle_slide(int* last_x_position, int* last_y_position, double* p_abscisse_offset, double* p_ordonnee_offset, double abscisse, double ordonnee){
    if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)){
        *p_abscisse_offset = *p_abscisse_offset + (GetMouseX() - *last_x_position)*0.01*abscisse;
        *p_ordonnee_offset = *p_ordonnee_offset - (GetMouseY() - *last_y_position)*0.01*ordonnee;
    }
    *last_x_position = GetMouseX();
    *last_y_position = GetMouseY();
}

void affiche_rectangle(double abscisse, double ordonnee, double abscisse_offset, double ordonnee_offset){
    InitWindow(WIDTH, HEIGHT, "mandelbrot");
    SetTargetFPS(50); 
    ClearBackground(RAYWHITE);
    int last_x_position = GetMouseX();
    int last_y_position = GetMouseY();
    while (!WindowShouldClose()){

        handle_zoom(&abscisse, &ordonnee);

        handle_slide(&last_x_position, &last_y_position, &abscisse_offset, &ordonnee_offset, abscisse, ordonnee);
        printf("%f, %f\n", abscisse_offset, ordonnee_offset);
        
        calcul_rectangle(abscisse, ordonnee, abscisse_offset, ordonnee_offset);
    }
    CloseWindow();
}

int main(){
    affiche_rectangle(4, 2, 0, 0);
    return 0;
}