Struct Angle

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

Implements an angle structure where angles are stored as integers.

Also keeps track of if the angle is going clockwise or counter-clockwise. It also stores a full circle different from a 0 degree arc. This gets rid of precision issues when storing angles as pi can be represented exactly if you change the units.

Implementations§

Source§

impl Angle

Source

pub fn is_zero(&self) -> bool

Is this angle zero?

use integer_angles::Angle;

assert!(Angle::zero().is_zero());
assert!(!Angle::pi().is_zero());
Source

pub fn pi_over(n: i64) -> Angle

Divide pi by some number, and return the result

use integer_angles::Angle;

assert_eq!(Angle::pi_over(2) * 2, Angle::pi());
Source

pub fn set_clockwise(&self, clockwise: bool) -> Angle

Don’t change the direction of the angle, but possibly change the direction to get to that angle.

use integer_angles::Angle;

assert_eq!(Angle::pi_over(2).set_clockwise(true), -3 * Angle::pi_over(2));
Source

pub fn add_or_subtract(&self, add: bool, other: Angle) -> Angle

Add or subtract an angle depending on the value of the add boolean.

use integer_angles::Angle;

assert_eq!(Angle::pi() + Angle::pi(), Angle::pi().add_or_subtract(true, Angle::pi()));
assert_eq!(Angle::pi() - Angle::pi(), Angle::pi().add_or_subtract(false, Angle::pi()));
Source

pub fn zero() -> Angle

Create a 0-degree angle

use integer_angles::Angle;

assert_eq!(Angle::zero() * 2, Angle::zero());
Source

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

Determine if the angles are equal while allowing wrapping (meaning, 0 == 2pi).

use integer_angles::Angle;

assert!(Angle::zero().wrapping_eq(Angle::two_pi()));
assert!(Angle::pi().wrapping_eq(-Angle::pi()));
Source

pub fn wrapping_approx_eq(&self, other: Angle, error: Angle) -> bool

Source

pub fn length<T: RealField>(&self, radius: T) -> T

Get the length of this as an arc given a specific radius

use integer_angles::Angle;
use nalgebra::RealField;

assert_eq!(Angle::zero().length(1.0f64), 0.0f64);
assert_eq!(Angle::two_pi().length(1.0f64), f64::two_pi());
assert_eq!(Angle::pi_2().length(1.0f64), f64::frac_pi_2());
Source

pub fn pi_2() -> Angle

Create an angle of pi over two counter-clockwise (for convenience).

use integer_angles::Angle;

assert_eq!(Angle::pi_2(), Angle::pi_over(2));
assert_eq!(Angle::pi_2() * 2, Angle::pi());
Source

pub fn pi() -> Angle

Create an angle of pi counter-clockwise.

use integer_angles::Angle;

assert_eq!(Angle::pi() / 2, Angle::pi_2());
Source

pub fn two_pi() -> Angle

Create an angle that is two_pi radians counter-clockwise.

use integer_angles::Angle;

assert_eq!(Angle::two_pi() * 2, Angle::pi() * 2);
Source

pub fn units(&self) -> Option<u64>

Get the number of units in this angle. This will be some number between 0 and 2^64 - 1 where a full circle is 2^64 units. If this represents a full cirle, None will be returned.

use integer_angles::Angle;

assert_eq!(Angle::zero().units(), Some(0));
assert_eq!(Angle::two_pi().units(), None);
Source

pub fn clockwise(&self) -> bool

Is this angle clockwise or counter-clockwise?

use integer_angles::Angle;

assert_eq!(Angle::pi().clockwise(), false);
assert_eq!((-Angle::pi()).clockwise(), true);
Source

pub fn to_i64(&self) -> i64

Returns the angle as an i64 (for doing math), and wrap 2pi to 0. If clockwise, return the negative of the result.

use integer_angles::Angle;

assert_eq!(Angle::pi().to_i64(), i64::min_value());
assert_eq!(Angle::two_pi().to_i64(), 0);
assert_eq!(-Angle::pi_2().to_i64(), i64::min_value() / 2);
assert_eq!(Angle::pi_2().to_i64(), -(i64::min_value() / 2));
Source

pub fn cos<T: RealField>(&self) -> T

Returns the cosine of the angle.

use integer_angles::Angle;

assert_eq!(Angle::zero().cos::<f64>(), 1.0f64);
assert_eq!(Angle::pi_2().cos::<f64>(), 0.0f64);
assert_eq!(Angle::pi().cos::<f64>(), -1.0f64);
assert_eq!((Angle::pi_2() * 3).cos::<f64>(), 0.0f64);
assert_eq!(Angle::two_pi().cos::<f64>(), 1.0f64);
Source

pub fn sin<T: RealField>(&self) -> T

Returns the cosine of the angle.

