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
impl Angle
Sourcepub fn is_zero(&self) -> bool
pub fn is_zero(&self) -> bool
Is this angle zero?
use integer_angles::Angle;
assert!(Angle::zero().is_zero());
assert!(!Angle::pi().is_zero());
Sourcepub fn pi_over(n: i64) -> Angle
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());
Sourcepub fn set_clockwise(&self, clockwise: bool) -> Angle
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));
Sourcepub fn add_or_subtract(&self, add: bool, other: Angle) -> Angle
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()));
Sourcepub fn zero() -> Angle
pub fn zero() -> Angle
Create a 0-degree angle
use integer_angles::Angle;
assert_eq!(Angle::zero() * 2, Angle::zero());
Sourcepub fn wrapping_eq(&self, other: Angle) -> bool
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()));
pub fn wrapping_approx_eq(&self, other: Angle, error: Angle) -> bool
Sourcepub fn length<T: RealField>(&self, radius: T) -> T
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());
Sourcepub fn pi_2() -> Angle
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());
Sourcepub fn pi() -> Angle
pub fn pi() -> Angle
Create an angle of pi counter-clockwise.
use integer_angles::Angle;
assert_eq!(Angle::pi() / 2, Angle::pi_2());
Sourcepub fn two_pi() -> Angle
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);
Sourcepub fn units(&self) -> Option<u64>
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);
Sourcepub fn clockwise(&self) -> bool
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);
Sourcepub fn to_i64(&self) -> i64
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));
Sourcepub fn cos<T: RealField>(&self) -> T
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);
Sourcepub fn sin<T: RealField>(&self) -> T
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);
Sourcepub fn tan<T: RealField>(&self) -> T
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);
Sourcepub fn acos<T: RealField>(x: T) -> Angle
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));
Sourcepub fn asin<T: RealField>(x: T) -> Angle
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);
Sourcepub fn atan<T: RealField>(x: T) -> Angle
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));
Sourcepub fn atan2<T: RealField>(y: T, x: T) -> Option<Angle>
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);
Sourcepub fn radians<T: RealField>(self) -> T
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());
pub fn degrees<T: RealField>(self) -> T
Trait Implementations§
Source§impl AddAssign for Angle
impl AddAssign for Angle
Source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
+=
operation. Read moreSource§impl<'de> Deserialize<'de> for Angle
impl<'de> Deserialize<'de> for Angle
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl DivAssign<i64> for Angle
impl DivAssign<i64> for Angle
Source§fn div_assign(&mut self, rhs: i64)
fn div_assign(&mut self, rhs: i64)
/=
operation. Read moreSource§impl MulAssign<i64> for Angle
impl MulAssign<i64> for Angle
Source§fn mul_assign(&mut self, rhs: i64)
fn mul_assign(&mut self, rhs: i64)
*=
operation. Read moreSource§impl Ord for Angle
impl Ord for Angle
Source§impl PartialOrd for Angle
impl PartialOrd for Angle
Source§impl SubAssign for Angle
impl SubAssign for Angle
Source§fn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
-=
operation. Read moreimpl Copy for Angle
impl Eq for Angle
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).Source§unsafe fn to_subset_unchecked(&self) -> SS
unsafe fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.