From 76e544a8e4f805bfdac1305f39f1ed48edbff3f1 Mon Sep 17 00:00:00 2001 From: JM <crewgan@pop-os.localdomain> Date: Tue, 17 May 2022 19:57:15 +0200 Subject: [PATCH] Programme fonctionnel --- src/field.c | 32 +++++++++++++++++++++++--------- src/main.c | 9 +++++---- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/field.c b/src/field.c index 1e0d717..356c8db 100644 --- a/src/field.c +++ b/src/field.c @@ -55,11 +55,19 @@ bool draw_field_line_point(struct gfx_context_t *ctxt, charge_t *charges, int nu compute_total_normalized_e(charges, num_charges, pos, 0.01, &e); double norm_e = vec2_norm(e); + // Problème ici pos_next->x = pos.x + delta * (e.x / norm_e); pos_next->y = pos.y + delta * (e.y / norm_e); + + if(pos_next->x <= 0 || pos_next->x >= 1 || pos_next->y <= 0 || pos_next->y >= 1) + return false; + coordinates_t coordinate_pos = position_to_coordinates(WID, HEI, x0, x1, y0, y1, pos); coordinates_t coordinate_pos_next = position_to_coordinates(WID, HEI, x0, x1, y0, y1, *pos_next); + if(coordinate_pos_next.column <= 0 || coordinate_pos_next.row <= 0) + return false; + gfx_draw_line(ctxt, coordinate_pos, coordinate_pos_next, COLOR_YELLOW); return !is_in_screen(coordinate_pos); @@ -84,22 +92,28 @@ static bool draw_field_line(struct gfx_context_t *ctxt, charge_t *charges, int n vec2 pos_negative = vec2_create(pos0.x, pos0.y); vec2 pos_next_positive; vec2 pos_next_negative; - bool stop = false; + bool stop_positive = false; + bool stop_negative = false; // * 5 à supprimer lorsque le code est optimisé - double delta = compute_delta_x() * 5; - int max_for = 50; - for (int i = 0; i < max_for && !stop; i++) + double delta = compute_delta_x(); + int max_for = 100000; + for (int i = 0; i < max_for && (!stop_positive || !stop_negative); i++) { - stop = line_reach_charge(pos_positive, charges, num_charges, dx) || line_reach_charge(pos_negative, charges, num_charges, dx); - if(!stop){ - stop = draw_field_line_point(ctxt, charges, num_charges, x0, x1, y0, y1, pos_positive, &pos_next_positive, delta) || - draw_field_line_point(ctxt, charges, num_charges, x0, x1, y0, y1, pos_negative, &pos_next_negative, -delta); + stop_positive = line_reach_charge(pos_positive, charges, num_charges, dx); + stop_negative = line_reach_charge(pos_negative, charges, num_charges, dx); + + if(!stop_positive){ + stop_positive = draw_field_line_point(ctxt, charges, num_charges, x0, x1, y0, y1, pos_positive, &pos_next_positive, delta); pos_positive = pos_next_positive; + } + + if(!stop_negative){ + stop_negative = draw_field_line_point(ctxt, charges, num_charges, x0, x1, y0, y1, pos_negative, &pos_next_negative, -delta); pos_negative = pos_next_negative; } } - return !stop; + return !stop_positive || !stop_negative; } // Draw all the charges diff --git a/src/main.c b/src/main.c index c9d41ed..636c039 100644 --- a/src/main.c +++ b/src/main.c @@ -8,19 +8,20 @@ #define NCHARGES 2 #define DX 25 -#define NLINES 5 +#define NLINES 50 int main() { charge_t charges[NCHARGES] = { - charge_create(-0.25, vec2_create(0.25, 0.5)), - charge_create(0.25, vec2_create(0.75, 0.5)) + charge_create(0.25, vec2_create(0.25, 0.5)), + charge_create(-0.25, vec2_create(0.75, 0.5)) }; struct gfx_context_t *ctxt = gfx_create("elec", WID, HEI); draw_everything(ctxt, charges, NCHARGES, NLINES, DX, 0.0, 1.0, 0.0, 1.0); + gfx_present(ctxt); - while (gfx_keypressed() != SDLK_ESCAPE) gfx_present(ctxt); + while (gfx_keypressed() != SDLK_ESCAPE); gfx_destroy(ctxt); return EXIT_SUCCESS; } -- GitLab