bevy_math/
aspect_ratio.rs

1//! Provides a simple aspect ratio struct to help with calculations.
2
3use crate::Vec2;
4
5#[cfg(feature = "bevy_reflect")]
6use bevy_reflect::Reflect;
7
8/// An `AspectRatio` is the ratio of width to height.
9#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
10#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
11pub struct AspectRatio(f32);
12
13impl AspectRatio {
14    /// Create a new `AspectRatio` from a given `width` and `height`.
15    #[inline]
16    pub fn new(width: f32, height: f32) -> Self {
17        Self(width / height)
18    }
19
20    /// Create a new `AspectRatio` from a given amount of `x` pixels and `y` pixels.
21    #[inline]
22    pub fn from_pixels(x: u32, y: u32) -> Self {
23        Self::new(x as f32, y as f32)
24    }
25}
26
27impl From<Vec2> for AspectRatio {
28    #[inline]
29    fn from(value: Vec2) -> Self {
30        Self::new(value.x, value.y)
31    }
32}
33
34impl From<AspectRatio> for f32 {
35    #[inline]
36    fn from(aspect_ratio: AspectRatio) -> Self {
37        aspect_ratio.0
38    }
39}