Skip to content

Commit 960d948

Browse files
cyrganinot-fl3
authored andcommitted
make all Rect methods const, fix a bug in Rect::contains regarding points on the border
1 parent 7ca3b0d commit 960d948

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

src/math/rect.rs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ use glam::*;
33
/// A 2D rectangle, defined by its top-left corner, width and height.
44
#[derive(Clone, Copy, Debug, Default, PartialEq)]
55
pub struct Rect {
6+
/// The x-coordinate of the top-left corner.
67
pub x: f32,
8+
/// The y-coordinate of the top-left corner.
79
pub y: f32,
10+
/// The width of the `Rect`, going to the right.
811
pub w: f32,
12+
/// The height of the `Rect`, going down.
913
pub h: f32,
1014
}
1115

@@ -32,70 +36,70 @@ impl Rect {
3236
}
3337

3438
/// Returns the center position of the `Rect`.
35-
pub fn center(&self) -> Vec2 {
39+
pub const fn center(&self) -> Vec2 {
3640
vec2(self.x + self.w * 0.5f32, self.y + self.h * 0.5f32)
3741
}
3842

39-
/// Returns the left edge of the `Rect`
43+
/// Returns the left edge of the `Rect`.
4044
pub const fn left(&self) -> f32 {
4145
self.x
4246
}
4347

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 {
4650
self.x + self.w
4751
}
4852

49-
/// Returns the top edge of the `Rect`
53+
/// Returns the top edge of the `Rect`.
5054
pub const fn top(&self) -> f32 {
5155
self.y
5256
}
5357

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 {
5660
self.y + self.h
5761
}
5862

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) {
6165
self.x = destination.x;
6266
self.y = destination.y;
6367
}
6468

6569
/// 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) {
6872
self.w *= sx;
6973
self.h *= sy;
7074
}
7175

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 {
7478
point.x >= self.left()
75-
&& point.x < self.right()
76-
&& point.y < self.bottom()
79+
&& point.x <= self.right()
80+
&& point.y <= self.bottom()
7781
&& point.y >= self.top()
7882
}
7983

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 {
8286
self.left() <= other.right()
8387
&& self.right() >= other.left()
8488
&& self.top() <= other.bottom()
8589
&& self.bottom() >= other.top()
8690
}
8791

8892
/// 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 {
9094
let x = f32::min(self.x, other.x);
9195
let y = f32::min(self.y, other.y);
9296
let w = f32::max(self.right(), other.right()) - x;
9397
let h = f32::max(self.bottom(), other.bottom()) - y;
9498
Rect { x, y, w, h }
9599
}
96100

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> {
99103
let left = self.x.max(other.x);
100104
let top = self.y.max(other.y);
101105
let right = self.right().min(other.right());
@@ -113,8 +117,8 @@ impl Rect {
113117
})
114118
}
115119

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 {
118122
Rect::new(self.x + offset.x, self.y + offset.y, self.w, self.h)
119123
}
120124
}
@@ -137,3 +141,13 @@ impl RectOffset {
137141
}
138142
}
139143
}
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

Comments
 (0)