Add ratio method to Size

Co-authored-by: tigerros <tigerros@users.noreply.github.com>
This commit is contained in:
Héctor Ramón Jiménez 2025-11-24 10:42:57 +01:00
parent 8bfd099c59
commit 47eb1942c9
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -52,7 +52,7 @@ impl Size {
} }
} }
/// Rotates the given [`Size`] and returns the minimum [`Size`] /// Rotates this [`Size`] and returns the minimum [`Size`]
/// containing it. /// containing it.
pub fn rotate(self, rotation: Radians) -> Size { pub fn rotate(self, rotation: Radians) -> Size {
let radians = f32::from(rotation); let radians = f32::from(rotation);
@ -64,6 +64,15 @@ impl Size {
+ (self.height * radians.cos()).abs(), + (self.height * radians.cos()).abs(),
} }
} }
/// Applies an aspect ratio to this [`Size`] without
/// exceeding its bounds.
pub const fn ratio(self, aspect_ratio: f32) -> Size {
Size {
width: (self.height * aspect_ratio).min(self.width),
height: (self.width / aspect_ratio).min(self.height),
}
}
} }
impl Size<Length> { impl Size<Length> {