Skip to content

Instantly share code, notes, and snippets.

@dermesser
Created October 23, 2018 07:28
Show Gist options
  • Save dermesser/24882241de9b4c75996051ba409775c2 to your computer and use it in GitHub Desktop.
Save dermesser/24882241de9b4c75996051ba409775c2 to your computer and use it in GitHub Desktop.
extern crate getopts;
use getopts::Options;
#[derive(Clone, Debug)]
struct State {
t: f64,
h: f64,
x: f64,
v_vert: f64,
v_horiz: f64,
g: f64,
}
fn iterate(s: State, delta: f64) -> State {
let mut new = s.clone();
let new_v_vert = s.v_vert - delta * s.g;
new.t += delta;
new.h += delta * ((s.v_vert + new_v_vert) / 2.);
new.x += delta * s.v_horiz;
new.v_vert = new_v_vert;
new.v_horiz = s.v_horiz;
return new;
}
fn main() {
let args: Vec<String> = std::env::args().collect();
let mut opts = Options::new();
opts.optopt("h", "h0", "initial height", "");
opts.optopt("v", "v0", "initial speed", "");
opts.optopt("b", "beta", "angle", "");
opts.optopt("t", "", "delta-t", "");
opts.optopt("g", "", "g", "");
let o = opts.parse(&args[1..]);
if !o.is_ok() {
eprintln!("Parsing options failed: {:?}", o.err().unwrap());
return;
}
let o = o.unwrap();
let h0: f64 = o.opt_get_default("h", 0.0).unwrap();
let v0: f64 = o.opt_get_default("v", 25.).unwrap();
let beta: f64 = o.opt_get_default("b", 30.).unwrap();
let t: f64 = o.opt_get_default("t", 0.1).unwrap();
let g: f64 = o.opt_get_default("g", 9.81).unwrap();
let init = State {
t: 0.,
h: h0,
x: 0.,
v_vert: ((beta / 180.) * std::f64::consts::PI).sin() * v0,
v_horiz: ((beta / 180.) * std::f64::consts::PI).cos() * v0,
g: g,
};
println!("{:?}", init);
let mut current = init;
loop {
current = iterate(current, t);
if current.h < 0. {
break;
}
println!("{:10.3} {:10.3} {:10.3}", current.t, current.x, current.h);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment