Created
February 21, 2017 07:47
-
-
Save Vatyx/39c1a727f05d78edaaf336b0387c1063 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Copy, Clone, Debug)] | |
pub struct Vector2<T> { | |
pub x: T, | |
pub y: T, | |
} | |
impl<T: Copy + Num + Signed> Vector2<T> { | |
pub fn new(x: T, y: T) -> Vector2<T> where T: Float { | |
assert!(!x.is_nan() && !y.is_nan()); | |
Vector2{x: x, y: y} | |
} | |
pub fn length(&self) -> f32 where T: NumCast + Add<T, Output = T> + Mul<T, Output = T> { | |
self.length_squared().sqrt() | |
} | |
pub fn length_squared(&self) -> f32 where T: NumCast + Add<T, Output = T> + Mul<T, Output = T> { | |
num::cast(self.x * self.x + self.y * self.y).unwrap() | |
} | |
pub fn abs(&self) -> Vector2<T> { | |
Vector2{x: self.x.abs(), y: self.y.abs()} | |
} | |
pub fn dot(&self, v2: Vector2<T>) -> T where T: Add<T, Output = T> + Mul<T, Output = T> { | |
self.x * v2.x + self.y * v2.y | |
} | |
pub fn abs_dot(&self, v2: Vector2<T>) -> T { | |
//self.dot(v2).abs() | |
num::abs(self.dot(v2)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment