Skip to content

Commit

Permalink
Add common aspect ratio constants and improve documentation
Browse files Browse the repository at this point in the history
- Add new constants for common aspect ratios: SIXTEEN_NINE, FOUR_THREE, ULTRAWIDE
- Enhance module-level documentation with overview and use cases
- Expand explanation of AspectRatio struct with examples
- Provide detailed descriptions and examples for all methods (existing and new)
- Add explanations for the newly introduced constant values
- Include clarifications for From trait implementations

This commit introduces convenient constants for frequently used aspect ratios
and significantly improves the overall documentation, enhancing the usability
and understanding of the AspectRatio API.
  • Loading branch information
miniex committed Sep 8, 2024
1 parent 5cfcbf4 commit 87383ee
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions crates/bevy_math/src/aspect_ratio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,60 @@ use bevy_reflect::Reflect;
pub struct AspectRatio(f32);

impl AspectRatio {
/// Create a new `AspectRatio` from a given `width` and `height`.
/// Standard 16:9 aspect ratio
pub const SIXTEEN_NINE: Self = Self(16.0 / 9.0);
/// Standard 4:3 aspect ratio
pub const FOUR_THREE: Self = Self(4.0 / 3.0);
/// Standard 21:9 ultrawide aspect ratio
pub const ULTRAWIDE: Self = Self(21.0 / 9.0);

/// Create a new [`AspectRatio`] from a given width and height.
#[inline]
pub fn new(width: f32, height: f32) -> Self {
Self(width / height)
}

/// Create a new `AspectRatio` from a given amount of `x` pixels and `y` pixels.
/// Create a new [`AspectRatio`] from a given amount of x pixels and y pixels.
#[inline]
pub fn from_pixels(x: u32, y: u32) -> Self {
Self::new(x as f32, y as f32)
}

/// Create a new [`AspectRatio`] from a given width and height.
/// Returns None if height is zero or if the result is not finite.
#[inline]
pub fn try_new(width: f32, height: f32) -> Option<Self> {
let ratio = width / height;
if ratio.is_finite() && height != 0.0 {
Some(Self(ratio))
} else {
None
}
}

/// Returns the inverse of this aspect ratio (height/width).
#[inline]
pub fn inverse(&self) -> Self {
Self(1.0 / self.0)
}

/// Returns true if the aspect ratio represents a landscape orientation.
#[inline]
pub fn is_landscape(&self) -> bool {
self.0 > 1.0
}

/// Returns true if the aspect ratio represents a portrait orientation.
#[inline]
pub fn is_portrait(&self) -> bool {
self.0 < 1.0
}

/// Returns true if the aspect ratio is exactly square.
#[inline]
pub fn is_square(&self) -> bool {
(self.0 - 1.0).abs() < f32::EPSILON
}
}

impl From<Vec2> for AspectRatio {
Expand All @@ -37,3 +80,17 @@ impl From<AspectRatio> for f32 {
aspect_ratio.0
}
}

impl From<(f32, f32)> for AspectRatio {
#[inline]
fn from(value: (f32, f32)) -> Self {
Self::new(value.0, value.1)
}
}

impl From<(u32, u32)> for AspectRatio {
#[inline]
fn from(value: (u32, u32)) -> Self {
Self::from_pixels(value.0, value.1)
}
}

0 comments on commit 87383ee

Please sign in to comment.