diff --git a/src/field.c b/src/field.c index 65cb90e13a16c3c83105da75057661b7f62c28a0..fd63ede9c7bb618343e522c53f50a7eb2b48604b 100644 --- a/src/field.c +++ b/src/field.c @@ -49,23 +49,30 @@ static bool draw_field_line(struct gfx_context_t *ctxt, charge_t *charges, int n vec2 pos_sum; vec2 pos_next; vec2 e; + bool stop = false; double delta = compute_delta_x(); // Remplacer par une boucle qui s'arrĂȘte lorsqu'on atteint une charge - for (int i = 0; i < 100; i++) + uint32_t color = COLOR_YELLOW; + for (int i = 0; i < 100 && !stop; i++) { - compute_total_normalized_e(charges, num_charges, pos, 0.01, &e); - pos_next.x = pos.x + delta * (e.x / vec2_normalize(e).x); - pos_next.y = pos.y + delta * (e.y / vec2_normalize(e).y); + for (int j = 0; j < num_charges && !stop; j++){ + if(vec2_norm(vec2_sub(pos, charges[j].pos)) < dx / HEI){ + color = COLOR_RED; + stop = true; + } + } + + if(!stop){ + compute_total_normalized_e(charges, num_charges, pos, 0.01, &e); + pos_next.x = pos.x + delta * (e.x / vec2_normalize(e).x); + pos_next.y = pos.y + delta * (e.y / vec2_normalize(e).y); - //printf("%f + %f * (%f / %f)\n", pos.x, delta, e.x, vec2_normalize(e).x); - //printf("%f %f\n", pos_next.x, pos_next.y); + 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); - 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); - //printf("%d %d\n", charge_1.column, charge_1.row); - //printf("%d %d\n\n", charge_2.column, charge_2.row); - gfx_draw_line(ctxt, coordinate_pos, coordinate_pos_next, COLOR_YELLOW); - pos = pos_next; + gfx_draw_line(ctxt, coordinate_pos, coordinate_pos_next, color); + pos = pos_next; + } } return EXIT_SUCCESS; @@ -109,7 +116,7 @@ void draw_everything( srand(time(NULL)); draw_charges(ctxt, charges, num_charges, x0, x1, y0, y1); for (int i = 0; i < num_lines; ++i) { - vec2 pos0 = vec2_create((double)rand() / RAND_MAX, (double)rand() / RAND_MAX); + vec2 pos0 = vec2_create(rand_one(), rand_one()); draw_field_line(ctxt, charges, num_charges, dx, pos0, x0, x1, y0, y1); } } diff --git a/src/main.c b/src/main.c index af327616c08c705db7b12bd0a3f97c01a1cb7bf9..5d2623134322509e7c3796716da5e3bccef53bf8 100644 --- a/src/main.c +++ b/src/main.c @@ -7,14 +7,14 @@ #include "field.h" #define NCHARGES 2 -#define DX 0.0005 +#define DX 25 #define NLINES 32 int main() { charge_t charges[NCHARGES] = { - {.q=-0.25, .pos=vec2_create(0.25, 0.5)}, - {.q=0.25, .pos=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);