diff --git a/src/field.c b/src/field.c index 6e6f104979b7a24c8581372241998bb504e64858..89a3483228263ad0a7a2cd3a716795bec47f21a1 100644 --- a/src/field.c +++ b/src/field.c @@ -59,8 +59,9 @@ static bool draw_field_line(struct gfx_context_t *ctxt, charge_t *charges, int n vec2 pos_next; vec2 e; bool stop = false; - double delta = compute_delta_x(); - for (int i = 0; i < 300 && !stop; i++) + double delta = compute_delta_x() * 5; + int max_for = 300; + for (int i = 0; i < max_for && !stop; i++) { for (int j = 0; j < num_charges && !stop; j++){ if(vec2_norm(vec2_sub(pos, charges[j].pos)) < dx / HEI){ @@ -79,12 +80,36 @@ static bool draw_field_line(struct gfx_context_t *ctxt, charge_t *charges, int n gfx_draw_line(ctxt, coordinate_pos, coordinate_pos_next, COLOR_YELLOW); pos = pos_next; - printf("F\n"); - //if(!is_in_screen(coordinate_pos)) - // stop = false; + if(!is_in_screen(coordinate_pos)) + stop = false; } } - + pos = vec2_create(pos0.x, pos0.y); + stop = false; + for (int i = 0; i < max_for && !stop; i++) + { + for (int j = 0; j < num_charges && !stop; j++){ + if(vec2_norm(vec2_sub(pos, charges[j].pos)) < dx / HEI){ + stop = true; + } + } + + if(!stop){ + compute_total_normalized_e(charges, num_charges, pos, 0.01, &e); + double norm_e = vec2_norm(e); + pos_next.x = pos.x - delta * (e.x / norm_e); + pos_next.y = pos.y - delta * (e.y / norm_e); + + 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); + + gfx_draw_line(ctxt, coordinate_pos, coordinate_pos_next, COLOR_YELLOW); + pos = pos_next; + if(!is_in_screen(coordinate_pos)) + stop = false; + } + } + return EXIT_SUCCESS; } diff --git a/src/main.c b/src/main.c index 5d2623134322509e7c3796716da5e3bccef53bf8..c9d41edbc2f42113f671a8d58d2988a30befb50d 100644 --- a/src/main.c +++ b/src/main.c @@ -8,7 +8,7 @@ #define NCHARGES 2 #define DX 25 -#define NLINES 32 +#define NLINES 5 int main() {