Add helper functions to Rect
This commit is contained in:
parent
0c9b4858f0
commit
4e852727c0
1 changed files with 71 additions and 1 deletions
|
@ -106,6 +106,34 @@ impl Rect {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A `Rect` that contains every point to the right of the given X coordinate.
|
||||||
|
pub fn everything_right_of(left_x: f32) -> Self {
|
||||||
|
let mut rect = Self::EVERYTHING;
|
||||||
|
rect.set_left(left_x);
|
||||||
|
rect
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A `Rect` that contains every point to the left of the given X coordinate.
|
||||||
|
pub fn everything_left_of(right_x: f32) -> Self {
|
||||||
|
let mut rect = Self::EVERYTHING;
|
||||||
|
rect.set_right(right_x);
|
||||||
|
rect
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A `Rect` that contains every point below a certain y coordinate
|
||||||
|
pub fn everything_below(top_y: f32) -> Self {
|
||||||
|
let mut rect = Self::EVERYTHING;
|
||||||
|
rect.set_top(top_y);
|
||||||
|
rect
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A `Rect` that contains every point above a certain y coordinate
|
||||||
|
pub fn everything_above(bottom_y: f32) -> Self {
|
||||||
|
let mut rect = Self::EVERYTHING;
|
||||||
|
rect.set_bottom(bottom_y);
|
||||||
|
rect
|
||||||
|
}
|
||||||
|
|
||||||
/// Expand by this much in each direction, keeping the center
|
/// Expand by this much in each direction, keeping the center
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn expand(self, amnt: f32) -> Self {
|
pub fn expand(self, amnt: f32) -> Self {
|
||||||
|
@ -265,20 +293,62 @@ impl Rect {
|
||||||
pub fn is_finite(&self) -> bool {
|
pub fn is_finite(&self) -> bool {
|
||||||
self.min.is_finite() && self.max.is_finite()
|
self.min.is_finite() && self.max.is_finite()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Convenience functions (assumes origin is towards left top):
|
/// ## Convenience functions (assumes origin is towards left top):
|
||||||
|
impl Rect {
|
||||||
|
/// `min.x`
|
||||||
pub fn left(&self) -> f32 {
|
pub fn left(&self) -> f32 {
|
||||||
self.min.x
|
self.min.x
|
||||||
}
|
}
|
||||||
|
/// `min.x`
|
||||||
|
pub fn left_mut(&mut self) -> &mut f32 {
|
||||||
|
&mut self.min.x
|
||||||
|
}
|
||||||
|
/// `min.x`
|
||||||
|
pub fn set_left(&mut self, x: f32) {
|
||||||
|
self.min.x = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `max.x`
|
||||||
pub fn right(&self) -> f32 {
|
pub fn right(&self) -> f32 {
|
||||||
self.max.x
|
self.max.x
|
||||||
}
|
}
|
||||||
|
/// `max.x`
|
||||||
|
pub fn right_mut(&mut self) -> &mut f32 {
|
||||||
|
&mut self.max.x
|
||||||
|
}
|
||||||
|
/// `max.x`
|
||||||
|
pub fn set_right(&mut self, x: f32) {
|
||||||
|
self.max.x = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `min.y`
|
||||||
pub fn top(&self) -> f32 {
|
pub fn top(&self) -> f32 {
|
||||||
self.min.y
|
self.min.y
|
||||||
}
|
}
|
||||||
|
/// `min.y`
|
||||||
|
pub fn top_mut(&mut self) -> &mut f32 {
|
||||||
|
&mut self.min.y
|
||||||
|
}
|
||||||
|
/// `min.y`
|
||||||
|
pub fn set_top(&mut self, y: f32) {
|
||||||
|
self.min.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `max.y`
|
||||||
pub fn bottom(&self) -> f32 {
|
pub fn bottom(&self) -> f32 {
|
||||||
self.max.y
|
self.max.y
|
||||||
}
|
}
|
||||||
|
/// `max.y`
|
||||||
|
pub fn bottom_mut(&mut self) -> &mut f32 {
|
||||||
|
&mut self.max.y
|
||||||
|
}
|
||||||
|
/// `max.y`
|
||||||
|
pub fn set_bottom(&mut self, y: f32) {
|
||||||
|
self.max.y = y;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn left_top(&self) -> Pos2 {
|
pub fn left_top(&self) -> Pos2 {
|
||||||
pos2(self.left(), self.top())
|
pos2(self.left(), self.top())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue