Struct airmash_protocol::Vector2 [−][src]
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: T
y: T
Methods
impl<T> Vector2<T>
[src]
impl<T> Vector2<T>
pub fn new<X>(x: X, y: X) -> Self where
X: Into<T>,
[src]
pub fn new<X>(x: X, y: X) -> Self where
X: Into<T>,
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]
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,
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]
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,
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]
pub fn length2(self) -> <<T as Mul>::Output as Add>::Output where
Self: Clone,
T: Mul,
T::Output: Add,
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]
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,
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: Default> Default for Vector2<T>
[src]
impl<T: Default> Default for Vector2<T>
impl<T: Clone> Clone for Vector2<T>
[src]
impl<T: Clone> Clone for Vector2<T>
fn clone(&self) -> Vector2<T>
[src]
fn clone(&self) -> Vector2<T>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl<T: Copy> Copy for Vector2<T>
[src]
impl<T: Copy> Copy for Vector2<T>
impl<T: PartialEq> PartialEq for Vector2<T>
[src]
impl<T: PartialEq> PartialEq for Vector2<T>
fn eq(&self, other: &Vector2<T>) -> bool
[src]
fn eq(&self, other: &Vector2<T>) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Vector2<T>) -> bool
[src]
fn ne(&self, other: &Vector2<T>) -> bool
This method tests for !=
.
impl<T: Debug> Debug for Vector2<T>
[src]
impl<T: Debug> Debug for Vector2<T>
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl<T, U> Add<U> for Vector2<T> where
T: Add<U>,
U: Clone + NotVec,
[src]
impl<T, U> Add<U> for Vector2<T> where
T: Add<U>,
U: Clone + NotVec,
type Output = Vector2<T::Output>
The resulting type after applying the +
operator.
fn add(self, rhs: U) -> Self::Output
[src]
fn add(self, rhs: U) -> Self::Output
Performs the +
operation.
impl<T, U> Sub<U> for Vector2<T> where
T: Sub<U>,
U: Clone + NotVec,
[src]
impl<T, U> Sub<U> for Vector2<T> where
T: Sub<U>,
U: Clone + NotVec,
type Output = Vector2<T::Output>
The resulting type after applying the -
operator.
fn sub(self, rhs: U) -> Self::Output
[src]
fn sub(self, rhs: U) -> Self::Output
Performs the -
operation.
impl<T, U> Mul<U> for Vector2<T> where
T: Mul<U>,
U: Clone + NotVec,
[src]
impl<T, U> Mul<U> for Vector2<T> where
T: Mul<U>,
U: Clone + NotVec,
type Output = Vector2<T::Output>
The resulting type after applying the *
operator.
fn mul(self, rhs: U) -> Self::Output
[src]
fn mul(self, rhs: U) -> Self::Output
Performs the *
operation.
impl<T, U> Div<U> for Vector2<T> where
T: Div<U>,
U: Clone + NotVec,
[src]
impl<T, U> Div<U> for Vector2<T> where
T: Div<U>,
U: Clone + NotVec,
type Output = Vector2<T::Output>
The resulting type after applying the /
operator.
fn div(self, rhs: U) -> Self::Output
[src]
fn div(self, rhs: U) -> Self::Output
Performs the /
operation.
impl<T, U> Add<Vector2<U>> for Vector2<T> where
T: Add<U>,
[src]
impl<T, U> Add<Vector2<U>> for Vector2<T> where
T: Add<U>,
type Output = Vector2<T::Output>
The resulting type after applying the +
operator.
fn add(self, rhs: Vector2<U>) -> Self::Output
[src]
fn add(self, rhs: Vector2<U>) -> Self::Output
Performs the +
operation.
impl<T, U> Sub<Vector2<U>> for Vector2<T> where
T: Sub<U>,
[src]
impl<T, U> Sub<Vector2<U>> for Vector2<T> where
T: Sub<U>,
type Output = Vector2<T::Output>
The resulting type after applying the -
operator.
fn sub(self, rhs: Vector2<U>) -> Self::Output
[src]
fn sub(self, rhs: Vector2<U>) -> Self::Output
Performs the -
operation.
impl<T, U> Mul<Vector2<U>> for Vector2<T> where
T: Mul<U>,
[src]
impl<T, U> Mul<Vector2<U>> for Vector2<T> where
T: Mul<U>,
type Output = Vector2<T::Output>
The resulting type after applying the *
operator.
fn mul(self, rhs: Vector2<U>) -> Self::Output
[src]
fn mul(self, rhs: Vector2<U>) -> Self::Output
Performs the *
operation.
impl<T, U> AddAssign<U> for Vector2<T> where
Self: Add<U, Output = Vector2<T>> + Clone,
[src]
impl<T, U> AddAssign<U> for Vector2<T> where
Self: Add<U, Output = Vector2<T>> + Clone,
fn add_assign(&mut self, rhs: U)
[src]
fn add_assign(&mut self, rhs: U)
Performs the +=
operation.
impl<T, U> SubAssign<U> for Vector2<T> where
Self: Sub<U, Output = Vector2<T>> + Clone,
[src]
impl<T, U> SubAssign<U> for Vector2<T> where
Self: Sub<U, Output = Vector2<T>> + Clone,
fn sub_assign(&mut self, rhs: U)
[src]
fn sub_assign(&mut self, rhs: U)
Performs the -=
operation.
impl<T, U> MulAssign<U> for Vector2<T> where
Self: Mul<U, Output = Vector2<T>> + Clone,
[src]
impl<T, U> MulAssign<U> for Vector2<T> where
Self: Mul<U, Output = Vector2<T>> + Clone,
fn mul_assign(&mut self, rhs: U)
[src]
fn mul_assign(&mut self, rhs: U)
Performs the *=
operation.
impl<T, U> DivAssign<U> for Vector2<T> where
Self: Div<U, Output = Vector2<T>> + Clone,
[src]
impl<T, U> DivAssign<U> for Vector2<T> where
Self: Div<U, Output = Vector2<T>> + Clone,
fn div_assign(&mut self, rhs: U)
[src]
fn div_assign(&mut self, rhs: U)
Performs the /=
operation.
impl<T> Serialize for Vector2<T> where
T: Serialize + Clone,
[src]
impl<T> Serialize for Vector2<T> where
T: Serialize + Clone,
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error> where
S: Serializer,
[src]
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error> where
S: Serializer,
Serialize this value into the given Serde serializer. Read more
impl<'de, T> Deserialize<'de> for Vector2<T> where
T: Deserialize<'de>,
[src]
impl<'de, T> Deserialize<'de> for Vector2<T> where
T: Deserialize<'de>,
fn deserialize<D>(de: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
[src]
fn deserialize<D>(de: D) -> Result<Self, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more