diff options
| author | Arne Dußin | 2020-12-15 00:46:54 +0100 |
|---|---|---|
| committer | Arne Dußin | 2020-12-15 22:51:46 +0100 |
| commit | 9799d3c6a8f0c242668203a1c70d7b6cfed3e855 (patch) | |
| tree | 9116acbc886f680f82309a42b4e6147e65c1433b /src/math/rect.rs | |
| parent | 3bc690803fb59493ea8180fd630d65b3e26642d0 (diff) | |
| download | graf_karto-9799d3c6a8f0c242668203a1c70d7b6cfed3e855.tar.gz graf_karto-9799d3c6a8f0c242668203a1c70d7b6cfed3e855.zip | |
Refactor to make interaction between tools easier
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 :/ |
