aboutsummaryrefslogtreecommitdiff
path: root/src/math/rect.rs
diff options
context:
space:
mode:
authorArne Dußin2020-12-21 21:12:01 +0100
committerGitHub2020-12-21 21:12:01 +0100
commitc435f278eddcada279fdc424120e4a1448843c20 (patch)
treebe9a5601e99608966d4ccd146c3bfb3a70c7fc02 /src/math/rect.rs
parent3bc690803fb59493ea8180fd630d65b3e26642d0 (diff)
parent82d11b7d3e15d8175accf7579db1fbe528fc6583 (diff)
downloadgraf_karto-c435f278eddcada279fdc424120e4a1448843c20.tar.gz
graf_karto-c435f278eddcada279fdc424120e4a1448843c20.zip
Merge pull request #24 from LordSentox/refactor
Refactor to make interaction between tools easier
Diffstat (limited to 'src/math/rect.rs')
-rw-r--r--src/math/rect.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/math/rect.rs b/src/math/rect.rs
index 5603642..50c1cb0 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 :/