Struct agb_fixnum::Vector2D
source · pub struct Vector2D<T: Number> {
pub x: T,
pub y: T,
}
Expand description
A vector of two points: (x, y) represented by integers or fixed point numbers
Fields§
§x: T
The x coordinate
y: T
The y coordinate
Implementations§
source§impl<T: FixedWidthSignedInteger> Vector2D<T>
impl<T: FixedWidthSignedInteger> Vector2D<T>
source§impl<I: FixedWidthUnsignedInteger, const N: usize> Vector2D<Num<I, N>>
impl<I: FixedWidthUnsignedInteger, const N: usize> Vector2D<Num<I, N>>
sourcepub fn trunc(self) -> Vector2D<I>
pub fn trunc(self) -> Vector2D<I>
Truncates the x and y coordinate, see Num::trunc
let v1: Vector2D<Num<i32, 8>> = (num!(1.56), num!(-2.2)).into();
let v2: Vector2D<i32> = (1, -2).into();
assert_eq!(v1.trunc(), v2);
sourcepub fn floor(self) -> Vector2D<I>
pub fn floor(self) -> Vector2D<I>
Floors the x and y coordinate, see Num::floor
let v1: Vector2D<Num<i32, 8>> = Vector2D::new(num!(1.56), num!(-2.2));
let v2: Vector2D<i32> = (1, -3).into();
assert_eq!(v1.floor(), v2);
sourcepub fn try_change_base<J: FixedWidthUnsignedInteger + TryFrom<I>, const M: usize>(
self
) -> Option<Vector2D<Num<J, M>>>
pub fn try_change_base<J: FixedWidthUnsignedInteger + TryFrom<I>, const M: usize>( self ) -> Option<Vector2D<Num<J, M>>>
Attempts to change the base returning None if the numbers cannot be represented
source§impl<const N: usize> Vector2D<Num<i32, N>>
impl<const N: usize> Vector2D<Num<i32, N>>
sourcepub fn magnitude_squared(self) -> Num<i32, N>
pub fn magnitude_squared(self) -> Num<i32, N>
Calculates the magnitude squared, ie (xx + yy)
let v1: Vector2D<Num<i32, 8>> = (num!(3.), num!(4.)).into();
assert_eq!(v1.magnitude_squared(), 25.into());
sourcepub fn manhattan_distance(self) -> Num<i32, N>
pub fn manhattan_distance(self) -> Num<i32, N>
Calculates the manhattan distance, x.abs() + y.abs().
let v1: Vector2D<Num<i32, 8>> = (num!(3.), num!(4.)).into();
assert_eq!(v1.manhattan_distance(), 7.into());
sourcepub fn magnitude(self) -> Num<i32, N>
pub fn magnitude(self) -> Num<i32, N>
Calculates the magnitude by square root
let v1: Vector2D<Num<i32, 8>> = (num!(3.), num!(4.)).into();
assert_eq!(v1.magnitude(), 5.into());
sourcepub fn fast_magnitude(self) -> Num<i32, N>
pub fn fast_magnitude(self) -> Num<i32, N>
Calculates the magnitude of a vector using the alpha max plus beta min algorithm this has a maximum error of less than 4% of the true magnitude, probably depending on the size of your fixed point approximation
let v1: Vector2D<Num<i32, 8>> = (num!(3.), num!(4.)).into();
assert!(v1.fast_magnitude() > num!(4.9) && v1.fast_magnitude() < num!(5.1));
sourcepub fn normalise(self) -> Self
pub fn normalise(self) -> Self
Normalises the vector to magnitude of one by performing a square root, due to fixed point imprecision this magnitude may not be exactly one
let v1: Vector2D<Num<i32, 8>> = (num!(4.), num!(4.)).into();
assert_eq!(v1.normalise().magnitude(), 1.into());
sourcepub fn fast_normalise(self) -> Self
pub fn fast_normalise(self) -> Self
Normalises the vector to magnitude of one using Vector2D::fast_magnitude.
let v1: Vector2D<Num<i32, 8>> = (num!(4.), num!(4.)).into();
assert_eq!(v1.fast_normalise().magnitude(), 1.into());
source§impl<T: Number> Vector2D<T>
impl<T: Number> Vector2D<T>
sourcepub fn change_base<U: Number + From<T>>(self) -> Vector2D<U>
pub fn change_base<U: Number + From<T>>(self) -> Vector2D<U>
Converts the representation of the vector to another type
let v1: Vector2D<i16> = Vector2D::new(1, 2);
let v2: Vector2D<i32> = v1.change_base();
source§impl<I: FixedWidthSignedInteger, const N: usize> Vector2D<Num<I, N>>
impl<I: FixedWidthSignedInteger, const N: usize> Vector2D<Num<I, N>>
sourcepub fn new_from_angle(angle: Num<I, N>) -> Self
pub fn new_from_angle(angle: Num<I, N>) -> Self
source§impl<T: Number> Vector2D<T>
impl<T: Number> Vector2D<T>
sourcepub const fn new(x: T, y: T) -> Self
pub const fn new(x: T, y: T) -> Self
Created a vector from the given coordinates
let v = Vector2D::new(1, 2);
assert_eq!(v.x, 1);
assert_eq!(v.y, 2);
sourcepub fn get(self) -> (T, T)
pub fn get(self) -> (T, T)
Returns the tuple of the coordinates
let v = Vector2D::new(1, 2);
assert_eq!(v.get(), (1, 2));
Trait Implementations§
source§impl<T: Number> AddAssign for Vector2D<T>
impl<T: Number> AddAssign for Vector2D<T>
source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+=
operation. Read moresource§impl<T, U: Copy> DivAssign<U> for Vector2D<T>
impl<T, U: Copy> DivAssign<U> for Vector2D<T>
source§fn div_assign(&mut self, rhs: U)
fn div_assign(&mut self, rhs: U)
/=
operation. Read moresource§impl<T, U: Copy> MulAssign<U> for Vector2D<T>
impl<T, U: Copy> MulAssign<U> for Vector2D<T>
source§fn mul_assign(&mut self, rhs: U)
fn mul_assign(&mut self, rhs: U)
*=
operation. Read moresource§impl<T: PartialEq + Number> PartialEq for Vector2D<T>
impl<T: PartialEq + Number> PartialEq for Vector2D<T>
source§impl<T: Number> SubAssign for Vector2D<T>
impl<T: Number> SubAssign for Vector2D<T>
source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-=
operation. Read more