From 9c951b16cbe48fc06fd867dae283d06f7174d903 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Sat, 28 Oct 2023 16:23:39 +0100 Subject: [PATCH 1/5] Added a method `normalize` to `Rect`. `normalize` expresses the coordinates of a `Rect` relative to a normalized [0..1] x [0..1] space defined by another `Rect`. --- crates/bevy_math/src/rects/rect.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/crates/bevy_math/src/rects/rect.rs b/crates/bevy_math/src/rects/rect.rs index a5c456f462121..d22cc290051c2 100644 --- a/crates/bevy_math/src/rects/rect.rs +++ b/crates/bevy_math/src/rects/rect.rs @@ -309,6 +309,29 @@ impl Rect { r } + /// Express the coordinates of `self` relative to a normalized [0..1] x [0..1] space defined by `other`. + /// + /// # Example + /// + /// ``` + /// # use bevy_math::{Rect, Vec2}; + /// let r = Rect::new(2.0, 3.0, 4.0, y: 6.0 }; + /// let s = Rect::new(0.0, 0.0, 10.0, 10.0 }; + /// 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) #[inline] pub fn as_irect(&self) -> IRect { From 0f14dce721ae5914071c95bfb30607a2b86ee652 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Sat, 28 Oct 2023 16:24:17 +0100 Subject: [PATCH 2/5] fixed doc example syntax --- crates/bevy_math/src/rects/rect.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_math/src/rects/rect.rs b/crates/bevy_math/src/rects/rect.rs index d22cc290051c2..a4921e867f77f 100644 --- a/crates/bevy_math/src/rects/rect.rs +++ b/crates/bevy_math/src/rects/rect.rs @@ -311,9 +311,9 @@ impl Rect { /// Express the coordinates of `self` relative to a normalized [0..1] x [0..1] space defined by `other`. /// - /// # Example + /// # Examples /// - /// ``` + /// ```rust /// # use bevy_math::{Rect, Vec2}; /// let r = Rect::new(2.0, 3.0, 4.0, y: 6.0 }; /// let s = Rect::new(0.0, 0.0, 10.0, 10.0 }; From 62b054b6c92537b66b260111d2ab0ea35bf5e49b Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Sat, 28 Oct 2023 16:26:03 +0100 Subject: [PATCH 3/5] fixed doctest --- crates/bevy_math/src/rects/rect.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_math/src/rects/rect.rs b/crates/bevy_math/src/rects/rect.rs index a4921e867f77f..07f64c0832dd3 100644 --- a/crates/bevy_math/src/rects/rect.rs +++ b/crates/bevy_math/src/rects/rect.rs @@ -315,8 +315,8 @@ impl Rect { /// /// ```rust /// # use bevy_math::{Rect, Vec2}; - /// let r = Rect::new(2.0, 3.0, 4.0, y: 6.0 }; - /// let s = Rect::new(0.0, 0.0, 10.0, 10.0 }; + /// 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); From 5d6a58ce454818d0ba3ff7dcfd8beb12c201a166 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Sat, 28 Oct 2023 16:27:24 +0100 Subject: [PATCH 4/5] Comment edit --- crates/bevy_math/src/rects/rect.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/bevy_math/src/rects/rect.rs b/crates/bevy_math/src/rects/rect.rs index 07f64c0832dd3..7c1507f387978 100644 --- a/crates/bevy_math/src/rects/rect.rs +++ b/crates/bevy_math/src/rects/rect.rs @@ -309,7 +309,8 @@ impl Rect { r } - /// Express the coordinates of `self` relative to a normalized [0..1] x [0..1] space defined by `other`. + /// Express the coordinates of this rectangle relative to a normalized [0..1] x [0..1] space + /// defined by another rectangle. /// /// # Examples /// From 92efe68ff955456a747e9b9cd3e6482b27b22b50 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Sat, 28 Oct 2023 17:57:07 +0100 Subject: [PATCH 5/5] Update crates/bevy_math/src/rects/rect.rs Co-authored-by: Rob Parrett --- crates/bevy_math/src/rects/rect.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_math/src/rects/rect.rs b/crates/bevy_math/src/rects/rect.rs index 7c1507f387978..d3c5aa5bb563f 100644 --- a/crates/bevy_math/src/rects/rect.rs +++ b/crates/bevy_math/src/rects/rect.rs @@ -309,8 +309,8 @@ impl Rect { r } - /// Express the coordinates of this rectangle relative to a normalized [0..1] x [0..1] space - /// defined by another rectangle. + /// 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 ///