From 835bca55dc3b4c8caa730412b37e2bb0bdee530f Mon Sep 17 00:00:00 2001 From: JM <crewgan@pop-os.localdomain> Date: Mon, 9 May 2022 11:23:13 +0200 Subject: [PATCH] ADD: draw_charges(...) RM: field.h: struct charge --- src/draw.c | 6 ------ src/field.c | 30 ++++++++++++++++++++++++------ src/field.h | 5 ----- src/main.c | 1 + utils/gfx/gfx.h | 2 +- 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/draw.c b/src/draw.c index 923a8e1..83194fe 100644 --- a/src/draw.c +++ b/src/draw.c @@ -1,11 +1,5 @@ #include "../utils/gfx/gfx.h" #include "field.h" -/* -(50, 50) → (75, 50)1 , (50, 50) → (72, 62), (50, 50) → (62, 72) -(50, 50) → (50, 75), (50, 50) → (38, 72), (50, 50) → (28, 62) -(50, 50) → (25, 50), (50, 50) → (28, 38), (50, 50) → (37, 28) -(50, 50) → (50, 25), (50, 50) → (62, 28), (50, 50) → (72, 37) -*/ void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0, coordinates_t p1, uint32_t color) { int dx = abs(p1.column - p0.column); diff --git a/src/field.c b/src/field.c index 506a826..b0bbd1d 100644 --- a/src/field.c +++ b/src/field.c @@ -5,6 +5,8 @@ #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 @@ -36,16 +38,32 @@ 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) { +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){ + sign_color = COLOR_BLUE; + sign_dst.row = charge_center.row + SIGN_SIZE; + gfx_draw_line(context, charge_center, sign_dst, sign_color); + sign_dst.row = charge_center.row - SIGN_SIZE; + gfx_draw_line(context, charge_center, sign_dst, sign_color); + } + + sign_dst.column = charge_center.column + SIGN_SIZE; + gfx_draw_line(context, charge_center, sign_dst, sign_color); + sign_dst.column = charge_center.column - SIGN_SIZE; + gfx_draw_line(context, charge_center, sign_dst, sign_color); + } } diff --git a/src/field.h b/src/field.h index eac1d4e..899912d 100644 --- a/src/field.h +++ b/src/field.h @@ -5,11 +5,6 @@ #include "../utils/utils.h" #include <stdio.h> -typedef struct _charge_t { - double q; - vec2 pos; -} charge_t; - // Compute E*qP/norm(qP) // Return false if norm(qP) < eps bool compute_e(charge_t c, vec2 p, double eps, vec2 *e); diff --git a/src/main.c b/src/main.c index 069ed50..517790d 100644 --- a/src/main.c +++ b/src/main.c @@ -2,5 +2,6 @@ #include "draw.h" int main() { + return EXIT_SUCCESS; } diff --git a/utils/gfx/gfx.h b/utils/gfx/gfx.h index 6e25656..2848429 100644 --- a/utils/gfx/gfx.h +++ b/utils/gfx/gfx.h @@ -31,7 +31,7 @@ struct gfx_context_t extern void gfx_putpixel( - struct gfx_context_t *ctxt, uint32_t column, uint32_t row, uint32_t color); +struct gfx_context_t *ctxt, uint32_t column, uint32_t row, uint32_t color); extern void gfx_clear(struct gfx_context_t *ctxt, uint32_t color); extern struct gfx_context_t *gfx_create(char *text, uint32_t width, uint32_t height); extern void gfx_destroy(struct gfx_context_t *ctxt); -- GitLab