diff options
Diffstat (limited to 'src/math/rect.rs')
| -rw-r--r-- | src/math/rect.rs | 36 |
1 files changed, 36 insertions, 0 deletions
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<T: Scalar + Copy> Rect<T> { } } + /// 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<T>]) -> 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<T>) -> Vec2<T> @@ -145,6 +177,10 @@ impl<T: Scalar + Copy + PartialOrd + ClosedAdd + ClosedSub + Zero> Surface<T> fo .iter() .all(|&corner| self.contains_point(&corner)) } + + fn is_inside_rect(&self, rect: &Rect<T>) -> bool { + rect.contains_rect(&self) + } } // This is sad, but also sadly necessary :/ |
