diff --git a/src/draw_tests.c b/src/draw_tests.c index 4eba0e1356e801c294ba359993e4c4c4777e40bd..5ca5e33d89f873eb40b6cd42d0a6b56eb97794f7 100644 --- a/src/draw_tests.c +++ b/src/draw_tests.c @@ -52,5 +52,6 @@ int main() { gfx_present(ctxt); } + gfx_destroy(ctxt); return EXIT_SUCCESS; } diff --git a/src/field.c b/src/field.c index 95f80f08c84b4f4223d977ed7ba0267ce719b18d..688c6b90d084697f5846818c7f583dbad885744e 100644 --- a/src/field.c +++ b/src/field.c @@ -6,7 +6,6 @@ #define SIGN_SIZE 10 #define CHARGE_R 25 - bool compute_e(charge_t c, vec2 p, double eps, vec2 *e) { vec2 relative_position = vec2_sub(p, c.pos); if (vec2_norm(relative_position) < eps) return false; @@ -33,10 +32,22 @@ double compute_delta_x() { return 1 / result; } +// Vérifie si la position se trouve sur l'écran (entre 0 et HEI-1 ou WID-1) bool is_in_screen(coordinates_t pos) { return pos.column < WID && pos.row < HEI; } +// Si la valeur sort de l'écran, force sa position à la limite la plus proche (-10 -> 0) +void force_in_screen(coordinates_t *pos){ + if (!is_in_screen(*pos)){ + pos->column = (int)pos->column <= 0 ? 0 : pos->column; + pos->column = (int)pos->column >= WID ? WID-1 : pos->column; + + pos->row = (int)pos->row <= 0 ? 0 : pos->row; + pos->row = (int)pos->row >= HEI ? HEI-1 : pos->row; + } +} + bool draw_field_line_point(struct gfx_context_t *ctxt, charge_t *charges, int num_charges, double x0, double x1, double y0, double y1, vec2 pos, vec2 *pos_next, double delta) { vec2 e; compute_total_normalized_e(charges, num_charges, pos, 0.01, &e); @@ -66,7 +77,6 @@ bool line_reach_charge(vec2 pos, charge_t *charges, int num_charges, double dx) return false; } - /// Compute and then draw all the points belonging to a field line, /// starting from pos0. /// Returns false if pos0 is not a valid position @@ -78,7 +88,7 @@ static bool draw_field_line(struct gfx_context_t *ctxt, charge_t *charges, int n vec2 pos_next_negative; bool stop_positive = false; bool stop_negative = false; - // * 5 à supprimer lorsque le code est optimisé + double delta = compute_delta_x(); int max_for = 100000; for (int i = 0; i < max_for && (!stop_positive || !stop_negative); i++) { @@ -99,7 +109,6 @@ static bool draw_field_line(struct gfx_context_t *ctxt, charge_t *charges, int n return !stop_positive || !stop_negative; } - /// Draw all the charges /// A circle with minus sign for negative charges /// A circle with a plus sign for positive charges @@ -107,7 +116,6 @@ static void draw_charges(struct gfx_context_t *context, charge_t *charges, int n for (int i = 0; i < num_charges; ++i) { coordinates_t charge_center = position_to_coordinates(WID, HEI, x0, x1, y0, y1, charges[i].pos); gfx_draw_circle(context, charge_center, CHARGE_R, COLOR_WHITE); - coordinates_t sign_src = charge_center; coordinates_t sign_dst = charge_center; uint32_t sign_color = COLOR_RED; @@ -119,9 +127,12 @@ static void draw_charges(struct gfx_context_t *context, charge_t *charges, int n sign_src.row = charge_center.row; sign_dst.row = charge_center.row; } - sign_src.column -= SIGN_SIZE; sign_dst.column += SIGN_SIZE; + + force_in_screen(&sign_src); + force_in_screen(&sign_dst); + gfx_draw_line(context, sign_src, sign_dst, sign_color); } } diff --git a/src/main.c b/src/main.c index 9c5e5a15d0b75e86d300438ac2d8000b8e3a6bde..9e31eb96b9449795f41aaf499e23755fc6020992 100644 --- a/src/main.c +++ b/src/main.c @@ -1,29 +1,11 @@ #include <stdlib.h> #include "field.h" - -#define NCHARGES 2 #define NC 3 #define DX 25 #define NLINES 50 - -void main_old() { - charge_t charges[NCHARGES] = { - 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_destroy(ctxt); -} - - -void main_new() { +int main() { charge_t charges[NC]; for (int i = 0; i < NC; ++i) charges[i] = charge_create(i % 2 == 0 ? rand_one() : -rand_one(), vec2_create(rand_one(), rand_one())); @@ -31,14 +13,8 @@ void main_new() { draw_everything(ctxt, charges, NC, 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); -} - -int main() { - main_new(); return EXIT_SUCCESS; }