@@ -3,9 +3,13 @@ use glam::*;
3
3
/// A 2D rectangle, defined by its top-left corner, width and height.
4
4
#[ derive( Clone , Copy , Debug , Default , PartialEq ) ]
5
5
pub struct Rect {
6
+ /// The x-coordinate of the top-left corner.
6
7
pub x : f32 ,
8
+ /// The y-coordinate of the top-left corner.
7
9
pub y : f32 ,
10
+ /// The width of the `Rect`, going to the right.
8
11
pub w : f32 ,
12
+ /// The height of the `Rect`, going down.
9
13
pub h : f32 ,
10
14
}
11
15
@@ -32,70 +36,70 @@ impl Rect {
32
36
}
33
37
34
38
/// Returns the center position of the `Rect`.
35
- pub fn center ( & self ) -> Vec2 {
39
+ pub const fn center ( & self ) -> Vec2 {
36
40
vec2 ( self . x + self . w * 0.5f32 , self . y + self . h * 0.5f32 )
37
41
}
38
42
39
- /// Returns the left edge of the `Rect`
43
+ /// Returns the left edge of the `Rect`.
40
44
pub const fn left ( & self ) -> f32 {
41
45
self . x
42
46
}
43
47
44
- /// Returns the right edge of the `Rect`
45
- pub fn right ( & self ) -> f32 {
48
+ /// Returns the right edge of the `Rect`.
49
+ pub const fn right ( & self ) -> f32 {
46
50
self . x + self . w
47
51
}
48
52
49
- /// Returns the top edge of the `Rect`
53
+ /// Returns the top edge of the `Rect`.
50
54
pub const fn top ( & self ) -> f32 {
51
55
self . y
52
56
}
53
57
54
- /// Returns the bottom edge of the `Rect`
55
- pub fn bottom ( & self ) -> f32 {
58
+ /// Returns the bottom edge of the `Rect`.
59
+ pub const fn bottom ( & self ) -> f32 {
56
60
self . y + self . h
57
61
}
58
62
59
- /// Moves the `Rect`'s origin to (x, y)
60
- pub fn move_to ( & mut self , destination : Vec2 ) {
63
+ /// Moves the `Rect`'s origin to (x, y).
64
+ pub const fn move_to ( & mut self , destination : Vec2 ) {
61
65
self . x = destination. x ;
62
66
self . y = destination. y ;
63
67
}
64
68
65
69
/// Scales the `Rect` by a factor of (sx, sy),
66
- /// growing towards the bottom-left
67
- pub fn scale ( & mut self , sx : f32 , sy : f32 ) {
70
+ /// growing towards the bottom-left.
71
+ pub const fn scale ( & mut self , sx : f32 , sy : f32 ) {
68
72
self . w *= sx;
69
73
self . h *= sy;
70
74
}
71
75
72
- /// Checks whether the `Rect` contains a `Point`
73
- pub fn contains ( & self , point : Vec2 ) -> bool {
76
+ /// Checks whether the `Rect` contains a `Point`.
77
+ pub const fn contains ( & self , point : Vec2 ) -> bool {
74
78
point. x >= self . left ( )
75
- && point. x < self . right ( )
76
- && point. y < self . bottom ( )
79
+ && point. x <= self . right ( )
80
+ && point. y <= self . bottom ( )
77
81
&& point. y >= self . top ( )
78
82
}
79
83
80
- /// Checks whether the `Rect` overlaps another `Rect`
81
- pub fn overlaps ( & self , other : & Rect ) -> bool {
84
+ /// Checks whether the `Rect` overlaps another `Rect`.
85
+ pub const fn overlaps ( & self , other : & Rect ) -> bool {
82
86
self . left ( ) <= other. right ( )
83
87
&& self . right ( ) >= other. left ( )
84
88
&& self . top ( ) <= other. bottom ( )
85
89
&& self . bottom ( ) >= other. top ( )
86
90
}
87
91
88
92
/// Returns a new `Rect` that includes all points of these two `Rect`s.
89
- pub fn combine_with ( self , other : Rect ) -> Rect {
93
+ pub const fn combine_with ( self , other : Rect ) -> Rect {
90
94
let x = f32:: min ( self . x , other. x ) ;
91
95
let y = f32:: min ( self . y , other. y ) ;
92
96
let w = f32:: max ( self . right ( ) , other. right ( ) ) - x;
93
97
let h = f32:: max ( self . bottom ( ) , other. bottom ( ) ) - y;
94
98
Rect { x, y, w, h }
95
99
}
96
100
97
- /// Returns an intersection rect there is any intersection
98
- pub fn intersect ( & self , other : Rect ) -> Option < Rect > {
101
+ /// Returns an intersection rect there is any intersection.
102
+ pub const fn intersect ( & self , other : Rect ) -> Option < Rect > {
99
103
let left = self . x . max ( other. x ) ;
100
104
let top = self . y . max ( other. y ) ;
101
105
let right = self . right ( ) . min ( other. right ( ) ) ;
@@ -113,8 +117,8 @@ impl Rect {
113
117
} )
114
118
}
115
119
116
- /// Translate rect origin be `offset` vector
117
- pub fn offset ( self , offset : Vec2 ) -> Rect {
120
+ /// Translate rect origin be `offset` vector.
121
+ pub const fn offset ( self , offset : Vec2 ) -> Rect {
118
122
Rect :: new ( self . x + offset. x , self . y + offset. y , self . w , self . h )
119
123
}
120
124
}
@@ -137,3 +141,13 @@ impl RectOffset {
137
141
}
138
142
}
139
143
}
144
+
145
+ #[ test]
146
+ fn rect_contains_border ( ) {
147
+ let rect = Rect :: new ( 1.0 , 1.0 , 2.0 , 2.0 ) ;
148
+ assert ! ( rect. contains( vec2( 1.0 , 1.0 ) ) ) ;
149
+ assert ! ( rect. contains( vec2( 3.0 , 1.0 ) ) ) ;
150
+ assert ! ( rect. contains( vec2( 1.0 , 3.0 ) ) ) ;
151
+ assert ! ( rect. contains( vec2( 3.0 , 3.0 ) ) ) ;
152
+ assert ! ( rect. contains( vec2( 2.0 , 2.0 ) ) ) ;
153
+ }
0 commit comments