1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
pub mod physics;
pub mod vector;
use physics::{magnetic_acceleration, magnetic_force, north_pole_magnetic_field};
use vector::Vec3;
pub fn calc(
velocity: &mut Vec3,
position: &mut Vec3,
acceleration: &mut Vec3,
force: &mut Vec3,
mass: f32,
charge: f32,
intensity: f32,
time_ms: f32,
) {
*force = magnetic_force(
charge,
velocity,
&north_pole_magnetic_field(intensity, position.x, position.y, position.z),
);
*acceleration = magnetic_acceleration(&force, mass);
*velocity = *velocity + *acceleration * ((time_ms / 1000.0) as f32);
*position = *position + *velocity * ((time_ms / 1000.0) as f32);
}
pub fn iterate(
count: u32,
velocity: &mut Vec3,
position: &mut Vec3,
acceleration: &mut Vec3,
force: &mut Vec3,
mass: f32,
charge: f32,
intensity: f32,
time_ms: f32,
_print: bool,
) {
for i in 0..count {
calc(
velocity,
position,
acceleration,
force,
mass,
charge,
intensity,
time_ms,
);
if _print {
println!(
"\n----- {}ms -----\nForce: {:?}\nAcceleration: {:?}\nNew Velocity: {:?}\nPosition: {:?}\nGeoGebra point: ({}, {}, {})",
i as f32 * time_ms, force, acceleration, velocity, position, position.x, position.y, position.z
);
}
}
}
pub fn exec<'a>(
count: u32,
velocity: &'a mut Vec3,
position: &'a mut Vec3,
mass: f32,
charge: f32,
intensity: f32,
time_ms: f32,
_print: bool,
) -> (&'a mut Vec3, &'a mut Vec3, Vec3, Vec3) {
let mut force: Vec3 = Vec3::ZERO;
let mut acceleration: Vec3 = Vec3::ZERO;
iterate(
count,
velocity,
position,
&mut acceleration,
&mut force,
mass,
charge,
intensity,
time_ms,
_print,
);
(velocity, position, acceleration, force)
}