iced_core/
rectangle.rs

1use crate::alignment;
2use crate::{Padding, Point, Radians, Size, Vector};
3
4/// An axis-aligned rectangle.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
6pub struct Rectangle<T = f32> {
7    /// X coordinate of the top-left corner.
8    pub x: T,
9
10    /// Y coordinate of the top-left corner.
11    pub y: T,
12
13    /// Width of the rectangle.
14    pub width: T,
15
16    /// Height of the rectangle.
17    pub height: T,
18}
19
20impl<T> Rectangle<T>
21where
22    T: Default,
23{
24    /// Creates a new [`Rectangle`] with its top-left corner at the origin
25    /// and with the provided [`Size`].
26    pub fn with_size(size: Size<T>) -> Self {
27        Self {
28            x: T::default(),
29            y: T::default(),
30            width: size.width,
31            height: size.height,
32        }
33    }
34}
35
36impl Rectangle<f32> {
37    /// A rectangle starting at [`Point::ORIGIN`] with infinite width and height.
38    pub const INFINITE: Self = Self::new(Point::ORIGIN, Size::INFINITY);
39
40    /// Creates a new [`Rectangle`] with its top-left corner in the given
41    /// [`Point`] and with the provided [`Size`].
42    pub const fn new(top_left: Point, size: Size) -> Self {
43        Self {
44            x: top_left.x,
45            y: top_left.y,
46            width: size.width,
47            height: size.height,
48        }
49    }
50
51    /// Creates a new square [`Rectangle`] with the center at the origin and
52    /// with the given radius.
53    pub fn with_radius(radius: f32) -> Self {
54        Self {
55            x: -radius,
56            y: -radius,
57            width: radius * 2.0,
58            height: radius * 2.0,
59        }
60    }
61
62    /// Creates a new axis-aligned [`Rectangle`] from the given vertices; returning the
63    /// rotation in [`Radians`] that must be applied to the axis-aligned [`Rectangle`]
64    /// to obtain the desired result.
65    pub fn with_vertices(
66        top_left: Point,
67        top_right: Point,
68        bottom_left: Point,
69    ) -> (Rectangle, Radians) {
70        let width = (top_right.x - top_left.x).hypot(top_right.y - top_left.y);
71
72        let height =
73            (bottom_left.x - top_left.x).hypot(bottom_left.y - top_left.y);
74
75        let rotation =
76            (top_right.y - top_left.y).atan2(top_right.x - top_left.x);
77
78        let rotation = if rotation < 0.0 {
79            2.0 * std::f32::consts::PI + rotation
80        } else {
81            rotation
82        };
83
84        let position = {
85            let center = Point::new(
86                (top_right.x + bottom_left.x) / 2.0,
87                (top_right.y + bottom_left.y) / 2.0,
88            );
89
90            let rotation = -rotation - std::f32::consts::PI * 2.0;
91
92            Point::new(
93                center.x + (top_left.x - center.x) * rotation.cos()
94                    - (top_left.y - center.y) * rotation.sin(),
95                center.y
96                    + (top_left.x - center.x) * rotation.sin()
97                    + (top_left.y - center.y) * rotation.cos(),
98            )
99        };
100
101        (
102            Rectangle::new(position, Size::new(width, height)),
103            Radians(rotation),
104        )
105    }
106
107    /// Returns the [`Point`] at the center of the [`Rectangle`].
108    pub fn center(&self) -> Point {
109        Point::new(self.center_x(), self.center_y())
110    }
111
112    /// Returns the X coordinate of the [`Point`] at the center of the
113    /// [`Rectangle`].
114    pub fn center_x(&self) -> f32 {
115        self.x + self.width / 2.0
116    }
117
118    /// Returns the Y coordinate of the [`Point`] at the center of the
119    /// [`Rectangle`].
120    pub fn center_y(&self) -> f32 {
121        self.y + self.height / 2.0
122    }
123
124    /// Returns the position of the top left corner of the [`Rectangle`].
125    pub fn position(&self) -> Point {
126        Point::new(self.x, self.y)
127    }
128
129    /// Returns the [`Size`] of the [`Rectangle`].
130    pub fn size(&self) -> Size {
131        Size::new(self.width, self.height)
132    }
133
134    /// Returns the area of the [`Rectangle`].
135    pub fn area(&self) -> f32 {
136        self.width * self.height
137    }
138
139    /// Returns true if the given [`Point`] is contained in the [`Rectangle`].
140    /// Excludes the right and bottom edges.
141    pub fn contains(&self, point: Point) -> bool {
142        self.x <= point.x
143            && point.x < self.x + self.width
144            && self.y <= point.y
145            && point.y < self.y + self.height
146    }
147
148    /// Returns the minimum distance from the given [`Point`] to any of the edges
149    /// of the [`Rectangle`].
150    pub fn distance(&self, point: Point) -> f32 {
151        let center = self.center();
152
153        let distance_x =
154            ((point.x - center.x).abs() - self.width / 2.0).max(0.0);
155
156        let distance_y =
157            ((point.y - center.y).abs() - self.height / 2.0).max(0.0);
158
159        distance_x.hypot(distance_y)
160    }
161
162    /// Computes the offset that must be applied to the [`Rectangle`] to be placed
163    /// inside the given `container`.
164    pub fn offset(&self, container: &Rectangle) -> Vector {
165        let Some(intersection) = self.intersection(container) else {
166            return Vector::ZERO;
167        };
168
169        let left = intersection.x - self.x;
170        let top = intersection.y - self.y;
171
172        Vector::new(
173            if left > 0.0 {
174                left
175            } else {
176                intersection.x + intersection.width - self.x - self.width
177            },
178            if top > 0.0 {
179                top
180            } else {
181                intersection.y + intersection.height - self.y - self.height
182            },
183        )
184    }
185
186    /// Returns true if the current [`Rectangle`] is within the given
187    /// `container`. Includes the right and bottom edges.
188    pub fn is_within(&self, container: &Rectangle) -> bool {
189        self.x >= container.x
190            && self.y >= container.y
191            && self.x + self.width <= container.x + container.width
192            && self.y + self.height <= container.y + container.height
193    }
194
195    /// Computes the intersection with the given [`Rectangle`].
196    pub fn intersection(
197        &self,
198        other: &Rectangle<f32>,
199    ) -> Option<Rectangle<f32>> {
200        let x = self.x.max(other.x);
201        let y = self.y.max(other.y);
202
203        let lower_right_x = (self.x + self.width).min(other.x + other.width);
204        let lower_right_y = (self.y + self.height).min(other.y + other.height);
205
206        let width = lower_right_x - x;
207        let height = lower_right_y - y;
208
209        if width > 0.0 && height > 0.0 {
210            Some(Rectangle {
211                x,
212                y,
213                width,
214                height,
215            })
216        } else {
217            None
218        }
219    }
220
221    /// Returns whether the [`Rectangle`] intersects with the given one.
222    pub fn intersects(&self, other: &Self) -> bool {
223        self.intersection(other).is_some()
224    }
225
226    /// Computes the union with the given [`Rectangle`].
227    pub fn union(&self, other: &Self) -> Self {
228        let x = self.x.min(other.x);
229        let y = self.y.min(other.y);
230
231        let lower_right_x = (self.x + self.width).max(other.x + other.width);
232        let lower_right_y = (self.y + self.height).max(other.y + other.height);
233
234        let width = lower_right_x - x;
235        let height = lower_right_y - y;
236
237        Rectangle {
238            x,
239            y,
240            width,
241            height,
242        }
243    }
244
245    /// Snaps the [`Rectangle`] to __unsigned__ integer coordinates.
246    pub fn snap(self) -> Option<Rectangle<u32>> {
247        let top_left = self.position().snap();
248        let bottom_right = (self.position() + Vector::from(self.size())).snap();
249
250        let width = bottom_right.x.checked_sub(top_left.x)?;
251        let height = bottom_right.y.checked_sub(top_left.y)?;
252
253        if width < 1 || height < 1 {
254            return None;
255        }
256
257        Some(Rectangle {
258            x: top_left.x,
259            y: top_left.y,
260            width,
261            height,
262        })
263    }
264
265    /// Expands the [`Rectangle`] a given amount.
266    pub fn expand(self, padding: impl Into<Padding>) -> Self {
267        let padding = padding.into();
268
269        Self {
270            x: self.x - padding.left,
271            y: self.y - padding.top,
272            width: self.width + padding.horizontal(),
273            height: self.height + padding.vertical(),
274        }
275    }
276
277    /// Shrinks the [`Rectangle`] a given amount.
278    pub fn shrink(self, padding: impl Into<Padding>) -> Self {
279        let padding = padding.into();
280
281        Self {
282            x: self.x + padding.left,
283            y: self.y + padding.top,
284            width: self.width - padding.horizontal(),
285            height: self.height - padding.vertical(),
286        }
287    }
288
289    /// Rotates the [`Rectangle`] and returns the smallest [`Rectangle`]
290    /// containing it.
291    pub fn rotate(self, rotation: Radians) -> Self {
292        let size = self.size().rotate(rotation);
293        let position = Point::new(
294            self.center_x() - size.width / 2.0,
295            self.center_y() - size.height / 2.0,
296        );
297
298        Self::new(position, size)
299    }
300
301    /// Scales the [`Rectangle`] without changing its position, effectively
302    /// "zooming" it.
303    pub fn zoom(self, zoom: f32) -> Self {
304        Self {
305            x: self.x - (self.width * (zoom - 1.0)) / 2.0,
306            y: self.y - (self.height * (zoom - 1.0)) / 2.0,
307            width: self.width * zoom,
308            height: self.height * zoom,
309        }
310    }
311
312    /// Returns the top-left position to render an object of the given [`Size`].
313    /// inside the [`Rectangle`] that is anchored to the edge or corner
314    /// defined by the alignment arguments.
315    pub fn anchor(
316        &self,
317        size: Size,
318        align_x: impl Into<alignment::Horizontal>,
319        align_y: impl Into<alignment::Vertical>,
320    ) -> Point {
321        let x = match align_x.into() {
322            alignment::Horizontal::Left => self.x,
323            alignment::Horizontal::Center => {
324                self.x + (self.width - size.width) / 2.0
325            }
326            alignment::Horizontal::Right => self.x + self.width - size.width,
327        };
328
329        let y = match align_y.into() {
330            alignment::Vertical::Top => self.y,
331            alignment::Vertical::Center => {
332                self.y + (self.height - size.height) / 2.0
333            }
334            alignment::Vertical::Bottom => self.y + self.height - size.height,
335        };
336
337        Point::new(x, y)
338    }
339}
340
341impl std::ops::Mul<f32> for Rectangle<f32> {
342    type Output = Self;
343
344    fn mul(self, scale: f32) -> Self {
345        Self {
346            x: self.x * scale,
347            y: self.y * scale,
348            width: self.width * scale,
349            height: self.height * scale,
350        }
351    }
352}
353
354impl From<Rectangle<u32>> for Rectangle<f32> {
355    fn from(rectangle: Rectangle<u32>) -> Rectangle<f32> {
356        Rectangle {
357            x: rectangle.x as f32,
358            y: rectangle.y as f32,
359            width: rectangle.width as f32,
360            height: rectangle.height as f32,
361        }
362    }
363}
364
365impl<T> std::ops::Add<Vector<T>> for Rectangle<T>
366where
367    T: std::ops::Add<Output = T>,
368{
369    type Output = Rectangle<T>;
370
371    fn add(self, translation: Vector<T>) -> Self {
372        Rectangle {
373            x: self.x + translation.x,
374            y: self.y + translation.y,
375            ..self
376        }
377    }
378}
379
380impl<T> std::ops::Sub<Vector<T>> for Rectangle<T>
381where
382    T: std::ops::Sub<Output = T>,
383{
384    type Output = Rectangle<T>;
385
386    fn sub(self, translation: Vector<T>) -> Self {
387        Rectangle {
388            x: self.x - translation.x,
389            y: self.y - translation.y,
390            ..self
391        }
392    }
393}
394
395impl<T> std::ops::Mul<Vector<T>> for Rectangle<T>
396where
397    T: std::ops::Mul<Output = T> + Copy,
398{
399    type Output = Rectangle<T>;
400
401    fn mul(self, scale: Vector<T>) -> Self {
402        Rectangle {
403            x: self.x * scale.x,
404            y: self.y * scale.y,
405            width: self.width * scale.x,
406            height: self.height * scale.y,
407        }
408    }
409}