diff --git a/src/field.c b/src/field.c index fd63ede9c7bb618343e522c53f50a7eb2b48604b..6e6f104979b7a24c8581372241998bb504e64858 100644 --- a/src/field.c +++ b/src/field.c @@ -40,38 +40,48 @@ double compute_delta_x(){ return 1 / result; } +bool is_in_screen(coordinates_t pos){ + if(pos.column > WID || pos.column < 0) + return false; + + if(pos.row > HEI || pos.row < 0) + return false; + + return true; +} + // Compute and then draw all the points belonging to a field line, // starting from pos0. // Returns false if pos0 is not a valid position // (for example if pos0 is too close to a charge). static bool draw_field_line(struct gfx_context_t *ctxt, charge_t *charges, int num_charges, double dx, vec2 pos0, double x0, double x1, double y0, double y1) { vec2 pos = vec2_create(pos0.x, pos0.y); - 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 - uint32_t color = COLOR_YELLOW; - for (int i = 0; i < 100 && !stop; i++) + for (int i = 0; i < 300 && !stop; i++) { 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); + 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); + 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; } }