Enum Angle

Source
pub enum Angle {
    Degree(f64),
    Radian(f64),
}
Expand description

Represents an angle measurement that can be expressed in either degrees or radians.

The Angle type provides a safe and convenient way to work with angles, ensuring that conversions between degrees and radians are handled correctly. It maintains the internal representation in either degrees or radians as originally specified, performing conversions only when needed.

§Examples

use rusticle::Angle;
 
// Create an angle in degrees
let deg_angle = Angle::from_degrees(90.0);
 
// Create an angle in radians
let rad_angle = Angle::from_radians(std::f64::consts::PI / 2.0);
 
// Both angles represent the same value
assert!((deg_angle.to_radians() - rad_angle.to_radians()).abs() < 1e-10);

Variants§

§

Degree(f64)

Represents an angle measured in degrees (0-360).

While any float value is accepted, you can normalize the angle to the range [0, 360) using the normalize() method.

§

Radian(f64)

Represents an angle measured in radians (0-2π).

While any float value is accepted, you can normalize the angle to the equivalent of [0, 360) degrees using the normalize() method.

Implementations§

Source§

impl Angle

Source

pub fn from_degrees(degrees: f64) -> Self

Creates a new angle from a value in degrees.

This method stores the angle internally as degrees. The value is stored as-is without normalization. Use normalize() if you need the angle in the range [0, 360).

§Examples
use rusticle::Angle;
 
let angle = Angle::from_degrees(90.0);
assert_eq!(angle.to_degrees(), 90.0);
 
// Values outside 0-360 are allowed
let large_angle = Angle::from_degrees(720.0); // Two full rotations
assert_eq!(large_angle.normalize().to_degrees(), 0.0);
Source

pub fn from_radians(radians: f64) -> Self

Creates a new angle from a value in radians.

This method stores the angle internally as radians. The value is stored as-is without normalization. Use normalize() if you need the angle in the standard range.

§Examples
use rusticle::Angle;
use std::f64::consts::PI;
 
let angle = Angle::from_radians(PI / 2.0);
assert_eq!(angle.to_degrees(), 90.0);
 
// Values outside 0-2π are allowed
let large_angle = Angle::from_radians(4.0 * PI); // Two full rotations
assert_eq!(large_angle.normalize().to_degrees(), 0.0);
Source

pub fn to_degrees(&self) -> f64

Converts the angle to degrees, regardless of its internal representation.

This method performs the conversion from radians to degrees if necessary. The returned value is not normalized and may be outside the range [0, 360).

§Examples
use rusticle::Angle;
use std::f64::consts::PI;
 
let deg = Angle::from_degrees(180.0);
assert_eq!(deg.to_degrees(), 180.0);
 
let rad = Angle::from_radians(PI);
assert_eq!(rad.to_degrees(), 180.0);
Source

pub fn to_radians(&self) -> f64

Converts the angle to radians, regardless of its internal representation.

This method performs the conversion from degrees to radians if necessary. The returned value is not normalized and may be outside the range [0, 2π).

§Examples
use rusticle::Angle;
use std::f64::consts::PI;
 
let rad = Angle::from_radians(PI / 2.0);
assert_eq!(rad.to_radians(), PI / 2.0);
 
let deg = Angle::from_degrees(90.0);
assert_eq!(deg.to_radians(), PI / 2.0);
Source

pub fn as_degrees(&self) -> Self

Returns a new angle in degrees, converting if necessary.

Unlike to_degrees() which returns a raw f64 value, this method returns a new Angle instance with the internal representation stored in degrees.

§Examples
use rusticle::Angle;
use std::f64::consts::PI;
 
let rad = Angle::from_radians(PI);
let deg = rad.as_degrees();
 
match deg {
    Angle::Degree(d) => assert_eq!(d, 180.0),
    _ => panic!("Should be in degrees"),
}
Source

pub fn as_radians(&self) -> Self

Returns a new angle in radians, converting if necessary.

Unlike to_radians() which returns a raw f64 value, this method returns a new Angle instance with the internal representation stored in radians.

§Examples
use rusticle::Angle;
use std::f64::consts::PI;
 
let deg = Angle::from_degrees(180.0);
let rad = deg.as_radians();
 
match rad {
    Angle::Radian(r) => assert_eq!(r, PI),
    _ => panic!("Should be in radians"),
}
Source

pub fn normalize(&self) -> Self

Normalizes the angle to be in the range [0, 360) degrees.

This method converts any angle to its equivalent in the range [0, 360) degrees. It handles both positive and negative angles correctly:

  • Positive angles > 360° will be wrapped to their equivalent in [0, 360)
  • Negative angles will be converted to their positive equivalent

The result is always returned as a degree measurement.

§Examples
use rusticle::Angle;
 
// Handling angles > 360°
let large = Angle::from_degrees(400.0);
assert_eq!(large.normalize().to_degrees(), 40.0);
 
// Handling negative angles
let negative = Angle::from_degrees(-45.0);
assert_eq!(negative.normalize().to_degrees(), 315.0);
 
// Handling multiple rotations
let multiple = Angle::from_degrees(720.0 + 45.0); // Two rotations plus 45°
assert_eq!(multiple.normalize().to_degrees(), 45.0);

Trait Implementations§

Source§

impl Clone for Angle

Source§

fn clone(&self) -> Angle

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Angle

Source§

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

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

impl From<f64> for Angle

Implements the conversion from f64 to Angle, interpreting the value as degrees.

This implementation allows for convenient creation of angles from numeric literals, always interpreting them as degree measurements.

§Examples

use rusticle::Angle;
 
let angle: Angle = 90.0.into();
assert_eq!(angle.to_degrees(), 90.0);
Source§

fn from(degrees: f64) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Angle

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Angle

Source§

impl StructuralPartialEq for Angle

Auto Trait Implementations§

§

impl Freeze for Angle

§

impl RefUnwindSafe for Angle

§

impl Send for Angle

§

impl Sync for Angle

§

impl Unpin for Angle

§

impl UnwindSafe for Angle

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.