Skip to content

Commit fdba9cf

Browse files
committedMar 14, 2024
feat: add awful natuurkunde code
1 parent 6fd4aeb commit fdba9cf

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed
 

‎Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ edition = "2021"
3030

3131
[dependencies]
3232
colored = "2.1.0"
33+
plotters = "0.3.5"
3334
rand = "0.8.5"
3435

3536

@@ -88,3 +89,7 @@ path = "Experiments/Minion/Minion.rs"
8889
[[bin]]
8990
name = "ErrorHandling"
9091
path = "Experiments/ErrorHandling/ErrorHandling.rs"
92+
93+
[[bin]]
94+
name = "NatuurkundeModellen"
95+
path = "Experiments/NatuurkundeModellen/NatuurkundeModellen.rs"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![allow(non_snake_case)]
2+
mod Util;
3+
4+
/// This is my awful code for school. Please don't look at it
5+
fn main() {
6+
let mut punten: Vec<(f64, f64)> = vec![]; // We store the x,y points here
7+
const HOOGTE: f32 = 1.0e3; // The height the kogel is dropped at. in metres
8+
let mut DELTA_HOOGTE: f32 = 1.0; // the difference in height
9+
const GRAVITATIECONSTANTE_NL: f32 = 9.81; // Gravitatieconstante
10+
const MASSA: f32 = 2.0; // mass of kogel in kg
11+
12+
while DELTA_HOOGTE >= 0.0 {
13+
println!(
14+
"x: {DELTA_HOOGTE}, y: {}",
15+
Util::kogelSnelheidBerekenen(HOOGTE, DELTA_HOOGTE, MASSA, GRAVITATIECONSTANTE_NL)
16+
);
17+
punten.push((
18+
DELTA_HOOGTE as f64,
19+
Util::kogelSnelheidBerekenen(HOOGTE, DELTA_HOOGTE, MASSA, GRAVITATIECONSTANTE_NL),
20+
));
21+
DELTA_HOOGTE -= 0.001; // each step is 0.001.
22+
}
23+
Util::plot(punten).expect("ERROR!");
24+
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use plotters::prelude::*;
2+
3+
/// Creates a plot.
4+
pub fn plot(lijnen: Vec<(f64, f64)>) -> Result<(), Box<dyn std::error::Error>> {
5+
let root = BitMapBackend::new("plot.png", (640, 480)).into_drawing_area();
6+
root.fill(&WHITE)?;
7+
let mut chart = ChartBuilder::on(&root)
8+
.caption("Kogelsnelheid", ("sans-serif", 30).into_font())
9+
.x_label_area_size(40)
10+
.y_label_area_size(40)
11+
.build_cartesian_2d(0.0..1.0, 0.0..4.5)?;
12+
13+
chart
14+
.configure_mesh()
15+
.x_desc("Hoogteverschil in m")
16+
.y_desc("Snelheid in m/s^2")
17+
.draw()?;
18+
19+
chart
20+
.draw_series(LineSeries::new(lijnen, &RED))?
21+
.legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &RED));
22+
23+
root.present()?;
24+
25+
Ok(())
26+
}
27+
28+
/// Calculates kogel snelheid
29+
pub fn kogelSnelheidBerekenen(
30+
mut hoogte: f32,
31+
deltaH: f32,
32+
massa: f32,
33+
valversnelling: f32,
34+
) -> f64 {
35+
hoogte -= deltaH;
36+
let zwaarte_energie = massa * valversnelling * hoogte;
37+
let kinetische_energie = 19620.0 - zwaarte_energie;
38+
(2f32 * kinetische_energie / massa).sqrt() as f64
39+
}

0 commit comments

Comments
 (0)