diff options
author | Julian T <julian@jtle.dk> | 2021-01-25 14:43:46 +0100 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-01-25 14:43:46 +0100 |
commit | 33a35c50c20e863123642c8c9ea31dfc006945f6 (patch) | |
tree | 6bbdd9f0f5d4deddec50fca1c3dbff3f2878ffac /src/bounding.rs | |
parent | 41ea2d8f94043d49fc26aaaa8030a73dcfdc2da9 (diff) |
Started work on implementing vectors bounding and film
Diffstat (limited to 'src/bounding.rs')
-rw-r--r-- | src/bounding.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/bounding.rs b/src/bounding.rs new file mode 100644 index 0000000..d5a3ed0 --- /dev/null +++ b/src/bounding.rs @@ -0,0 +1,62 @@ +use crate::{Number, Float}; +use crate::vector::Vector2; +use std::cmp; + +pub struct Bound2<T: Number> { + pub min: Vector2<T>, + pub max: Vector2<T> +} + +pub type Bound2i = Bound2<i32>; +pub type Bound2f = Bound2<Float>; + +fn min<T: Number> (a: T, b: T) -> T { + if b < a { + return b; + } + a +} + +fn max<T: Number> (a: T, b: T) -> T { + if b > a { + return b; + } + a +} + +impl<T: Number> Bound2<T> { + fn new(p0: &Vector2<T>, p1: &Vector2<T>) -> Bound2<T> { + 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<T> { + self.max - self.min + } + + fn area(&self) -> T { + let diag = self.diagonal(); + return diag.x * diag.y; + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn create_test() -> Bound2<i32> { + Bound2::new( + &Vector2::from_xy(1, 2), + &Vector2::from_xy(10, 3) + ) + } + + #[test] + fn area() { + let b = create_test(); + + assert!(b.area() == 9); + } +} |