From 4edafe06a469dd8c2af5214c4541c3ca291bbdf3 Mon Sep 17 00:00:00 2001
From: Boris Stefanovic <owldev@bluewin.ch>
Date: Mon, 2 May 2022 11:40:25 +0200
Subject: [PATCH] ADD: compute_e(...) and compute_total_normalized_e(...)

---
 src/field.c | 31 ++++++++++++++++++++++---------
 src/field.h |  9 ++++-----
 2 files changed, 26 insertions(+), 14 deletions(-)

diff --git a/src/field.c b/src/field.c
index 8bff078..0e2754b 100644
--- a/src/field.c
+++ b/src/field.c
@@ -1,31 +1,44 @@
-#include "physics.h"
 #include <math.h>
+#include <stdbool.h>
 #include <stdlib.h>
 
+#include "physics.h"
+
 
 // Compute E*qP/norm(qP)
 // Return false if norm(qP) < eps
-bool compute_e(charge_t c, vec2 p, double eps, vec2 *e){
- return EXIT_SUCCESS;
+/// qP = vectoriel(P-q)
+/// Compute the vector value of the field generated by a charge at a given point in space.
+bool compute_e(charge_t c, vec2 p, double eps, vec2 *e) {
+	vec2 relative_position = vec2_sub(p, c.pos);
+	if (vec2_norm(relative_position) < eps) return false;
+	double field = K * c.q / vec2_norm_sqr(relative_position);
+	*e = vec2_mul(field, vec2_normalize(relative_position));
+	return true;
 }
 
 // Compute the normalized sum of Ei*qiP/norm(qiP)
 // Return false if for some qiP, norm(qiP) < eps
-bool compute_total_normalized_e(charge_t *charges, int num_charges, vec2 p, double eps, vec2 *e){
- return EXIT_SUCCESS;
+bool compute_total_normalized_e(charge_t *charges, int num_charges, vec2 p, double eps, vec2 *e) {
+	*e = vec2_create_zero();
+	vec2 ei = vec2_create_zero();
+	for (size_t i = 0; i < num_charges; ++i) {
+		if (!compute_e(charges[i], p, eps, &ei)) return false;
+		*e = vec2_add(acc, ei);
+	}
+	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){
- return EXIT_SUCCESS;
+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){
-
+static void draw_charges(struct gfx_context_t *context, charge_t *charges, int num_charges, double x0, double x1, double y0, double y1) {
 }
diff --git a/src/field.h b/src/field.h
index 8c1a162..eac1d4e 100644
--- a/src/field.h
+++ b/src/field.h
@@ -5,11 +5,10 @@
 #include "../utils/utils.h"
 #include <stdio.h>
 
-typedef struct charge_t
-{
-    double q;
-    vec2 pos;
-} charge;
+typedef struct _charge_t {
+	double q;
+	vec2 pos;
+} charge_t;
 
 // Compute E*qP/norm(qP)
 // Return false if norm(qP) < eps
-- 
GitLab