Skip to main content

GravityBody

Struct GravityBody 

Source
pub struct GravityBody {
    pub mass: f64,
    pub radius: f64,
}
Expand description

Mass and radius for a body used in gravity calculations.

Fields§

§mass: f64§radius: f64

Implementations§

Source§

impl GravityBody

Source

pub fn new(mass: f64, radius: f64) -> Option<Self>

Creates a gravity body from a mass and radius.

Returns None when mass is negative, when radius is less than or equal to zero, or when either input is not finite.

Examples found in repository?
examples/basic_usage.rs (line 4)
3fn main() -> Result<(), &'static str> {
4    let earth = GravityBody::new(5.972e24, 6.371e6).ok_or("invalid Earth body")?;
5    let surface_gravity = earth
6        .surface_gravity()
7        .ok_or("invalid surface gravity calculation")?;
8    let force = gravitational_force(1.0, 1.0, 1.0).ok_or("invalid force calculation")?;
9    let velocity = escape_velocity(5.972e24, 6.371e6).ok_or("invalid escape calculation")?;
10
11    assert!((force - GRAVITATIONAL_CONSTANT).abs() < f64::EPSILON);
12    assert!(surface_gravity > 9.8);
13    assert!(velocity > 11_000.0);
14
15    Ok(())
16}
Source

pub fn surface_gravity(&self) -> Option<f64>

Computes the surface gravity of the body.

§Examples
use use_gravity::GravityBody;

let earth = GravityBody::new(5.972e24, 6.371e6).unwrap();
let gravity = earth.surface_gravity().unwrap();

assert!((gravity - 9.82).abs() < 0.05);
Examples found in repository?
examples/basic_usage.rs (line 6)
3fn main() -> Result<(), &'static str> {
4    let earth = GravityBody::new(5.972e24, 6.371e6).ok_or("invalid Earth body")?;
5    let surface_gravity = earth
6        .surface_gravity()
7        .ok_or("invalid surface gravity calculation")?;
8    let force = gravitational_force(1.0, 1.0, 1.0).ok_or("invalid force calculation")?;
9    let velocity = escape_velocity(5.972e24, 6.371e6).ok_or("invalid escape calculation")?;
10
11    assert!((force - GRAVITATIONAL_CONSTANT).abs() < f64::EPSILON);
12    assert!(surface_gravity > 9.8);
13    assert!(velocity > 11_000.0);
14
15    Ok(())
16}
Source

pub fn escape_velocity(&self) -> Option<f64>

Computes the escape velocity from the body’s surface.

Source

pub fn circular_orbital_velocity_at_radius( &self, orbital_radius: f64, ) -> Option<f64>

Computes the circular orbital velocity at a given radius from the body’s center.

Trait Implementations§

Source§

impl Clone for GravityBody

Source§

fn clone(&self) -> GravityBody

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for GravityBody

Source§

impl Debug for GravityBody

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for GravityBody

Source§

fn eq(&self, other: &GravityBody) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for GravityBody

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.