Skip to content

Commit b592a72

Browse files
authored
Change Ellipse representation and improve helpers (#11435)
# Objective Currently, the `Ellipse` primitive is represented by a `half_width` and `half_height`. To improve consistency (similarly to #11434), it might make more sense to use a `Vec2` `half_size` instead. Alternatively, to make the elliptical nature clearer, the properties could also be called `radius_x` and `radius_y`. Secondly, `Ellipse::new` currently takes a *full* width and height instead of two radii. I would expect it to take the half-width and half-height because ellipses and circles are almost always defined using radii. I wouldn't expect `Circle::new` to take a diameter (if we had that method). ## Solution Change `Ellipse` to store a `half_size` and `new` to take the half-width and half-height. I also added a `from_size` method similar to `Rectangle::from_size`, and added the `semi_minor` and `semi_major` helpers to get the semi-minor/major radius.
1 parent 6337fb3 commit b592a72

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

crates/bevy_math/src/bounding/bounded2d/primitive_impls.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl Bounded2d for Ellipse {
3333
// ### ###
3434
// ###########
3535

36-
let (hw, hh) = (self.half_width, self.half_height);
36+
let (hw, hh) = (self.half_size.x, self.half_size.y);
3737

3838
// Sine and cosine of rotation angle alpha.
3939
let (alpha_sin, alpha_cos) = rotation.sin_cos();
@@ -56,7 +56,7 @@ impl Bounded2d for Ellipse {
5656
}
5757

5858
fn bounding_circle(&self, translation: Vec2, _rotation: f32) -> BoundingCircle {
59-
BoundingCircle::new(translation, self.half_width.max(self.half_height))
59+
BoundingCircle::new(translation, self.semi_major())
6060
}
6161
}
6262

@@ -276,10 +276,7 @@ mod tests {
276276

277277
#[test]
278278
fn ellipse() {
279-
let ellipse = Ellipse {
280-
half_width: 1.0,
281-
half_height: 0.5,
282-
};
279+
let ellipse = Ellipse::new(1.0, 0.5);
283280
let translation = Vec2::new(2.0, 1.0);
284281

285282
let aabb = ellipse.aabb_2d(translation, 0.0);

crates/bevy_math/src/primitives/dim2.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,45 @@ impl Primitive2d for Circle {}
8585
/// An ellipse primitive
8686
#[derive(Clone, Copy, Debug)]
8787
pub struct Ellipse {
88-
/// The half "width" of the ellipse
89-
pub half_width: f32,
90-
/// The half "height" of the ellipse
91-
pub half_height: f32,
88+
/// Half of the width and height of the ellipse.
89+
///
90+
/// This corresponds to the two perpendicular radii defining the ellipse.
91+
pub half_size: Vec2,
9292
}
9393
impl Primitive2d for Ellipse {}
9494

9595
impl Ellipse {
96-
/// Create a new `Ellipse` from a "width" and a "height"
97-
pub fn new(width: f32, height: f32) -> Self {
96+
/// Create a new `Ellipse` from half of its width and height.
97+
///
98+
/// This corresponds to the two perpendicular radii defining the ellipse.
99+
#[inline]
100+
pub const fn new(half_width: f32, half_height: f32) -> Self {
101+
Self {
102+
half_size: Vec2::new(half_width, half_height),
103+
}
104+
}
105+
106+
/// Create a new `Ellipse` from a given full size.
107+
///
108+
/// `size.x` is the diameter along the X axis, and `size.y` is the diameter along the Y axis.
109+
#[inline]
110+
pub fn from_size(size: Vec2) -> Self {
98111
Self {
99-
half_width: width / 2.0,
100-
half_height: height / 2.0,
112+
half_size: size / 2.0,
101113
}
102114
}
115+
116+
/// Returns the length of the semi-major axis. This corresponds to the longest radius of the ellipse.
117+
#[inline]
118+
pub fn semi_major(self) -> f32 {
119+
self.half_size.max_element()
120+
}
121+
122+
/// Returns the length of the semi-minor axis. This corresponds to the shortest radius of the ellipse.
123+
#[inline]
124+
pub fn semi_minor(self) -> f32 {
125+
self.half_size.min_element()
126+
}
103127
}
104128

105129
/// An unbounded plane in 2D space. It forms a separating surface through the origin,

0 commit comments

Comments
 (0)