From 10da82071880f873ac2a089fc8a33de4bfe290ef Mon Sep 17 00:00:00 2001 From: Florian Burgener <florian.burgener@etu.hesge.ch> Date: Mon, 9 May 2022 15:00:03 +0200 Subject: [PATCH] Add comments --- src/Simulation.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/src/Simulation.c b/src/Simulation.c index 21f4203..3e31199 100644 --- a/src/Simulation.c +++ b/src/Simulation.c @@ -16,14 +16,34 @@ #include "constants.h" #include "random_number.h" +/** + * @brief Checks if the point is outside the limits. + * + * @param universe The universe rectangle. + * @param point The point + * @return true Is outside the limits. + * @return false Is not outside the limits. + */ static bool is_out_of_bounds(Rectangle *universe, Vector2 point); +/** + * @brief Generates a random charge. + * + * @return Charge The random charge. + */ static Charge generate_random_charge() { Vector2 position = vector2_init(random_number_between_0_and_1(), random_number_between_0_and_1()); int sign = rand() % 2 == 0 ? 1 : -1; return charge_init(ELEMENTARY_CHARGE * sign, position); } +/** + * @brief Generates random charges. + * + * @param universe The universe rectangle. + * @param charges_length The number of charges. + * @return Charge* The charge array. + */ static Charge *generate_random_charges(Rectangle *universe, int *charges_length) { *charges_length = random_number_between(MIN_CHARGES, MAX_CHARGES); Charge *charges = (Charge *)malloc(sizeof(Charge) * *charges_length); @@ -58,12 +78,28 @@ static Charge *generate_random_charges(Rectangle *universe, int *charges_length) return charges; } +/** + * @brief Draws the charges. + * + * @param simulation The simulation. + * @param graphics The graphics in which we draw. + */ static void draw_charges(Simulation *simulation, Graphics *graphics) { for (int i = 0; i < simulation->charges_length; i++) { charge_draw(simulation->charges[i], graphics, simulation->universe); } } +/** + * @brief Calculates the electric field. + * + * @param charge The charge. + * @param point The point + * @param eps + * @param e The electric field. + * @return true The electric field is valid. + * @return false The electric field is invalid. + */ static bool compute_e(Charge charge, Vector2 point, double eps, Vector2 *e) { Vector2 r = vector2_substract(charge.position, point); double e_intensity = K * fabs(charge.q) / vector2_norm_sqr(r); @@ -76,6 +112,17 @@ static bool compute_e(Charge charge, Vector2 point, double eps, Vector2 *e) { return vector2_norm(r) >= eps; } +/** + * @brief Calculates the electric field at the given point. + * + * @param charges_length The number of charges. + * @param charges The charge array. + * @param point The point. + * @param eps + * @param e The electric field. + * @return true The electric field is valid. + * @return false The electric field is invalid. + */ static bool compute_total_normalized_e(int charges_length, Charge *charges, Vector2 point, double eps, Vector2 *e) { *e = vector2_init_zero(); @@ -96,6 +143,17 @@ static bool is_out_of_bounds(Rectangle *universe, Vector2 point) { return point.x < universe->top_left.x || point.x > universe->bottom_right.x || point.y < universe->top_left.y || point.y > universe->bottom_right.y; } +/** + * @brief Calculates the next point. + * + * @param simulation The simulation. + * @param direction The direction in which we draw. + * @param current_point The point from which the next point is calculated. + * @param eps + * @param next_point The next point. + * @return true The point is valid. + * @return false The point is invalid. + */ static bool compute_next_point(Simulation *simulation, int direction, Vector2 current_point, double eps, Vector2 *next_point) { Vector2 electric_field_normalized; if (!compute_total_normalized_e(simulation->charges_length, simulation->charges, current_point, eps, &electric_field_normalized)) { @@ -112,6 +170,14 @@ static bool compute_next_point(Simulation *simulation, int direction, Vector2 cu return true; } +/** + * @brief Draws the field line in one direction. + * + * @param simulation The simulation. + * @param graphics The graphics in which we draw. + * @param starting_point The starting point. + * @param direction The direction in which we draw. + */ static void draw_field_line_with_direction(Simulation *simulation, Graphics *graphics, Vector2 starting_point, int direction) { Vector2 current_point = starting_point; @@ -128,6 +194,13 @@ static void draw_field_line_with_direction(Simulation *simulation, Graphics *gra } } +/** + * @brief Draws the field line. + * + * @param simulation The simulation. + * @param graphics The graphics in which we draw. + * @param starting_point The starting point. + */ static void draw_field_line(Simulation *simulation, Graphics *graphics, Vector2 starting_point) { draw_field_line_with_direction(simulation, graphics, starting_point, -1); draw_field_line_with_direction(simulation, graphics, starting_point, 1); -- GitLab