Skip to content

Commit 01dce47

Browse files
13ros27Jondolfjames7132alice-i-cecile
authored
Add to_inner_rectangle, area and perimeter methods to Capsule2d (#15388)
# Objective Unlike `Capsule3d` which has the `.to_cylinder` method, `Capsule2d` doesn't have an equivalent `.to_inner_rectangle` method and as shown by #15191 this is surprisingly easy to get wrong ## Solution Implemented a `Capsule2d::to_inner_rectangle` method as it is implemented in the fixed `Capsule2d` shape sampling, and as I was adding tests I noticed `Capsule2d` didn't implement `Measure2d` so I did this as well. ## Changelog ### Added - `Capsule2d::to_inner_rectangle`, `Capsule2d::area` and `Capsule2d::perimeter` --------- Co-authored-by: Joona Aalto <[email protected]> Co-authored-by: James Liu <[email protected]> Co-authored-by: Alice Cecile <[email protected]>
1 parent c5742ff commit 01dce47

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

crates/bevy_math/src/primitives/dim2.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,6 +1817,28 @@ impl Capsule2d {
18171817
half_length: length / 2.0,
18181818
}
18191819
}
1820+
1821+
/// Get the part connecting the semicircular ends of the capsule as a [`Rectangle`]
1822+
#[inline]
1823+
pub fn to_inner_rectangle(&self) -> Rectangle {
1824+
Rectangle::new(self.radius * 2.0, self.half_length * 2.0)
1825+
}
1826+
}
1827+
1828+
impl Measured2d for Capsule2d {
1829+
/// Get the area of the capsule
1830+
#[inline]
1831+
fn area(&self) -> f32 {
1832+
// pi*r^2 + (2r)*l
1833+
PI * self.radius.squared() + self.to_inner_rectangle().area()
1834+
}
1835+
1836+
/// Get the perimeter of the capsule
1837+
#[inline]
1838+
fn perimeter(&self) -> f32 {
1839+
// 2pi*r + 2l
1840+
2.0 * PI * self.radius + 4.0 * self.half_length
1841+
}
18201842
}
18211843

18221844
#[cfg(test)]
@@ -1892,6 +1914,18 @@ mod tests {
18921914
assert_eq!(circle.perimeter(), 18.849556, "incorrect perimeter");
18931915
}
18941916

1917+
#[test]
1918+
fn capsule_math() {
1919+
let capsule = Capsule2d::new(2.0, 9.0);
1920+
assert_eq!(
1921+
capsule.to_inner_rectangle(),
1922+
Rectangle::new(4.0, 9.0),
1923+
"rectangle wasn't created correctly from a capsule"
1924+
);
1925+
assert_eq!(capsule.area(), 48.566371, "incorrect area");
1926+
assert_eq!(capsule.perimeter(), 30.566371, "incorrect perimeter");
1927+
}
1928+
18951929
#[test]
18961930
fn annulus_math() {
18971931
let annulus = Annulus::new(2.5, 3.5);

crates/bevy_math/src/sampling/shape_sampling.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,7 @@ impl ShapeSample for Capsule2d {
450450
if capsule_area > 0.0 {
451451
// Check if the random point should be inside the rectangle
452452
if rng.gen_bool((rectangle_area / capsule_area) as f64) {
453-
let rectangle = Rectangle::new(self.radius * 2.0, self.half_length * 2.0);
454-
rectangle.sample_interior(rng)
453+
self.to_inner_rectangle().sample_interior(rng)
455454
} else {
456455
let circle = Circle::new(self.radius);
457456
let point = circle.sample_interior(rng);

0 commit comments

Comments
 (0)