1use crate::{Length, Radians, Vector};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
5pub struct Size<T = f32> {
6 pub width: T,
8 pub height: T,
10}
11
12impl<T> Size<T> {
13 pub const fn new(width: T, height: T) -> Self {
15 Size { width, height }
16 }
17}
18
19impl Size {
20 pub const ZERO: Size = Size::new(0., 0.);
22
23 pub const UNIT: Size = Size::new(1., 1.);
25
26 pub const INFINITE: Size = Size::new(f32::INFINITY, f32::INFINITY);
28
29 pub fn min(self, other: Self) -> Self {
31 Size {
32 width: self.width.min(other.width),
33 height: self.height.min(other.height),
34 }
35 }
36
37 pub fn max(self, other: Self) -> Self {
39 Size {
40 width: self.width.max(other.width),
41 height: self.height.max(other.height),
42 }
43 }
44
45 pub fn expand(self, other: impl Into<Size>) -> Self {
47 let other = other.into();
48
49 Size {
50 width: self.width + other.width,
51 height: self.height + other.height,
52 }
53 }
54
55 pub fn rotate(self, rotation: Radians) -> Size {
58 let radians = f32::from(rotation);
59
60 Size {
61 width: (self.width * radians.cos()).abs()
62 + (self.height * radians.sin()).abs(),
63 height: (self.width * radians.sin()).abs()
64 + (self.height * radians.cos()).abs(),
65 }
66 }
67}
68
69impl Size<Length> {
70 #[inline]
72 pub fn is_void(&self) -> bool {
73 matches!(self.width, Length::Fixed(0.0))
74 || matches!(self.height, Length::Fixed(0.0))
75 }
76}
77
78impl<T> From<[T; 2]> for Size<T> {
79 fn from([width, height]: [T; 2]) -> Self {
80 Size { width, height }
81 }
82}
83
84impl<T> From<(T, T)> for Size<T> {
85 fn from((width, height): (T, T)) -> Self {
86 Self { width, height }
87 }
88}
89
90impl<T> From<Vector<T>> for Size<T> {
91 fn from(vector: Vector<T>) -> Self {
92 Size {
93 width: vector.x,
94 height: vector.y,
95 }
96 }
97}
98
99impl<T> From<Size<T>> for [T; 2] {
100 fn from(size: Size<T>) -> Self {
101 [size.width, size.height]
102 }
103}
104
105impl<T> From<Size<T>> for Vector<T> {
106 fn from(size: Size<T>) -> Self {
107 Vector::new(size.width, size.height)
108 }
109}
110
111impl<T> std::ops::Add for Size<T>
112where
113 T: std::ops::Add<Output = T>,
114{
115 type Output = Size<T>;
116
117 fn add(self, rhs: Self) -> Self::Output {
118 Size {
119 width: self.width + rhs.width,
120 height: self.height + rhs.height,
121 }
122 }
123}
124
125impl<T> std::ops::Sub for Size<T>
126where
127 T: std::ops::Sub<Output = T>,
128{
129 type Output = Size<T>;
130
131 fn sub(self, rhs: Self) -> Self::Output {
132 Size {
133 width: self.width - rhs.width,
134 height: self.height - rhs.height,
135 }
136 }
137}
138
139impl<T> std::ops::Mul<T> for Size<T>
140where
141 T: std::ops::Mul<Output = T> + Copy,
142{
143 type Output = Size<T>;
144
145 fn mul(self, rhs: T) -> Self::Output {
146 Size {
147 width: self.width * rhs,
148 height: self.height * rhs,
149 }
150 }
151}
152
153impl<T> std::ops::Mul<Vector<T>> for Size<T>
154where
155 T: std::ops::Mul<Output = T> + Copy,
156{
157 type Output = Size<T>;
158
159 fn mul(self, scale: Vector<T>) -> Self::Output {
160 Size {
161 width: self.width * scale.x,
162 height: self.height * scale.y,
163 }
164 }
165}