Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various improvements, clean up warnings, restructure constructors #81

Merged
merged 9 commits into from Jun 12, 2015

Migrate Point2D primary constructor to associated fn

  • Loading branch information
frewsxcv committed Jun 11, 2015
commit 90bc0ac2ac2e472c54ab1c97199bcc17715de6a2
@@ -116,8 +116,8 @@ impl Matrix4 {
/// Returns the given point transformed by this matrix.
#[inline]
pub fn transform_point(&self, p: &Point2D<f32>) -> Point2D<f32> {
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] {
@@ -77,8 +77,8 @@ impl<T:Add<T, Output=T> +
/// Returns the given point transformed by this matrix.
#[inline]
pub fn transform_point(&self, point: &Point2D<T>) -> Point2D<T> {
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,6 @@ impl<T:Add<T, Output=T> +
max_y = point.y.clone()
}
}
Rect(Point2D(min_x.clone(), min_y.clone()), Size2D(max_x - min_x, max_y - min_y))
Rect(Point2D::new(min_x.clone(), min_y.clone()), Size2D(max_x - min_x, max_y - min_y))
}
}

@@ -39,22 +39,24 @@ impl<T: fmt::Display> fmt::Display for Point2D<T> {
}
}

pub fn Point2D<T>(x: T, y: T) -> Point2D<T> {
Point2D {x: x, y: y}
impl<T> Point2D<T> {
pub fn new(x: T, y: T) -> Point2D<T> {
Point2D {x: x, y: y}
}
}


impl<T:Clone + Add<T, Output=T>> Add for Point2D<T> {
type Output = Point2D<T>;
fn add(self, other: Point2D<T>) -> Point2D<T> {
Point2D(self.x + other.x, self.y + other.y)
Point2D::new(self.x + other.x, self.y + other.y)
}
}

impl<T:Clone + Add<T, Output=T>> Add<Size2D<T>> for Point2D<T> {
type Output = Point2D<T>;
fn add(self, other: Size2D<T>) -> Point2D<T> {
Point2D(self.x + other.width, self.y + other.height)
Point2D::new(self.x + other.width, self.y + other.height)
}
}

@@ -67,31 +69,31 @@ impl<T: Copy + Add<T, Output=T>> Point2D<T> {
impl<T:Clone + Sub<T, Output=T>> Sub for Point2D<T> {
type Output = Point2D<T>;
fn sub(self, other: Point2D<T>) -> Point2D<T> {
Point2D(self.x - other.x, self.y - other.y)
Point2D::new(self.x - other.x, self.y - other.y)
}
}

impl <T:Clone + Neg<Output=T>> Neg for Point2D<T> {
type Output = Point2D<T>;
#[inline]
fn neg(self) -> Point2D<T> {
Point2D(-self.x, -self.y)
Point2D::new(-self.x, -self.y)
}
}

impl<Scale: Copy, T0: Mul<Scale, Output=T1>, T1: Clone> Mul<Scale> for Point2D<T0> {
type Output = Point2D<T1>;
#[inline]
fn mul(self, scale: Scale) -> Point2D<T1> {
Point2D(self.x * scale, self.y * scale)
Point2D::new(self.x * scale, self.y * scale)
}
}

impl<Scale: Copy, T0: Div<Scale, Output=T1>, T1: Clone> Div<Scale> for Point2D<T0> {
type Output = Point2D<T1>;
#[inline]
fn div(self, scale: Scale) -> Point2D<T1> {
Point2D(self.x / scale, self.y / scale)
Point2D::new(self.x / scale, self.y / scale)
}
}

@@ -100,26 +102,26 @@ impl<Scale: Copy, T0: Div<Scale, Output=T1>, T1: Clone> Div<Scale> for Point2D<T
pub type TypedPoint2D<Unit, T> = Point2D<Length<Unit, T>>;

pub fn TypedPoint2D<Unit, T: Clone>(x: T, y: T) -> TypedPoint2D<Unit, T> {
Point2D(Length::new(x), Length::new(y))
Point2D::new(Length::new(x), Length::new(y))
}

impl<Unit, T: Clone> Point2D<Length<Unit, T>> {
/// Drop the units, preserving only the numeric value.
pub fn to_untyped(&self) -> Point2D<T> {
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<T>) -> TypedPoint2D<Unit, T> {
Point2D(Length::new(p.x.clone()), Length::new(p.y.clone()))
Point2D::new(Length::new(p.x.clone()), Length::new(p.y.clone()))
}
}

impl<Unit, T0: NumCast + Clone> Point2D<Length<Unit, T0>> {
/// Cast from one numeric representation to another, preserving the units.
pub fn cast<T1: NumCast + Clone>(&self) -> Option<Point2D<Length<Unit, T1>>> {
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
}
}
@@ -77,11 +77,10 @@ impl<T: Copy + Clone + PartialOrd + Add<T, Output=T> + Sub<T, Output=T> + 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)))
@@ -96,11 +95,11 @@ impl<T: Copy + Clone + PartialOrd + Add<T, Output=T> + Sub<T, Output=T> + 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(),
@@ -111,7 +110,7 @@ impl<T: Copy + Clone + PartialOrd + Add<T, Output=T> + Sub<T, Output=T> + Zero>
#[inline]
pub fn translate(&self, other: &Point2D<T>) -> Rect<T> {
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 +124,30 @@ impl<T: Copy + Clone + PartialOrd + Add<T, Output=T> + Sub<T, Output=T> + Zero>
#[inline]
pub fn inflate(&self, width: T, height: T) -> Rect<T> {
Rect {
origin: Point2D(self.origin.x - width, self.origin.y - height),
origin: Point2D::new(self.origin.x - width, self.origin.y - height),
size: Size2D(self.size.width + width + width, self.size.height + height + height),
}
}

#[inline]
pub fn top_right(&self) -> Point2D<T> {
Point2D(self.max_x(), self.origin.y.clone())
Point2D::new(self.max_x(), self.origin.y.clone())
}

#[inline]
pub fn bottom_left(&self) -> Point2D<T> {
Point2D(self.origin.x.clone(), self.max_y())
Point2D::new(self.origin.x.clone(), self.max_y())
}

#[inline]
pub fn bottom_right(&self) -> Point2D<T> {
Point2D(self.max_x(), self.max_y())
Point2D::new(self.max_x(), self.max_y())
}

#[inline]
pub fn translate_by_size(&self, size: &Size2D<T>) -> Rect<T> {
Rect(Point2D(self.origin.x + size.width, self.origin.y + size.height), self.size.clone())
Rect(Point2D::new(self.origin.x + size.width, self.origin.y + size.height),
self.size.clone())
}
}

