From fdb3e8cb8d53449c107388e143345beae162f95e Mon Sep 17 00:00:00 2001 From: Julian T Date: Fri, 29 Jan 2021 00:58:32 +0100 Subject: Finish get_tile on Film --- src/bound.rs | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) (limited to 'src/bound.rs') diff --git a/src/bound.rs b/src/bound.rs index b7ce05d..ab0e703 100644 --- a/src/bound.rs +++ b/src/bound.rs @@ -24,18 +24,25 @@ fn max (a: T, b: T) -> T { } impl Bound2 { - fn new(p0: &Vector2, p1: &Vector2) -> Bound2 { - let min = Vector2::from_xy(min(p0.x, p1.x), min(p0.y, p1.y)); - let max = Vector2::from_xy(max(p0.x, p1.x), max(p0.y, p1.y)); + pub fn new(p0: &Vector2, p1: &Vector2) -> Self { + let min = Vector2::new_xy(min(p0.x, p1.x), min(p0.y, p1.y)); + let max = Vector2::new_xy(max(p0.x, p1.x), max(p0.y, p1.y)); - Bound2 { min, max } + Self { min, max } } - fn diagonal(&self) -> Vector2 { + pub fn new_xyxy(x1: T, y1: T, x2: T, y2: T) -> Self { + Self::new( + &Vector2::new_xy(x1, y1), + &Vector2::new_xy(x2, y2), + ) + } + + pub fn diagonal(&self) -> Vector2 { self.max - self.min } - fn area(&self) -> T { + pub fn area(&self) -> T { let diag = self.diagonal(); return diag.x * diag.y; } @@ -61,8 +68,8 @@ impl From<&Bound2f> for Bound2i { pub fn intersect(a: &Bound2, b: &Bound2) -> Bound2 { Bound2::new( - &Vector2::from_xy(max(a.min.x, b.min.x), max(a.min.y, b.min.y)), - &Vector2::from_xy(min(a.max.x, b.max.x), min(a.max.y, b.max.y)), + &Vector2::new_xy(max(a.min.x, b.min.x), max(a.min.y, b.min.y)), + &Vector2::new_xy(min(a.max.x, b.max.x), min(a.max.y, b.max.y)), ) } @@ -72,8 +79,8 @@ mod tests { fn create_test() -> Bound2 { Bound2::new( - &Vector2::from_xy(1, 2), - &Vector2::from_xy(10, 3) + &Vector2::new_xy(1, 2), + &Vector2::new_xy(10, 3) ) } @@ -83,4 +90,19 @@ mod tests { assert!(b.area() == 9); } + + #[test] + fn intersect_test() { + let b1 = Bound2i::new_xyxy(10, 10, 20, 20); + let b2 = Bound2i::new_xyxy(2, 11, 22, 17); + + let b = intersect(&b1, &b2); + + assert!( + b.min.x == 10 && + b.min.y == 11 && + b.max.x == 20 && + b.max.y == 17 + ) + } } -- cgit v1.2.3