Commit c52e4e27 authored by Luke Jones's avatar Luke Jones

Vec2d use floats only

parent 5d5fb7d1
Pipeline #62785053 passed with stages
in 5 minutes and 57 seconds
......@@ -7,6 +7,7 @@ pub struct Vec2d<T> {
pub y: T,
}
use num_traits::float::Float;
use std::convert::From;
use std::ops::{
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign,
......@@ -62,7 +63,7 @@ impl_vec_ops_for!(f32 f64);
Standard Vector operators
******************************************/
impl<T> Vec2d<T> {
impl<T: Float> Vec2d<T> {
pub fn new(x: T, y: T) -> Vec2d<T>
where
T: Mul,
......@@ -78,8 +79,8 @@ impl<T> Vec2d<T> {
}
}
/// Add scalar to `Vec2d`
impl<T: Add + Copy> Add<T> for Vec2d<T> {
type Output = Vec2d<T::Output>;
impl<T: Float + Add + Copy> Add<T> for Vec2d<T> {
type Output = Vec2d<T>;
fn add(self, rhs: T) -> Self::Output {
Vec2d {
x: self.x + rhs,
......@@ -88,8 +89,8 @@ impl<T: Add + Copy> Add<T> for Vec2d<T> {
}
}
/// Add two `Vec2d` together
impl<T: Add> Add for Vec2d<T> {
type Output = Vec2d<T::Output>;
impl<T: Float + Add> Add for Vec2d<T> {
type Output = Vec2d<T>;
fn add(self, rhs: Self) -> Self::Output {
Vec2d {
x: self.x + rhs.x,
......@@ -98,8 +99,8 @@ impl<T: Add> Add for Vec2d<T> {
}
}
/// Subtract a scalar from this `Vec2d`
impl<T: Sub + Copy> Sub<T> for Vec2d<T> {
type Output = Vec2d<T::Output>;
impl<T: Float + Sub + Copy> Sub<T> for Vec2d<T> {
type Output = Vec2d<T>;
fn sub(self, rhs: T) -> Self::Output {
Vec2d {
x: self.x - rhs,
......@@ -108,8 +109,8 @@ impl<T: Sub + Copy> Sub<T> for Vec2d<T> {
}
}
/// Subtract a `Vec2d` from this `Vec2d`
impl<T: Sub> Sub for Vec2d<T> {
type Output = Vec2d<T::Output>;
impl<T: Float + Sub> Sub for Vec2d<T> {
type Output = Vec2d<T>;
fn sub(self, rhs: Self) -> Self::Output {
Vec2d {
x: self.x - rhs.x,
......@@ -118,8 +119,8 @@ impl<T: Sub> Sub for Vec2d<T> {
}
}
/// Multiply `Vec2d` by scalar
impl<T: Mul + Copy> Mul<T> for Vec2d<T> {
type Output = Vec2d<T::Output>;
impl<T: Float + Mul + Copy> Mul<T> for Vec2d<T> {
type Output = Vec2d<T>;
fn mul(self, rhs: T) -> Self::Output {
Vec2d {
x: self.x * rhs,
......@@ -128,7 +129,7 @@ impl<T: Mul + Copy> Mul<T> for Vec2d<T> {
}
}
/// Calculate and return the scalar product of this `Vec2d` by `Vec2d`
impl<T: Mul + Add> Mul for Vec2d<T>
impl<T: Float + Mul + Add> Mul for Vec2d<T>
where
<T as Mul>::Output: Add {
type Output = <<T as Mul>::Output as Add>::Output;
......@@ -137,8 +138,8 @@ impl<T: Mul + Add> Mul for Vec2d<T>
}
}
/// Divide `Vec2d` by scalar
impl<T: Div + Copy> Div<T> for Vec2d<T> {
type Output = Vec2d<T::Output>;
impl<T: Float + Div + Copy> Div<T> for Vec2d<T> {
type Output = Vec2d<T>;
fn div(self, rhs: T) -> Self::Output {
Vec2d {
x: self.x / rhs,
......@@ -147,8 +148,8 @@ impl<T: Div + Copy> Div<T> for Vec2d<T> {
}
}
/// Divide `Vec2d` by scalar
impl<T: Div> Div for Vec2d<T> {
type Output = Vec2d<T::Output>;
impl<T: Float + Div> Div for Vec2d<T> {
type Output = Vec2d<T>;
fn div(self, rhs: Self) -> Self::Output {
Vec2d {
x: self.x / rhs.x,
......@@ -157,64 +158,64 @@ impl<T: Div> Div for Vec2d<T> {
}
}
impl<T: AddAssign + Copy> AddAssign<T> for Vec2d<T> {
impl<T: Float + AddAssign + Copy> AddAssign<T> for Vec2d<T> {
fn add_assign(&mut self, rhs: T) {
self.x += rhs;
self.y += rhs;
}
}
impl<T: AddAssign> AddAssign<Vec2d<T>> for Vec2d<T> {
impl<T: Float + AddAssign> AddAssign<Vec2d<T>> for Vec2d<T> {
fn add_assign(&mut self, rhs: Self) {
self.x += rhs.x;
self.y += rhs.y;
}
}
impl<T: SubAssign + Copy> SubAssign<T> for Vec2d<T> {
impl<T: Float + SubAssign + Copy> SubAssign<T> for Vec2d<T> {
fn sub_assign(&mut self, rhs: T) {
self.x -= rhs;
self.y -= rhs;
}
}
impl<T: SubAssign> SubAssign<Vec2d<T>> for Vec2d<T> {
impl<T: Float + SubAssign> SubAssign<Vec2d<T>> for Vec2d<T> {
fn sub_assign(&mut self, rhs: Self) {
self.x -= rhs.x;
self.y -= rhs.y;
}
}
impl<T: MulAssign + Copy> MulAssign<T> for Vec2d<T> {
impl<T: Float + MulAssign + Copy> MulAssign<T> for Vec2d<T> {
fn mul_assign(&mut self, rhs: T) {
self.x *= rhs;
self.y *= rhs;
}
}
impl<T: MulAssign> MulAssign<Vec2d<T>> for Vec2d<T> {
impl<T: Float + MulAssign> MulAssign<Vec2d<T>> for Vec2d<T> {
fn mul_assign(&mut self, rhs: Self) {
self.x *= rhs.x;
self.y *= rhs.y;
}
}
impl<T: DivAssign + Copy> DivAssign<T> for Vec2d<T> {
impl<T: Float + DivAssign + Copy> DivAssign<T> for Vec2d<T> {
fn div_assign(&mut self, rhs: T) {
self.x /= rhs;
self.y /= rhs;
}
}
impl<T: DivAssign> DivAssign<Vec2d<T>> for Vec2d<T> {
impl<T: Float + DivAssign> DivAssign<Vec2d<T>> for Vec2d<T> {
fn div_assign(&mut self, rhs: Self) {
self.x /= rhs.x;
self.y /= rhs.y;
}
}
impl<T: Neg> Neg for Vec2d<T> {
type Output = Vec2d<T::Output>;
impl<T: Float + Neg> Neg for Vec2d<T> {
type Output = Vec2d<T>;
fn neg(self) -> Self::Output {
Vec2d {
......@@ -231,7 +232,7 @@ impl<T> Into<[T; 2]> for Vec2d<T> {
}
}
impl<T: Copy> From<[T; 2]> for Vec2d<T> {
impl<T: Float + Copy> From<[T; 2]> for Vec2d<T> {
#[inline]
fn from(array: [T; 2]) -> Self {
Vec2d {
......@@ -325,4 +326,16 @@ mod tests {
let point: Vec2d<f32> = Vec2d::new(4.0, 3.0);
assert_eq!(point.magnitude(), 5.0);
}
#[test]
fn normalise_f64() {
let point: Vec2d<f64> = Vec2d::new(4.0, 3.0);
assert_eq!(point.normalise(), Vec2d::new(0.8, 0.6000000000000001));
}
#[test]
fn magnitude_f64() {
let point: Vec2d<f64> = Vec2d::new(4.0, 3.0);
assert_eq!(point.magnitude(), 5.0);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment