pub struct Vector2<T1, T2> {
pub x_: T1,
pub y_: T2,
}Expand description
The code defines a generic struct called Vector2 with two fields, x_ and y_.
Properties:
x_: Thex_property represents the x-coordinate of the Vector2 object. It is of typeT, which means it can be any type specified when creating an instance of the Vector2 struct.y_: They_property is the y-coordinate of theVector2object. It represents the vertical position of the vector in a 2D coordinate system.
Fields§
§x_: T1x portion of the Vector2 object
y_: T2y portion of the Vector2 object
Implementations§
Source§impl<T1, T2> Vector2<T1, T2>
impl<T1, T2> Vector2<T1, T2>
Sourcepub const fn new(x_: T1, y_: T2) -> Self
pub const fn new(x_: T1, y_: T2) -> Self
The function new creates a new Vector2 with the given x and y values.
Arguments:
x_: The parameterx_represents the x-coordinate of the Vector2.y_: The parametery_represents the y-coordinate of the Vector2. It is of typeT, which means it can be any type that is specified when the Vector2 is created.
Returns:
The new function is returning a new instance of the Vector2 struct with the provided x_
and y_ values.
§Example
use physdes::vector2::Vector2;
assert_eq!(Vector2::new(1, 2), Vector2 { x_: 1, y_: 2 });
assert_eq!(Vector2::new(3, 4), Vector2 { x_: 3, y_: 4 });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
The dot function calculates the dot product of two vectors.
Arguments:
other: Theotherparameter is of the same type asself, which means it is an instance of the same struct or class that thedotmethod is defined in.
Returns:
The dot product of two vectors is being returned.
§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
The cross function calculates the cross product of two vectors.
Arguments:
other: Theotherparameter is of typeSelf, which means it is the same type as the current object.
§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, t: T1) -> Self
pub fn scale(&self, t: T1) -> Self
The scale function multiplies the vector by a scalar value.
Arguments:
t: The parametertis a scalar value that will be used to multiply each component ofself.
Returns:
The scale method returns a new instance of the same type as self.
Multiplies self by the scalar t.
§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, t: T1) -> Self
pub fn unscale(&self, t: T1) -> Self
The unscale function divides the coordinates of a vector by a scalar value.
Arguments:
t: The parametertis a scalar value that is used to divide theselfobject. It is of typeT, which is a generic type parameter. The division operation is performed on thex_andy_fields of theselfobject.
Returns:
The unscale method returns a new instance of the same type as self.
Divides self by the scalar t.
§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 + Signed> Vector2<T1, T1>
impl<T1: Clone + Signed> Vector2<T1, T1>
Sourcepub fn l1_norm(&self) -> T1
pub fn l1_norm(&self) -> T1
The l1_norm function calculates the Manhattan distance from the origin.
Returns:
The L1 norm, which is the Manhattan distance from the origin.
Returns the L1 norm |x_| + |y_| – the Manhattan distance from the origin.
§Example
use physdes::vector2::Vector2;
assert_eq!(Vector2::new(1, 2).l1_norm(), 3);
assert_eq!(Vector2::new(3, 4).l1_norm(), 7);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
The norm_inf function returns the maximum absolute value between x_ and y_.
Returns:
The norm_inf function returns the maximum value between |x_| and |y_|.
Returns the infinity norm max(|x_| + |y_|)
§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<'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
§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 + Num, T2: Clone + Num> Add for Vector2<T1, T2>
impl<T1: Clone + Num, T2: Clone + Num> Add for Vector2<T1, T2>
Source§fn add(self, other: Self) -> Self::Output
fn add(self, other: Self) -> Self::Output
The function add takes two values of the same type and returns their sum.
Arguments:
other: Theotherparameter is of the same type asselfand represents the other object that you want to add toself.
§Example
use physdes::vector2::Vector2;
use std::ops::Add;
assert_eq!(Vector2::new(1, 2).add(Vector2::new(3, 4)), Vector2::new(4, 6));
assert_eq!(Vector2::new(3, 4).add(Vector2::new(1, 2)), Vector2::new(4, 6));Source§impl<'a, T1: Clone + Num + AddAssign, T2: Clone + Num + AddAssign> AddAssign<&'a Vector2<T1, T2>> for Point<T1, T2>
impl<'a, T1: Clone + Num + AddAssign, T2: Clone + Num + AddAssign> AddAssign<&'a Vector2<T1, T2>> for Point<T1, T2>
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<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>)
+= operation. Read moreSource§impl<T1: Clone + Num + AddAssign, T2: Clone + Num + AddAssign> AddAssign<Vector2<T1, T2>> for Point<T1, T2>
impl<T1: Clone + Num + AddAssign, T2: Clone + Num + AddAssign> AddAssign<Vector2<T1, T2>> for Point<T1, T2>
Source§fn add_assign(&mut self, other: Vector2<T1, T2>)
fn add_assign(&mut self, other: Vector2<T1, T2>)
+= operation. Read moreSource§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)
The function add_assign adds the values of other.x_ and other.y_ to self.x_ and
self.y_ respectively.
Arguments:
other: The “other” parameter is of type Self, which means it is a reference to another instance of the same struct or class that the method is defined in. In this case, it represents another instance of the struct or class that has the same fields or properties as self.
§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 + 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)
The function divides the values of self.x_ and self.y_ by the value of other.
Arguments:
other: The parameterotheris of typeT, which means it can be any type that implements theClonetrait.
§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));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)
The function multiplies the values of self.x_ and self.y_ by the value of other.
Arguments:
other: The parameterotheris of typeT, which means it can be any type that implements theClonetrait.
§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<'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)
§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 + Num, T2: Clone + Num> Sub for Vector2<T1, T2>
impl<T1: Clone + Num, T2: Clone + Num> Sub for Vector2<T1, T2>
Source§fn sub(self, other: Self) -> Self::Output
fn sub(self, other: Self) -> Self::Output
The function subtracts the coordinates of two points and returns a new point.
Arguments:
other: Theotherparameter is of the same type asselfand represents the other value that you want to subtract fromself.
§Example
use physdes::vector2::Vector2;
use std::ops::Sub;
assert_eq!(Vector2::new(1, 2).sub(Vector2::new(3, 4)), Vector2::new(-2, -2));
assert_eq!(Vector2::new(3, 4).sub(Vector2::new(1, 2)), Vector2::new(2, 2));Source§impl<'a, T1: Clone + Num + SubAssign, T2: Clone + Num + SubAssign> SubAssign<&'a Vector2<T1, T2>> for Point<T1, T2>
impl<'a, T1: Clone + Num + SubAssign, T2: Clone + Num + SubAssign> SubAssign<&'a Vector2<T1, T2>> for Point<T1, T2>
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<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>)
-= operation. Read moreSource§impl<T1: Clone + Num + SubAssign, T2: Clone + Num + SubAssign> SubAssign<Vector2<T1, T2>> for Point<T1, T2>
impl<T1: Clone + Num + SubAssign, T2: Clone + Num + SubAssign> SubAssign<Vector2<T1, T2>> for Point<T1, T2>
Source§fn sub_assign(&mut self, other: Vector2<T1, T2>)
fn sub_assign(&mut self, other: Vector2<T1, T2>)
-= operation. Read moreSource§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)
The function subtracts the values of another object from the values of the current object.
Arguments:
other: The parameter “other” is of type Self, which means it is a reference to another instance of the same struct or class that the method is defined in. In this case, it is a reference to another instance of the struct or class that has the same fields as self (x_ and 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));