[][src]Struct vector2d::Vector2D

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

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: Ty: T

Methods

impl<T: Copy + Clone> Vector2D<T>[src]

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

Create a new Vector2D with the provided components.

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

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

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

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

impl<T: Default> Vector2D<T>[src]

pub fn horizontal(self) -> Self[src]

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

pub fn vertical(self) -> Self[src]

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

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

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

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

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

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

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

impl<T> Vector2D<T> where
    T: Neg<Output = T> + Copy + Clone
[src]

pub fn normal(self) -> Self[src]

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

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

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

Get the scalar/dot product of the two Vector2D.

pub fn length_squared(self) -> V[src]

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.

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

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

Linearly interpolates between two vectors

impl Vector2D<f32>[src]

pub fn length(self) -> f32[src]

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

pub fn normalise(self) -> Self[src]

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.

pub fn angle(self) -> f32[src]

Get the vector's direction in radians.

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

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

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

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

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

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

impl Vector2D<f64>[src]

pub fn length(self) -> f64[src]

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

pub fn normalise(self) -> Self[src]

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.

pub fn angle(self) -> f64[src]

Get the vector's direction in radians.

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

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

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

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

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

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

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

impl Vector2D<i32>[src]

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

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

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

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

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

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

impl Vector2D<i64>[src]

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

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

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

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

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

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

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

impl Vector2D<isize>[src]

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

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

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

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

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

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

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

impl Vector2D<u32>[src]

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

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

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

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

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

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

impl Vector2D<u64>[src]

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

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

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

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

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

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

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

impl Vector2D<usize>[src]

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

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

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

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

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

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

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

Trait Implementations

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

impl<T: Copy> Copy for Vector2D<T>[src]

impl<T: PartialEq> PartialEq<Vector2D<T>> for Vector2D<T>[src]

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

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

impl<T: Clone> Clone for Vector2D<T>[src]

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl<T: Eq> Eq for Vector2D<T>[src]

impl<T: Debug> Debug for Vector2D<T>[src]

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

type Output = Vector2D<O>

The resulting type after applying the + operator.

impl<T, O, '_, '_> Add<&'_ Vector2D<T>> for &'_ Vector2D<T> where
    T: Add<T, Output = O> + Copy + Clone
[src]

type Output = Vector2D<O>

The resulting type after applying the + operator.

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

type Output = Vector2D<O>

The resulting type after applying the - operator.

impl<T, O, '_, '_> Sub<&'_ Vector2D<T>> for &'_ Vector2D<T> where
    T: Sub<T, Output = O> + Copy + Clone
[src]

type Output = Vector2D<O>

The resulting type after applying the - operator.

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

type Output = Vector2D<O>

The resulting type after applying the * operator.

impl<T, O, '_> Mul<T> for &'_ Vector2D<T> where
    T: Mul<T, Output = O> + Copy + Clone
[src]

type Output = Vector2D<O>

The resulting type after applying the * operator.

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

type Output = Vector2D<O>

The resulting type after applying the / operator.

impl<T, O, '_> Div<T> for &'_ Vector2D<T> where
    T: Div<T, Output = O> + Copy + Clone
[src]

type Output = Vector2D<O>

The resulting type after applying the / operator.

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

type Output = Vector2D<U>

The resulting type after applying the - operator.

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

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

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

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

Auto Trait Implementations

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

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

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]