[][src]Struct gamemath::Vec2

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

A two-component Euclidean vector useful for linear algebra computation in game development and 3D rendering.

Fields

x: T

The X/first component of the vector.

y: T

The Y/second component of the vector.

Implementations

impl<T> Vec2<T> where
    T: Copy + Debug + PartialEq + Default + Mul<Output = T> + Add<Output = T> + Sub<Output = T> + Neg<Output = T> + PartialOrd
[src]

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

Constructs a new Vec2<T> from two initial values.

Examples

use gamemath::Vec2;

let v = Vec2::new(1.0, 5.0);

assert_eq!(v.x, 1.0);
assert_eq!(v.y, 5.0);

pub fn dot(&self, right: Vec2<T>) -> T[src]

Calculates the dot/scalar product of two Vec2<T>s. The calling object is considered the left value and the argument object is considered the right value.

Examples

use gamemath::Vec2;

let v1 = Vec2::new(1.0, 2.0);
let v2 = Vec2::new(3.0, 4.0);

assert_eq!(v1.dot(v2), 11.0);
assert_eq!(v2.dot(v1), 11.0);

pub fn fill(&mut self, value: T)[src]

Fills all components of the calling Vec2<T> with the provided value.

Examples

use gamemath::Vec2;

let mut v = Vec2::new(0.0, 0.0);

v.fill(6.0);

assert_eq!(v, Vec2::new(6.0, 6.0));

pub fn length_squared(&self) -> T[src]

Calculates the squared length/magnitude/norm of a Vec2<T>. This saves an expensive square root calculation compared to calculating the actual length, and comparing two squared lengths can therefore often be cheaper than, and yield the same result as, computing two real lengths.

Also useful for data types that does not implement a square root function, i.e. non-floating-point data types.

Examples

use gamemath::Vec2;

let v = Vec2::new(1.0, 2.0);

assert_eq!(v.length_squared(), 5.0);

pub fn manhattan_distance(&self, right: Vec2<T>) -> T[src]

Calculates and returns the manhattan distance between the two points pointed to by two Vec2<T> objects.

Examples

use gamemath::Vec2;

let v1 = Vec2::new(1.0, 2.0);
let v2 = Vec2::new(2.0, 4.0);

assert_eq!(v1.manhattan_distance(v2), 3.0);

impl Vec2<f32>[src]

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

Calculates the real length/magnitude/norm of a Vec2<f32>. This results in an expensive square root calculation, and you might want to consider using a squared length instead when possible.

Examples

use gamemath::Vec2;

let v = Vec2::new(3.0_f32, 4.0_f32);

assert_eq!(v.length(), 5.0_f32);

pub fn normalized(self) -> Vec2<f32>[src]

Calculates and returns the unit vector representation of a Vec2<f32>. This results in an an expensive square root calculation.

Examples

use gamemath::Vec2;

let v = Vec2::new(3.0_f32, 4.0_f32);

assert_eq!(v.normalized(), Vec2::new(0.6_f32, 0.8_f32));

pub fn normalize(&mut self)[src]

Normalizes a Vec2<f32> into its unit vector representation. This results in an an expensive square root calculation.

Examples

use gamemath::Vec2;

let mut v = Vec2::new(3.0_f32, 4.0_f32);

v.normalize();

assert_eq!(v, Vec2::new(0.6_f32, 0.8_f32));

impl Vec2<f64>[src]

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

Calculates the real length/magnitude/norm of a Vec2<f64>. This results in an expensive square root calculation, and you might want to consider using a squared length instead when possible.

Examples

use gamemath::Vec2;

let v = Vec2::new(3.0_f64, 4.0_f64);

assert_eq!(v.length(), 5.0_f64);

pub fn normalized(&self) -> Vec2<f64>[src]

Calculates and returns the unit vector representation of a Vec2<f64>. This results in an an expensive square root calculation.

Examples

use gamemath::Vec2;

let v = Vec2::new(3.0_f64, 4.0_f64);

assert_eq!(v.normalized(), Vec2::new(0.6_f64, 0.8_f64));

pub fn normalize(&mut self)[src]

Normalizes a Vec2<f32> into its unit vector representation. This results in an an expensive square root calculation.

Examples

use gamemath::Vec2;

let mut v = Vec2::new(3.0_f64, 4.0_f64);

v.normalize();

assert_eq!(v, Vec2::new(0.6_f64, 0.8_f64));

Trait Implementations

impl<T: Add<Output = T>> Add<Vec2<T>> for Vec2<T>[src]

type Output = Vec2<T>

The resulting type after applying the + operator.

impl<T: AddAssign> AddAssign<Vec2<T>> for Vec2<T>[src]

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

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

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

impl<T> Default for Vec2<T> where
    T: Default
[src]

impl<T: Copy> From<[T; 2]> for Vec2<T>[src]

impl<T> From<(T, T)> for Vec2<T>[src]

impl<T: Copy> From<T> for Vec2<T>[src]

impl<T: Default> From<Vec2<T>> for Vec3<T>[src]

impl<T: Default> From<Vec2<T>> for Vec4<T>[src]

impl<T> From<Vec3<T>> for Vec2<T>[src]

impl<T> From<Vec4<T>> for Vec2<T>[src]

impl<T> Index<usize> for Vec2<T>[src]

type Output = T

The returned type after indexing.

impl<T> IndexMut<usize> for Vec2<T>[src]

impl<T: Mul<Output = T> + Copy> Mul<T> for Vec2<T>[src]

type Output = Vec2<T>

The resulting type after applying the * operator.

impl Mul<Vec2<f32>> for Mat2[src]

type Output = Vec2<f32>

The resulting type after applying the * operator.

impl<T: MulAssign + Copy> MulAssign<T> for Vec2<T>[src]

impl<T: Neg<Output = T>> Neg for Vec2<T>[src]

type Output = Vec2<T>

The resulting type after applying the - operator.

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

impl<T> StructuralPartialEq for Vec2<T>[src]

impl<T: Sub<Output = T>> Sub<Vec2<T>> for Vec2<T>[src]

type Output = Vec2<T>

The resulting type after applying the - operator.

impl<T: SubAssign> SubAssign<Vec2<T>> for Vec2<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for Vec2<T> where
    T: RefUnwindSafe

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

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

impl<T> Unpin for Vec2<T> where
    T: Unpin

impl<T> UnwindSafe for Vec2<T> where
    T: UnwindSafe

Blanket Implementations

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

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

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

impl<T> From<!> for T[src]

impl<T> From<T> for T[src]

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

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

type Owned = T

The resulting type after obtaining ownership.

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.