pub struct Vector2<T1, T2> {
pub x_: T1,
pub y_: T2,
}Expand description
A 2D vector with x and y components.
y
^
|
|
*-----> x
/|
/ | y_
/ |
/ |
*----+-----> x
(0,0) x_§Examples
use physdes::vector2::Vector2;
let v = Vector2::new(3, 4);
assert_eq!(v.x_, 3);
assert_eq!(v.y_, 4);Fields§
§x_: T1x portion of the Vector2 object
y_: T2y portion of the Vector2 object
Implementations§
Source§impl<T1: Clone + Num> Vector2<T1, T1>
impl<T1: Clone + Num> Vector2<T1, T1>
Sourcepub fn dot(&self, other: &Self) -> T1
pub fn dot(&self, other: &Self) -> T1
Computes the dot product of two vectors.
$$\vec{a} \cdot \vec{b} = a_x b_x + a_y b_y$$
§Example
use physdes::vector2::Vector2;
assert_eq!(Vector2::new(1, 2).dot(&Vector2::new(3, 4)), 11);
assert_eq!(Vector2::new(3, 4).dot(&Vector2::new(1, 2)), 11);Sourcepub fn cross(&self, other: &Self) -> T1
pub fn cross(&self, other: &Self) -> T1
Computes the cross product (2D scalar) of two vectors.
$$\vec{a} \times \vec{b} = a_x b_y - a_y b_x$$
§Example
use physdes::vector2::Vector2;
assert_eq!(Vector2::new(1, 2).cross(&Vector2::new(3, 4)), -2);
assert_eq!(Vector2::new(3, 4).cross(&Vector2::new(1, 2)), 2);Sourcepub fn scale(&self, factor: T1) -> Self
pub fn scale(&self, factor: T1) -> Self
Multiplies the vector by a scalar factor.
$$\vec{v}’ = \vec{v} \times f = (v_x \cdot f,; v_y \cdot f)$$
§Example
use physdes::vector2::Vector2;
assert_eq!(Vector2::new(1, 2).scale(3), Vector2::new(3, 6));
assert_eq!(Vector2::new(3, 4).scale(2), Vector2::new(6, 8));Sourcepub fn unscale(&self, factor: T1) -> Self
pub fn unscale(&self, factor: T1) -> Self
Divides the vector by a scalar factor.
$$\vec{v}’ = \vec{v} / f = (v_x / f,; v_y / f)$$
§Example
use physdes::vector2::Vector2;
assert_eq!(Vector2::new(3, 6).unscale(3), Vector2::new(1, 2));
assert_eq!(Vector2::new(6, 8).unscale(2), Vector2::new(3, 4));Source§impl<T1: Clone + PartialOrd> Vector2<T1, T1>
impl<T1: Clone + PartialOrd> Vector2<T1, T1>
Sourcepub fn norm_inf(&self) -> T1
pub fn norm_inf(&self) -> T1
Computes the Chebyshev (infinity) norm: max(x_, y_).
$$|\vec{v}|_\infty = \max(v_x, v_y)$$
Assumes non-negative coordinate values (does not take absolute values internally).
§Example
use physdes::vector2::Vector2;
assert_eq!(Vector2::new(1, 2).norm_inf(), 2);
assert_eq!(Vector2::new(3, 4).norm_inf(), 4);Trait Implementations§
Source§impl<T1: Clone + Num, T2: Clone + Num> Add for Vector2<T1, T2>
impl<T1: Clone + Num, T2: Clone + Num> Add for Vector2<T1, T2>
Source§impl<'a, 'b, T1: Clone + Num, T2: Clone + Num> Add<&'b Vector2<T1, T2>> for &'a Point<T1, T2>
impl<'a, 'b, T1: Clone + Num, T2: Clone + Num> Add<&'b Vector2<T1, T2>> for &'a Point<T1, T2>
Source§impl<T1: Clone + Num, T2: Clone + Num> Add<Vector2<T1, T2>> for Point<T1, T2>
impl<T1: Clone + Num, T2: Clone + Num> Add<Vector2<T1, T2>> for Point<T1, T2>
Source§fn add(self, other: Vector2<T1, T2>) -> Self::Output
fn add(self, other: Vector2<T1, T2>) -> Self::Output
Translate a point by a vector
$$P’ = (x + v_x,; y + v_y)$$
§Examples
use physdes::point::Point;
use physdes::vector2::Vector2;
assert_eq!(Point::new(3, 4) + Vector2::new(5, 3), Point::new(8, 7));
assert_eq!(Point::new(3, 4) + Vector2::new(-5, -3), Point::new(-2, 1));
assert_eq!(Point::new(3, 4) + Vector2::new(5, -3), Point::new(8, 1));
assert_eq!(Point::new(3, 4) + Vector2::new(-5, 3), Point::new(-2, 7));
assert_eq!(Point::new(3, 4) + Vector2::new(0, 0), Point::new(3, 4));
assert_eq!(Point::new(3, 4) + Vector2::new(0, 5), Point::new(3, 9));Source§impl<T1: Clone + NumAssign, T2: Clone + NumAssign> AddAssign for Vector2<T1, T2>
impl<T1: Clone + NumAssign, T2: Clone + NumAssign> AddAssign for Vector2<T1, T2>
Source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
Adds another vector to this one component-wise.
$$\vec{a} \mathrel{+}= \vec{b} \implies (a_x + b_x,; a_y + b_y)$$
§Example
use physdes::vector2::Vector2;
use std::ops::AddAssign;
let mut v = Vector2::new(1, 2);
let v2 = Vector2::new(3, 4);
v.add_assign(v2);
assert_eq!(v, Vector2::new(4, 6));Source§impl<'a, T1: Clone + Num + AddAssign, T2: Clone + Num + AddAssign> AddAssign<&'a Vector2<T1, T2>> for Point<T1, T2>
Translates the point by adding a vector reference to its coordinates
impl<'a, T1: Clone + Num + AddAssign, T2: Clone + Num + AddAssign> AddAssign<&'a Vector2<T1, T2>> for Point<T1, T2>
Translates the point by adding a vector reference to its coordinates
Source§fn add_assign(&mut self, other: &'a Vector2<T1, T2>)
fn add_assign(&mut self, other: &'a Vector2<T1, T2>)
+= operation. Read moreSource§impl<'a, T1: Clone + NumAssign, T2: Clone + NumAssign> AddAssign<&'a Vector2<T1, T2>> for Vector2<T1, T2>
impl<'a, T1: Clone + NumAssign, T2: Clone + NumAssign> AddAssign<&'a Vector2<T1, T2>> for Vector2<T1, T2>
Source§fn add_assign(&mut self, other: &Self)
fn add_assign(&mut self, other: &Self)
+= operation. Read moreSource§impl<T1: Clone + Num + AddAssign, T2: Clone + Num + AddAssign> AddAssign<Vector2<T1, T2>> for Point<T1, T2>
Translates the point by adding a vector to its coordinates
impl<T1: Clone + Num + AddAssign, T2: Clone + Num + AddAssign> AddAssign<Vector2<T1, T2>> for Point<T1, T2>
Translates the point by adding a vector to its coordinates
$$P \mathrel{+}= (v_x, v_y) \implies (x + v_x,; y + v_y)$$
Source§fn add_assign(&mut self, other: Vector2<T1, T2>)
fn add_assign(&mut self, other: Vector2<T1, T2>)
+= operation. Read moreSource§impl<T: AddAssign + Clone + Num> AddAssign<Vector2<T, T>> for Polygon<T>
impl<T: AddAssign + Clone + Num> AddAssign<Vector2<T, T>> for Polygon<T>
Source§fn add_assign(&mut self, rhs: Vector2<T, T>)
fn add_assign(&mut self, rhs: Vector2<T, T>)
Translates the polygon by adding a vector to its origin.
impl<T1: Copy, T2: Copy> Copy for Vector2<T1, T2>
Source§impl<'a, T1: Clone + NumAssign> DivAssign<&'a T1> for Vector2<T1, T1>
impl<'a, T1: Clone + NumAssign> DivAssign<&'a T1> for Vector2<T1, T1>
Source§fn div_assign(&mut self, other: &T1)
fn div_assign(&mut self, other: &T1)
/= operation. Read moreSource§impl<T1: Clone + NumAssign> DivAssign<T1> for Vector2<T1, T1>
impl<T1: Clone + NumAssign> DivAssign<T1> for Vector2<T1, T1>
Source§fn div_assign(&mut self, other: T1)
fn div_assign(&mut self, other: T1)
Divides each component of the vector by a scalar.
$$\vec{v} \mathrel{/}= s \implies (v_x / s,; v_y / s)$$
§Example
use physdes::vector2::Vector2;
use std::ops::DivAssign;
let mut v = Vector2::new(3, 6);
v.div_assign(3);
assert_eq!(v, Vector2::new(1, 2));impl<T1: Eq, T2: Eq> Eq for Vector2<T1, T2>
Source§impl<'a, T1: Clone + NumAssign> MulAssign<&'a T1> for Vector2<T1, T1>
impl<'a, T1: Clone + NumAssign> MulAssign<&'a T1> for Vector2<T1, T1>
Source§fn mul_assign(&mut self, other: &T1)
fn mul_assign(&mut self, other: &T1)
*= operation. Read moreSource§impl<T1: Clone + NumAssign> MulAssign<T1> for Vector2<T1, T1>
impl<T1: Clone + NumAssign> MulAssign<T1> for Vector2<T1, T1>
Source§fn mul_assign(&mut self, other: T1)
fn mul_assign(&mut self, other: T1)
Multiplies each component of the vector by a scalar.
$$\vec{v} \mathrel{*}= s \implies (v_x \cdot s,; v_y \cdot s)$$
§Example
use physdes::vector2::Vector2;
use std::ops::MulAssign;
let mut v = Vector2::new(1, 2);
v.mul_assign(3);
assert_eq!(v, Vector2::new(3, 6));Source§impl<T1: Clone + Num + Neg<Output = T1>, T2: Clone + Num + Neg<Output = T2>> Neg for Vector2<T1, T2>
impl<T1: Clone + Num + Neg<Output = T1>, T2: Clone + Num + Neg<Output = T2>> Neg for Vector2<T1, T2>
Source§impl<T1: Clone + Num + Neg<Output = T1>, T2: Clone + Num + Neg<Output = T2>> Neg for &Vector2<T1, T2>
impl<T1: Clone + Num + Neg<Output = T1>, T2: Clone + Num + Neg<Output = T2>> Neg for &Vector2<T1, T2>
Source§impl<T1: PartialEq, T2: PartialEq> PartialEq for Vector2<T1, T2>
impl<T1: PartialEq, T2: PartialEq> PartialEq for Vector2<T1, T2>
impl<T1: PartialEq, T2: PartialEq> StructuralPartialEq for Vector2<T1, T2>
Source§impl<T1: Clone + Num, T2: Clone + Num> Sub for Vector2<T1, T2>
impl<T1: Clone + Num, T2: Clone + Num> Sub for Vector2<T1, T2>
Source§impl<'a, 'b, T1: Clone + Num, T2: Clone + Num> Sub<&'b Vector2<T1, T2>> for &'a Point<T1, T2>
impl<'a, 'b, T1: Clone + Num, T2: Clone + Num> Sub<&'b Vector2<T1, T2>> for &'a Point<T1, T2>
Source§impl<T1: Clone + Num, T2: Clone + Num> Sub<Vector2<T1, T2>> for Point<T1, T2>
impl<T1: Clone + Num, T2: Clone + Num> Sub<Vector2<T1, T2>> for Point<T1, T2>
Source§fn sub(self, other: Vector2<T1, T2>) -> Self::Output
fn sub(self, other: Vector2<T1, T2>) -> Self::Output
Translate a point by a vector (subtraction)
$$P’ = (x - v_x,; y - v_y)$$
§Examples
use physdes::point::Point;
use physdes::vector2::Vector2;
assert_eq!(Point::new(3, 4) - Vector2::new(5, 3), Point::new(-2, 1));
assert_eq!(Point::new(3, 4) - Vector2::new(-5, -3), Point::new(8, 7));
assert_eq!(Point::new(3, 4) - Vector2::new(5, -3), Point::new(-2, 7));
assert_eq!(Point::new(3, 4) - Vector2::new(-5, 3), Point::new(8, 1));
assert_eq!(Point::new(3, 4) - Vector2::new(0, 0), Point::new(3, 4));
assert_eq!(Point::new(3, 4) - Vector2::new(0, 5), Point::new(3, -1));
assert_eq!(Point::new(3, 4) - Vector2::new(5, 0), Point::new(-2, 4));Source§impl<T1: Clone + NumAssign, T2: Clone + NumAssign> SubAssign for Vector2<T1, T2>
impl<T1: Clone + NumAssign, T2: Clone + NumAssign> SubAssign for Vector2<T1, T2>
Source§fn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
Subtracts another vector from this one component-wise.
$$\vec{a} \mathrel{-}= \vec{b} \implies (a_x - b_x,; a_y - b_y)$$
§Example
use physdes::vector2::Vector2;
use std::ops::SubAssign;
let mut v = Vector2::new(1, 2);
let v2 = Vector2::new(3, 4);
v.sub_assign(v2);
assert_eq!(v, Vector2::new(-2, -2));Source§impl<'a, T1: Clone + Num + SubAssign, T2: Clone + Num + SubAssign> SubAssign<&'a Vector2<T1, T2>> for Point<T1, T2>
Translates the point by subtracting a vector reference from its coordinates
impl<'a, T1: Clone + Num + SubAssign, T2: Clone + Num + SubAssign> SubAssign<&'a Vector2<T1, T2>> for Point<T1, T2>
Translates the point by subtracting a vector reference from its coordinates
Source§fn sub_assign(&mut self, other: &'a Vector2<T1, T2>)
fn sub_assign(&mut self, other: &'a Vector2<T1, T2>)
-= operation. Read moreSource§impl<'a, T1: Clone + NumAssign, T2: Clone + NumAssign> SubAssign<&'a Vector2<T1, T2>> for Vector2<T1, T2>
impl<'a, T1: Clone + NumAssign, T2: Clone + NumAssign> SubAssign<&'a Vector2<T1, T2>> for Vector2<T1, T2>
Source§fn sub_assign(&mut self, other: &Self)
fn sub_assign(&mut self, other: &Self)
-= operation. Read moreSource§impl<T1: Clone + Num + SubAssign, T2: Clone + Num + SubAssign> SubAssign<Vector2<T1, T2>> for Point<T1, T2>
Translates the point by subtracting a vector from its coordinates
impl<T1: Clone + Num + SubAssign, T2: Clone + Num + SubAssign> SubAssign<Vector2<T1, T2>> for Point<T1, T2>
Translates the point by subtracting a vector from its coordinates
$$P \mathrel{-}= (v_x, v_y) \implies (x - v_x,; y - v_y)$$
Source§fn sub_assign(&mut self, other: Vector2<T1, T2>)
fn sub_assign(&mut self, other: Vector2<T1, T2>)
-= operation. Read moreSource§impl<T: SubAssign + Clone + Num> SubAssign<Vector2<T, T>> for Polygon<T>
impl<T: SubAssign + Clone + Num> SubAssign<Vector2<T, T>> for Polygon<T>
Source§fn sub_assign(&mut self, rhs: Vector2<T, T>)
fn sub_assign(&mut self, rhs: Vector2<T, T>)
Translates the polygon by subtracting a vector from its origin.