Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
phys_tp_elec
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
boris.stefanov
phys_tp_elec
Commits
5fec85a2
Commit
5fec85a2
authored
3 years ago
by
Boris Stefanovic
Browse files
Options
Downloads
Plain Diff
Merge remote-tracking branch 'origin/physics' into physics
# Conflicts: # src/draw.c
parents
e6580eff
36a4455e
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/draw.c
+24
-24
24 additions, 24 deletions
src/draw.c
src/field.c
+79
-21
79 additions, 21 deletions
src/field.c
src/field.h
+6
-6
6 additions, 6 deletions
src/field.h
src/main.c
+7
-9
7 additions, 9 deletions
src/main.c
with
116 additions
and
60 deletions
src/draw.c
+
24
−
24
View file @
5fec85a2
...
@@ -35,28 +35,28 @@ void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0, coordinates_t p
...
@@ -35,28 +35,28 @@ void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0, coordinates_t p
}
}
void
gfx_draw_circle
(
struct
gfx_context_t
*
ctxt
,
coordinates
c
,
uint32_t
r
,
uint32_t
color
)
{
void
gfx_draw_circle
(
struct
gfx_context_t
*
ctxt
,
coordinates
c
,
uint32_t
r
,
uint32_t
color
)
{
int
x
=
0
;
int
x
=
0
;
int
y
=
r
;
int
y
=
r
;
int
d
=
r
-
1
;
int
d
=
r
-
1
;
while
(
y
>=
x
)
{
while
(
y
>=
x
){
gfx_putpixel
(
ctxt
,
c
.
row
+
x
,
c
.
column
+
y
,
color
);
gfx_putpixel
(
ctxt
,
c
.
column
+
x
,
c
.
row
+
y
,
color
);
gfx_putpixel
(
ctxt
,
c
.
row
+
y
,
c
.
column
+
x
,
color
);
gfx_putpixel
(
ctxt
,
c
.
column
+
y
,
c
.
row
+
x
,
color
);
gfx_putpixel
(
ctxt
,
c
.
row
-
x
,
c
.
column
+
y
,
color
);
gfx_putpixel
(
ctxt
,
c
.
column
-
x
,
c
.
row
+
y
,
color
);
gfx_putpixel
(
ctxt
,
c
.
row
-
y
,
c
.
column
+
x
,
color
);
gfx_putpixel
(
ctxt
,
c
.
column
-
y
,
c
.
row
+
x
,
color
);
gfx_putpixel
(
ctxt
,
c
.
row
+
x
,
c
.
column
-
y
,
color
);
gfx_putpixel
(
ctxt
,
c
.
column
+
x
,
c
.
row
-
y
,
color
);
gfx_putpixel
(
ctxt
,
c
.
row
+
y
,
c
.
column
-
x
,
color
);
gfx_putpixel
(
ctxt
,
c
.
column
+
y
,
c
.
row
-
x
,
color
);
gfx_putpixel
(
ctxt
,
c
.
row
-
x
,
c
.
column
-
y
,
color
);
gfx_putpixel
(
ctxt
,
c
.
column
-
x
,
c
.
row
-
y
,
color
);
gfx_putpixel
(
ctxt
,
c
.
row
-
y
,
c
.
column
-
x
,
color
);
gfx_putpixel
(
ctxt
,
c
.
column
-
y
,
c
.
row
-
x
,
color
);
if
(
d
>=
2
*
x
)
{
if
(
d
>=
2
*
x
){
d
=
d
-
2
*
x
-
1
;
d
=
d
-
2
*
x
-
1
;
x
++
;
x
++
;
}
else
if
(
d
<
2
*
(
r
-
y
))
{
}
else
if
(
d
<
2
*
(
r
-
y
)){
d
=
d
+
2
*
y
-
1
;
d
=
d
+
2
*
y
-
1
;
y
--
;
y
--
;
}
else
{
}
else
{
d
=
d
+
2
*
(
y
-
x
-
1
);
d
=
d
+
2
*
(
y
-
x
-
1
);
y
--
;
y
--
;
x
++
;
x
++
;
}
}
}
}
}
}
This diff is collapsed.
Click to expand it.
src/field.c
+
79
−
21
View file @
5fec85a2
...
@@ -3,12 +3,12 @@
...
@@ -3,12 +3,12 @@
#include
"draw.h"
#include
"draw.h"
#include
"field.h"
#include
"field.h"
#include
"draw.h"
#include
"../utils/utils.h"
#define SIGN_SIZE 10
#define SIGN_SIZE 10
#define CHARGE_R 25
#define CHARGE_R 25
// Compute E*qP/norm(qP)
// Compute E*qP/norm(qP)
// Return false if norm(qP) < eps
// Return false if norm(qP) < eps
/// qP = vectoriel(P-q)
/// qP = vectoriel(P-q)
...
@@ -34,41 +34,98 @@ bool compute_total_normalized_e(charge_t *charges, int num_charges, vec2 p, doub
...
@@ -34,41 +34,98 @@ bool compute_total_normalized_e(charge_t *charges, int num_charges, vec2 p, doub
return
true
;
return
true
;
}
}
double
compute_delta_x
(){
double
result
=
pow
(
WID
,
2
)
+
pow
(
HEI
,
2
);
result
=
sqrt
(
result
);
return
1
/
result
;
}
bool
is_in_screen
(
coordinates_t
pos
){
if
(
pos
.
column
>
WID
||
pos
.
column
<
0
)
return
false
;
if
(
pos
.
row
>
HEI
||
pos
.
row
<
0
)
return
false
;
return
true
;
}
bool
draw_field_line_point
(
struct
gfx_context_t
*
ctxt
,
charge_t
*
charges
,
int
num_charges
,
double
x0
,
double
x1
,
double
y0
,
double
y1
,
vec2
pos
,
vec2
*
pos_next
,
double
delta
){
vec2
e
;
compute_total_normalized_e
(
charges
,
num_charges
,
pos
,
0
.
01
,
&
e
);
double
norm_e
=
vec2_norm
(
e
);
pos_next
->
x
=
pos
.
x
+
delta
*
(
e
.
x
/
norm_e
);
pos_next
->
y
=
pos
.
y
+
delta
*
(
e
.
y
/
norm_e
);
coordinates_t
coordinate_pos
=
position_to_coordinates
(
WID
,
HEI
,
x0
,
x1
,
y0
,
y1
,
pos
);
coordinates_t
coordinate_pos_next
=
position_to_coordinates
(
WID
,
HEI
,
x0
,
x1
,
y0
,
y1
,
*
pos_next
);
gfx_draw_line
(
ctxt
,
coordinate_pos
,
coordinate_pos_next
,
COLOR_YELLOW
);
return
!
is_in_screen
(
coordinate_pos
);
}
bool
line_reach_charge
(
vec2
pos
,
charge_t
*
charges
,
double
num_charges
,
double
dx
){
for
(
int
j
=
0
;
j
<
num_charges
;
j
++
){
if
(
vec2_norm
(
vec2_sub
(
pos
,
charges
[
j
].
pos
))
<
dx
/
HEI
){
return
true
;
}
}
return
false
;
}
// Compute and then draw all the points belonging to a field line,
// Compute and then draw all the points belonging to a field line,
// starting from pos0.
// starting from pos0.
// Returns false if pos0 is not a valid position
// Returns false if pos0 is not a valid position
// (for example if pos0 is too close to a charge).
// (for example if pos0 is too close to a charge).
static
bool
static
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
)
{
draw_field_line
(
struct
gfx_context_t
*
ctxt
,
charge_t
*
charges
,
int
num_charges
,
double
dx
,
vec2
pos0
,
double
x0
,
vec2
pos_positive
=
vec2_create
(
pos0
.
x
,
pos0
.
y
);
double
x1
,
double
y0
,
double
y1
)
{
vec2
pos_negative
=
vec2_create
(
pos0
.
x
,
pos0
.
y
);
return
EXIT_SUCCESS
;
vec2
pos_next_positive
;
vec2
pos_next_negative
;
bool
stop
=
false
;
// * 5 à supprimer lorsque le code est optimisé
double
delta
=
compute_delta_x
()
*
5
;
int
max_for
=
50
;
for
(
int
i
=
0
;
i
<
max_for
&&
!
stop
;
i
++
)
{
stop
=
line_reach_charge
(
pos_positive
,
charges
,
num_charges
,
dx
)
||
line_reach_charge
(
pos_negative
,
charges
,
num_charges
,
dx
);
if
(
!
stop
){
stop
=
draw_field_line_point
(
ctxt
,
charges
,
num_charges
,
x0
,
x1
,
y0
,
y1
,
pos_positive
,
&
pos_next_positive
,
delta
)
||
draw_field_line_point
(
ctxt
,
charges
,
num_charges
,
x0
,
x1
,
y0
,
y1
,
pos_negative
,
&
pos_next_negative
,
-
delta
);
pos_positive
=
pos_next_positive
;
pos_negative
=
pos_next_negative
;
}
}
return
!
stop
;
}
}
// Draw all the charges
// Draw all the charges
// A circle with minus sign for negative charges
// A circle with minus sign for negative charges
// A circle with a plus sign for positive charges
// A circle with a plus sign for positive charges
static
void
static
void
draw_charges
(
struct
gfx_context_t
*
context
,
charge_t
*
charges
,
int
num_charges
,
double
x0
,
double
x1
,
double
y0
,
double
y1
)
{
draw_charges
(
struct
gfx_context_t
*
context
,
charge_t
*
charges
,
int
num_charges
,
double
x0
,
double
x1
,
double
y0
,
for
(
int
i
=
0
;
i
<
num_charges
;
i
++
)
double
y1
)
{
{
for
(
int
i
=
0
;
i
<
num_charges
;
i
++
)
{
coordinates_t
charge_center
=
position_to_coordinates
(
WID
,
HEI
,
x0
,
x1
,
y0
,
y1
,
charges
[
i
].
pos
);
coordinates_t
charge_center
=
position_to_coordinates
(
CHARGE_R
,
CHARGE_R
,
x0
,
x1
,
y0
,
y1
,
charges
[
i
].
pos
);
gfx_draw_circle
(
context
,
charge_center
,
CHARGE_R
,
COLOR_WHITE
);
gfx_draw_circle
(
context
,
charge_center
,
CHARGE_R
,
COLOR_WHITE
);
coordinates_t
sign_dst
;
coordinates_t
sign_src
=
charge_center
;
coordinates_t
sign_dst
=
charge_center
;
uint32_t
sign_color
=
COLOR_RED
;
uint32_t
sign_color
=
COLOR_RED
;
if
(
charges
[
i
].
q
>
0
)
{
if
(
charges
[
i
].
q
>
0
)
{
sign_color
=
COLOR_BLUE
;
sign_color
=
COLOR_BLUE
;
sign_dst
.
row
=
charge_center
.
row
+
SIGN_SIZE
;
sign_src
.
row
-=
SIGN_SIZE
;
gfx_draw_line
(
context
,
charge_center
,
sign_dst
,
sign_color
);
sign_dst
.
row
+=
SIGN_SIZE
;
sign_dst
.
row
=
charge_center
.
row
-
SIGN_SIZE
;
gfx_draw_line
(
context
,
sign_src
,
sign_dst
,
sign_color
);
gfx_draw_line
(
context
,
charge_center
,
sign_dst
,
sign_color
);
sign_src
.
row
=
charge_center
.
row
;
sign_dst
.
row
=
charge_center
.
row
;
}
}
sign_dst
.
column
=
charge_center
.
column
+
SIGN_SIZE
;
sign_src
.
column
-=
SIGN_SIZE
;
gfx_draw_line
(
context
,
charge_center
,
sign_dst
,
sign_color
);
sign_dst
.
column
+=
SIGN_SIZE
;
sign_dst
.
column
=
charge_center
.
column
-
SIGN_SIZE
;
gfx_draw_line
(
context
,
sign_src
,
sign_dst
,
sign_color
);
gfx_draw_line
(
context
,
charge_center
,
sign_dst
,
sign_color
);
}
}
}
}
...
@@ -80,9 +137,10 @@ void draw_everything(
...
@@ -80,9 +137,10 @@ void draw_everything(
double
dx
,
double
dx
,
double
x0
,
double
x1
,
double
x0
,
double
x1
,
double
y0
,
double
y1
)
{
double
y0
,
double
y1
)
{
srand
(
time
(
NULL
));
draw_charges
(
ctxt
,
charges
,
num_charges
,
x0
,
x1
,
y0
,
y1
);
draw_charges
(
ctxt
,
charges
,
num_charges
,
x0
,
x1
,
y0
,
y1
);
for
(
int
i
=
0
;
i
<
num_lines
;
++
i
)
{
for
(
int
i
=
0
;
i
<
num_lines
;
++
i
)
{
vec2
pos0
=
vec2_normalize
(
vec2_create
(
rand
(),
rand
(
)
));
vec2
pos0
=
vec2_create
(
rand
_one
(),
rand
_one
());
draw_field_line
(
ctxt
,
charges
,
num_charges
,
dx
,
pos0
,
x0
,
x1
,
y0
,
y1
);
draw_field_line
(
ctxt
,
charges
,
num_charges
,
dx
,
pos0
,
x0
,
x1
,
y0
,
y1
);
}
}
}
}
This diff is collapsed.
Click to expand it.
src/field.h
+
6
−
6
View file @
5fec85a2
...
@@ -6,6 +6,10 @@
...
@@ -6,6 +6,10 @@
#include
"../utils/utils.h"
#include
"../utils/utils.h"
#include
<stdio.h>
#include
<stdio.h>
#define SIDE_LEN 1000
#define WID SIDE_LEN
#define HEI WID
// Compute E*qP/norm(qP)
// Compute E*qP/norm(qP)
// Return false if norm(qP) < eps
// Return false if norm(qP) < eps
bool
compute_e
(
charge_t
c
,
vec2
p
,
double
eps
,
vec2
*
e
);
bool
compute_e
(
charge_t
c
,
vec2
p
,
double
eps
,
vec2
*
e
);
...
@@ -18,16 +22,12 @@ bool compute_total_normalized_e(charge_t *charges, int num_charges, vec2 p, doub
...
@@ -18,16 +22,12 @@ bool compute_total_normalized_e(charge_t *charges, int num_charges, vec2 p, doub
// starting from pos0.
// starting from pos0.
// Returns false if pos0 is not a valid position
// Returns false if pos0 is not a valid position
// (for example if pos0 is too close to a charge).
// (for example if pos0 is too close to a charge).
static
bool
static
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
);
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
);
// Draw all the charges
// Draw all the charges
// A circle with minus sign for negative charges
// A circle with minus sign for negative charges
// A circle with a plus sign for positive charges
// A circle with a plus sign for positive charges
static
void
static
void
draw_charges
(
struct
gfx_context_t
*
context
,
charge_t
*
charges
,
int
num_charges
,
double
x0
,
double
x1
,
double
y0
,
double
y1
);
draw_charges
(
struct
gfx_context_t
*
context
,
charge_t
*
charges
,
int
num_charges
,
double
x0
,
double
x1
,
double
y0
,
double
y1
);
void
draw_everything
(
void
draw_everything
(
struct
gfx_context_t
*
ctxt
,
struct
gfx_context_t
*
ctxt
,
...
...
This diff is collapsed.
Click to expand it.
src/main.c
+
7
−
9
View file @
5fec85a2
...
@@ -4,24 +4,22 @@
...
@@ -4,24 +4,22 @@
#include
"draw.h"
#include
"draw.h"
#include
"field.h"
#include
"field.h"
#include
"../utils/utils.h"
#include
"../utils/utils.h"
#include
"field.h"
#define SIDE_LEN 1000
#define WID SIDE_LEN
#define HEI WID
#define NCHARGES 2
#define NCHARGES 2
#define DX
0.000
5
#define DX
2
5
#define NLINES
32
#define NLINES
5
int
main
()
{
int
main
()
{
srand
(
time
(
NULL
));
charge_t
charges
[
NCHARGES
]
=
{
charge_t
charges
[
NCHARGES
]
=
{
{.
q
=
-
0
.
25
,
.
pos
=
vec2_create
(
0
.
25
,
0
.
5
)
}
,
charge_create
(
-
0
.
25
,
vec2_create
(
0
.
25
,
0
.
5
)
)
,
{.
q
=
0
.
25
,
.
pos
=
vec2_create
(
0
.
75
,
0
.
5
)
},
charge_create
(
0
.
25
,
vec2_create
(
0
.
75
,
0
.
5
)
)
};
};
struct
gfx_context_t
*
ctxt
=
gfx_create
(
"elec"
,
WID
,
HEI
);
struct
gfx_context_t
*
ctxt
=
gfx_create
(
"elec"
,
WID
,
HEI
);
draw_everything
(
ctxt
,
charges
,
NCHARGES
,
NLINES
,
DX
,
0
.
0
,
1
.
0
,
0
.
0
,
1
.
0
);
draw_everything
(
ctxt
,
charges
,
NCHARGES
,
NLINES
,
DX
,
0
.
0
,
1
.
0
,
0
.
0
,
1
.
0
);
while
(
gfx_keypressed
()
!=
SDLK_ESCAPE
)
gfx_present
(
ctxt
);
while
(
gfx_keypressed
()
!=
SDLK_ESCAPE
)
gfx_present
(
ctxt
);
gfx_destroy
(
ctxt
);
gfx_destroy
(
ctxt
);
return
EXIT_SUCCESS
;
return
EXIT_SUCCESS
;
...
...
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