From 47eb1942c96c4096e0856a208956e9aea830101f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Mon, 24 Nov 2025 10:42:57 +0100 Subject: [PATCH] Add `ratio` method to `Size` Co-authored-by: tigerros --- core/src/size.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/core/src/size.rs b/core/src/size.rs index baa80b01..70eb38fe 100644 --- a/core/src/size.rs +++ b/core/src/size.rs @@ -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. pub fn rotate(self, rotation: Radians) -> Size { let radians = f32::from(rotation); @@ -64,6 +64,15 @@ impl Size { + (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 {