From f467334b26f31b19ebbd222de2b4167b1538ccee Mon Sep 17 00:00:00 2001 From: Julian T Date: Thu, 28 Jan 2021 23:53:56 +0100 Subject: Unfinished film --- src/bound.rs | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/bound.rs (limited to 'src/bound.rs') diff --git a/src/bound.rs b/src/bound.rs new file mode 100644 index 0000000..b7ce05d --- /dev/null +++ b/src/bound.rs @@ -0,0 +1,86 @@ +use crate::{Number, Float}; +use crate::vector::*; + +pub struct Bound2 { + pub min: Vector2, + pub max: Vector2 +} + +pub type Bound2i = Bound2; +pub type Bound2f = Bound2; + +fn min (a: T, b: T) -> T { + if b < a { + return b; + } + a +} + +fn max (a: T, b: T) -> T { + if b > a { + return b; + } + a +} + +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)); + + Bound2 { min, max } + } + + fn diagonal(&self) -> Vector2 { + self.max - self.min + } + + fn area(&self) -> T { + let diag = self.diagonal(); + return diag.x * diag.y; + } +} + +impl From<&Bound2i> for Bound2f { + fn from(b: &Bound2i) -> Self { + Self { + min: Vector2f::from(b.min), + max: Vector2f::from(b.max), + } + } +} + +impl From<&Bound2f> for Bound2i { + fn from(b: &Bound2f) -> Self { + Self { + min: Vector2i::from(b.min), + max: Vector2i::from(b.max), + } + } +} + +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)), + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn create_test() -> Bound2 { + Bound2::new( + &Vector2::from_xy(1, 2), + &Vector2::from_xy(10, 3) + ) + } + + #[test] + fn area() { + let b = create_test(); + + assert!(b.area() == 9); + } +} -- cgit v1.2.3