Skip to content

Commit f778c1f

Browse files
cyrganinot-fl3
authored andcommitted
make several Circle methods const and add a few doc comments
1 parent 1b109f5 commit f778c1f

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/math/circle.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,44 @@ pub struct Circle {
88
}
99

1010
impl Circle {
11+
/// Constructs a new `Circle` with the center `(x, y)` and radius `r`.
1112
pub const fn new(x: f32, y: f32, r: f32) -> Self {
1213
Circle { x, y, r }
1314
}
1415

16+
/// Returns the center point of the `Circle`.
1517
pub const fn point(&self) -> Vec2 {
1618
vec2(self.x, self.y)
1719
}
1820

21+
/// Returns the radius of the `Circle`.
1922
pub const fn radius(&self) -> f32 {
2023
self.r
2124
}
2225

23-
/// Moves the `Circle`'s origin to (x, y)
24-
pub fn move_to(&mut self, destination: Vec2) {
26+
/// Moves the `Circle`'s origin to (x, y).
27+
pub const fn move_to(&mut self, destination: Vec2) {
2528
self.x = destination.x;
2629
self.y = destination.y;
2730
}
2831

29-
/// Scales the `Circle` by a factor of sr
30-
pub fn scale(&mut self, sr: f32) {
32+
/// Scales the `Circle` by a factor of `sr`.
33+
pub const fn scale(&mut self, sr: f32) {
3134
self.r *= sr;
3235
}
3336

34-
/// Checks whether the `Circle` contains a `Point`
37+
/// Checks whether the `Circle` contains a `Point`.
3538
pub fn contains(&self, pos: &Vec2) -> bool {
3639
pos.distance(vec2(self.x, self.y)) < self.r
3740
}
3841

39-
/// Checks whether the `Circle` overlaps a `Circle`
42+
/// Checks whether the `Circle` overlaps a `Circle`.
4043
pub fn overlaps(&self, other: &Circle) -> bool {
4144
self.point().distance(other.point()) < self.r + other.r
4245
}
4346

44-
/// Checks whether the `Circle` overlaps a `Rect`
45-
pub fn overlaps_rect(&self, rect: &Rect) -> bool {
47+
/// Checks whether the `Circle` overlaps a `Rect`.
48+
pub const fn overlaps_rect(&self, rect: &Rect) -> bool {
4649
let dist_x = (self.x - rect.center().x).abs();
4750
let dist_y = (self.y - rect.center().y).abs();
4851
if dist_x > rect.w / 2.0 + self.r || dist_y > rect.h / 2.0 + self.r {
@@ -57,8 +60,8 @@ impl Circle {
5760
dist_sq <= self.r * self.r
5861
}
5962

60-
/// Translate `Circle` origin by `offset` vector
61-
pub fn offset(self, offset: Vec2) -> Circle {
63+
/// Translate `Circle` origin by `offset` vector.
64+
pub const fn offset(self, offset: Vec2) -> Circle {
6265
Circle::new(self.x + offset.x, self.y + offset.y, self.r)
6366
}
6467
}

src/math/rect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl Rect {
9898
Rect { x, y, w, h }
9999
}
100100

101-
/// Returns an intersection rect there is any intersection.
101+
/// Returns an intersection rect if there is any intersection.
102102
pub const fn intersect(&self, other: Rect) -> Option<Rect> {
103103
let left = self.x.max(other.x);
104104
let top = self.y.max(other.y);
@@ -117,7 +117,7 @@ impl Rect {
117117
})
118118
}
119119

120-
/// Translate rect origin be `offset` vector.
120+
/// Translate rect origin by `offset` vector.
121121
pub const fn offset(self, offset: Vec2) -> Rect {
122122
Rect::new(self.x + offset.x, self.y + offset.y, self.w, self.h)
123123
}

0 commit comments

Comments
 (0)