From 1ed00daee8f7cb0194d11a2aa32fc8ffc544ae98 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Sun, 1 Nov 2020 22:53:24 +0100 Subject: Add basic deletion tool --- src/math/rect.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/math/rect.rs') diff --git a/src/math/rect.rs b/src/math/rect.rs index c126d56..6988b2c 100644 --- a/src/math/rect.rs +++ b/src/math/rect.rs @@ -1,7 +1,9 @@ use super::Vec2; +//use alga::general::{Additive, Identity}; use nalgebra::{RealField, Scalar}; +use num_traits::identities::Zero; use serde::{Deserialize, Serialize}; -use std::ops::{Add, AddAssign}; +use std::ops::{Add, AddAssign, Sub}; /// Represents a Rectangle with the value type T. #[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)] @@ -114,6 +116,21 @@ impl Rect { && point.y <= self.y + self.h } + /// Returns true if the entire rect is contained inside this rectangle. + pub fn contains_rect(&self, rect: Rect) -> bool + where + T: Add + Sub + 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. -- cgit v1.2.3-70-g09d2