[][src]Struct airmash_protocol::Vector2

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

A 2D Vector that works with unit conversions.

Note: Position, Velocity, and Accel are all instances of this struct with different units.

Fields

x: Ty: T

Methods

impl<T> Vector2<T>
[src]

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

Create a new vector with components that convert into the vectors types.

This is easier to use when creating a vector from components, but it may prevent type inference when the vector type is not specified.

Example

// Note that this is the same as Position
let vec: Vector2<Distance> = Vector2::new(0.0, 0.0);

pub fn dot<U>(self, rhs: Vector2<U>) -> <<T as Mul<U>>::Output as Add>::Output where
    T: Mul<U>,
    <T as Mul<U>>::Output: Add
[src]

Take the dot product of two vectors.

The dot product for a 2D vector is defined (given two vectors a and b) as: a.x * b.x + a.y * b.x.

Example

let a: Vector2<i32> = Vector2::new(1, 2);
let b: Vector2<i32> = Vector2::new(3, 4);

let c = Vector2::dot(a, b);

assert_eq!(c, 11);

pub fn length(self) -> <<<T as Mul>::Output as Add>::Output as Sqrt>::Output where
    Self: Clone,
    T: Mul,
    T::Output: Add,
    <T::Output as Add>::Output: Sqrt
[src]

Calculate the magnitude of the vector.

Examples

Calculate the distance between two points.

let a: Vector2<f32> = Vector2::new(4.0, 0.0);
let b: Vector2<f32> = Vector2::new(-4.0, 0.0);

// The distance is length of the vector going
// from b to a
let dist = (a - b).length();

assert_eq!(dist, 8.0);

pub fn length2(self) -> <<T as Mul>::Output as Add>::Output where
    Self: Clone,
    T: Mul,
    T::Output: Add
[src]

Calculate the length squared.

pub fn normalized(
    self
) -> Vector2<<T as Div<<<<T as Mul>::Output as Add>::Output as Sqrt>::Output>>::Output> where
    Self: Clone,
    T: Mul + Div<<<<T as Mul>::Output as Add>::Output as Sqrt>::Output>,
    <T as Mul>::Output: Add,
    <<T as Mul>::Output as Add>::Output: Sqrt,
    <<<T as Mul>::Output as Add>::Output as Sqrt>::Output: Clone + NotVec, 
[src]

Return a vector pointing in the same direction as this one but with a magniture of 1.

Note

When used with units this will always return a dimensionless vector.

Trait Implementations

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

impl<T: Default> Default for Vector2<T>
[src]

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

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

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

Performs copy-assignment from source. Read more

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

impl<T, U> Add<U> for Vector2<T> where
    T: Add<U>,
    U: Clone + NotVec, 
[src]

type Output = Vector2<T::Output>

The resulting type after applying the + operator.

impl<T, U> Add<Vector2<U>> for Vector2<T> where
    T: Add<U>, 
[src]

type Output = Vector2<T::Output>

The resulting type after applying the + operator.

impl<T, U> Sub<U> for Vector2<T> where
    T: Sub<U>,
    U: Clone + NotVec, 
[src]

type Output = Vector2<T::Output>

The resulting type after applying the - operator.

impl<T, U> Sub<Vector2<U>> for Vector2<T> where
    T: Sub<U>, 
[src]

type Output = Vector2<T::Output>

The resulting type after applying the - operator.

impl<T, U> Mul<U> for Vector2<T> where
    T: Mul<U>,
    U: Clone + NotVec, 
[src]

type Output = Vector2<T::Output>

The resulting type after applying the * operator.

impl<T, U> Mul<Vector2<U>> for Vector2<T> where
    T: Mul<U>, 
[src]

type Output = Vector2<T::Output>

The resulting type after applying the * operator.

impl<T, U> Div<U> for Vector2<T> where
    T: Div<U>,
    U: Clone + NotVec, 
[src]

type Output = Vector2<T::Output>

The resulting type after applying the / operator.

impl<T, U> AddAssign<U> for Vector2<T> where
    Self: Add<U, Output = Vector2<T>> + Clone
[src]

impl<T, U> SubAssign<U> for Vector2<T> where
    Self: Sub<U, Output = Vector2<T>> + Clone
[src]

impl<T, U> MulAssign<U> for Vector2<T> where
    Self: Mul<U, Output = Vector2<T>> + Clone
[src]

impl<T, U> DivAssign<U> for Vector2<T> where
    Self: Div<U, Output = Vector2<T>> + Clone
[src]

impl<T> Serialize for Vector2<T> where
    T: Serialize + Clone
[src]

impl<'de, T> Deserialize<'de> for Vector2<T> where
    T: Deserialize<'de>, 
[src]

Auto Trait Implementations

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

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

Blanket Implementations

impl<T> From for T
[src]

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, U> TryFrom for T where
    T: From<U>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

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

impl<T> Same for T

type Output = T

Should always be Self

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