Explorar el Código

added vec2 functionality for subtract & magnitude

Ali Hirschmann hace 4 años
padre
commit
1649a4b384
Se han modificado 1 ficheros con 11 adiciones y 1 borrados
  1. 11 1
      vector2/src/lib.rs

+ 11 - 1
vector2/src/lib.rs

@@ -3,9 +3,11 @@ pub struct Vector2 {
     pub x: i32,
     pub y: i32,
 }
-//Implement vector 2 functions
+
+//implement vector 2 functions
 impl Vector2 {   
 
+    //Direction
     pub fn right() -> Vector2 {
         Vector2{x:1, y:0}
     }
@@ -21,10 +23,18 @@ impl Vector2 {
     pub fn zero() -> Vector2 {
         Vector2{x:0, y:0}
     }
+
+    //Mathematical
     pub fn add(&self, other: &Self) -> Vector2 {
         Vector2{x: self.x + other.x, y: self.y + other.y}
     }
+    pub fn subtract(&self, other: &Self) -> Vector2 {
+        Vector2{x: self.x - other.x, y: self.y - other.y}
+    }
     pub fn distance(&self, to: &Self) -> i32{
         ((to.x - self.x.abs()) + (to.y - self.y.abs()))
     }
+    pub fn magnitude(&self) -> i32 {
+        (self.x * self.x + self.y * self.y)
+    }
 }