@@ -247,17 +247,17 @@ 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(Point2D::new(0u32, 0u32), Size2D(50u32, 40u32));
let pp = p.translate(&Point2D::new(10,15));

assert!(pp.size.width == 50);
assert!(pp.size.height == 40);
assert!(pp.origin.x == 10);
assert!(pp.origin.y == 15);


let r = Rect(Point2D(-10i32, -5i32), Size2D(50i32, 40i32));
let rr = r.translate(&Point2D(0,-10));
let r = Rect(Point2D::new(-10i32, -5i32), Size2D(50i32, 40i32));
let rr = r.translate(&Point2D::new(0,-10));

assert!(rr.size.width == 50);
assert!(rr.size.height == 40);
@@ -267,41 +267,41 @@ 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(Point2D::new(0i32, 0i32), Size2D(50i32, 40i32));
let q = Rect(Point2D::new(20i32 ,20i32), Size2D(5i32, 5i32));
let r = Rect(Point2D::new(-15i32, -30i32), Size2D(200i32, 15i32));
let s = Rect(Point2D::new(20i32, -15i32), Size2D(250i32, 200i32));

let pq = p.union(&q);
assert!(pq.origin == Point2D(0, 0));
assert!(pq.origin == Point2D::new(0, 0));
assert!(pq.size == Size2D(50, 40));

let pr = p.union(&r);
assert!(pr.origin == Point2D(-15, -30));
assert!(pr.origin == Point2D::new(-15, -30));
assert!(pr.size == Size2D(200, 70));

let ps = p.union(&s);
assert!(ps.origin == Point2D(0, -15));
assert!(ps.origin == Point2D::new(0, -15));
assert!(ps.size == Size2D(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(Point2D::new(0i32, 0i32), Size2D(10i32, 20i32));
let q = Rect(Point2D::new(5i32, 15i32), Size2D(10i32, 10i32));
let r = Rect(Point2D::new(-5i32, -5i32), Size2D(8i32, 8i32));

let pq = p.intersection(&q);
assert!(pq.is_some());
let pq = pq.unwrap();
assert!(pq.origin == Point2D(5, 15));
assert!(pq.origin == Point2D::new(5, 15));
assert!(pq.size == Size2D(5, 5));

let pr = p.intersection(&r);
assert!(pr.is_some());
let pr = pr.unwrap();
assert!(pr.origin == Point2D(0, 0));
assert!(pr.origin == Point2D::new(0, 0));
assert!(pr.size == Size2D(3, 3));

let qr = q.intersection(&r);
@@ -310,46 +310,46 @@ fn test_intersection() {

#[test]
fn test_contains() {
let r = Rect(Point2D(-20i32, 15i32), Size2D(100i32, 200i32));
let r = Rect(Point2D::new(-20i32, 15i32), Size2D(100i32, 200i32));

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(Point2D::new(0u32, 0u32), Size2D(50u32, 40u32));
let pp = p.scale(10, 15);

assert!(pp.size.width == 500);
assert!(pp.size.height == 600);
assert!(pp.origin.x == 0);
assert!(pp.origin.y == 0);

let r = Rect(Point2D(-10i32, -5i32), Size2D(50i32, 40i32));
let r = Rect(Point2D::new(-10i32, -5i32), Size2D(50i32, 40i32));
let rr = r.scale(1, 20);

assert!(rr.size.width == 50);
@@ -360,15 +360,15 @@ fn test_scale() {

#[test]
fn test_inflate() {
let p = Rect(Point2D(0i32, 0i32), Size2D(10i32, 10i32));
let p = Rect(Point2D::new(0i32, 0i32), Size2D(10i32, 10i32));
let pp = p.inflate(10, 20);

assert!(pp.size.width == 30);
assert!(pp.size.height == 50);
assert!(pp.origin.x == -10);
assert!(pp.origin.y == -20);

let r = Rect(Point2D(0i32, 0i32), Size2D(10i32, 20i32));
let r = Rect(Point2D::new(0i32, 0i32), Size2D(10i32, 20i32));
let rr = r.inflate(-2, -5);

assert!(rr.size.width == 6);
@@ -379,13 +379,13 @@ fn test_inflate() {

#[test]
fn test_min_max_x_y() {
let p = Rect(Point2D(0u32, 0u32), Size2D(50u32, 40u32));
let p = Rect(Point2D::new(0u32, 0u32), Size2D(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(Point2D::new(-10i32, -5i32), Size2D(50i32, 40i32));
assert!(r.max_y() == 35);
assert!(r.min_y() == -5);
assert!(r.max_x() == 40);
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.