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>

source

pub fn abs(self) -> Self

Calculates the absolute value of the x and y components.

source§

impl<I: FixedWidthUnsignedInteger, const N: usize> Vector2D<Num<I, N>>

source

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);
source

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);
source

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>>

source

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());
source

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());
source

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());
source

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));
source

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());
source

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>

source

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>>

source

pub fn new_from_angle(angle: Num<I, N>) -> Self

Creates a unit vector from an angle, noting that the domain of the angle is [0, 1], see Num::cos and Num::sin.

let v: Vector2D<Num<i32, 8>> = Vector2D::new_from_angle(num!(0.0));
assert_eq!(v, (num!(1.0), num!(0.0)).into());
source§

impl<T: Number> Vector2D<T>

source

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);
source

pub fn get(self) -> (T, T)

Returns the tuple of the coordinates

let v = Vector2D::new(1, 2);
assert_eq!(v.get(), (1, 2));
source

pub fn hadamard(self, other: Self) -> Self

Calculates the hadamard product of two vectors

let v1 = Vector2D::new(2, 3);
let v2 = Vector2D::new(4, 5);

let r = v1.hadamard(v2);
assert_eq!(r, Vector2D::new(v1.x * v2.x, v1.y * v2.y));
source

pub fn swap(self) -> Self

Swaps the x and y coordinate

let v1 = Vector2D::new(2, 3);
assert_eq!(v1.swap(), Vector2D::new(3, 2));

Trait Implementations§

source§

impl<T: Number> Add for Vector2D<T>

§

type Output = Vector2D<T>

The resulting type after applying the + operator.
source§

fn add(self, rhs: Vector2D<T>) -> Self::Output

Performs the + operation. Read more
source§

impl<T: Number> AddAssign for Vector2D<T>

source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
source§

impl<T: Clone + Number> Clone for Vector2D<T>

source§

fn clone(&self) -> Vector2D<T>

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<T: Debug + Number> Debug for Vector2D<T>

source§

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

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

impl<T: Default + Number> Default for Vector2D<T>

source§

fn default() -> Vector2D<T>

Returns the “default value” for a type. Read more
source§

impl<T, U: Copy> Div<U> for Vector2D<T>
where T: Div<U, Output = T> + Number,

§

type Output = Vector2D<T>

The resulting type after applying the / operator.
source§

fn div(self, rhs: U) -> Self::Output

Performs the / operation. Read more
source§

impl<T, U: Copy> DivAssign<U> for Vector2D<T>
where T: Div<U, Output = T> + Number,

source§

fn div_assign(&mut self, rhs: U)

Performs the /= operation. Read more
source§

impl<T: Number, P: Number + Into<T>> From<(P, P)> for Vector2D<T>

source§

fn from(f: (P, P)) -> Self

Converts to this type from the input type.
source§

impl<I: FixedWidthUnsignedInteger, const N: usize> From<Vector2D<I>> for Vector2D<Num<I, N>>

source§

fn from(n: Vector2D<I>) -> Self

Converts to this type from the input type.
source§

impl<T: Hash + Number> Hash for Vector2D<T>

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<T, U: Copy> Mul<U> for Vector2D<T>
where T: Mul<U, Output = T> + Number,

§

type Output = Vector2D<T>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: U) -> Self::Output

Performs the * operation. Read more
source§

impl<T, U: Copy> MulAssign<U> for Vector2D<T>
where T: Mul<U, Output = T> + Number,

source§

fn mul_assign(&mut self, rhs: U)

Performs the *= operation. Read more
source§

impl<T: Number + Neg<Output = T>> Neg for Vector2D<T>

§

type Output = Vector2D<T>

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl<T: PartialEq + Number> PartialEq for Vector2D<T>

source§

fn eq(&self, other: &Vector2D<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: Number> Sub for Vector2D<T>

§

type Output = Vector2D<T>

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Vector2D<T>) -> Self::Output

Performs the - operation. Read more
source§

impl<T: Number> SubAssign for Vector2D<T>

source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
source§

impl<T: Copy + Number> Copy for Vector2D<T>

source§

impl<T: Eq + Number> Eq for Vector2D<T>

source§

impl<T: Number> StructuralPartialEq for Vector2D<T>

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Vector2D<T>
where T: RefUnwindSafe,

§

impl<T> Send for Vector2D<T>
where T: Send,

§

impl<T> Sync for Vector2D<T>
where T: Sync,

§

impl<T> Unpin for Vector2D<T>
where T: Unpin,

§

impl<T> UnwindSafe for Vector2D<T>
where T: UnwindSafe,

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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.