diff --git a/charge.c b/charge.c new file mode 100644 index 0000000000000000000000000000000000000000..20733aa2b8b9d5711354769dcf159ebc20af17ba --- /dev/null +++ b/charge.c @@ -0,0 +1,50 @@ +/** + * @file charge.c + * @author Tanguy Cavagna (tanguy.cavagna@etu.hesge.ch) + * @brief Chrage library + * @version 0.1 + * @date 2021-07-12 + * + * @copyright Copyright (c) 2021 + * + */ +#include "draw/draw.h" +#include "vector/vector.h" +#include "utils.h" +#include "charge.h" + +bool compute_e(charge_t c, vec2 p, double eps, vec2 *e) { +} + +bool compute_total_normalized_e(charge_t *charges, int num_charges, vec2 p, double eps, vec2 *e) { +} + +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) { +} + +void draw_charges(struct gfx_context_t *ctxt, charge_t *charges, int num_charges, double x0, double x1, double y0, double y1) { + // Center coordinates + coordinates_t c; + + // Charges coordinates + coordinates_t ch0; + coordinates_t ch1; + + int radius = 45; + int chargePadding = 10; + + // Draw charges + for (int i = 0; i < num_charges; i++) { + coordinates_t chCoord = position_to_coordinates(ctxt->width, ctxt->height, x0, x1, y0, y1, charges[i].pos); + + // Draw sign according to charge value + if (charges[i].q < 0) { + gfx_draw_line(ctxt, coordinates_create(chCoord.row, chCoord.column - chargePadding), coordinates_create(chCoord.row, chCoord.column + chargePadding), COLOR_BLUE); + } else { + gfx_draw_line(ctxt, coordinates_create(chCoord.row, chCoord.column - chargePadding), coordinates_create(chCoord.row, chCoord.column + chargePadding), COLOR_RED); + gfx_draw_line(ctxt, coordinates_create(chCoord.row - chargePadding, chCoord.column), coordinates_create(chCoord.row + chargePadding, chCoord.column), COLOR_RED); + } + + gfx_draw_circle(ctxt, chCoord, radius, charges[i].q < 0 ? COLOR_BLUE : COLOR_RED); + } +} \ No newline at end of file diff --git a/charge.h b/charge.h new file mode 100644 index 0000000000000000000000000000000000000000..07af84c9cbe685c22434a92525998a91415912de --- /dev/null +++ b/charge.h @@ -0,0 +1,79 @@ +/** + * @file charge.h + * @author Tanguy Cavagna (tanguy.cavagna@etu.hesge.ch) + * @brief Charge file header + * @version 0.1 + * @date 2021-07-12 + * + * @copyright Copyright (c) 2021 + * + */ +#ifndef _CHARGE_H_ +#define _CHARGE_H_ + +#include <stdlib.h> +#include <stdbool.h> +#include "utils.h" +#include "vector/vector.h" +#include "gfx/gfx.h" + +/** + * @brief Compute E*qP/norm(qP) + * + * @param c Parent charge + * @param p Point on which to compute e + * @param eps Minimal gap between field and charge + * @param e Electric field + * @return bool false norm(qP) < eps + */ +bool compute_e(charge_t c, vec2 p, double eps, vec2 *e); + +/** + * @brief Compute the normilized sum of Ei*qiP/norm(qiP) + * + * @param charges All the charges + * @param num_charges Number of charges in action + * @param p Current computed point + * @param eps Minimal gap between field and charge + * @param e Electric field + * @return bool false for some qiP, norm(qiP) < eps + */ +bool compute_total_normalized_e(charge_t *charges, int num_charges, vec2 p, + double eps, vec2 *e); + +/** + * @brief Compute and then draw all the points belonging to a field line, + * starting from pos0 + * + * @param ctxt Context to draw on + * @param charges All the charges + * @param num_charges Number of charges in action + * @param dx Space between computed points on the field line + * @param pos0 First post to start the computing of the field line + * @param x0 Minal x of the universe + * @param x1 Maximal x of the universe + * @param y0 Minal y of the universe + * @param y1 Maximal y of the universe + * @return bool false if pos0 not a valid pos (e.g. too close to a charge) + */ +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); + +/** + * @brief Draw all charges + * A circle with a minus sign for negative charges + * A circle with a plus sign for positive charges + * + * @param ctxt Context to draw on + * @param charges All the charges + * @param num_charges Number of charges in action + * @param x0 Minal x of the universe + * @param x1 Maximal x of the universe + * @param y0 Minal y of the universe + * @param y1 Maximal y of the universe + */ +void draw_charges(struct gfx_context_t *ctxt, charge_t *charges, + int num_charges, double x0, double x1, double y0, double y1); + +#endif // _CHARGE_H_ \ No newline at end of file diff --git a/main.c b/main.c index 83699785be4f34c784301c9936284376e40e350f..f70c311adaa4a9450aa62b9cf7572935bb91ae0c 100644 --- a/main.c +++ b/main.c @@ -1,21 +1,44 @@ +/** + * @file main.c + * @author Tanguy Cavagna (tanguy.cavagna@etu.hesge.ch) + * @brief Main file for the electric field exercice + * @version 0.1 + * @date 2021-07-12 + * + * @copyright Copyright (c) 2021 + * + */ +#include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> #include "gfx/gfx.h" +#include "charge.h" -int main(void) { - const int WINDOW_SIZE_X = 500; - const int WINDOW_SIZE_Y = 500; +#define WINDOW_WIDTH 500 +#define WINDOW_HEIGHT 500 +#define x0 0 // Minimal x of the universe +#define x1 1 // Maximal x of the universe +#define y0 0 // Minimal y of the universe +#define y1 1 // Maximal y of the universe +#define NB_CHARGES 2 +int main(void) { srand(time(NULL)); - struct gfx_context_t *ctxt = gfx_create("draw", WINDOW_SIZE_X, WINDOW_SIZE_Y); - + // GFX initialization + struct gfx_context_t *ctxt = gfx_create("draw", WINDOW_WIDTH, WINDOW_HEIGHT); if (!ctxt) { fprintf(stderr, "Graphics initialization failed !\n"); return EXIT_FAILURE; } + charge_t charges[NB_CHARGES]; + charges[0] = charge_create(10, init_vector(0.25, 0.5)); + charges[1] = charge_create(-10, init_vector(0.75, 0.5)); + draw_charges(ctxt, charges, NB_CHARGES, x0, x1, y0, y1); + + // GFX Draw loop while (gfx_keypressed() != SDLK_ESCAPE){ gfx_present(ctxt); } diff --git a/utils.c b/utils.c index fc22d8aee6436e20a871dbd46159103477e9a2a8..86e1a7cb2c6642b8fcddc888bd25a226b473fab3 100644 --- a/utils.c +++ b/utils.c @@ -4,7 +4,7 @@ #include "utils.h" // Transform a position in the univers [x0,y0]x[x1,y1] to a screen position -coordinates_t position_to_coordinates(int width, int height, double x0, double x1, double y0, double y1, vector pos) +coordinates_t position_to_coordinates(int width, int height, double x0, double x1, double y0, double y1, vec2 pos) { double dx = x1 - x0; double dy = y1 - y0; @@ -16,7 +16,7 @@ double rand_one() return (double)rand() / (double)RAND_MAX; } -charge_t charge_create(double q, vector pos) +charge_t charge_create(double q, vec2 pos) { charge_t c = {.q = q, .pos = pos}; return c; diff --git a/utils.h b/utils.h index 81f456ea5c552325d17c78222fc7bd9a7ce8a3dd..54d49d0276659eae9467156495a03ff50f6db966 100644 --- a/utils.h +++ b/utils.h @@ -8,17 +8,17 @@ #include "vector/vector.h" #include "draw/draw.h" -typedef struct +typedef struct _charge_t { double q; - vector pos; + vec2 pos; } charge_t; // Transform a position in the univers [x0,y0]x[x1,y1] to a screen position -coordinates_t position_to_coordinates(int width, int height, double x0, double x1, double y0, double y1, vector pos); +coordinates_t position_to_coordinates(int width, int height, double x0, double x1, double y0, double y1, vec2 pos); double rand_one(); -charge_t charge_create(double q, vector pos); +charge_t charge_create(double q, vec2 pos); #endif \ No newline at end of file diff --git a/vector/vector.c b/vector/vector.c index 5aaaae322e405030e010134b8d53f0567115f4d5..bbe8504b89a1c90e8409972b9db91909ee77989c 100644 --- a/vector/vector.c +++ b/vector/vector.c @@ -11,6 +11,12 @@ #include <stdio.h> #include "vector.h" +vec2 init_vector(double x, double y) { + vec2 v = {x, y}; + + return v; +} + /*! \fn vector add_vector(vector v1, vector v2) \brief Add two vector and return the final vector \param v1 First vector to add diff --git a/vector/vector.h b/vector/vector.h index d78bbc56e0e23a1d2eda97bf8bbb04a40687de9f..205768d70c343e518640243199fca8aa8014c0cd 100644 --- a/vector/vector.h +++ b/vector/vector.h @@ -21,6 +21,7 @@ typedef struct { double a; /* Angle of the vector */ } polarVector; +vec2 init_vector(double x, double y); /* Initialize a new vector */ vec2 add_vector(vec2 v1, vec2 v2); /* Add a vector to another and return the result as a vector */ vec2 sub_vector(vec2 v1, vec2 v2); /* Substract a vector to another vector and return the result as a vector */ vec2 mult_by_scalar(vec2 v1, int scalar); /* Multiply a vector by a scalar */