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
e9a95bc0
Commit
e9a95bc0
authored
3 years ago
by
Florian Burgener
Browse files
Options
Downloads
Patches
Plain Diff
Implementation done.
parent
10da8207
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/Simulation.c
+19
-5
19 additions, 5 deletions
src/Simulation.c
src/constants.c
+0
-4
0 additions, 4 deletions
src/constants.c
src/constants.h
+12
-4
12 additions, 4 deletions
src/constants.h
src/main.c
+1
-3
1 addition, 3 deletions
src/main.c
with
32 additions
and
16 deletions
src/Simulation.c
+
19
−
5
View file @
e9a95bc0
...
...
@@ -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.
...
...
This diff is collapsed.
Click to expand it.
src/constants.c
+
0
−
4
View file @
e9a95bc0
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
src/constants.h
+
12
−
4
View file @
e9a95bc0
...
...
@@ -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
This diff is collapsed.
Click to expand it.
src/main.c
+
1
−
3
View file @
e9a95bc0
...
...
@@ -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
;
}
...
...
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