vector2d

Struct Vector2D

Source
pub struct Vector2D<T> {
    pub x: T,
    pub y: T,
}
Expand description

A 2D vector, containing an x and a y component. While many types can be used for a Vector2D’s components, the traits they implement determine what functions are available.

Provided that the components implement the necessary traits, Vector2Ds can be added to or subtracted from one-another, and they can be mulitplied and divided by scalar values.

There are generally two options for converting between Vector2D types. If the internal components’ type has an implementation of Into that targets the desired type, then into_vec2d() can be called from the source object, or from_vec2d(..) can be called and the source object can be provided.

If no Into implementation exists, then the only option is to use one of the flavours of casting with as. These are in the form as_types(), and are only implemented for specific types of components. An example usage would look like this:

use vector2d::Vector2D;
let f64_vector: Vector2D<f64> = Vector2D::new(10.3, 11.1);
let i32_vector: Vector2D<i32> = f64_vector.as_i32s();
assert_eq!(Vector2D::new(10, 11), i32_vector);

Implementations of as_types() are only available when an implementation of into_vec2d() is unavailable. This is to seperate between the lossless casting of primitives with into() and from(..), and the lossy casting between primitives of varying detail.

Casts from signed types to unsigned types have a small additional check that ensures a lower bound of 0 on the signed value, to reduce the chances of experiencing undefined behaviour. This means that a Vector2D<f64> with a value of (-10.3, 11.1) would become (0, 11) when cast to a Vector2D<u32> with as_u32s().

The current list of interoperable types that can be cast with the as family of functions is as follows:

  • i32
  • i64,
  • isize
  • u32
  • u64
  • usize
  • f32
  • f64

Fields§

§x: T§y: T

Implementations§

Source§

impl<T: Copy + Clone> Vector2D<T>

Source

pub fn new(x: T, y: T) -> Self

Create a new Vector2D with the provided components.

Source

pub fn from_vec2d<U: Into<T> + Copy + Clone>(src: Vector2D<U>) -> Vector2D<T>

Convert a Vector2 of type U to one of type T. Available only when type T has implemented From<U>.

§Example
use vector2d::Vector2D;
let i32_vector: Vector2D<i32> = Vector2D::new(25, 8);
let f64_vector: Vector2D<f64> = Vector2D::from_vec2d(i32_vector);
assert_eq!(Vector2D::new(25.0, 8.0), f64_vector);
Source

pub fn into_vec2d<U: From<T>>(self) -> Vector2D<U>

Convert a Vector2 of type T to one of type U. Available only when type T has implemented Into<U>.

§Example
use vector2d::Vector2D;
let i32_vector: Vector2D<i32> = Vector2D::new(25, 8);
let i32_vector: Vector2D<i32> = Vector2D::new(25, 8);
let f64_vector: Vector2D<f64> = i32_vector.into_vec2d();
assert_eq!(Vector2D::new(25.0, 8.0), f64_vector);
Source§

impl<T: Default> Vector2D<T>

Source

pub fn horizontal(self) -> Self

Returns a vector with only the horizontal component of the current one

§Example
use vector2d::Vector2D;
let v = Vector2D::new(10, 20);
assert_eq!(Vector2D::new(10, 0), v.horizontal());
Source

pub fn vertical(self) -> Self

Returns a vector with only the vertical component of the current one

§Example
use vector2d::Vector2D;
let v = Vector2D::new(10, 20);
assert_eq!(Vector2D::new(0, 20), v.vertical());
Source§

impl<T> Vector2D<T>
where T: Mul<T, Output = T> + Copy + Clone,

Source

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

Returns a new vector with components equal to each of the current vector’s components multiplied by the corresponding component of the provided vector

§Example
use vector2d::Vector2D;
let v1 = Vector2D::new(11.0, -2.5);
let v2 = Vector2D::new(0.5, -2.0);
assert_eq!(Vector2D::new(5.5, 5.0), v1.mul_components(v2));
Source§

impl<T> Vector2D<T>
where T: Div<T, Output = T> + Copy + Clone,

Source

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

Returns a new vector with components equal to each of the current vector’s components divided by the corresponding component of the provided vector

§Example
use vector2d::Vector2D;
let v1 = Vector2D::new(11.0, -2.5);
let v2 = Vector2D::new(0.5, -2.0);
assert_eq!(Vector2D::new(22.0, 1.25), v1.div_components(v2));
Source§

