diff --git a/src/homogen.rs b/src/homogen.rs index 86ce833..2bf9baa 100644 --- a/src/homogen.rs +++ b/src/homogen.rs @@ -12,6 +12,7 @@ use vector::{TypedVector2D, TypedVector3D}; use num::{One, Zero}; +use std::fmt; use std::marker::PhantomData; use std::ops::Div; @@ -82,6 +83,18 @@ impl From> for HomogeneousVector { } } +impl fmt::Debug for HomogeneousVector { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "({:?},{:?},{:?},{:?})", self.x, self.y, self.z, self.w) + } +} + +impl fmt::Display for HomogeneousVector { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "({},{},{},{})", self.x, self.y, self.z, self.w) + } +} + #[cfg(test)] mod homogeneous { diff --git a/src/rect.rs b/src/rect.rs index 03d4ffc..750d15c 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -19,11 +19,14 @@ use size::TypedSize2D; use num_traits::NumCast; #[cfg(feature = "serde")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +use std::borrow::Borrow; use std::cmp::PartialOrd; use std::fmt; use std::hash::{Hash, Hasher}; use std::ops::{Add, Div, Mul, Sub}; + /// A 2d Rectangle optionally tagged with a unit. #[repr(C)] pub struct TypedRect { @@ -281,34 +284,32 @@ where /// semantic on these edges. This means that the right-most and bottom-most /// points provided to `from_points` will count as not contained by the rect. /// This behavior may change in the future. - pub fn from_points<'a, I>(points: I) -> Self + pub fn from_points(points: I) -> Self where - U: 'a, - T: 'a, - I: IntoIterator>, + I: IntoIterator, + I::Item: Borrow>, { let mut points = points.into_iter(); - let first = if let Some(first) = points.next() { - first - } else { - return TypedRect::zero(); + let (mut min_x, mut min_y) = match points.next() { + Some(first) => (first.borrow().x, first.borrow().y), + None => return TypedRect::zero(), }; - let (mut min_x, mut min_y) = (first.x, first.y); let (mut max_x, mut max_y) = (min_x, min_y); for point in points { - if point.x < min_x { - min_x = point.x + let p = point.borrow(); + if p.x < min_x { + min_x = p.x } - if point.x > max_x { - max_x = point.x + if p.x > max_x { + max_x = p.x } - if point.y < min_y { - min_y = point.y + if p.y < min_y { + min_y = p.y } - if point.y > max_y { - max_y = point.y + if p.y > max_y { + max_y = p.y } } TypedRect::new( diff --git a/src/vector.rs b/src/vector.rs index 37974aa..deef463 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -279,7 +279,7 @@ impl, U> DivAssign for TypedVector2D { impl, U1, U2> Mul> for TypedVector2D { type Output = TypedVector2D; #[inline] - fn mul(self, scale: TypedScale) -> TypedVector2D { + fn mul(self, scale: TypedScale) -> Self::Output { vec2(self.x * scale.get(), self.y * scale.get()) } } @@ -287,7 +287,7 @@ impl, U1, U2> Mul> for TypedV impl, U1, U2> Div> for TypedVector2D { type Output = TypedVector2D; #[inline] - fn div(self, scale: TypedScale) -> TypedVector2D { + fn div(self, scale: TypedScale) -> Self::Output { vec2(self.x / scale.get(), self.y / scale.get()) } } @@ -704,6 +704,22 @@ impl TypedVector3D { } } +impl, U1, U2> Mul> for TypedVector3D { + type Output = TypedVector3D; + #[inline] + fn mul(self, scale: TypedScale) -> Self::Output { + vec3(self.x * scale.get(), self.y * scale.get(), self.z * scale.get()) + } +} + +impl, U1, U2> Div> for TypedVector3D { + type Output = TypedVector3D; + #[inline] + fn div(self, scale: TypedScale) -> Self::Output { + vec3(self.x / scale.get(), self.y / scale.get(), self.z / scale.get()) + } +} + impl TypedVector3D { /// Rounds each component to the nearest integer value. /// @@ -971,7 +987,7 @@ mod typedvector2d { #[test] pub fn test_scalar_mul() { let p1 = Vector2DMm::new(1.0, 2.0); - let cm_per_mm: TypedScale = TypedScale::new(0.1); + let cm_per_mm = TypedScale::::new(0.1); let result: Vector2DCm = p1 * cm_per_mm; @@ -987,7 +1003,9 @@ mod typedvector2d { #[cfg(test)] mod vector3d { - use super::{Vector3D, vec2, vec3}; + use super::{TypedVector3D, Vector3D, vec2, vec3}; + use scale::TypedScale; + type Vec3 = Vector3D; #[test] @@ -1037,6 +1055,19 @@ mod vector3d { assert_eq!(result, vec3(2.0, 3.0, 5.0)); } + #[test] + pub fn test_scalar_mul() { + enum Mm {} + enum Cm {} + + let p1 = TypedVector3D::::new(1.0, 2.0, 3.0); + let cm_per_mm = TypedScale::::new(0.1); + + let result: TypedVector3D = p1 * cm_per_mm; + + assert_eq!(result, vec3(0.1, 0.2, 0.3)); + } + #[test] pub fn test_swizzling() { let p: Vector3D = vec3(1, 2, 3);