diff options
Diffstat (limited to 'src/math/rect.rs')
| -rw-r--r-- | src/math/rect.rs | 43 |
1 files changed, 24 insertions, 19 deletions
diff --git a/src/math/rect.rs b/src/math/rect.rs index 876e728..5f4e5f5 100644 --- a/src/math/rect.rs +++ b/src/math/rect.rs @@ -1,9 +1,9 @@ -use super::{LineSegment, Surface, Vec2}; +use super::{LineSegment, Polygon, Surface, Vec2}; //use alga::general::{Additive, Identity}; -use nalgebra::{ClosedAdd, RealField, Scalar}; +use nalgebra::{ClosedAdd, ClosedSub, RealField, Scalar}; use num_traits::identities::Zero; use serde::{Deserialize, Serialize}; -use std::ops::{Add, AddAssign, Sub}; +use std::ops::{Add, AddAssign}; /// Represents a Rectangle with the value type T. #[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)] @@ -63,21 +63,6 @@ impl<T: Scalar + Copy> Rect<T> { || this.y + this.h < other.y) } - /// Returns true if the entire rect is contained inside this rectangle. - pub fn contains_rect(&self, rect: Rect<T>) -> bool - where - T: Add<Output = T> + Sub<Output = T> + PartialOrd + Zero, - { - /* True, if the rightmost x-coordinate of the called rectangle is further right or the same - * as the rightmost coordinate of the given rect. - */ - let x_exceeds = self.x + self.w - rect.x - rect.w >= T::zero(); - // The same for the y-coordinates - let y_exceeds = self.y + self.h - rect.y - rect.h >= T::zero(); - - x_exceeds && y_exceeds && self.x <= rect.x && self.y <= rect.y - } - /// Function to calculate the bounding rectangle that is between two vectors. The order of the /// vectors is irrelevent for this. As long as they are diagonally opposite of each other, this /// function will work. @@ -128,7 +113,7 @@ impl<T: Scalar + Copy> Rect<T> { } } -impl<T: Scalar + Copy + PartialOrd + ClosedAdd> Surface<T> for Rect<T> { +impl<T: Scalar + Copy + PartialOrd + ClosedAdd + ClosedSub + Zero> Surface<T> for Rect<T> { fn contains_point(&self, point: &Vec2<T>) -> bool { point.x >= self.x && point.x <= self.x + self.w @@ -139,6 +124,26 @@ impl<T: Scalar + Copy + PartialOrd + ClosedAdd> Surface<T> for Rect<T> { fn contains_line_segment(&self, line_segment: &LineSegment<T>) -> bool { self.contains_point(&line_segment.start) && self.contains_point(&line_segment.end) } + + fn contains_rect(&self, rect: &Rect<T>) -> bool { + /* True, if the rightmost x-coordinate of the called rectangle is further right or the same + * as the rightmost coordinate of the given rect. + */ + let x_exceeds = self.x + self.w - rect.x - rect.w >= T::zero(); + // The same for the y-coordinates + let y_exceeds = self.y + self.h - rect.y - rect.h >= T::zero(); + + x_exceeds && y_exceeds && self.x <= rect.x && self.y <= rect.y + } + + fn contains_polygon(&self, polygon: &Polygon<T>) -> bool { + // Check if all vertices of the polygon lie inside the rectangle. If so, the polygon must + // be contained in its entirety. + polygon + .corners() + .iter() + .all(|&corner| self.contains_point(&corner)) + } } // This is sad, but also sadly necessary :/ |
