mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 22:30:03 -04:00
Add emath::Rangef and helpers for splitting a Rect (#2978)
* emath: add Rangef helper * emath: Add Rect splitting helper * Add helper for interpolating Pos2
This commit is contained in:
@@ -53,7 +53,7 @@ impl Rect {
|
||||
max: pos2(-INFINITY, -INFINITY),
|
||||
};
|
||||
|
||||
/// An invalid [`Rect`] filled with [`f32::NAN`];
|
||||
/// An invalid [`Rect`] filled with [`f32::NAN`].
|
||||
pub const NAN: Self = Self {
|
||||
min: pos2(f32::NAN, f32::NAN),
|
||||
max: pos2(f32::NAN, f32::NAN),
|
||||
@@ -82,7 +82,12 @@ impl Rect {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn from_x_y_ranges(x_range: RangeInclusive<f32>, y_range: RangeInclusive<f32>) -> Self {
|
||||
pub fn from_x_y_ranges(
|
||||
x_range: impl Into<RangeInclusive<f32>>,
|
||||
y_range: impl Into<RangeInclusive<f32>>,
|
||||
) -> Self {
|
||||
let x_range = x_range.into();
|
||||
let y_range = y_range.into();
|
||||
Rect {
|
||||
min: pos2(*x_range.start(), *y_range.start()),
|
||||
max: pos2(*x_range.end(), *y_range.end()),
|
||||
@@ -361,13 +366,28 @@ impl Rect {
|
||||
|
||||
/// Linearly interpolate so that `[0, 0]` is [`Self::min`] and
|
||||
/// `[1, 1]` is [`Self::max`].
|
||||
#[deprecated = "Use `lerp_inside` instead"]
|
||||
pub fn lerp(&self, t: Vec2) -> Pos2 {
|
||||
self.lerp_inside(t)
|
||||
}
|
||||
|
||||
/// Linearly interpolate so that `[0, 0]` is [`Self::min`] and
|
||||
/// `[1, 1]` is [`Self::max`].
|
||||
pub fn lerp_inside(&self, t: Vec2) -> Pos2 {
|
||||
Pos2 {
|
||||
x: lerp(self.min.x..=self.max.x, t.x),
|
||||
y: lerp(self.min.y..=self.max.y, t.y),
|
||||
}
|
||||
}
|
||||
|
||||
/// Linearly self towards other rect.
|
||||
pub fn lerp_towards(&self, other: &Rect, t: f32) -> Self {
|
||||
Self {
|
||||
min: self.min.lerp(other.min, t),
|
||||
max: self.max.lerp(other.max, t),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn x_range(&self) -> RangeInclusive<f32> {
|
||||
self.min.x..=self.max.x
|
||||
@@ -521,6 +541,30 @@ impl Rect {
|
||||
pub fn right_bottom(&self) -> Pos2 {
|
||||
pos2(self.right(), self.bottom())
|
||||
}
|
||||
|
||||
/// Split rectangle in left and right halves. `t` is expected to be in the (0,1) range.
|
||||
pub fn split_left_right_at_fraction(&self, t: f32) -> (Rect, Rect) {
|
||||
self.split_left_right_at_x(lerp(self.min.x..=self.max.x, t))
|
||||
}
|
||||
|
||||
/// Split rectangle in left and right halves at the given `x` coordinate.
|
||||
pub fn split_left_right_at_x(&self, split_x: f32) -> (Rect, Rect) {
|
||||
let left = Rect::from_min_max(self.min, Pos2::new(split_x, self.max.y));
|
||||
let right = Rect::from_min_max(Pos2::new(split_x, self.min.y), self.max);
|
||||
(left, right)
|
||||
}
|
||||
|
||||
/// Split rectangle in top and bottom halves. `t` is expected to be in the (0,1) range.
|
||||
pub fn split_top_bottom_at_fraction(&self, t: f32) -> (Rect, Rect) {
|
||||
self.split_top_bottom_at_y(lerp(self.min.y..=self.max.y, t))
|
||||
}
|
||||
|
||||
/// Split rectangle in top and bottom halves at the given `y` coordinate.
|
||||
pub fn split_top_bottom_at_y(&self, split_y: f32) -> (Rect, Rect) {
|
||||
let top = Rect::from_min_max(self.min, Pos2::new(self.max.x, split_y));
|
||||
let bottom = Rect::from_min_max(Pos2::new(self.min.x, split_y), self.max);
|
||||
(top, bottom)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Rect {
|
||||
|
||||
Reference in New Issue
Block a user