From cf3c8378557457363853d6795e4ddf9e70a4738e Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Thu, 26 Nov 2020 20:50:30 +0100 Subject: Make polygons deletable Before, the deletion tool was not targeting polygons. I also took the liberty to broaden the functionality of the surface trait, which now can check if a rectangle or polygon is contained. --- src/math/rect.rs | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) (limited to 'src/math/rect.rs') 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 Rect { || this.y + this.h < other.y) } - /// 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. @@ -128,7 +113,7 @@ impl Rect { } } -impl Surface for Rect { +impl Surface for Rect { fn contains_point(&self, point: &Vec2) -> bool { point.x >= self.x && point.x <= self.x + self.w @@ -139,6 +124,26 @@ impl Surface for Rect { fn contains_line_segment(&self, line_segment: &LineSegment) -> bool { self.contains_point(&line_segment.start) && self.contains_point(&line_segment.end) } + + fn contains_rect(&self, rect: &Rect) -> 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) -> 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 :/ -- cgit v1.2.3-70-g09d2