Download node-graph/gcore/src/math/rect.rs from openfree/graphite2: direct link, hf CLI and curl.
- Browser
- Download file 3.2 kB
-
https://huggingface.co/spaces/openfree/graphite2/resolve/main/node-graph/gcore/src/math/rect.rs
- Command line
-
hf download hf://spaces/openfree/graphite2/node-graph/gcore/src/math/rect.rs
-
curl -L -o rect.rs https://huggingface.co/spaces/openfree/graphite2/resolve/main/node-graph/gcore/src/math/rect.rs
3.2 kB
| use crate::math::quad::Quad; | |
| use glam::{DAffine2, DVec2}; | |
| /// An axis aligned rect defined by two vertices. | |
| pub struct Rect(pub [DVec2; 2]); | |
| impl Rect { | |
| /// Create a zero sized quad at the point | |
| pub fn from_point(point: DVec2) -> Self { | |
| Self([point; 2]) | |
| } | |
| /// Convert a box defined by two corner points to a quad. | |
| pub fn from_box(bbox: [DVec2; 2]) -> Self { | |
| Self([bbox[0].min(bbox[1]), bbox[0].max(bbox[1])]) | |
| } | |
| /// Create a quad from the center and offset (distance from center to middle of an edge) | |
| pub fn from_square(center: DVec2, offset: f64) -> Self { | |
| Self::from_box([center - offset, center + offset]) | |
| } | |
| /// Create an AABB from an iter of points, returning None if empty. | |
| pub fn point_iter(points: impl Iterator<Item = DVec2>) -> Option<Self> { | |
| let mut bounds = None; | |
| for point in points { | |
| let bounds = bounds.get_or_insert(Self::from_point(point)); | |
| bounds[0] = bounds[0].min(point); | |
| bounds[1] = bounds[1].max(point); | |
| } | |
| bounds | |
| } | |
| /// Get all the edges in the rect. | |
| pub fn edges(&self) -> [[DVec2; 2]; 4] { | |
| let corners = [self[0], DVec2::new(self[0].x, self[1].y), self[1], DVec2::new(self[1].y, self[0].x)]; | |
| [[corners[0], corners[1]], [corners[1], corners[2]], [corners[2], corners[3]], [corners[3], corners[0]]] | |
| } | |
| /// Gets the center of a rect | |
| pub fn center(&self) -> DVec2 { | |
| self.0.iter().sum::<DVec2>() / 2. | |
| } | |
| /// Take the outside bounds of two axis aligned rectangles, which are defined by two corner points. | |
| pub fn combine_bounds(a: Self, b: Self) -> Self { | |
| Self::from_box([a[0].min(b[0]), a[1].max(b[1])]) | |
| } | |
| /// Expand a rect by a certain amount on top/bottom and on left/right | |
| pub fn expand_by(&self, x: f64, y: f64) -> Self { | |
| let delta = DVec2::new(x, y); | |
| Self::from_box([self[0] - delta, self[1] + delta]) | |
| } | |
| /// Checks if two rects intersect | |
| pub fn intersects(&self, other: Self) -> bool { | |
| let [mina, maxa] = [self[0].min(self[1]), self[0].max(self[1])]; | |
| let [minb, maxb] = [other[0].min(other[1]), other[0].max(other[1])]; | |
| mina.x <= maxb.x && minb.x <= maxa.x && mina.y <= maxb.y && minb.y <= maxa.y | |
| } | |
| /// Does this rect contain a point | |
| pub fn contains(&self, p: DVec2) -> bool { | |
| (self[0].x < p.x && p.x < self[1].x) && (self[0].y < p.y && p.y < self[1].y) | |
| } | |
| pub fn min(&self) -> DVec2 { | |
| self.0[0].min(self.0[1]) | |
| } | |
| pub fn max(&self) -> DVec2 { | |
| self.0[0].max(self.0[1]) | |
| } | |
| pub fn translate(&self, offset: DVec2) -> Self { | |
| Self([self.0[0] + offset, self.0[1] + offset]) | |
| } | |
| } | |
| impl std::ops::Mul<Rect> for DAffine2 { | |
| type Output = Quad; | |
| fn mul(self, rhs: Rect) -> Self::Output { | |
| self * Quad::from_box(rhs.0) | |
| } | |
| } | |
| impl std::ops::Index<usize> for Rect { | |
| type Output = DVec2; | |
| fn index(&self, index: usize) -> &Self::Output { | |
| &self.0[index] | |
| } | |
| } | |
| impl std::ops::IndexMut<usize> for Rect { | |
| fn index_mut(&mut self, index: usize) -> &mut Self::Output { | |
| &mut self.0[index] | |
| } | |
| } | |
| impl From<Rect> for Quad { | |
| fn from(val: Rect) -> Self { | |
| Quad::from_box(val.0) | |
| } | |
| } | |