From 6634f73a6768f61f928d51941be5e970e6011f07 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Fri, 23 Feb 2018 07:40:05 -0600 Subject: -changed to floats and got vec type --- src/astroid.rs | 8 ++++---- src/bin/server.rs | 2 +- src/connection.rs | 22 +++++++++++----------- src/mass.rs | 6 +++--- src/ship.rs | 8 ++++---- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/astroid.rs b/src/astroid.rs index 37460cf..0eed4ff 100644 --- a/src/astroid.rs +++ b/src/astroid.rs @@ -4,11 +4,11 @@ extern crate serde_json; #[derive(Serialize, Deserialize, Debug)] pub struct Astroid { name : String, - location : (isize, isize, isize), + location : (f64, f64, f64), } impl Mass for Astroid { - fn new(name : &str, location : (isize, isize, isize)) -> Astroid { + fn new(name : &str, location : (f64, f64, f64)) -> Astroid { Astroid { name : String::from(name), location : location, @@ -19,11 +19,11 @@ impl Mass for Astroid { &self.name } - fn location(&self) -> (isize, isize, isize) { + fn location(&self) -> (f64, f64, f64) { self.location } - fn set_location(&mut self, location : (isize, isize, isize)) { + fn set_location(&mut self, location : (f64, f64, f64)) { self.location = location; } diff --git a/src/bin/server.rs b/src/bin/server.rs index e042855..c327257 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -11,7 +11,7 @@ use space::connection::Connection; fn populate() -> Vec> { let mut masses : Vec> = Vec::new(); - masses.push(Box::new(Astroid::new("cZfAJ", (10, -5, 4)))); + masses.push(Box::new(Astroid::new("cZfAJ", (10.0, -5.0, 4.0)))); masses } diff --git a/src/connection.rs b/src/connection.rs index ac57176..59031c6 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -28,7 +28,7 @@ impl Connection { let index = match result { Some(index) => index, None => { - let ship = Box::new(Ship::new(name, (0,0,0))); + let ship = Box::new(Ship::new(name, (0.0,0.0,0.0))); masses.push(ship); masses.len() - 1 }, @@ -67,12 +67,12 @@ impl Connection { let mut data = String::new(); match self.buff_r.read_line(&mut data) { Ok(result) => match data.as_bytes() { - b"5\n" => location.0 += 1, - b"0\n" => location.0 -= 1, - b"8\n" => location.1 += 1, - b"2\n" => location.1 -= 1, - b"4\n" => location.2 += 1, - b"6\n" => location.2 -= 1, + b"5\n" => location.0 += 1.0, + b"0\n" => location.0 -= 1.0, + b"8\n" => location.1 += 1.0, + b"2\n" => location.1 -= 1.0, + b"4\n" => location.2 += 1.0, + b"6\n" => location.2 -= 1.0, _ => { if result == 0 { self.open = false; @@ -86,8 +86,8 @@ impl Connection { Module::Navigation => { let ship = &masses[self.index].downcast_ref::().unwrap(); - let within_range : Vec<_> = masses.iter().filter(|mass| - distance(ship.location(), mass.location()) < ship.range()).collect(); + let within_range : Vec<&Box> = masses.iter().filter(|mass| + distance(ship.location(), mass.location()) < ship.range()).collect(); let mut send = String::new(); for mass in within_range { @@ -105,6 +105,6 @@ impl Connection { } -fn distance(l0 : (isize, isize, isize), l1 : (isize, isize, isize)) -> f64 { - (((l1.0-l0.0).pow(2) + (l1.1-l0.1).pow(2) + (l1.2-l0.2).pow(2)) as f64).sqrt() +fn distance(l0 : (f64, f64, f64), l1 : (f64, f64, f64)) -> f64 { + (((l1.0-l0.0).powf(2.0) + (l1.1-l0.1).powf(2.0) + (l1.2-l0.2).powf(2.0))).sqrt() } diff --git a/src/mass.rs b/src/mass.rs index 8c50348..0a49dc6 100644 --- a/src/mass.rs +++ b/src/mass.rs @@ -1,10 +1,10 @@ use downcast::Any; pub trait Mass : Any { - fn new(name : &str, location : (isize, isize, isize)) -> Self where Self: Sized; + fn new(name : &str, location : (f64, f64, f64)) -> Self where Self: Sized; fn name(&self) -> &String; - fn location(&self) -> (isize, isize, isize); - fn set_location(&mut self, location : (isize, isize, isize)); + fn location(&self) -> (f64, f64, f64); + fn set_location(&mut self, location : (f64, f64, f64)); fn serialize(&self) -> String; fn deserialize(&mut self, data : &str); } diff --git a/src/ship.rs b/src/ship.rs index 49da84d..e5186c8 100644 --- a/src/ship.rs +++ b/src/ship.rs @@ -4,7 +4,7 @@ extern crate serde_json; #[derive(Serialize, Deserialize, Debug)] pub struct Ship { name : String, - location : (isize, isize, isize), + location : (f64, f64, f64), r : f64, } @@ -15,7 +15,7 @@ impl Ship { } impl Mass for Ship { - fn new(name : &str, location : (isize, isize, isize)) -> Ship { + fn new(name : &str, location : (f64, f64, f64)) -> Ship { Ship { name : String::from(name), location : location, @@ -27,11 +27,11 @@ impl Mass for Ship { &self.name } - fn location(&self) -> (isize, isize, isize) { + fn location(&self) -> (f64, f64, f64) { self.location } - fn set_location(&mut self, location : (isize, isize, isize)) { + fn set_location(&mut self, location : (f64, f64, f64)) { self.location = location; } -- cgit v1.2.3