From a26003e4abb3e7712fef9a88cba3aae6a045b4a1 Mon Sep 17 00:00:00 2001
From: Boris Stefanovic <owldev@bluewin.ch>
Date: Mon, 9 May 2022 11:46:48 +0200
Subject: [PATCH] ADD: draw_everything(...)

---
 src/field.c | 33 ++++++++++++++++++++++++++-------
 src/field.h |  9 +++++++++
 src/main.c  | 12 ++++++------
 3 files changed, 41 insertions(+), 13 deletions(-)

diff --git a/src/field.c b/src/field.c
index b0bbd1d..173d618 100644
--- a/src/field.c
+++ b/src/field.c
@@ -1,13 +1,14 @@
-#include <math.h>
 #include <stdbool.h>
 #include <stdlib.h>
 
+#include "draw.h"
 #include "field.h"
-#include "../utils/utils.h"
+
 
 #define SIGN_SIZE 10
 #define CHARGE_R 25
 
+
 // Compute E*qP/norm(qP)
 // Return false if norm(qP) < eps
 /// qP = vectoriel(P-q)
@@ -38,22 +39,25 @@ bool compute_total_normalized_e(charge_t *charges, int num_charges, vec2 p, doub
 // 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) {
+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) {
 	return EXIT_SUCCESS;
 }
 
 // Draw all the charges
 // A circle with minus sign for negative charges
 // A circle with a plus sign for positive charges
-static void draw_charges(struct gfx_context_t *context, charge_t *charges, int num_charges, double x0, double x1, double y0, double y1) {
-	for (int i = 0; i < num_charges; i++)
-	{
+static void
+draw_charges(struct gfx_context_t *context, charge_t *charges, int num_charges, double x0, double x1, double y0,
+			 double y1) {
+	for (int i = 0; i < num_charges; i++) {
 		coordinates_t charge_center = position_to_coordinates(CHARGE_R, CHARGE_R, x0, x1, y0, y1, charges[i].pos);
 		gfx_draw_circle(context, charge_center, CHARGE_R, COLOR_WHITE);
 
 		coordinates_t sign_dst;
 		uint32_t sign_color = COLOR_RED;
-		if (charges[i].q > 0){
+		if (charges[i].q > 0) {
 			sign_color = COLOR_BLUE;
 			sign_dst.row = charge_center.row + SIGN_SIZE;
 			gfx_draw_line(context, charge_center, sign_dst, sign_color);
@@ -67,3 +71,18 @@ static void draw_charges(struct gfx_context_t *context, charge_t *charges, int n
 		gfx_draw_line(context, charge_center, sign_dst, sign_color);
 	}
 }
+
+void draw_everything(
+		struct gfx_context_t *ctxt,
+		charge_t *charges,
+		int num_charges,
+		int num_lines,
+		double dx,
+		double x0, double x1,
+		double y0, double y1) {
+	draw_charges(ctxt, charges, num_charges, x0, x1, y0, y1);
+	for (int i = 0; i < num_lines; ++i) {
+		vec2 pos0 = vec2_normalize(vec2_create(rand(), rand()));
+		draw_field_line(ctxt, charges, num_charges, dx, pos0, x0, x1, y0, y1);
+	}
+}
diff --git a/src/field.h b/src/field.h
index 45c00f3..89f707c 100644
--- a/src/field.h
+++ b/src/field.h
@@ -29,4 +29,13 @@ static void
 draw_charges(struct gfx_context_t *context, charge_t *charges, int num_charges, double x0, double x1, double y0,
 			 double y1);
 
+void draw_everything(
+		struct gfx_context_t *ctxt,
+		charge_t *charges,
+		int num_charges,
+		int num_lines,
+		double dx,
+		double x0, double x1,
+		double y0, double y1);
+
 #endif
diff --git a/src/main.c b/src/main.c
index 2953200..0cbdf1a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,14 +1,18 @@
 #include <stdlib.h>
 #include <time.h>
+
 #include "draw.h"
+#include "field.h"
 #include "../utils/utils.h"
 
+
 #define SIDE_LEN 1000
 #define WID SIDE_LEN
 #define HEI WID
 #define NCHARGES 2
 #define DX 0.0005
-#define NPOINTS 32
+#define NLINES 32
+
 
 int main() {
 	srand(time(NULL));
@@ -17,11 +21,7 @@ int main() {
 			{.q=0.25, .pos=vec2_create(0.75, 0.5)},
 	};
 	struct gfx_context_t *ctxt = gfx_create("elec", WID, HEI);
-	draw_charges(ctxt, charges, NCHARGES, 0.0, 1.0, 0.0, 1.0);
-	for (int i = 0; i < NPOINTS; ++i) {
-		vec2 start = vec2_normalize(vec2_create(rand(), rand()));
-		draw_field_line(ctxt, charges, NCHARGES, DX, start, 0.0, 1.0, 0.0, 1.0);
-	}
+	draw_everything(ctxt, charges, NCHARGES, NLINES, DX, 0.0, 1.0, 0.0, 1.0);
 	while (gfx_keypressed() != SDLK_ESCAPE) gfx_present(ctxt);
 	gfx_destroy(ctxt);
 	return EXIT_SUCCESS;
-- 
GitLab