Skip to content

Instantly share code, notes, and snippets.

@Vatyx
Created February 21, 2017 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vatyx/39c1a727f05d78edaaf336b0387c1063 to your computer and use it in GitHub Desktop.
Save Vatyx/39c1a727f05d78edaaf336b0387c1063 to your computer and use it in GitHub Desktop.
#[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