From 21f81f18a52b93d9aa941e9c655fc20e7e6df6ab Mon Sep 17 00:00:00 2001
From: JM <crewgan@pop-os.localdomain>
Date: Sun, 15 May 2022 19:06:42 +0200
Subject: [PATCH] draw_field_line fonctionnel mais pas beau et besoin
 d'optimisation

---
 src/field.c | 37 +++++++++++++++++++++++++++++++------
 src/main.c  |  2 +-
 2 files changed, 32 insertions(+), 7 deletions(-)

diff --git a/src/field.c b/src/field.c
index 6e6f104..89a3483 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 5d26231..c9d41ed 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() {
-- 
GitLab