From 9799d3c6a8f0c242668203a1c70d7b6cfed3e855 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Tue, 15 Dec 2020 00:46:54 +0100 Subject: Refactor to make interaction between tools easier --- src/math/rect.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src/math/rect.rs') diff --git a/src/math/rect.rs b/src/math/rect.rs index 5603642..befa4da 100644 --- a/src/math/rect.rs +++ b/src/math/rect.rs @@ -84,6 +84,38 @@ impl Rect { } } + /// Function to calculate the bounding rectangle of n vertices provided. The order of them is + /// not relevant and a point that is contained by the vertices will not change the result. + /// + /// # Panics + /// If there is not at least one vertex in the vertices slice, the function will panic, since it + /// is impossible to calculate any bounds in such a case. + pub fn bounding_rect_n(vertices: &[Vec2]) -> Self + where + T: RealField, + { + if vertices.is_empty() { + panic!("Cannot create bounding rectangle without any vertices"); + } + + let mut min = vertices[0]; + let mut max = vertices[1]; + + for vertex in vertices.iter().skip(1) { + min.x = super::partial_min(min.x, vertex.x); + min.y = super::partial_min(min.y, vertex.y); + max.x = super::partial_max(max.x, vertex.x); + max.y = super::partial_max(max.y, vertex.y); + } + + Self { + x: min.x, + y: min.y, + w: max.x - min.x, + h: max.y - min.y + } + } + /// Get the shortest way that must be applied to this Rect to clear out of /// another Rect of the same type so that they would not intersect any more. pub fn shortest_way_out(&self, of: &Rect) -> Vec2 @@ -145,6 +177,10 @@ impl Surface fo .iter() .all(|&corner| self.contains_point(&corner)) } + + fn is_inside_rect(&self, rect: &Rect) -> bool { + rect.contains_rect(&self) + } } // This is sad, but also sadly necessary :/ -- cgit v1.2.3-70-g09d2