Skip to content

Commit dd4d07d

Browse files
authored
Add new constructors for Circle and Sphere (#11526)
# Objective Make APIs more consistent and ergonomic by adding a `new` constructor for `Circle` and `Sphere`. This could be seen as a redundant "trivial constructor", but in practise, it seems valuable to me. I have lots of cases where formatting becomes ugly because of the lack of a constructor, like this: ```rust Circle { radius: self.radius(), } .contains_local_point(centered_pt) ``` With `new`, it'd be formatted much nicer: ```rust Circle::new(self.radius()).contains_local_point(centered_pt) ``` Of course, this is just one example, but my circle/sphere definitions very frequently span three or more lines when they could fit on one. Adding `new` also increases consistency. `Ellipse` has `new` already, and so does the mesh version of `Circle`. ## Solution Add a `new` constructor for `Circle` and `Sphere`.
1 parent 10f9595 commit dd4d07d

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

crates/bevy_math/src/primitives/dim2.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ pub struct Circle {
8888
impl Primitive2d for Circle {}
8989

9090
impl Circle {
91+
/// Create a new [`Circle`] from a `radius`
92+
#[inline(always)]
93+
pub const fn new(radius: f32) -> Self {
94+
Self { radius }
95+
}
96+
9197
/// Finds the point on the circle that is closest to the given `point`.
9298
///
9399
/// If the point is outside the circle, the returned point will be on the perimeter of the circle.

crates/bevy_math/src/primitives/dim3.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ pub struct Sphere {
9292
impl Primitive3d for Sphere {}
9393

9494
impl Sphere {
95+
/// Create a new [`Sphere`] from a `radius`
96+
#[inline(always)]
97+
pub const fn new(radius: f32) -> Self {
98+
Self { radius }
99+
}
100+
95101
/// Finds the point on the sphere that is closest to the given `point`.
96102
///
97103
/// If the point is outside the sphere, the returned point will be on the surface of the sphere.

0 commit comments

Comments
 (0)