diff --git a/src/Simulation.c b/src/Simulation.c index 3e31199a0626745a074d49c15b60143ecccb157a..d97a8cb049b5a1cea70d9483dc2b7ef5ef43ee52 100644 --- a/src/Simulation.c +++ b/src/Simulation.c @@ -44,6 +44,7 @@ static Charge generate_random_charge() { * @param charges_length The number of charges. * @return Charge* The charge array. */ +#include <stdio.h> static Charge *generate_random_charges(Rectangle *universe, int *charges_length) { *charges_length = random_number_between(MIN_CHARGES, MAX_CHARGES); Charge *charges = (Charge *)malloc(sizeof(Charge) * *charges_length); @@ -52,9 +53,11 @@ static Charge *generate_random_charges(Rectangle *universe, int *charges_length) while (true) { Charge charge = generate_random_charge(); + // Checks if the charge is not too close to the edge of the universe. Vector2 position = charge.position; double eps = RADIUS_TRESHOLD; if (is_out_of_bounds(universe, vector2_add(position, vector2_init(0, -eps)))) { + // Starts the loop again with a new random charge. continue; } if (is_out_of_bounds(universe, vector2_add(position, vector2_init(eps, 0)))) { @@ -67,8 +70,19 @@ static Charge *generate_random_charges(Rectangle *universe, int *charges_length) continue; } - // TODO : avoid two charge to bee to close. - // TODO : eps as const + bool charges_are_too_close = false; + for (int j = 0; j < i; j++) { + double norm = vector2_norm(vector2_substract(position, charges[j].position)); + if (norm < RADIUS_TRESHOLD * 3) { + // The two charges are too close. + charges_are_too_close = true; + } + } + + if (charges_are_too_close) { + // Starts the loop again with a new random charge. + continue; + } charges[i] = charge; break; @@ -95,7 +109,7 @@ static void draw_charges(Simulation *simulation, Graphics *graphics) { * * @param charge The charge. * @param point The point - * @param eps + * @param eps Minimum distance between a point and the position of a charge. * @param e The electric field. * @return true The electric field is valid. * @return false The electric field is invalid. @@ -118,7 +132,7 @@ static bool compute_e(Charge charge, Vector2 point, double eps, Vector2 *e) { * @param charges_length The number of charges. * @param charges The charge array. * @param point The point. - * @param eps + * @param eps Minimum distance between a point and the position of a charge. * @param e The electric field. * @return true The electric field is valid. * @return false The electric field is invalid. @@ -149,7 +163,7 @@ static bool is_out_of_bounds(Rectangle *universe, Vector2 point) { * @param simulation The simulation. * @param direction The direction in which we draw. * @param current_point The point from which the next point is calculated. - * @param eps + * @param eps Minimum distance between a point and the position of a charge. * @param next_point The next point. * @return true The point is valid. * @return false The point is invalid. diff --git a/src/constants.c b/src/constants.c index eb2f4c0827bf0f1af663e008343104a5484eff06..0c67568e2e804ad79be5abbb427eed131fa557ea 100644 --- a/src/constants.c +++ b/src/constants.c @@ -9,17 +9,13 @@ const int SCREEN_WIDTH = 750; const int SCREEN_HEIGHT = 750; - const int UNIVERSE_X0 = 0; const int UNIVERSE_Y0 = 0; const int UNIVERSE_X1 = 1; const int UNIVERSE_Y1 = 1; - const int CHARGE_CIRCLE_RADIUS = 20; - const int MIN_CHARGES = 2; const int MAX_CHARGES = 5; - const double K = 8.988e9; const double ELEMENTARY_CHARGE = 1.602e-19; // Represents the acceptable distance between the last point and the charge, this distance is calculated with diff --git a/src/constants.h b/src/constants.h index dcb6e23560917861b148e19366e219780d5cf54a..bf85dbbd9eb65438a3ef44e9e12ea77fbc022551 100644 --- a/src/constants.h +++ b/src/constants.h @@ -8,21 +8,29 @@ #ifndef CONSTANTS_H #define CONSTANTS_H +// Screen width in pixels. extern const int SCREEN_WIDTH; +// Screen height in pixels. extern const int SCREEN_HEIGHT; - +// The x-component of the universe at the top left. extern const int UNIVERSE_X0; +// The y-component of the universe at the top left. extern const int UNIVERSE_Y0; +// The x-component of the universe at the bottom right. extern const int UNIVERSE_X1; +// The y-component of the universe at the bottom right. extern const int UNIVERSE_Y1; - +// Radius of the circle of a charge. extern const int CHARGE_CIRCLE_RADIUS; - +// Minimum number of charges to generate. extern const int MIN_CHARGES; +// Maximum number of charges to generate. extern const int MAX_CHARGES; - +// Coulomb's constant. extern const double K; +// Value of the elementary charge. extern const double ELEMENTARY_CHARGE; +// Minimum distance between a point and the position of a charge. extern const double RADIUS_TRESHOLD; #endif diff --git a/src/main.c b/src/main.c index 0e872f69e7eededfb171f380ee68c9ef3be6a347..de0337b397b9e98343333790b12094bb3af9de24 100644 --- a/src/main.c +++ b/src/main.c @@ -38,11 +38,9 @@ double compute_delta_x(int width, int height) { int main(int UNUSED(argc), char *UNUSED(argv[])) { Graphics *graphics = gfx_create("Field Lines Simulation", SCREEN_WIDTH, SCREEN_HEIGHT); srand(time(NULL)); - // srand(10); - // srand(11); if (graphics == NULL) { - fprintf(stderr, "Graphic initialization failed!\n"); + fprintf(stderr, "Impossible to initialize the window.\n"); return EXIT_FAILURE; }