Skip to content

Commit

Permalink
Simplify mathematical computations
Browse files Browse the repository at this point in the history
Compiler should optimize these
  • Loading branch information
Jondolf committed Nov 19, 2023
1 parent 72d0ab7 commit 2ca7ce8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
10 changes: 5 additions & 5 deletions crates/bevy_math/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Circle {

/// Get the area of the circle
pub fn area(&self) -> f32 {
PI * self.radius * self.radius
PI * self.radius.powi(2)
}

/// Get the perimeter or circumference of the circle
Expand Down Expand Up @@ -116,7 +116,7 @@ impl Segment2d {
pub fn new(direction: Direction2d, length: f32) -> Self {
Self {
direction,
half_length: length / 2.,
half_length: length / 2.0,
}
}

Expand All @@ -128,7 +128,7 @@ impl Segment2d {
let length = diff.length();
(
Self::new(Direction2d::from_normalized(diff / length), length),
(point1 + point2) / 2.,
(point1 + point2) / 2.0,
)
}

Expand Down Expand Up @@ -270,8 +270,8 @@ impl Rectangle {
/// Create a new `Rectangle` from two corner points
pub fn from_corners(point1: Vec2, point2: Vec2) -> Self {
Self {
half_width: 0.5 * (point2.x - point1.x).abs(),
half_height: 0.5 * (point2.y - point1.y).abs(),
half_width: (point2.x - point1.x).abs() / 2.0,
half_height: (point2.y - point1.y).abs() / 2.0,
}
}

Expand Down
23 changes: 10 additions & 13 deletions crates/bevy_math/src/primitives/dim3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ use std::f32::consts::{FRAC_PI_3, PI};
use super::{Circle, Primitive3d};
use crate::Vec3;

#[allow(clippy::excessive_precision)]
const TWO_PI_SQUARED: f32 = 19.73920880217871723766898199;

/// A normalized vector pointing in a direction in 3D space
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Direction3d(Vec3);
Expand Down Expand Up @@ -47,12 +44,12 @@ impl Sphere {

/// Get the surface area of the sphere
pub fn area(&self) -> f32 {
4.0 * PI * self.radius * self.radius
4.0 * PI * self.radius.powi(2)
}

/// Get the volume of the sphere
pub fn volume(&self) -> f32 {
4.0 * FRAC_PI_3 * self.radius * self.radius * self.radius
4.0 * FRAC_PI_3 * self.radius.powi(2) * self.radius
}
}

Expand Down Expand Up @@ -108,7 +105,7 @@ impl Segment3d {
pub fn new(direction: Direction3d, length: f32) -> Self {
Self {
direction,
half_length: length / 2.,
half_length: length / 2.0,
}
}

Expand All @@ -122,7 +119,7 @@ impl Segment3d {
let length = diff.length();
(
Self::new(Direction3d::from_normalized(diff / length), length),
(point1 + point2) / 2.,
(point1 + point2) / 2.0,
)
}

Expand Down Expand Up @@ -209,7 +206,7 @@ impl Cuboid {
/// Create a new `Cuboid` from a given full size
pub fn from_size(size: Vec3) -> Self {
Self {
half_size: size / 2.,
half_size: size / 2.0,
}
}

Expand Down Expand Up @@ -253,7 +250,7 @@ impl Cylinder {
pub fn new(radius: f32, height: f32) -> Self {
Self {
radius,
half_height: height / 2.,
half_height: height / 2.0,
}
}

Expand All @@ -273,7 +270,7 @@ impl Cylinder {

/// Get the surface area of one base of the cylinder
pub fn base_area(&self) -> f32 {
PI * self.radius * self.radius
PI * self.radius.powi(2)
}

/// Get the total surface area of the cylinder
Expand Down Expand Up @@ -365,7 +362,7 @@ impl Cone {

/// Get the surface area of the base of the cone
pub fn base_area(&self) -> f32 {
PI * self.radius * self.radius
PI * self.radius.powi(2)
}

/// Get the total surface area of the cone
Expand Down Expand Up @@ -417,13 +414,13 @@ impl Torus {
/// Get the surface area of the torus. Note that this only produces
/// the expected result when the torus has a ring and isn't self-intersecting
pub fn area(&self) -> f32 {
2.0 * TWO_PI_SQUARED * self.major_radius() * self.ring_radius
4.0 * PI.powi(2) * self.major_radius() * self.ring_radius
}

/// Get the volume of the torus. Note that this only produces
/// the expected result when the torus has a ring and isn't self-intersecting
pub fn volume(&self) -> f32 {
TWO_PI_SQUARED * self.major_radius() * self.ring_radius.powi(2)
2.0 * PI.powi(2) * self.major_radius() * self.ring_radius.powi(2)
}
}

Expand Down

0 comments on commit 2ca7ce8

Please sign in to comment.