impl<T> Vector2D<T>
where T: Neg<Output = T> + Copy + Clone,

Source

pub fn normal(self) -> Self

Returns a vector perpendicular to the current one.

§Example
use vector2d::Vector2D;
let v = Vector2D::new(21.3, -98.1);
assert_eq!(Vector2D::new(98.1, 21.3), v.normal());
Source§

impl<T, U, V> Vector2D<T>
where T: Mul<T, Output = U> + Copy + Clone, U: Add<U, Output = V> + Copy + Clone,

Source

pub fn dot(v1: Self, v2: Self) -> V

Get the scalar/dot product of the two Vector2D.

Source

pub fn length_squared(self) -> V

Get the squared length of a Vector2D. This is more performant than using length() – which is only available for Vector2D<f32> and Vector2D<f64> – as it does not perform any square root operation.

Source§

impl<T> Vector2D<T>
where T: Sub<T, Output = T> + Mul<T, Output = T> + Add<T, Output = T> + Copy + Clone,

Source

pub fn lerp(start: Self, end: Self, progress: T) -> Self

Linearly interpolates between two vectors

Source§

impl Vector2D<f32>

Source

pub fn length(self) -> f32

Get the length of the vector. If possible, favour length_squared() over this function, as it is more performant.

Source

pub fn normalise(self) -> Self

Get a new vector with the same direction as this vector, but with a length of 1.0. If the the length of the vector is 0, then the original vector is returned.

Source

pub fn angle(self) -> f32

Get the vector’s direction in radians.

Source

pub fn as_i32s(&self) -> Vector2D<i32>

Source

pub fn as_i64s(&self) -> Vector2D<i64>

Source

pub fn as_isizes(&self) -> Vector2D<isize>

Source

pub fn as_u32s(&self) -> Vector2D<u32>

Source

pub fn as_u64s(&self) -> Vector2D<u64>

Source

pub fn as_usizes(&self) -> Vector2D<usize>

Source§

impl Vector2D<f64>

Source

pub fn length(self) -> f64

Get the length of the vector. If possible, favour length_squared() over this function, as it is more performant.

Source

pub fn normalise(self) -> Self

Get a new vector with the same direction as this vector, but with a length of 1.0. If the the length of the vector is 0, then the original vector is returned.

Source

pub fn angle(self) -> f64

Get the vector’s direction in radians.

Source

pub fn as_i32s(&self) -> Vector2D<i32>

Source

pub fn as_i64s(&self) -> Vector2D<i64>

Source

pub fn as_isizes(&self) -> Vector2D<isize>

Source

pub fn as_f32s(&self) -> Vector2D<f32>

Source

pub fn as_u32s(&self) -> Vector2D<u32>

Source

pub fn as_u64s(&self) -> Vector2D<u64>

Source

pub fn as_usizes(&self) -> Vector2D<usize>

Source§

impl Vector2D<i32>

Source

pub fn as_isizes(&self) -> Vector2D<isize>

Source

pub fn as_f32s(&self) -> Vector2D<f32>

Source

pub fn as_f64s(&self) -> Vector2D<f64>

Source

pub fn as_u32s(&self) -> Vector2D<u32>

Source

pub fn as_u64s(&self) -> Vector2D<u64>

Source

pub fn as_usizes(&self) -> Vector2D<usize>

Source§

impl Vector2D<i64>

Source

pub fn as_i32s(&self) -> Vector2D<i32>

Source

pub fn as_isizes(&self) -> Vector2D<isize>

Source

pub fn as_f32s(&self) -> Vector2D<f32>

Source

pub fn as_f64s(&self) -> Vector2D<f64>

Source

pub fn as_u32s(&self) -> Vector2D<u32>

Source

pub fn as_u64s(&self) -> Vector2D<u64>

Source

pub fn as_usizes(&self) -> Vector2D<usize>

Source§

impl Vector2D<isize>

Source

pub fn as_i32s(&self) -> Vector2D<i32>

Source

pub fn as_i64s(&self) -> Vector2D<i64>

Source

pub fn as_f32s(&self) -> Vector2D<f32>

Source

pub fn as_f64s(&self) -> Vector2D<f64>

Source

pub fn as_u32s(&self) -> Vector2D<u32>

Source

pub fn as_u64s(&self) -> Vector2D<u64>

Source

pub fn as_usizes(&self) -> Vector2D<usize>

Source§

impl Vector2D<u32>

Source

