diff --git a/src/length.rs b/src/length.rs index 2cc7d9e..7e39c77 100644 --- a/src/length.rs +++ b/src/length.rs @@ -127,7 +127,6 @@ impl Zero for Length { mod tests { use super::Length; use scale_factor::ScaleFactor; - use num::Zero; #[derive(Debug)] enum Inch {} diff --git a/src/matrix.rs b/src/matrix.rs index 4641109..e3f3f74 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -10,19 +10,6 @@ use approxeq::ApproxEq; use point::Point2D; -pub fn Matrix4( - m11: f32, m12: f32, m13: f32, m14: f32, - m21: f32, m22: f32, m23: f32, m24: f32, - m31: f32, m32: f32, m33: f32, m34: f32, - m41: f32, m42: f32, m43: f32, m44: f32) - -> Matrix4 { - Matrix4 { - m11: m11, m12: m12, m13: m13, m14: m14, - m21: m21, m22: m22, m23: m23, m24: m24, - m31: m31, m32: m32, m33: m33, m34: m34, - m41: m41, m42: m42, m43: m43, m44: m44 - } -} #[derive(Debug, Copy, Clone)] pub struct Matrix4 { @@ -33,6 +20,55 @@ pub struct Matrix4 { } impl Matrix4 { + pub fn new( + m11: f32, m12: f32, m13: f32, m14: f32, + m21: f32, m22: f32, m23: f32, m24: f32, + m31: f32, m32: f32, m33: f32, m34: f32, + m41: f32, m42: f32, m43: f32, m44: f32) + -> Matrix4 { + Matrix4 { + m11: m11, m12: m12, m13: m13, m14: m14, + m21: m21, m22: m22, m23: m23, m24: m24, + m31: m31, m32: m32, m33: m33, m34: m34, + m41: m41, m42: m42, m43: m43, m44: m44 + } + } + + pub fn ortho(left: f32, right: f32, + bottom: f32, top: f32, + near: f32, far: f32) -> Matrix4 { + let tx = -((right + left) / (right - left)); + let ty = -((top + bottom) / (top - bottom)); + let tz = -((far + near) / (far - near)); + + Matrix4::new(2.0 / (right - left), + 0.0, + 0.0, + 0.0, + + 0.0, + 2.0 / (top - bottom), + 0.0, + 0.0, + + 0.0, + 0.0, + -2.0 / (far - near), + 0.0, + + tx, + ty, + tz, + 1.0) + } + + pub fn identity() -> Matrix4 { + Matrix4::new(1.0, 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + 0.0, 0.0, 0.0, 1.0) + } + pub fn approx_eq(&self, other: &Matrix4) -> bool { self.m11.approx_eq(&other.m11) && self.m12.approx_eq(&other.m12) && self.m13.approx_eq(&other.m13) && self.m14.approx_eq(&other.m14) && @@ -45,43 +81,43 @@ impl Matrix4 { } pub fn mul(&self, m: &Matrix4) -> Matrix4 { - Matrix4(m.m11*self.m11 + m.m12*self.m21 + m.m13*self.m31 + m.m14*self.m41, - m.m11*self.m12 + m.m12*self.m22 + m.m13*self.m32 + m.m14*self.m42, - m.m11*self.m13 + m.m12*self.m23 + m.m13*self.m33 + m.m14*self.m43, - m.m11*self.m14 + m.m12*self.m24 + m.m13*self.m34 + m.m14*self.m44, - m.m21*self.m11 + m.m22*self.m21 + m.m23*self.m31 + m.m24*self.m41, - m.m21*self.m12 + m.m22*self.m22 + m.m23*self.m32 + m.m24*self.m42, - m.m21*self.m13 + m.m22*self.m23 + m.m23*self.m33 + m.m24*self.m43, - m.m21*self.m14 + m.m22*self.m24 + m.m23*self.m34 + m.m24*self.m44, - m.m31*self.m11 + m.m32*self.m21 + m.m33*self.m31 + m.m34*self.m41, - m.m31*self.m12 + m.m32*self.m22 + m.m33*self.m32 + m.m34*self.m42, - m.m31*self.m13 + m.m32*self.m23 + m.m33*self.m33 + m.m34*self.m43, - m.m31*self.m14 + m.m32*self.m24 + m.m33*self.m34 + m.m34*self.m44, - m.m41*self.m11 + m.m42*self.m21 + m.m43*self.m31 + m.m44*self.m41, - m.m41*self.m12 + m.m42*self.m22 + m.m43*self.m32 + m.m44*self.m42, - m.m41*self.m13 + m.m42*self.m23 + m.m43*self.m33 + m.m44*self.m43, - m.m41*self.m14 + m.m42*self.m24 + m.m43*self.m34 + m.m44*self.m44) + Matrix4::new(m.m11*self.m11 + m.m12*self.m21 + m.m13*self.m31 + m.m14*self.m41, + m.m11*self.m12 + m.m12*self.m22 + m.m13*self.m32 + m.m14*self.m42, + m.m11*self.m13 + m.m12*self.m23 + m.m13*self.m33 + m.m14*self.m43, + m.m11*self.m14 + m.m12*self.m24 + m.m13*self.m34 + m.m14*self.m44, + m.m21*self.m11 + m.m22*self.m21 + m.m23*self.m31 + m.m24*self.m41, + m.m21*self.m12 + m.m22*self.m22 + m.m23*self.m32 + m.m24*self.m42, + m.m21*self.m13 + m.m22*self.m23 + m.m23*self.m33 + m.m24*self.m43, + m.m21*self.m14 + m.m22*self.m24 + m.m23*self.m34 + m.m24*self.m44, + m.m31*self.m11 + m.m32*self.m21 + m.m33*self.m31 + m.m34*self.m41, + m.m31*self.m12 + m.m32*self.m22 + m.m33*self.m32 + m.m34*self.m42, + m.m31*self.m13 + m.m32*self.m23 + m.m33*self.m33 + m.m34*self.m43, + m.m31*self.m14 + m.m32*self.m24 + m.m33*self.m34 + m.m34*self.m44, + m.m41*self.m11 + m.m42*self.m21 + m.m43*self.m31 + m.m44*self.m41, + m.m41*self.m12 + m.m42*self.m22 + m.m43*self.m32 + m.m44*self.m42, + m.m41*self.m13 + m.m42*self.m23 + m.m43*self.m33 + m.m44*self.m43, + m.m41*self.m14 + m.m42*self.m24 + m.m43*self.m34 + m.m44*self.m44) } pub fn mul_s(&self, x: f32) -> Matrix4 { - Matrix4(self.m11 * x, self.m12 * x, self.m13 * x, self.m14 * x, - self.m21 * x, self.m22 * x, self.m23 * x, self.m24 * x, - self.m31 * x, self.m32 * x, self.m33 * x, self.m34 * x, - self.m41 * x, self.m42 * x, self.m43 * x, self.m44 * x) + Matrix4::new(self.m11 * x, self.m12 * x, self.m13 * x, self.m14 * x, + self.m21 * x, self.m22 * x, self.m23 * x, self.m24 * x, + self.m31 * x, self.m32 * x, self.m33 * x, self.m34 * x, + self.m41 * x, self.m42 * x, self.m43 * x, self.m44 * x) } pub fn scale(&self, x: f32, y: f32, z: f32) -> Matrix4 { - Matrix4(self.m11 * x, self.m12, self.m13, self.m14, - self.m21 , self.m22 * y, self.m23, self.m24, - self.m31 , self.m32, self.m33 * z, self.m34, - self.m41 , self.m42, self.m43, self.m44) + Matrix4::new(self.m11 * x, self.m12, self.m13, self.m14, + self.m21 , self.m22 * y, self.m23, self.m24, + self.m31 , self.m32, self.m33 * z, self.m34, + self.m41 , self.m42, self.m43, self.m44) } /// Returns the given point transformed by this matrix. #[inline] pub fn transform_point(&self, p: &Point2D) -> Point2D { - Point2D(p.x * self.m11 + p.y * self.m21 + self.m41, - p.x * self.m12 + p.y * self.m22 + self.m42) + Point2D::new(p.x * self.m11 + p.y * self.m21 + self.m41, + p.x * self.m12 + p.y * self.m22 + self.m42) } pub fn to_array(&self) -> [f32; 16] { @@ -94,28 +130,28 @@ impl Matrix4 { } pub fn translate(&self, x: f32, y: f32, z: f32) -> Matrix4 { - let matrix = Matrix4(1.0, 0.0, 0.0, 0.0, - 0.0, 1.0, 0.0, 0.0, - 0.0, 0.0, 1.0, 0.0, - x, y, z, 1.0); + let matrix = Matrix4::new(1.0, 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + x, y, z, 1.0); return self.mul(&matrix); } /// Create a 3d translation matrix pub fn create_translation(x: f32, y: f32, z: f32) -> Matrix4 { - Matrix4(1.0, 0.0, 0.0, 0.0, - 0.0, 1.0, 0.0, 0.0, - 0.0, 0.0, 1.0, 0.0, - x, y, z, 1.0) + Matrix4::new(1.0, 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + x, y, z, 1.0) } /// Create a 3d scale matrix pub fn create_scale(x: f32, y: f32, z: f32) -> Matrix4 { - Matrix4( x, 0.0, 0.0, 0.0, - 0.0, y, 0.0, 0.0, - 0.0, 0.0, z, 0.0, - 0.0, 0.0, 0.0, 1.0) + Matrix4::new( x, 0.0, 0.0, 0.0, + 0.0, y, 0.0, 0.0, + 0.0, 0.0, z, 0.0, + 0.0, 0.0, 0.0, 1.0) } /// Create a 3d rotation matrix from an angle / axis. @@ -129,7 +165,7 @@ impl Matrix4 { let sc = half_theta.sin() * half_theta.cos(); let sq = half_theta.sin() * half_theta.sin(); - Matrix4( + Matrix4::new( 1.0 - 2.0 * (yy + zz) * sq, 2.0 * (x * y * sq - z * sc), 2.0 * (x * z * sq + y * sc), @@ -154,67 +190,30 @@ impl Matrix4 { /// Create a 2d skew matrix pub fn create_skew(sx: f32, sy: f32) -> Matrix4 { - Matrix4(1.0, sx, 0.0, 0.0, - sy, 1.0, 0.0, 0.0, - 0.0, 0.0, 1.0, 0.0, - 0.0, 0.0, 0.0, 1.0) + Matrix4::new(1.0, sx, 0.0, 0.0, + sy, 1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, 0.0, + 0.0, 0.0, 0.0, 1.0) } /// Create a simple perspective projection matrix pub fn create_perspective(d: f32) -> Matrix4 { - Matrix4(1.0, 0.0, 0.0, 0.0, - 0.0, 1.0, 0.0, 0.0, - 0.0, 0.0, 1.0, -1.0 / d, - 0.0, 0.0, 0.0, 1.0) + Matrix4::new(1.0, 0.0, 0.0, 0.0, + 0.0, 1.0, 0.0, 0.0, + 0.0, 0.0, 1.0, -1.0 / d, + 0.0, 0.0, 0.0, 1.0) } } -// TODO(gw): Move ortho and identity into static functions of the Matrix type. -pub fn ortho(left: f32, right: f32, - bottom: f32, top: f32, - near: f32, far: f32) -> Matrix4 { - let tx = -((right + left) / (right - left)); - let ty = -((top + bottom) / (top - bottom)); - let tz = -((far + near) / (far - near)); - - Matrix4(2.0 / (right - left), - 0.0, - 0.0, - 0.0, - - 0.0, - 2.0 / (top - bottom), - 0.0, - 0.0, - - 0.0, - 0.0, - -2.0 / (far - near), - 0.0, - - tx, - ty, - tz, - 1.0) -} - -pub fn identity() -> Matrix4 { - Matrix4(1.0, 0.0, 0.0, 0.0, - 0.0, 1.0, 0.0, 0.0, - 0.0, 0.0, 1.0, 0.0, - 0.0, 0.0, 0.0, 1.0) -} - #[test] pub fn test_ortho() { let (left, right, bottom, top) = (0.0f32, 1.0f32, 0.1f32, 1.0f32); let (near, far) = (-1.0f32, 1.0f32); - let result = ortho(left, right, bottom, top, near, far); - let expected = Matrix4(2.0, 0.0, 0.0, 0.0, - 0.0, 2.22222222, 0.0, 0.0, - 0.0, 0.0, -1.0, 0.0, - -1.0, -1.22222222, -0.0, 1.0); + let result = Matrix4::ortho(left, right, bottom, top, near, far); + let expected = Matrix4::new(2.0, 0.0, 0.0, 0.0, + 0.0, 2.22222222, 0.0, 0.0, + 0.0, 0.0, -1.0, 0.0, + -1.0, -1.22222222, -0.0, 1.0); debug!("result={:?} expected={:?}", result, expected); assert!(result.approx_eq(&expected)); } - diff --git a/src/matrix2d.rs b/src/matrix2d.rs index fced206..269b3be 100644 --- a/src/matrix2d.rs +++ b/src/matrix2d.rs @@ -77,8 +77,8 @@ impl + /// Returns the given point transformed by this matrix. #[inline] pub fn transform_point(&self, point: &Point2D) -> Point2D { - Point2D(point.x * self.m11 + point.y * self.m21 + self.m31, - point.x * self.m12 + point.y * self.m22 + self.m32) + Point2D::new(point.x * self.m11 + point.y * self.m21 + self.m31, + point.x * self.m12 + point.y * self.m22 + self.m32) } /// Returns a rectangle that encompasses the result of transforming the given rectangle by this @@ -105,7 +105,7 @@ impl + max_y = point.y.clone() } } - Rect(Point2D(min_x.clone(), min_y.clone()), Size2D(max_x - min_x, max_y - min_y)) + Rect::new(Point2D::new(min_x.clone(), min_y.clone()), + Size2D::new(max_x - min_x, max_y - min_y)) } } - diff --git a/src/point.rs b/src/point.rs index ea13515..5f2ae2a 100644 --- a/src/point.rs +++ b/src/point.rs @@ -39,22 +39,24 @@ impl fmt::Display for Point2D { } } -pub fn Point2D(x: T, y: T) -> Point2D { - Point2D {x: x, y: y} +impl Point2D { + pub fn new(x: T, y: T) -> Point2D { + Point2D {x: x, y: y} + } } impl> Add for Point2D { type Output = Point2D; fn add(self, other: Point2D) -> Point2D { - Point2D(self.x + other.x, self.y + other.y) + Point2D::new(self.x + other.x, self.y + other.y) } } impl> Add> for Point2D { type Output = Point2D; fn add(self, other: Size2D) -> Point2D { - Point2D(self.x + other.width, self.y + other.height) + Point2D::new(self.x + other.width, self.y + other.height) } } @@ -67,7 +69,7 @@ impl> Point2D { impl> Sub for Point2D { type Output = Point2D; fn sub(self, other: Point2D) -> Point2D { - Point2D(self.x - other.x, self.y - other.y) + Point2D::new(self.x - other.x, self.y - other.y) } } @@ -75,7 +77,7 @@ impl > Neg for Point2D { type Output = Point2D; #[inline] fn neg(self) -> Point2D { - Point2D(-self.x, -self.y) + Point2D::new(-self.x, -self.y) } } @@ -83,7 +85,7 @@ impl, T1: Clone> Mul for Point2D; #[inline] fn mul(self, scale: Scale) -> Point2D { - Point2D(self.x * scale, self.y * scale) + Point2D::new(self.x * scale, self.y * scale) } } @@ -91,7 +93,7 @@ impl, T1: Clone> Div for Point2D; #[inline] fn div(self, scale: Scale) -> Point2D { - Point2D(self.x / scale, self.y / scale) + Point2D::new(self.x / scale, self.y / scale) } } @@ -99,19 +101,19 @@ impl, T1: Clone> Div for Point2D = Point2D>; -pub fn TypedPoint2D(x: T, y: T) -> TypedPoint2D { - Point2D(Length::new(x), Length::new(y)) -} - impl Point2D> { + pub fn typed(x: T, y: T) -> TypedPoint2D { + Point2D::new(Length::new(x), Length::new(y)) + } + /// Drop the units, preserving only the numeric value. pub fn to_untyped(&self) -> Point2D { - Point2D(self.x.get(), self.y.get()) + Point2D::new(self.x.get(), self.y.get()) } /// Tag a unitless value with units. pub fn from_untyped(p: &Point2D) -> TypedPoint2D { - Point2D(Length::new(p.x.clone()), Length::new(p.y.clone())) + Point2D::new(Length::new(p.x.clone()), Length::new(p.y.clone())) } } @@ -119,7 +121,7 @@ impl Point2D> { /// Cast from one numeric representation to another, preserving the units. pub fn cast(&self) -> Option>> { match (self.x.cast(), self.y.cast()) { - (Some(x), Some(y)) => Some(Point2D(x, y)), + (Some(x), Some(y)) => Some(Point2D::new(x, y)), _ => None } } diff --git a/src/rect.rs b/src/rect.rs index 372b842..4ad3ec4 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -35,10 +35,12 @@ impl fmt::Display for Rect { } } -pub fn Rect(origin: Point2D, size: Size2D) -> Rect { - Rect { - origin: origin, - size: size +impl Rect { + pub fn new(origin: Point2D, size: Size2D) -> Rect { + Rect { + origin: origin, + size: size + } } } @@ -77,14 +79,13 @@ impl + Sub + Zero> return None; } - let upper_left = Point2D(max(self.min_x(), other.min_x()), - max(self.min_y(), other.min_y())); - - let lower_right = Point2D(min(self.max_x(), other.max_x()), - min(self.max_y(), other.max_y())); + let upper_left = Point2D::new(max(self.min_x(), other.min_x()), + max(self.min_y(), other.min_y())); + let lower_right = Point2D::new(min(self.max_x(), other.max_x()), + min(self.max_y(), other.max_y())); - Some(Rect(upper_left.clone(), Size2D(lower_right.x - upper_left.x, - lower_right.y - upper_left.y))) + Some(Rect::new(upper_left.clone(), Size2D::new(lower_right.x - upper_left.x, + lower_right.y - upper_left.y))) } #[inline] @@ -96,22 +97,22 @@ impl + Sub + Zero> return *self } - let upper_left = Point2D(min(self.min_x(), other.min_x()), - min(self.min_y(), other.min_y())); + let upper_left = Point2D::new(min(self.min_x(), other.min_x()), + min(self.min_y(), other.min_y())); - let lower_right = Point2D(max(self.max_x(), other.max_x()), - max(self.max_y(), other.max_y())); + let lower_right = Point2D::new(max(self.max_x(), other.max_x()), + max(self.max_y(), other.max_y())); Rect { origin: upper_left.clone(), - size: Size2D(lower_right.x - upper_left.x, lower_right.y - upper_left.y) + size: Size2D::new(lower_right.x - upper_left.x, lower_right.y - upper_left.y) } } #[inline] pub fn translate(&self, other: &Point2D) -> Rect { Rect { - origin: Point2D(self.origin.x + other.x, self.origin.y + other.y), + origin: Point2D::new(self.origin.x + other.x, self.origin.y + other.y), size: self.size.clone() } } @@ -125,29 +126,30 @@ impl + Sub + Zero> #[inline] pub fn inflate(&self, width: T, height: T) -> Rect { Rect { - origin: Point2D(self.origin.x - width, self.origin.y - height), - size: Size2D(self.size.width + width + width, self.size.height + height + height), + origin: Point2D::new(self.origin.x - width, self.origin.y - height), + size: Size2D::new(self.size.width + width + width, self.size.height + height + height), } } #[inline] pub fn top_right(&self) -> Point2D { - Point2D(self.max_x(), self.origin.y.clone()) + Point2D::new(self.max_x(), self.origin.y.clone()) } #[inline] pub fn bottom_left(&self) -> Point2D { - Point2D(self.origin.x.clone(), self.max_y()) + Point2D::new(self.origin.x.clone(), self.max_y()) } #[inline] pub fn bottom_right(&self) -> Point2D { - Point2D(self.max_x(), self.max_y()) + Point2D::new(self.max_x(), self.max_y()) } #[inline] pub fn translate_by_size(&self, size: &Size2D) -> Rect { - Rect(Point2D(self.origin.x + size.width, self.origin.y + size.height), self.size.clone()) + Rect::new(Point2D::new(self.origin.x + size.width, self.origin.y + size.height), + self.size.clone()) } } @@ -188,7 +190,7 @@ impl, T1: Clone> Mul for Rect type Output = Rect; #[inline] fn mul(self, scale: Scale) -> Rect { - Rect(self.origin * scale, self.size * scale) + Rect::new(self.origin * scale, self.size * scale) } } @@ -196,7 +198,7 @@ impl, T1: Clone> Div for Rect type Output = Rect; #[inline] fn div(self, scale: Scale) -> Rect { - Rect(self.origin / scale, self.size / scale) + Rect::new(self.origin / scale, self.size / scale) } } @@ -206,12 +208,12 @@ pub type TypedRect = Rect>; impl Rect> { /// Drop the units, preserving only the numeric value. pub fn to_untyped(&self) -> Rect { - Rect(self.origin.to_untyped(), self.size.to_untyped()) + Rect::new(self.origin.to_untyped(), self.size.to_untyped()) } /// Tag a unitless value with units. pub fn from_untyped(r: &Rect) -> TypedRect { - Rect(Point2D::from_untyped(&r.origin), Size2D::from_untyped(&r.size)) + Rect::new(Point2D::from_untyped(&r.origin), Size2D::from_untyped(&r.size)) } } @@ -219,7 +221,7 @@ impl Rect> { /// Cast from one numeric representation to another, preserving the units. pub fn cast(&self) -> Option>> { match (self.origin.cast(), self.size.cast()) { - (Some(origin), Some(size)) => Some(Rect(origin, size)), + (Some(origin), Some(size)) => Some(Rect::new(origin, size)), _ => None } } @@ -247,8 +249,8 @@ fn test_min_max() { #[test] fn test_translate() { - let p = Rect(Point2D(0u32, 0u32), Size2D(50u32, 40u32)); - let pp = p.translate(&Point2D(10,15)); + let p = Rect::new(Point2D::new(0u32, 0u32), Size2D::new(50u32, 40u32)); + let pp = p.translate(&Point2D::new(10,15)); assert!(pp.size.width == 50); assert!(pp.size.height == 40); @@ -256,8 +258,8 @@ fn test_translate() { assert!(pp.origin.y == 15); - let r = Rect(Point2D(-10i32, -5i32), Size2D(50i32, 40i32)); - let rr = r.translate(&Point2D(0,-10)); + let r = Rect::new(Point2D::new(-10, -5), Size2D::new(50, 40)); + let rr = r.translate(&Point2D::new(0,-10)); assert!(rr.size.width == 50); assert!(rr.size.height == 40); @@ -267,42 +269,42 @@ fn test_translate() { #[test] fn test_union() { - let p = Rect(Point2D(0i32, 0i32), Size2D(50i32, 40i32)); - let q = Rect(Point2D(20i32 ,20i32), Size2D(5i32, 5i32)); - let r = Rect(Point2D(-15i32, -30i32), Size2D(200i32, 15i32)); - let s = Rect(Point2D(20i32, -15i32), Size2D(250i32, 200i32)); + let p = Rect::new(Point2D::new(0, 0), Size2D::new(50, 40)); + let q = Rect::new(Point2D::new(20,20), Size2D::new(5, 5)); + let r = Rect::new(Point2D::new(-15, -30), Size2D::new(200, 15)); + let s = Rect::new(Point2D::new(20, -15), Size2D::new(250, 200)); let pq = p.union(&q); - assert!(pq.origin == Point2D(0, 0)); - assert!(pq.size == Size2D(50, 40)); + assert!(pq.origin == Point2D::new(0, 0)); + assert!(pq.size == Size2D::new(50, 40)); let pr = p.union(&r); - assert!(pr.origin == Point2D(-15, -30)); - assert!(pr.size == Size2D(200, 70)); + assert!(pr.origin == Point2D::new(-15, -30)); + assert!(pr.size == Size2D::new(200, 70)); let ps = p.union(&s); - assert!(ps.origin == Point2D(0, -15)); - assert!(ps.size == Size2D(270, 200)); + assert!(ps.origin == Point2D::new(0, -15)); + assert!(ps.size == Size2D::new(270, 200)); } #[test] fn test_intersection() { - let p = Rect(Point2D(0i32, 0i32), Size2D(10i32, 20i32)); - let q = Rect(Point2D(5i32, 15i32), Size2D(10i32, 10i32)); - let r = Rect(Point2D(-5i32, -5i32), Size2D(8i32, 8i32)); + let p = Rect::new(Point2D::new(0, 0), Size2D::new(10, 20)); + let q = Rect::new(Point2D::new(5, 15), Size2D::new(10, 10)); + let r = Rect::new(Point2D::new(-5, -5), Size2D::new(8, 8)); let pq = p.intersection(&q); assert!(pq.is_some()); let pq = pq.unwrap(); - assert!(pq.origin == Point2D(5, 15)); - assert!(pq.size == Size2D(5, 5)); + assert!(pq.origin == Point2D::new(5, 15)); + assert!(pq.size == Size2D::new(5, 5)); let pr = p.intersection(&r); assert!(pr.is_some()); let pr = pr.unwrap(); - assert!(pr.origin == Point2D(0, 0)); - assert!(pr.size == Size2D(3, 3)); + assert!(pr.origin == Point2D::new(0, 0)); + assert!(pr.size == Size2D::new(3, 3)); let qr = q.intersection(&r); assert!(qr.is_none()); @@ -310,38 +312,38 @@ fn test_intersection() { #[test] fn test_contains() { - let r = Rect(Point2D(-20i32, 15i32), Size2D(100i32, 200i32)); + let r = Rect::new(Point2D::new(-20, 15), Size2D::new(100, 200)); - assert!(r.contains(&Point2D(0, 50))); - assert!(r.contains(&Point2D(-10, 200))); + assert!(r.contains(&Point2D::new(0, 50))); + assert!(r.contains(&Point2D::new(-10, 200))); // The `contains` method is inclusive of the top/left edges, but not the // bottom/right edges. - assert!(r.contains(&Point2D(-20, 15))); - assert!(!r.contains(&Point2D(80, 15))); - assert!(!r.contains(&Point2D(80, 215))); - assert!(!r.contains(&Point2D(-20, 215))); + assert!(r.contains(&Point2D::new(-20, 15))); + assert!(!r.contains(&Point2D::new(80, 15))); + assert!(!r.contains(&Point2D::new(80, 215))); + assert!(!r.contains(&Point2D::new(-20, 215))); // Points beyond the top-left corner. - assert!(!r.contains(&Point2D(-25, 15))); - assert!(!r.contains(&Point2D(-15, 10))); + assert!(!r.contains(&Point2D::new(-25, 15))); + assert!(!r.contains(&Point2D::new(-15, 10))); // Points beyond the top-right corner. - assert!(!r.contains(&Point2D(85, 20))); - assert!(!r.contains(&Point2D(75, 10))); + assert!(!r.contains(&Point2D::new(85, 20))); + assert!(!r.contains(&Point2D::new(75, 10))); // Points beyond the bottom-right corner. - assert!(!r.contains(&Point2D(85, 210))); - assert!(!r.contains(&Point2D(75, 220))); + assert!(!r.contains(&Point2D::new(85, 210))); + assert!(!r.contains(&Point2D::new(75, 220))); // Points beyond the bottom-left corner. - assert!(!r.contains(&Point2D(-25, 210))); - assert!(!r.contains(&Point2D(-15, 220))); + assert!(!r.contains(&Point2D::new(-25, 210))); + assert!(!r.contains(&Point2D::new(-15, 220))); } #[test] fn test_scale() { - let p = Rect(Point2D(0u32, 0u32), Size2D(50u32, 40u32)); + let p = Rect::new(Point2D::new(0u32, 0u32), Size2D::new(50u32, 40u32)); let pp = p.scale(10, 15); assert!(pp.size.width == 500); @@ -349,7 +351,7 @@ fn test_scale() { assert!(pp.origin.x == 0); assert!(pp.origin.y == 0); - let r = Rect(Point2D(-10i32, -5i32), Size2D(50i32, 40i32)); + let r = Rect::new(Point2D::new(-10, -5), Size2D::new(50, 40)); let rr = r.scale(1, 20); assert!(rr.size.width == 50); @@ -360,7 +362,7 @@ fn test_scale() { #[test] fn test_inflate() { - let p = Rect(Point2D(0i32, 0i32), Size2D(10i32, 10i32)); + let p = Rect::new(Point2D::new(0, 0), Size2D::new(10, 10)); let pp = p.inflate(10, 20); assert!(pp.size.width == 30); @@ -368,7 +370,7 @@ fn test_inflate() { assert!(pp.origin.x == -10); assert!(pp.origin.y == -20); - let r = Rect(Point2D(0i32, 0i32), Size2D(10i32, 20i32)); + let r = Rect::new(Point2D::new(0, 0), Size2D::new(10, 20)); let rr = r.inflate(-2, -5); assert!(rr.size.width == 6); @@ -379,13 +381,13 @@ fn test_inflate() { #[test] fn test_min_max_x_y() { - let p = Rect(Point2D(0u32, 0u32), Size2D(50u32, 40u32)); + let p = Rect::new(Point2D::new(0u32, 0u32), Size2D::new(50u32, 40u32)); assert!(p.max_y() == 40); assert!(p.min_y() == 0); assert!(p.max_x() == 50); assert!(p.min_x() == 0); - let r = Rect(Point2D(-10i32, -5i32), Size2D(50i32, 40i32)); + let r = Rect::new(Point2D::new(-10, -5), Size2D::new(50, 40)); assert!(r.max_y() == 35); assert!(r.min_y() == -5); assert!(r.max_x() == 40); diff --git a/src/side_offsets.rs b/src/side_offsets.rs index 3b5a86c..d7f0ae3 100644 --- a/src/side_offsets.rs +++ b/src/side_offsets.rs @@ -159,7 +159,6 @@ impl SideOffsets2DSimdI32 { #[cfg(test)] mod tests { - use num::Zero; use super::SideOffsets2DSimdI32; #[test] diff --git a/src/size.rs b/src/size.rs index f61f4ac..5939363 100644 --- a/src/size.rs +++ b/src/size.rs @@ -32,10 +32,12 @@ impl fmt::Display for Size2D { } } -pub fn Size2D(width: T, height: T) -> Size2D { - Size2D { - width: width, - height: height +impl Size2D { + pub fn new(width: T, height: T) -> Size2D { + Size2D { + width: width, + height: height + } } } @@ -65,7 +67,7 @@ impl, T1: Clone> Mul for Size2D; #[inline] fn mul(self, scale: Scale) -> Size2D { - Size2D(self.width * scale, self.height * scale) + Size2D::new(self.width * scale, self.height * scale) } } @@ -73,7 +75,7 @@ impl, T1: Clone> Div for Size2D; #[inline] fn div(self, scale: Scale) -> Size2D { - Size2D(self.width / scale, self.height / scale) + Size2D::new(self.width / scale, self.height / scale) } } @@ -81,19 +83,19 @@ impl, T1: Clone> Div for Size2D = Size2D>; -pub fn TypedSize2D(width: T, height: T) -> TypedSize2D { - Size2D(Length::new(width), Length::new(height)) -} - impl Size2D> { + pub fn typed(width: T, height: T) -> TypedSize2D { + Size2D::new(Length::new(width), Length::new(height)) + } + /// Drop the units, preserving only the numeric value. pub fn to_untyped(&self) -> Size2D { - Size2D(self.width.get(), self.height.get()) + Size2D::new(self.width.get(), self.height.get()) } /// Tag a unitless value with units. pub fn from_untyped(p: &Size2D) -> TypedSize2D { - Size2D(Length::new(p.width.clone()), Length::new(p.height.clone())) + Size2D::new(Length::new(p.width.clone()), Length::new(p.height.clone())) } } @@ -101,7 +103,7 @@ impl Size2D> { /// Cast from one numeric representation to another, preserving the units. pub fn cast(&self) -> Option>> { match (self.width.cast(), self.height.cast()) { - (Some(w), Some(h)) => Some(Size2D(w, h)), + (Some(w), Some(h)) => Some(Size2D::new(w, h)), _ => None } }