Commit eb78a0f0 authored by Luke Jones's avatar Luke Jones

Work check-in

parent 1495a8aa
Pipeline #63286313 passed with stages
in 4 minutes and 27 seconds
[package]
name = "vec2d"
version = "0.3.2"
version = "0.3.3"
authors = ["Luke Jones <luke@sphereidentity.com>"]
edition = "2018"
......
......@@ -72,6 +72,7 @@ impl<T> Vec2d<T> {
Vec2d { x: x, y: y }
}
}
/// Add scalar to `Vec2d`
impl<T: Float + Add + Copy> Add<T> for Vec2d<T> {
type Output = Vec2d<T>;
......@@ -167,6 +168,7 @@ impl<T: Float + Mul + Copy> Mul<T> for Vec2d<T> {
}
}
}
/// Calculate and return the scalar product of this `Vec2d` by `Vec2d`
impl<T: Float + Mul + Add> Mul for Vec2d<T>
where
......@@ -178,17 +180,6 @@ impl<T: Float + Mul + Add> Mul for Vec2d<T>
}
}
/// Calculate and return the cross product of this `Vec2d` by `Vec2d`
impl<T: Float + Mul + Sub> Rem for Vec2d<T>
where
<T as Mul>::Output: Add {
type Output = <<T as Mul>::Output as Sub>::Output;
#[inline]
fn rem(self, rhs: Self) -> Self::Output {
self.x * rhs.y - self.y * rhs.x
}
}
/// Multiply this `Vec2d` by scalar
impl<T: Float + MulAssign + Copy> MulAssign<T> for Vec2d<T> {
#[inline]
......@@ -250,6 +241,96 @@ impl<T: Float + DivAssign> DivAssign<Vec2d<T>> for Vec2d<T> {
}
}
// The following macros are ripped straight from core rust lib
//
// implements binary operators "&T op U", "T op &U", "&T op &U"
// based on "T op U" where T and U are expected to be `Copy`able
macro_rules! forward_ref_binop {
(impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
forward_ref_binop!(impl $imp, $method for $t, $u,
#[stable(feature = "rust1", since = "1.0.0")]);
};
(impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
impl<'a> $imp<$u> for &'a $t {
type Output = <$t as $imp<$u>>::Output;
#[inline]
fn $method(self, other: $u) -> <$t as $imp<$u>>::Output {
$imp::$method(*self, other)
}
}
impl $imp<&$u> for $t {
type Output = <$t as $imp<$u>>::Output;
#[inline]
fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
$imp::$method(self, *other)
}
}
impl $imp<&$u> for &$t {
type Output = <$t as $imp<$u>>::Output;
#[inline]
fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
$imp::$method(*self, *other)
}
}
}
}
forward_ref_binop! { impl Add, add for Vec2d<f32>, Vec2d<f32> }
forward_ref_binop! { impl Add, add for Vec2d<f32>, f32 }
forward_ref_binop! { impl Sub, sub for Vec2d<f32>, Vec2d<f32> }
forward_ref_binop! { impl Sub, sub for Vec2d<f32>, f32 }
forward_ref_binop! { impl Mul, mul for Vec2d<f32>, Vec2d<f32> }
forward_ref_binop! { impl Mul, mul for Vec2d<f32>, f32 }
forward_ref_binop! { impl Div, div for Vec2d<f32>, Vec2d<f32> }
forward_ref_binop! { impl Div, div for Vec2d<f32>, f32 }
forward_ref_binop! { impl Add, add for Vec2d<f64>, Vec2d<f64> }
forward_ref_binop! { impl Add, add for Vec2d<f64>, f64 }
forward_ref_binop! { impl Sub, sub for Vec2d<f64>, Vec2d<f64> }
forward_ref_binop! { impl Sub, sub for Vec2d<f64>, f64 }
forward_ref_binop! { impl Mul, mul for Vec2d<f64>, Vec2d<f64> }
forward_ref_binop! { impl Mul, mul for Vec2d<f64>, f64 }
forward_ref_binop! { impl Div, div for Vec2d<f64>, Vec2d<f64> }
forward_ref_binop! { impl Div, div for Vec2d<f64>, f64 }
// implements "T op= &U", based on "T op= U"
// where U is expected to be `Copy`able
macro_rules! forward_ref_op_assign {
(impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
forward_ref_op_assign!(impl $imp, $method for $t, $u,
#[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]);
};
(impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
impl $imp<&$u> for $t {
#[inline]
fn $method(&mut self, other: &$u) {
$imp::$method(self, *other);
}
}
}
}
forward_ref_op_assign! { impl AddAssign, add_assign for Vec2d<f32>, Vec2d<f32> }
forward_ref_op_assign! { impl AddAssign, add_assign for Vec2d<f32>, f32 }
forward_ref_op_assign! { impl SubAssign, sub_assign for Vec2d<f32>, Vec2d<f32> }
forward_ref_op_assign! { impl SubAssign, sub_assign for Vec2d<f32>, f32 }
forward_ref_op_assign! { impl MulAssign, mul_assign for Vec2d<f32>, Vec2d<f32> }
forward_ref_op_assign! { impl MulAssign, mul_assign for Vec2d<f32>, f32 }
forward_ref_op_assign! { impl DivAssign, div_assign for Vec2d<f32>, Vec2d<f32> }
forward_ref_op_assign! { impl DivAssign, div_assign for Vec2d<f32>, f32 }
forward_ref_op_assign! { impl AddAssign, add_assign for Vec2d<f64>, Vec2d<f64> }
forward_ref_op_assign! { impl AddAssign, add_assign for Vec2d<f64>, f64 }
forward_ref_op_assign! { impl SubAssign, sub_assign for Vec2d<f64>, Vec2d<f64> }
forward_ref_op_assign! { impl SubAssign, sub_assign for Vec2d<f64>, f64 }
forward_ref_op_assign! { impl MulAssign, mul_assign for Vec2d<f64>, Vec2d<f64> }
forward_ref_op_assign! { impl MulAssign, mul_assign for Vec2d<f64>, f64 }
forward_ref_op_assign! { impl DivAssign, div_assign for Vec2d<f64>, Vec2d<f64> }
forward_ref_op_assign! { impl DivAssign, div_assign for Vec2d<f64>, f64 }
/// Invert the sign of a `Vec2d`
impl<T: Float + Neg> Neg for Vec2d<T> {
type Output = Vec2d<T>;
......@@ -304,53 +385,108 @@ impl<T> From<(T, T)> for Vec2d<T> {
mod tests {
use super::*;
use std::f32::consts::PI;
use std::f64::consts::PI as F64_PI;
#[test]
fn multiply_two_vec2d_for_scalar() {
fn multiply_two_vec2d_for_scalar_f32() {
let point1: Vec2d<f32> = Vec2d::new(1.0, 2.0);
let point2: Vec2d<f32> = Vec2d::new(2.0, 3.0);
assert_eq!(point1 * point2, 8.0);
assert_eq!(&point1 * point2, 8.0);
assert_eq!(point1 * &point2, 8.0);
assert_eq!(&point1 * &point2, 8.0);
}
#[test]
fn multiply_vec2d_by_scalar_f32() {
let point1: Vec2d<f32> = Vec2d::new(1.0, 2.0);
let scalar: f32 = 3.0;
let res = Vec2d::new(3.0, 6.0);
assert_eq!(point1 * scalar, res);
assert_eq!(&point1 * scalar, res);
assert_eq!(point1 * &scalar, res);
assert_eq!(&point1 * &scalar, res);
}
#[test]
fn unit_vector_float_32() {
fn multiply_two_vec2d_for_scalar_f64() {
let point1: Vec2d<f64> = Vec2d::new(1.0, 2.0);
let point2: Vec2d<f64> = Vec2d::new(2.0, 3.0);
assert_eq!(point1 * point2, 8.0);
assert_eq!(&point1 * point2, 8.0);
assert_eq!(point1 * &point2, 8.0);
assert_eq!(&point1 * &point2, 8.0);
}
#[test]
fn multiply_vec2d_by_scalar_f64() {
let point1: Vec2d<f64> = Vec2d::new(1.0, 2.0);
let scalar: f64 = 3.0;
let res = Vec2d::new(3.0, 6.0);
assert_eq!(point1 * scalar, res);
assert_eq!(&point1 * scalar, res);
assert_eq!(point1 * &scalar, res);
assert_eq!(&point1 * &scalar, res);
}
#[test]
fn unit_vector_32() {
let unit: Vec2d<f32> = Vec2d::<f32>::unit_vector(PI);
assert_eq!(unit, Vec2d::new(-1.0, -0.00000008742278));
}
#[test]
fn normalise_float_32() {
fn unit_vector_64() {
let unit: Vec2d<f64> = Vec2d::<f64>::unit_vector(F64_PI);
assert_eq!(unit, Vec2d::new(-1.0, 0.00000000000000012246467991473532));
}
#[test]
fn normalise_32() {
let point: Vec2d<f32> = Vec2d::new(4.0, 3.0);
assert_eq!(point.normalise(), Vec2d::new(0.8, 0.6));
}
#[test]
fn magnitude_float_32() {
fn normalise_64() {
let point: Vec2d<f64> = Vec2d::new(4.0, 3.0);
assert_eq!(point.normalise(), Vec2d::new(0.8, 0.6000000000000001));
}
#[test]
fn magnitude_32() {
let point: Vec2d<f32> = Vec2d::new(4.0, 3.0);
assert_eq!(point.magnitude(), 5.0);
assert_eq!(point.magnitude(), 5.0);
}
#[test]
fn direction_pi_float_32() {
fn magnitude_64() {
let point: Vec2d<f64> = Vec2d::new(4.0, 3.0);
assert_eq!(point.magnitude(), 5.0);
}
#[test]
fn direction_pi_32() {
let point: Vec2d<f32> = Vec2d::new(-1.0, 0.0);
assert_eq!(point.direction(), PI);
}
#[test]
fn direction_pi2_float_32() {
let point: Vec2d<f32> = Vec2d::new(0.0, 1.0);
assert_eq!(point.direction(), PI / 2.0);
fn direction_pi_64() {
let point: Vec2d<f64> = Vec2d::new(-1.0, 0.0);
assert_eq!(point.direction(), F64_PI);
}
#[test]
fn normalise_f32() {
let point: Vec2d<f32> = Vec2d::new(4.0, 3.0);
assert_eq!(point.normalise(), Vec2d::new(0.8, 0.6));
fn direction_pi2_32() {
let point: Vec2d<f32> = Vec2d::new(0.0, 1.0);
assert_eq!(point.direction(), PI / 2.0);
}
#[test]
fn magnitude_f32() {
let point: Vec2d<f32> = Vec2d::new(4.0, 3.0);
assert_eq!(point.magnitude(), 5.0);
fn direction_pi2_64() {
let point: Vec2d<f64> = Vec2d::new(0.0, 1.0);
assert_eq!(point.direction(), F64_PI / 2.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