mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 20:53:53 +00:00
normalize
method for Rect
(#10297)
# Objective `normalize` method that expresses a rectangle relative to a normalized [0..1] x [0..1] space defined by another rectangle. Useful for UI and texture atlas calculations etc. --------- Co-authored-by: Rob Parrett <robparrett@gmail.com>
This commit is contained in:
parent
08248126f0
commit
c959c1ba58
1 changed files with 24 additions and 0 deletions
|
@ -309,6 +309,30 @@ impl Rect {
|
||||||
r
|
r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Build a new rectangle from this one with its coordinates expressed
|
||||||
|
/// relative to `other` in a normalized ([0..1] x [0..1]) coordinate system.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use bevy_math::{Rect, Vec2};
|
||||||
|
/// let r = Rect::new(2., 3., 4., 6.);
|
||||||
|
/// let s = Rect::new(0., 0., 10., 10.);
|
||||||
|
/// let n = r.normalize(s);
|
||||||
|
///
|
||||||
|
/// assert_eq!(n.min.x, 0.2);
|
||||||
|
/// assert_eq!(n.min.y, 0.3);
|
||||||
|
/// assert_eq!(n.max.x, 0.4);
|
||||||
|
/// assert_eq!(n.max.y, 0.6);
|
||||||
|
/// ```
|
||||||
|
pub fn normalize(&self, other: Self) -> Self {
|
||||||
|
let outer_size = other.size();
|
||||||
|
Self {
|
||||||
|
min: (self.min - other.min) / outer_size,
|
||||||
|
max: (self.max - other.min) / outer_size,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns self as [`IRect`] (i32)
|
/// Returns self as [`IRect`] (i32)
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn as_irect(&self) -> IRect {
|
pub fn as_irect(&self) -> IRect {
|
||||||
|
|
Loading…
Reference in a new issue