Skip to content
Snippets Groups Projects
Commit 2b7778ba authored by tanguy.cavagna's avatar tanguy.cavagna :desktop:
Browse files

Feature : Draw charges on the window.

parent b1bbc2c7
Branches
Tags
No related merge requests found
charge.c 0 → 100644
/**
* @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
charge.h 0 → 100644
/**
* @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
/**
* @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);
}
......
......@@ -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;
......
......@@ -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
......@@ -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
......
......@@ -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 */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment