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