Skip to content
Snippets Groups Projects
Commit e9a95bc0 authored by Florian Burgener's avatar Florian Burgener
Browse files

Implementation done.

parent 10da8207
Branches
No related tags found
No related merge requests found
......@@ -44,6 +44,7 @@ static Charge generate_random_charge() {
* @param charges_length The number of charges.
* @return Charge* The charge array.
*/
#include <stdio.h>
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);
......@@ -52,9 +53,11 @@ static Charge *generate_random_charges(Rectangle *universe, int *charges_length)
while (true) {
Charge charge = generate_random_charge();
// Checks if the charge is not too close to the edge of the universe.
Vector2 position = charge.position;
double eps = RADIUS_TRESHOLD;
if (is_out_of_bounds(universe, vector2_add(position, vector2_init(0, -eps)))) {
// Starts the loop again with a new random charge.
continue;
}
if (is_out_of_bounds(universe, vector2_add(position, vector2_init(eps, 0)))) {
......@@ -67,8 +70,19 @@ static Charge *generate_random_charges(Rectangle *universe, int *charges_length)
continue;
}
// TODO : avoid two charge to bee to close.
// TODO : eps as const
bool charges_are_too_close = false;
for (int j = 0; j < i; j++) {
double norm = vector2_norm(vector2_substract(position, charges[j].position));
if (norm < RADIUS_TRESHOLD * 3) {
// The two charges are too close.
charges_are_too_close = true;
}
}
if (charges_are_too_close) {
// Starts the loop again with a new random charge.
continue;
}
charges[i] = charge;
break;
......@@ -95,7 +109,7 @@ static void draw_charges(Simulation *simulation, Graphics *graphics) {
*
* @param charge The charge.
* @param point The point
* @param eps
* @param eps Minimum distance between a point and the position of a charge.
* @param e The electric field.
* @return true The electric field is valid.
* @return false The electric field is invalid.
......@@ -118,7 +132,7 @@ static bool compute_e(Charge charge, Vector2 point, double eps, Vector2 *e) {
* @param charges_length The number of charges.
* @param charges The charge array.
* @param point The point.
* @param eps
* @param eps Minimum distance between a point and the position of a charge.
* @param e The electric field.
* @return true The electric field is valid.
* @return false The electric field is invalid.
......@@ -149,7 +163,7 @@ static bool is_out_of_bounds(Rectangle *universe, Vector2 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 eps Minimum distance between a point and the position of a charge.
* @param next_point The next point.
* @return true The point is valid.
* @return false The point is invalid.
......
......@@ -9,17 +9,13 @@
const int SCREEN_WIDTH = 750;
const int SCREEN_HEIGHT = 750;
const int UNIVERSE_X0 = 0;
const int UNIVERSE_Y0 = 0;
const int UNIVERSE_X1 = 1;
const int UNIVERSE_Y1 = 1;
const int CHARGE_CIRCLE_RADIUS = 20;
const int MIN_CHARGES = 2;
const int MAX_CHARGES = 5;
const double K = 8.988e9;
const double ELEMENTARY_CHARGE = 1.602e-19;
// Represents the acceptable distance between the last point and the charge, this distance is calculated with
......
......@@ -8,21 +8,29 @@
#ifndef CONSTANTS_H
#define CONSTANTS_H
// Screen width in pixels.
extern const int SCREEN_WIDTH;
// Screen height in pixels.
extern const int SCREEN_HEIGHT;
// The x-component of the universe at the top left.
extern const int UNIVERSE_X0;
// The y-component of the universe at the top left.
extern const int UNIVERSE_Y0;
// The x-component of the universe at the bottom right.
extern const int UNIVERSE_X1;
// The y-component of the universe at the bottom right.
extern const int UNIVERSE_Y1;
// Radius of the circle of a charge.
extern const int CHARGE_CIRCLE_RADIUS;
// Minimum number of charges to generate.
extern const int MIN_CHARGES;
// Maximum number of charges to generate.
extern const int MAX_CHARGES;
// Coulomb's constant.
extern const double K;
// Value of the elementary charge.
extern const double ELEMENTARY_CHARGE;
// Minimum distance between a point and the position of a charge.
extern const double RADIUS_TRESHOLD;
#endif
......@@ -38,11 +38,9 @@ double compute_delta_x(int width, int height) {
int main(int UNUSED(argc), char *UNUSED(argv[])) {
Graphics *graphics = gfx_create("Field Lines Simulation", SCREEN_WIDTH, SCREEN_HEIGHT);
srand(time(NULL));
// srand(10);
// srand(11);
if (graphics == NULL) {
fprintf(stderr, "Graphic initialization failed!\n");
fprintf(stderr, "Impossible to initialize the window.\n");
return EXIT_FAILURE;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment