Skip to main content

Vector2D

Struct Vector2D 

Source
#[repr(C)]
pub struct Vector2D<T>
where 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> Vector2D<T>
where T: Number + Signed,

Source

pub fn abs(self) -> Vector2D<T>

Calculates the absolute value of the x and y components.

Source

pub fn manhattan_distance(self) -> T

Calculates the manhattan (or taxicab) distance, x.abs() + y.abs().

let v1: Vector2D<Num<i32, 8>> = (num!(3.), num!(4.)).into();
assert_eq!(v1.manhattan_distance(), 7.into());
Source§

impl<I, 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>> = vec2(num!(1.56), num!(-2.2));
let v2: Vector2D<i32> = (1, -3).into();
assert_eq!(v1.floor(), v2);
Source

pub fn round(self) -> Vector2D<I>

Rounds the x and y coordinate, see Num::round

let v1: Vector2D<Num<i32, 8>> = vec2(num!(1.56), num!(-2.2));
let v2: Vector2D<i32> = (2, -2).into();
assert_eq!(v1.round(), v2);
Source

pub fn try_change_base<J, const M: usize>(self) -> Option<Vector2D<Num<J, M>>>
where J: FixedWidthUnsignedInteger + TryFrom<I>,

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(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) -> Vector2D<Num<i32, N>>

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) -> Vector2D<Num<i32, N>>

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> Vector2D<T>
where T: Number,

Source

pub fn change_base<U>(self) -> Vector2D<U>
where U: Number + From<T>,

Converts the representation of the vector to another type

let v1: Vector2D<i16> = vec2(1, 2);
let v2: Vector2D<i32> = v1.change_base();
Source§

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

Source

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

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> Vector2D<T>
where T: Number,

Source

pub const fn new(x: T, y: T) -> Vector2D<T>

Created a vector from the given coordinates.

You should use vec2() instead.

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 = vec2(1, 2);
assert_eq!(v.get(), (1, 2));
Source

pub fn hadamard(self, other: Vector2D<T>) -> Vector2D<T>

Calculates the hadamard product of two vectors

let v1 = vec2(2, 3);
let v2 = vec2(4, 5);

let r = v1.hadamard(v2);
assert_eq!(r, vec2(v1.x * v2.x, v1.y * v2.y));
Source

pub fn dot(self, b: Vector2D<T>) -> T

Calculates the dot product / scalar product of two vectors

use agb_fixnum::vec2;

let v1 = vec2(3, 5);
let v2 = vec2(7, 11);

let dot = v1.dot(v2);
assert_eq!(dot, 76);

The dot product for vectors A and B is defined as

Ax × Bx + Ay × By.

Source

pub fn cross(self, b: Vector2D<T>) -> T

Calculates the z component of the cross product / vector product of two vectors

use agb_fixnum::vec2;

let v1 = vec2(3, 5);
let v2 = vec2(7, 11);

let dot = v1.cross(v2);
assert_eq!(dot, -2);

The z component cross product for vectors A and B is defined as

Ax × By - Ay × Bx.

Normally the cross product / vector product is itself a vector. This is in the 3D case where the cross product of two vectors is perpendicular to both vectors. The only vector perpendicular to two 2D vectors is purely in the z direction, hence why this method only returns that component. The x and y components are always zero.

Source

pub fn swap(self) -> Vector2D<T>

Swaps the x and y coordinate

let v1 = vec2(2, 3);
assert_eq!(v1.swap(), vec2(3, 2));
Source

pub fn magnitude_squared(self) -> T

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

Trait Implementations§

Source§

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

Source§

type Output = Vector2D<T>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

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

Source§

fn add_assign(&mut self, rhs: Vector2D<T>)

Performs the += operation. Read more
Source§

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

Source§

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

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Vector2D<T>
where T: Debug + Number,

Source§

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

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

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

Source§

fn default() -> Vector2D<T>

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

impl<'de, T> Deserialize<'de> for Vector2D<T>
where T: Number + Deserialize<'de>,

Source§

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

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

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

Source§

type Output = Vector2D<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: U) -> <Vector2D<T> as Div<U>>::Output

Performs the / operation. Read more
Source§

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

Source§

fn div_assign(&mut self, rhs: U)

Performs the /= operation. Read more
Source§

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

Source§

fn from(f: (P, P)) -> Vector2D<T>

Converts to this type from the input type.
Source§

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

Source§

fn from(n: Vector2D<I>) -> Vector2D<Num<I, N>>

Converts to this type from the input type.
Source§

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

Source§

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

Feeds this value into the given [Hasher]. Read more
1.3.0§

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> Mul<U> for Vector2D<T>
where T: Number<Output = T> + Mul<U>, U: Copy,

Source§

type Output = Vector2D<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: U) -> <Vector2D<T> as Mul<U>>::Output

Performs the * operation. Read more
Source§

impl<T: SignedNumber> Mul<Vector2D<T>> for AffineMatrix<T>

Source§

type Output = Vector2D<T>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

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

Source§

fn mul_assign(&mut self, rhs: U)

Performs the *= operation. Read more
Source§

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

Source§

type Output = Vector2D<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> <Vector2D<T> as Neg>::Output

Performs the unary - operation. Read more
Source§

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

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0§

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<T> Serialize for Vector2D<T>
where T: Number + Serialize,

Source§

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

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

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

Source§

type Output = Vector2D<T>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

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

Source§

fn sub_assign(&mut self, rhs: Vector2D<T>)

Performs the -= operation. Read more
Source§

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

Source§

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

Source§

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

Auto Trait Implementations§

§

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

§

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§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

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
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

Source§

const WITNESS: W = W::MAKE

A constant of the type witness
Source§

impl<T> Identity for T
where T: ?Sized,

Source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
Source§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

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

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

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