@@ -5,18 +5,29 @@ use crate::*;
5
5
6
6
/// A rectangular region of space.
7
7
///
8
- /// Normally given in points, e.g. logical pixels.
8
+ /// Usually a `Rect` has a positive (or zero) size,
9
+ /// and then [`Self::min`] `<=` [`Self::max`].
10
+ /// In these cases [`Self::min`] is the left-top corner
11
+ /// and [`Self::max`] is the right-bottom corner.
12
+ ///
13
+ /// A rectangle is allowed to have a negative size, which happens when the order
14
+ /// of `min` and `max` are swapped. These are usually a sign of an error.
15
+ ///
16
+ /// Normally the unit is points (logical pixels) in screen space coordinates.
9
17
#[ repr( C ) ]
10
18
#[ derive( Clone , Copy , Eq , PartialEq ) ]
11
19
#[ cfg_attr( feature = "serde" , derive( serde:: Deserialize , serde:: Serialize ) ) ]
12
20
#[ cfg_attr( feature = "bytemuck" , derive( bytemuck:: Pod , bytemuck:: Zeroable ) ) ]
13
21
pub struct Rect {
22
+ /// One of the corners of the rectangle, usually the left top one.
14
23
pub min : Pos2 ,
24
+
25
+ /// The other corner, opposing [`Self::min`]. Usually the right bottom one.
15
26
pub max : Pos2 ,
16
27
}
17
28
18
29
impl Rect {
19
- /// Infinite rectangle that contains everything .
30
+ /// Infinite rectangle that contains every point .
20
31
pub const EVERYTHING : Self = Self {
21
32
min : pos2 ( -INFINITY , -INFINITY ) ,
22
33
max : pos2 ( INFINITY , INFINITY ) ,
@@ -25,19 +36,14 @@ impl Rect {
25
36
/// The inverse of [`Self::EVERYTHING`]: stretches from positive infinity to negative infinity.
26
37
/// Contains no points.
27
38
///
28
- /// This is useful as the seed for bounding bounding boxes.
29
- ///
30
- /// ```
31
- /// # use emath::*;
32
- /// let inf = f32::INFINITY;
33
- /// assert!(Rect::NOTHING.size() == Vec2::splat(-inf));
34
- /// assert!(Rect::NOTHING.contains(pos2(0.0, 0.0)) == false);
35
- /// ```
39
+ /// This is useful as the seed for bounding boxes.
36
40
///
37
41
/// # Example:
38
42
/// ```
39
43
/// # use emath::*;
40
44
/// let mut rect = Rect::NOTHING;
45
+ /// assert!(rect.size() == Vec2::splat(-f32::INFINITY));
46
+ /// assert!(rect.contains(pos2(0.0, 0.0)) == false);
41
47
/// rect.extend_with(pos2(2.0, 1.0));
42
48
/// rect.extend_with(pos2(0.0, 3.0));
43
49
/// assert_eq!(rect, Rect::from_min_max(pos2(0.0, 1.0), pos2(2.0, 3.0)))
@@ -50,14 +56,15 @@ impl Rect {
50
56
/// An invalid `Rect` filled with [`f32::NAN`];
51
57
pub const NAN : Self = Self {
52
58
min : pos2 ( f32:: NAN , f32:: NAN ) ,
53
- max : pos2 ( - f32:: NAN , - f32:: NAN ) ,
59
+ max : pos2 ( f32:: NAN , f32:: NAN ) ,
54
60
} ;
55
61
56
62
#[ inline( always) ]
57
63
pub const fn from_min_max ( min : Pos2 , max : Pos2 ) -> Self {
58
64
Rect { min, max }
59
65
}
60
66
67
+ /// left-top corner plus a size (stretching right-down).
61
68
#[ inline( always) ]
62
69
pub fn from_min_size ( min : Pos2 , size : Vec2 ) -> Self {
63
70
Rect {
@@ -82,6 +89,7 @@ impl Rect {
82
89
}
83
90
}
84
91
92
+ /// Returns the bounding rectangle of the two points.
85
93
#[ inline]
86
94
pub fn from_two_pos ( a : Pos2 , b : Pos2 ) -> Self {
87
95
Rect {
@@ -90,7 +98,7 @@ impl Rect {
90
98
}
91
99
}
92
100
93
- /// Bounding-box around the points
101
+ /// Bounding-box around the points.
94
102
pub fn from_points ( points : & [ Pos2 ] ) -> Self {
95
103
let mut rect = Rect :: NOTHING ;
96
104
for & p in points {
0 commit comments