Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
ISC_126 - Travail Pratique 002 - Simulation de lignes de champ
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
florian.burgener
ISC_126 - Travail Pratique 002 - Simulation de lignes de champ
Commits
10da8207
Commit
10da8207
authored
3 years ago
by
florian.burgener
Browse files
Options
Downloads
Patches
Plain Diff
Add comments
parent
6235fac5
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Simulation.c
+73
-0
73 additions, 0 deletions
src/Simulation.c
with
73 additions
and
0 deletions
src/Simulation.c
+
73
−
0
View file @
10da8207
...
@@ -16,14 +16,34 @@
...
@@ -16,14 +16,34 @@
#include
"constants.h"
#include
"constants.h"
#include
"random_number.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
);
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
()
{
static
Charge
generate_random_charge
()
{
Vector2
position
=
vector2_init
(
random_number_between_0_and_1
(),
random_number_between_0_and_1
());
Vector2
position
=
vector2_init
(
random_number_between_0_and_1
(),
random_number_between_0_and_1
());
int
sign
=
rand
()
%
2
==
0
?
1
:
-
1
;
int
sign
=
rand
()
%
2
==
0
?
1
:
-
1
;
return
charge_init
(
ELEMENTARY_CHARGE
*
sign
,
position
);
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
)
{
static
Charge
*
generate_random_charges
(
Rectangle
*
universe
,
int
*
charges_length
)
{
*
charges_length
=
random_number_between
(
MIN_CHARGES
,
MAX_CHARGES
);
*
charges_length
=
random_number_between
(
MIN_CHARGES
,
MAX_CHARGES
);
Charge
*
charges
=
(
Charge
*
)
malloc
(
sizeof
(
Charge
)
*
*
charges_length
);
Charge
*
charges
=
(
Charge
*
)
malloc
(
sizeof
(
Charge
)
*
*
charges_length
);
...
@@ -58,12 +78,28 @@ static Charge *generate_random_charges(Rectangle *universe, int *charges_length)
...
@@ -58,12 +78,28 @@ static Charge *generate_random_charges(Rectangle *universe, int *charges_length)
return
charges
;
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
)
{
static
void
draw_charges
(
Simulation
*
simulation
,
Graphics
*
graphics
)
{
for
(
int
i
=
0
;
i
<
simulation
->
charges_length
;
i
++
)
{
for
(
int
i
=
0
;
i
<
simulation
->
charges_length
;
i
++
)
{
charge_draw
(
simulation
->
charges
[
i
],
graphics
,
simulation
->
universe
);
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
)
{
static
bool
compute_e
(
Charge
charge
,
Vector2
point
,
double
eps
,
Vector2
*
e
)
{
Vector2
r
=
vector2_substract
(
charge
.
position
,
point
);
Vector2
r
=
vector2_substract
(
charge
.
position
,
point
);
double
e_intensity
=
K
*
fabs
(
charge
.
q
)
/
vector2_norm_sqr
(
r
);
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) {
...
@@ -76,6 +112,17 @@ static bool compute_e(Charge charge, Vector2 point, double eps, Vector2 *e) {
return
vector2_norm
(
r
)
>=
eps
;
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
)
{
static
bool
compute_total_normalized_e
(
int
charges_length
,
Charge
*
charges
,
Vector2
point
,
double
eps
,
Vector2
*
e
)
{
*
e
=
vector2_init_zero
();
*
e
=
vector2_init_zero
();
...
@@ -96,6 +143,17 @@ static bool is_out_of_bounds(Rectangle *universe, Vector2 point) {
...
@@ -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
;
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
)
{
static
bool
compute_next_point
(
Simulation
*
simulation
,
int
direction
,
Vector2
current_point
,
double
eps
,
Vector2
*
next_point
)
{
Vector2
electric_field_normalized
;
Vector2
electric_field_normalized
;
if
(
!
compute_total_normalized_e
(
simulation
->
charges_length
,
simulation
->
charges
,
current_point
,
eps
,
&
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
...
@@ -112,6 +170,14 @@ static bool compute_next_point(Simulation *simulation, int direction, Vector2 cu
return
true
;
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
)
{
static
void
draw_field_line_with_direction
(
Simulation
*
simulation
,
Graphics
*
graphics
,
Vector2
starting_point
,
int
direction
)
{
Vector2
current_point
=
starting_point
;
Vector2
current_point
=
starting_point
;
...
@@ -128,6 +194,13 @@ static void draw_field_line_with_direction(Simulation *simulation, Graphics *gra
...
@@ -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
)
{
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
);
draw_field_line_with_direction
(
simulation
,
graphics
,
starting_point
,
1
);
draw_field_line_with_direction
(
simulation
,
graphics
,
starting_point
,
1
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment