Skip to main content

Angle

Struct Angle 

Source
pub struct Angle { /* private fields */ }
Expand description

A struct for working with angles in different units (radians, degrees, or rotations).

Angles are stored internally as radians.

let angle_deg = Angle::from_degrees(90.0);
let angle_rad = Angle::from_rad(std::f32::consts::PI / 2.0);
let angle_rot = Angle::from_rotations(0.25);
assert_eq!(angle_deg, angle_rad);
assert_eq!(angle_deg, angle_rot);

Implementations§

Source§

impl Angle

Source

pub fn zero() -> Self

Creates a zero angle.

Source

pub fn from_rad(rad: f32) -> Self

Creates a new angle from radians.

Source

pub fn from_degrees(degree: f32) -> Self

Creates a new angle from degrees.

Source

pub fn from_rotations(rotations: f32) -> Self

Creates a new angle from number of rotations.

let angle = Angle::from_rotations(0.25);
assert_eq!(angle.to_degree(), 90.0);
Source

pub fn rand() -> Self

Creates a random angle between 0 and 2π (0° and 360°).

Source

pub fn quarter_rotation() -> Self

Returns an angle representing a quarter rotation (90°).

Source

pub fn half_rotation() -> Self

Returns an angle representing a half rotation (180°).

Source

pub fn full_rotation() -> Self

Returns an angle representing a full rotation (360°).

Source

pub fn right_cc() -> Self

Returns an angle representing a direction to the right. Equivalent to Angle::zero(). see also crate::V2::right()

Source

pub fn right_cw() -> Self

Returns an angle representing a direction to the right. Equivalent to Angle::zero(). see also crate::V2::right()

Source

pub fn up_cc() -> Self

Returns an angle representing a direction upwards, counter clockwise (left / positive) from Angle::zero(). see also crate::V2::up()

Source

pub fn up_cw() -> Self

Returns an angle representing a direction upwards, clockwise (right / negative) from Angle::zero(). see also crate::V2::up()

Source

pub fn left_cc() -> Self

Returns an angle representing a direction to the left, counter clockwise (left / positive) from Angle::zero(). see also crate::V2::left()

Source

pub fn left_cw() -> Self

Returns an angle representing a direction to the left, clockwise (right / negative) from Angle::zero(). see also crate::V2::left()

Source

pub fn down_cc() -> Self

Returns an angle representing a direction downwards, counter clockwise (left / positive) from Angle::zero(). see also crate::V2::down()

Source

pub fn down_cw() -> Self

Returns an angle representing a direction downwards, clockwise (right / negative) from Angle::zero(). see also crate::V2::down()

Source

pub fn golden_ratio() -> Self

Creates an angle with the golden ratio number of rotations. See GR.

Source

pub fn golden_ratio_inverse() -> Self

Creates an angle with 1.0 / golden ratio number of rotations. See GR.

Source

pub fn root_two() -> Self

Creates an angle with √2 number of rotations.

Source

pub fn root_two_inverse() -> Self

Creates an angle with 1.0 / √2 number of rotations.

Source

pub fn to_rad(&self) -> f32

Gets the angle in radians.

Source

pub fn to_degree(&self) -> f32

Gets the angle in degrees.

Source

pub fn to_rotations(&self) -> f32

Gets the angle as a number of rotations.

Source

pub fn rad_sin_cos(&self) -> (f32, f32)

Gets both sine and cosine of this angle in radians: (self.to_rad().sin(), self.to_rad().cos()).

Source

pub fn mod_one_rotation(&self) -> Self

Returns a new angle modulo 2π (360°), resulting in a positive angle between 0 and 2π.

Source

pub fn modulo(&self, other: Angle) -> Self

Returns a new angle modulo other, resulting in a positive angle between Angle::zero and other.

Source

pub fn positive(&self) -> Self

Returns a new angle with the same direction but positive value.

let angle = Angle::from_degrees(-90.0);
assert_eq!(angle.positive().to_degree(), 270.0);
Source

pub fn abs(&self) -> Self

Returns the absolute value of the angle.

Source

pub fn flip_sign(&self) -> Self

Returns a new angle with flipped sign: self * -1.0.

Source

pub fn normal_right(&self) -> Self

Returns a new angle orthogonal to this one (to the right). Equivalent to self + Angle::quarter_rotation().

Source

pub fn lerp(&self, end: Angle, t: f32) -> Angle

Interpolates between self and other with t as the interpolation factor.

A value of t = 0.0 returns self, a value of t = 1.0 returns other. Values between 0.0 and 1.0 return points between self and other.

§Example
let a1 = Angle::zero();
let a2 = Angle::from_degrees(180.0);
assert_eq!(a1.lerp(a2, 0.5), Angle::from_degrees(90.0));
Source

pub fn lerp_iter_fixed(&self, end: Angle, steps: usize) -> AngleInterpolator

Returns an iterator to interpolate from self to end in steps number of steps.

The iterator will return steps + 1 angles, including both self and end.

§Example
let a1 = Angle::zero();
let a2 = Angle::from_degrees(90.0);
for angle in a1.lerp_iter_fixed(a2, 9) {
    println!("{:?}", angle);
}
Source

pub fn lerp_iter( &self, end: Angle, sample_settings: SampleSettings, radius: f32, ) -> AngleInterpolator

Returns an iterator to interpolate from self to end.

The number of steps is determined by sample_settings.points_per_unit and the given radius. The arc length of the arc with radius radius is used as the distance between self and end.

§Example
let a1 = Angle::zero();
let a2 = Angle::from_degrees(90.0);
for angle in a1.lerp_iter(a2, SampleSettings::new(10.0), 1.0) {
    println!("{:?}", angle);
}
Source

pub fn with_smallest_rotation_to(&self, other: Angle) -> Angle

Returns a new angle that represents the smallest rotation to other.

Source

pub fn dist_mod_one_rotation(&self, other: Angle) -> Angle

Returns the smallest angular difference between two angles, accounting for wrapping around a full rotation.

Source

pub fn min(&self, other: Angle) -> Angle

Returns the smaller of self and other.

Source

pub fn max(&self, other: Angle) -> Angle

Returns the larger of self and other.

Trait Implementations§

Source§

impl Add for Angle

Source§

type Output = Angle

The resulting type after applying the + operator.
Source§

fn add(self, _rhs: Angle) -> Angle

Performs the + operation. Read more
Source§

impl Add<&Angle> for Angle

Source§

type Output = Angle

The resulting type after applying the + operator.
Source§

fn add(self, _rhs: &Angle) -> Angle

Performs the + operation. Read more
Source§

impl Add<&Angle> for &Angle

Source§

type Output = Angle

The resulting type after applying the + operator.
Source§

fn add(self, _rhs: &Angle) -> Angle

Performs the + operation. Read more
Source§

impl Add<Angle> for &Angle

Source§

type Output = Angle

The resulting type after applying the + operator.
Source§

fn add(self, _rhs: Angle) -> Angle

Performs the + operation. Read more
Source§

impl AddAssign for Angle

Source§

fn add_assign(&mut self, rhs: Angle)

Performs the += operation. Read more
Source§

impl AddAssign<&Angle> for Angle

Source§

fn add_assign(&mut self, rhs: &Angle)

Performs the += operation. Read more
Source§

impl Clone for Angle

Source§

fn clone(&self) -> Angle

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 Angle

Source§

impl Debug for Angle

Source§

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

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

impl<'de> Deserialize<'de> for Angle

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Div<f32> for Angle

Source§

type Output = Angle

The resulting type after applying the / operator.
Source§

fn div(self, _rhs: f32) -> Angle

Performs the / operation. Read more
Source§

impl Div<f32> for &Angle

Source§

type Output = Angle

The resulting type after applying the / operator.
Source§

fn div(self, _rhs: f32) -> Angle

Performs the / operation. Read more
Source§

impl DivAssign<f32> for Angle

Source§

fn div_assign(&mut self, rhs: f32)

Performs the /= operation. Read more
Source§

impl From<Angle> for f32

Source§

fn from(angle: Angle) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for Angle

Source§

fn from(rad: f32) -> Self

Converts to this type from the input type.
Source§

impl Mul<f32> for Angle

Source§

type Output = Angle

The resulting type after applying the * operator.
Source§

fn mul(self, _rhs: f32) -> Angle

Performs the * operation. Read more
Source§

impl Mul<f32> for &Angle

Source§

type Output = Angle

The resulting type after applying the * operator.
Source§

fn mul(self, _rhs: f32) -> Angle

Performs the * operation. Read more
Source§

impl MulAssign<f32> for Angle

Source§

fn mul_assign(&mut self, rhs: f32)

Performs the *= operation. Read more
Source§

impl PartialEq for Angle

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 PartialEq<&Angle> for Angle

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 PartialEq<Angle> for &Angle

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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 PartialOrd for Angle

Source§

fn partial_cmp(&self, other: &Angle) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Angle

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Sub for Angle

Source§

type Output = Angle

The resulting type after applying the - operator.
Source§

fn sub(self, _rhs: Angle) -> Angle

Performs the - operation. Read more
Source§

impl Sub<&Angle> for Angle

Source§

type Output = Angle

The resulting type after applying the - operator.
Source§

fn sub(self, _rhs: &Angle) -> Angle

Performs the - operation. Read more
Source§

impl Sub<&Angle> for &Angle

Source§

type Output = Angle

The resulting type after applying the - operator.
Source§

fn sub(self, _rhs: &Angle) -> Angle

Performs the - operation. Read more
Source§

impl Sub<Angle> for &Angle

Source§

type Output = Angle

The resulting type after applying the - operator.
Source§

fn sub(self, _rhs: Angle) -> Angle

Performs the - operation. Read more
Source§

impl SubAssign for Angle

Source§

fn sub_assign(&mut self, rhs: Angle)

Performs the -= operation. Read more
Source§

impl SubAssign<&Angle> for Angle

Source§

fn sub_assign(&mut self, rhs: &Angle)

Performs the -= operation. Read more

Auto Trait Implementations§

§

impl Freeze for Angle

§

impl RefUnwindSafe for Angle

§

impl Send for Angle

§

impl Sync for Angle

§

impl Unpin for Angle

§

impl UnsafeUnpin 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<G1, G2> Within<G2> for G1
where G2: Contains<G1>,

Source§

fn is_within(&self, b: &G2) -> bool