pub fn as_i32s(&self) -> Vector2D<i32>

Source

pub fn as_i64s(&self) -> Vector2D<i64>

Source

pub fn as_isizes(&self) -> Vector2D<isize>

Source

pub fn as_f32s(&self) -> Vector2D<f32>

Source

pub fn as_f64s(&self) -> Vector2D<f64>

Source

pub fn as_usizes(&self) -> Vector2D<usize>

Source§

impl Vector2D<u64>

Source

pub fn as_i32s(&self) -> Vector2D<i32>

Source

pub fn as_i64s(&self) -> Vector2D<i64>

Source

pub fn as_isizes(&self) -> Vector2D<isize>

Source

pub fn as_f32s(&self) -> Vector2D<f32>

Source

pub fn as_f64s(&self) -> Vector2D<f64>

Source

pub fn as_u32s(&self) -> Vector2D<u32>

Source

pub fn as_usizes(&self) -> Vector2D<usize>

Source§

impl Vector2D<usize>

Source

pub fn as_i32s(&self) -> Vector2D<i32>

Source

pub fn as_i64s(&self) -> Vector2D<i64>

Source

pub fn as_isizes(&self) -> Vector2D<isize>

Source

pub fn as_f32s(&self) -> Vector2D<f32>

Source

pub fn as_f64s(&self) -> Vector2D<f64>

Source

pub fn as_u32s(&self) -> Vector2D<u32>

Source

pub fn as_u64s(&self) -> Vector2D<u64>

Trait Implementations§

Source§

impl<T, O> Add<&Vector2D<T>> for &Vector2D<T>
where T: Add<T, Output = O> + Copy + Clone,

Source§

type Output = Vector2D<O>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<T, O> Add for Vector2D<T>
where T: Add<T, Output = O> + Copy + Clone,

Source§

type Output = Vector2D<O>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<T> AddAssign for Vector2D<T>
where T: Add<T, Output = T> + Copy + Clone,

Source§

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

Performs the += operation. Read more
Source§

impl<T: Clone> 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> Debug for Vector2D<T>

Source§

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

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

impl<T, O> Div<T> for &Vector2D<T>
where T: Div<T, Output = O> + Copy + Clone,

Source§

type Output = Vector2D<O>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl<T, O> Div<T> for Vector2D<T>
where T: Div<T, Output = O> + Copy + Clone,

Source§

type Output = Vector2D<O>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

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

Source§

fn div_assign(&mut self, rhs: T)

Performs the /= operation. Read more
Source§

impl<T, U> From<[U; 2]> for Vector2D<T>
where T: From<U>, U: Copy + Clone,

Source§

fn from(src: [U; 2]) -> Vector2D<T>

Converts to this type from the input type.
Source§

impl<T, U> From<(U, U)> for Vector2D<T>
where T: From<U>, U: Copy + Clone,

Source§

fn from(src: (U, U)) -> Vector2D<T>

Converts to this type from the input type.
Source§

impl<T, U> Into<(U, U)> for Vector2D<T>
where T: Into<U> + Copy + Clone,

Source§

fn into(self) -> (U, U)

Converts this type into the (usually inferred) input type.
Source§

impl<T, O> Mul<T> for &Vector2D<T>
where T: Mul<T, Output = O> + Copy + Clone,

Source§

type Output = Vector2D<O>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<T, O> Mul<T> for Vector2D<T>
where T: Mul<T, Output = O> + Copy + Clone,

Source§

type Output = Vector2D<O>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

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

Source§

fn mul_assign(&mut self, rhs: T)

Performs the *= operation. Read more
Source§

impl<T, U> Neg for Vector2D<T>
where T: Neg<Output = U> + Copy + Clone,

Source§

type Output = Vector2D<U>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

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

Source§

fn eq(&self, other: &Vector2D<T>) -> 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<T, O> Sub<&Vector2D<T>> for &Vector2D<T>
where T: Sub<T, Output = O> + Copy + Clone,

Source§

type Output = Vector2D<O>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<T, O> Sub for Vector2D<T>
where T: Sub<T, Output = O> + Copy + Clone,

Source§

type Output = Vector2D<O>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<T> SubAssign for Vector2D<T>
where T: Sub<T, Output = T> + Copy + Clone,

Source§

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

Performs the -= operation. Read more
Source§

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

Source§

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

Source§

impl<T> StructuralPartialEq for Vector2D<T>

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§

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> 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.