diff --git a/src/point.rs b/src/point.rs index 362f1b8..33dfc14 100644 --- a/src/point.rs +++ b/src/point.rs @@ -172,6 +172,19 @@ macro_rules! impl_point { fn rem_assign(&mut self, scalar) { $(self.$field %= scalar);+ } }); + impl_scalar_ops!($PointN { $($field),+ }); + impl_scalar_ops!($PointN { $($field),+ }); + impl_scalar_ops!($PointN { $($field),+ }); + impl_scalar_ops!($PointN { $($field),+ }); + impl_scalar_ops!($PointN { $($field),+ }); + impl_scalar_ops!($PointN { $($field),+ }); + impl_scalar_ops!($PointN { $($field),+ }); + impl_scalar_ops!($PointN { $($field),+ }); + impl_scalar_ops!($PointN { $($field),+ }); + impl_scalar_ops!($PointN { $($field),+ }); + impl_scalar_ops!($PointN { $($field),+ }); + impl_scalar_ops!($PointN { $($field),+ }); + impl_index_operators!($PointN, $n, S, usize); impl_index_operators!($PointN, $n, [S], Range); impl_index_operators!($PointN, $n, [S], RangeTo); @@ -180,6 +193,20 @@ macro_rules! impl_point { } } +macro_rules! impl_scalar_ops { + ($PointN:ident<$S:ident> { $($field:ident),+ }) => { + impl_operator!(Mul<$PointN<$S>> for $S { + fn mul(scalar, vector) -> $PointN<$S> { $PointN::new($(scalar * vector.$field),+) } + }); + impl_operator!(Div<$PointN<$S>> for $S { + fn div(scalar, vector) -> $PointN<$S> { $PointN::new($(scalar / vector.$field),+) } + }); + impl_operator!(Rem<$PointN<$S>> for $S { + fn rem(scalar, vector) -> $PointN<$S> { $PointN::new($(scalar % vector.$field),+) } + }); + }; +} + impl_point!(Point2 { x, y }, Vector2, 2); impl_point!(Point3 { x, y, z }, Vector3, 3); diff --git a/tests/point.rs b/tests/point.rs index 3dbfef6..fcd2714 100644 --- a/tests/point.rs +++ b/tests/point.rs @@ -16,11 +16,62 @@ extern crate cgmath; -use cgmath::Point3; +use cgmath::{Point2, Point3}; use cgmath::ApproxEq; +macro_rules! impl_test_mul { + ($PointN:ident { $($field:ident),+ }, $s:expr, $v:expr) => ( + // point * scalar ops + assert_eq!($v * $s, $PointN::new($($v.$field * $s),+)); + assert_eq!($s * $v, $PointN::new($($s * $v.$field),+)); + assert_eq!(&$v * $s, $v * $s); + assert_eq!($s * &$v, $s * $v); + // commutativity + assert_eq!($v * $s, $s * $v); + ) +} + +macro_rules! impl_test_div { + ($PointN:ident { $($field:ident),+ }, $s:expr, $v:expr) => ( + // point / scalar ops + assert_eq!($v / $s, $PointN::new($($v.$field / $s),+)); + assert_eq!($s / $v, $PointN::new($($s / $v.$field),+)); + assert_eq!(&$v / $s, $v / $s); + assert_eq!($s / &$v, $s / $v); + ) +} + +macro_rules! impl_test_rem { + ($PointN:ident { $($field:ident),+ }, $s:expr, $v:expr) => ( + // point % scalar ops + assert_eq!($v % $s, $PointN::new($($v.$field % $s),+)); + assert_eq!($s % $v, $PointN::new($($s % $v.$field),+)); + assert_eq!(&$v % $s, $v % $s); + assert_eq!($s % &$v, $s % $v); + ) +} + #[test] fn test_homogeneous() { let p = Point3::new(1.0f64, 2.0f64, 3.0f64); assert!(p.approx_eq(&Point3::from_homogeneous(p.to_homogeneous()))); } + +#[test] +fn test_mul() { + impl_test_mul!(Point3 { x, y, z }, 2.0f32, Point3::new(2.0f32, 4.0, 6.0)); + impl_test_mul!(Point2 { x, y }, 2.0f32, Point2::new(2.0f32, 4.0)); +} + +#[test] +fn test_div() { + impl_test_div!(Point3 { x, y, z }, 2.0f32, Point3::new(2.0f32, 4.0, 6.0)); + impl_test_div!(Point2 { x, y }, 2.0f32, Point2::new(2.0f32, 4.0)); +} + +#[test] +fn test_rem() { + impl_test_rem!(Point3 { x, y, z }, 2.0f32, Point3::new(2.0f32, 4.0, 6.0)); + impl_test_rem!(Point2 { x, y }, 2.0f32, Point2::new(2.0f32, 4.0)); +} +