use integer_angles::Angle;

assert_eq!(Angle::zero().sin::<f64>(), 0.0f64);
assert_eq!(Angle::pi_2().sin::<f64>(), 1.0f64);
assert_eq!(Angle::pi().sin::<f64>(), 0.0f64);
assert_eq!((3 * Angle::pi_2()).sin::<f64>(), -1.0f64);
assert_eq!(Angle::two_pi().sin::<f64>(), 0.0f64);
Source

pub fn tan<T: RealField>(&self) -> T

Returns the tangent of the angle.

use integer_angles::Angle;

assert_eq!(Angle::pi_over(4).tan::<f64>(), 1.0f64);
Source

pub fn acos<T: RealField>(x: T) -> Angle

Compute the arccos of the number.

use integer_angles::Angle;

assert_eq!(Angle::acos(0.0f64), Angle::pi_over(2));
Source

pub fn asin<T: RealField>(x: T) -> Angle

Compute the arcsin of the number.

use integer_angles::Angle;

assert_eq!(Angle::asin(0.5f64).sin::<f64>(), 0.5f64);
Source

pub fn atan<T: RealField>(x: T) -> Angle

Compute the arctan of the number.

use integer_angles::Angle;

assert_eq!(Angle::atan(1.0f64), Angle::pi_over(4));
Source

pub fn atan2<T: RealField>(y: T, x: T) -> Option<Angle>

Compute the atan(y / x) but keeping the sign of y and x.

Returns None if both y and x are 0.

use integer_angles::Angle;

assert_eq!(Angle::atan2(1.0f64, 0.0).unwrap(), Angle::pi_2());
assert_eq!(Angle::atan2(0.0, 0.0f64), None);
Source

pub fn radians<T: RealField>(self) -> T

Convert the angle into radians. This can lose precision pretty easily

use integer_angles::Angle;
use nalgebra::RealField;

assert_eq!(Angle::pi().radians::<f64>(), f64::pi());
Source

pub fn degrees<T: RealField>(self) -> T

Trait Implementations§

Source§

impl Add for Angle

Source§

type Output = Angle

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl AddAssign for Angle

Source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
Source§

impl Arbitrary for Angle

Source§

fn arbitrary<G: Gen>(g: &mut G) -> Self

Source§

fn shrink(&self) -> Box<dyn Iterator<Item = Self>>

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 Default for Angle

Source§

fn default() -> Angle

Returns the “default value” for a type. 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<i64> for Angle

Source§

type Output = Angle

The resulting type after applying the / operator.
Source§

fn div(self, rhs: i64) -> Angle

Performs the / operation. Read more
Source§

impl DivAssign<i64> for Angle

Source§

fn div_assign(&mut self, rhs: i64)

Performs the /= operation. Read more
Source§

impl<T: RealField> From<T> for Angle

Source§

fn from(real: T) -> Angle

Convert from radians into an angle. Panics if NaN or infinity is received

use integer_angles::Angle;
use nalgebra::RealField;

assert_eq!(Angle::from(f64::pi()), Angle::pi());
Source§

impl Mul<Angle> for i64

Source§

type Output = Angle

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Angle) -> Angle

Performs the * operation. Read more
Source§

impl Mul<i64> for Angle

Source§

type Output = Angle

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: i64) -> Self

Performs the * operation. Read more
Source§

impl MulAssign<i64> for Angle

Source§

fn mul_assign(&mut self, rhs: i64)

Performs the *= operation. Read more
Source§

impl Neg for Angle

Source§

type Output = Angle

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Ord for Angle

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
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 PartialOrd for Angle

Source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · 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 · 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 · 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 · 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, other: Self) -> Self

Performs the - operation. Read more
Source§

impl SubAssign for Angle

Source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more
Source§

impl Copy for Angle

Source§

impl Eq 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Scalar for T
where T: Copy + PartialEq + Debug + Any,

Source§

fn inlined_clone(&self) -> T

Performance hack: Clone doesn’t get inlined for Copy types in debug mode, so make it inline anyway.
Source§

fn is<T>() -> bool
where T: Scalar,

Tests if Self the same as the type T Read more
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

unsafe fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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<T, Right> ClosedAdd<Right> for T
where T: Add<Right, Output = T> + AddAssign<Right>,

Source§

impl<T, Right> ClosedDiv<Right> for T
where T: Div<Right, Output = T> + DivAssign<Right>,

Source§

impl<T, Right> ClosedMul<Right> for T
where T: Mul<Right, Output = T> + MulAssign<Right>,

Source§

impl<T> ClosedNeg for T
where T: Neg<Output = T>,

Source§

impl<T, Right> ClosedSub<Right> for T
where T: Sub<Right, Output = T> + SubAssign<Right>,

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,