Johann Woelper 5 years ago
commit
b5a558861e
5 changed files with 142 additions and 0 deletions
  1. 12 0
      .gitignore
  2. 12 0
      Cargo.toml
  3. 24 0
      loading_data.json
  4. 12 0
      sample.json
  5. 82 0
      src/lib.rs

+ 12 - 0
.gitignore

@@ -0,0 +1,12 @@
+/target
+**/*.rs.bk
+.history
+
+
+#Added by cargo
+#
+#already existing elements are commented out
+
+#/target
+#**/*.rs.bk
+Cargo.lock

+ 12 - 0
Cargo.toml

@@ -0,0 +1,12 @@
+[package]
+name = "drop"
+version = "0.1.0"
+authors = ["Johann Woelper <woelper@gmail.com>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+serde = "1.0"
+serde_derive = "1.0"
+serde_json = "1.0"

+ 24 - 0
loading_data.json

@@ -0,0 +1,24 @@
+{
+    "Hammerhead 14.3": {
+        "velocity": [
+            [0, 720],
+            [100, 653],
+            [200, 590],
+            [300, 530]
+        ],
+        "energy": [
+            [0, 3695],
+            [100, 3041],
+            [200, 2478],
+            [300, 2006]
+        ],
+        "trajectory": [
+            [50, 2.4],
+            [100, 4.0],
+            [150, 0],
+            [200, -10.1],
+            [250, -27.5],
+            [300, -53.3]
+        ]
+    }
+}

+ 12 - 0
sample.json

@@ -0,0 +1,12 @@
+{
+  "Hammerhead": {
+    "velocity": [
+      [
+        0,
+        720.0
+      ]
+    ],
+    "energy": [],
+    "trajectory": []
+  }
+}

+ 82 - 0
src/lib.rs

@@ -0,0 +1,82 @@
+#[macro_use]
+extern crate serde_derive;
+extern crate serde;
+extern crate serde_json;
+use std::io::BufWriter;
+use std::io::BufReader;
+use std::fs::File;
+use std::collections::HashMap;
+
+#[derive(Serialize, Deserialize, Debug, Default)]
+struct BallisticsData {
+    // velocity:   HashMap<i32, f32>,
+    // energy:     HashMap<i32, f32>,
+    // trajectory: HashMap<i32, f32>
+    velocity:   Vec<(i32, f32)>,
+    energy:     Vec<(i32, f32)>,
+    trajectory: Vec<(i32, f32)>,
+}
+
+impl BallisticsData {
+    fn velocity_at(&self, d: i32) -> f32{
+        for i in 0..self.velocity.len() {
+            if self.velocity[i].0 >= d {
+                let mu = normalized_pos_in_range(self.velocity[i-1].0 as f32, self.velocity[i].0 as f32, d as f32);
+                dbg!(self.velocity[i].0, self.velocity[i].1, self.velocity[i+1].1);
+                return lerp(self.velocity[i-1].1 as f32, self.velocity[i].1 as f32, mu);
+            }
+        }
+        0.0
+    }
+
+
+}
+
+fn normalized_pos_in_range(min: f32, max: f32, d: f32) -> f32 {
+    (d - min) / (max - min)
+}
+
+fn lerp(y1: f32, y2: f32, mu: f32) -> f32 {
+    y1*(1.0-mu)+y2*mu
+}
+
+
+
+fn write_sample() {
+    let mut bd = BallisticsData::default();
+    bd.velocity.push((0,720.0));
+    let mut map = HashMap::new();
+    map.insert("Hammerhead", bd);
+    // write out the file
+    let writer = BufWriter::new(File::create("sample.json").unwrap());
+    serde_json::to_writer_pretty(writer, &map).unwrap();
+}
+
+
+fn load_cartridges() -> HashMap<String, BallisticsData> {
+    let reader = BufReader::new(File::open("loading_data.json").unwrap());
+    serde_json::from_reader(reader).unwrap()
+    // println!("{:?}", loaded_car);
+}
+
+
+
+
+#[test]
+fn w() {
+    write_sample();
+
+    let c = load_cartridges();
+    // dbg!(&c);
+    
+    let d = 130;
+
+    for (name, cartridge) in c {
+        println!("Cartridge {}", name);
+        println!("Cartridge {}", cartridge.velocity_at(d));
+    }
+
+
+
+
+}