use std::ops::{Add, Sub, Mul, Div, Neg};
use sfml::window::VideoMode;
#[derive(Debug, Clone, Copy)]
pub struct Vector<T: num::Num> (pub T, pub T);
impl<T: num::Num + num::NumCast + Copy> Vector<T> {
pub fn cast<U: num::Num + num::NumCast>(self) -> Vector<U> {
Vector(num::cast(self.0).unwrap(), num::cast(self.1).unwrap())
}
pub fn scalar<U: num::NumCast>(self, rhs: Vector<T>) -> U {
num::cast(self.0 * rhs.0 + self.1 * rhs.1).unwrap()
}
pub fn mag<U: num::Float>(self) -> U {
let scalar: U = self.scalar(self);
scalar.sqrt()
}
pub fn norm<U: num::Float>(self) -> Vector<U> {
let mag = self.mag::<U>();
let mut new = Vector::<U>(num::cast(self.0).unwrap(), num::cast(self.1).unwrap());
if mag > num::cast(0).unwrap() {
new.0 = new.0 / mag;
new.1 = new.1 / mag;
}
new
}
}
impl<T: num::Num + num::NumCast> Into<VideoMode> for Vector<T> {
fn into(self) -> VideoMode {
VideoMode {
width: num::cast(self.0).unwrap(),
height: num::cast(self.1).unwrap(),
bits_per_pixel: VideoMode::desktop_mode().bits_per_pixel
}
}
}
impl<T: num::Num> Into<Vector<T>> for (T, T) {
fn into(self) -> Vector<T> {
Vector(self.0, self.1)
}
}
impl<T: num::Num> Add<Vector<T>> for Vector<T> {
type Output = Self;
fn add(self, rhs: Vector<T>) -> Self::Output {
Self(self.0 + rhs.0, self.1 + rhs.1)
}
}
impl<T: num::Num> Sub<Vector<T>> for Vector<T> {
type Output = Self;
fn sub(self, rhs: Vector<T>) -> Self::Output {
Self(self.0 - rhs.0, self.1 - rhs.1)
}
}
impl<T: num::Num> Mul<Vector<T>> for Vector<T> {
type Output = Self;
fn mul(self, rhs: Vector<T>) -> Self::Output {
Self(self.0 * rhs.0, self.1 * rhs.1)
}
}
impl<T: num::Num> Div<Vector<T>> for Vector<T> {
type Output = Self;
fn div(self, rhs: Vector<T>) -> Self::Output {
Self(self.0 / rhs.0, self.1 / rhs.1)
}
}
impl<T: num::Num + Copy> Add<T> for Vector<T> {
type Output = Self;
fn add(self, rhs: T) -> Self::Output {
Self(self.0 + rhs, self.1 + rhs)
}
}
impl<T: num::Num + Copy> Sub<T> for Vector<T> {
type Output = Self;
fn sub(self, rhs: T) -> Self::Output {
Self(self.0 - rhs, self.1 - rhs)
}
}
impl<T: num::Num + Copy> Mul<T> for Vector<T> {
type Output = Self;
fn mul(self, rhs: T) -> Self::Output {
Self(self.0 * rhs, self.1 * rhs)
}
}
impl<T: num::Num + Copy> Div<T> for Vector<T> {
type Output = Self;
fn div(self, rhs: T) -> Self::Output {
Self(self.0 / rhs, self.1 / rhs)
}
}
impl<T: num::Num + Neg<Output = T>> Neg for Vector<T> {
type Output = Self;
fn neg(self) -> Self::Output {
Self(-self.0, -self.1)
}
}