pub struct Vector2d<T>{
pub x: T,
pub y: T,
}Fields§
§x: T§y: TImplementations§
Source§impl<T> Vector2d<T>
impl<T> Vector2d<T>
Sourcepub fn new(x: T, y: T) -> Self
pub fn new(x: T, y: T) -> Self
Create a new Vector2d with the given x and y components.
§Examples
use vectorlib::math::vector2d_module::Vector2d;
let v = Vector2d::new(3, 4);
assert_eq!(v.x, 3);
assert_eq!(v.y, 4);Sourcepub fn zero_vector() -> Self
pub fn zero_vector() -> Self
Constructs a zero 2D vector with both x and y components set to zero.
§Examples
use vectorlib::math::vector2d_module::Vector2d;
let zero: Vector2d<f32> = Vector2d::zero_vector();
assert_eq!(zero.x, 0.0);
assert_eq!(zero.y, 0.0);Sourcepub fn ones_vector() -> Self
pub fn ones_vector() -> Self
Constructs a 2D vector with both x and y components set to one.
§Examples
use vectorlib::math::vector2d_module::Vector2d;
let ones: Vector2d<f32> = Vector2d::ones_vector();
assert_eq!(ones.x, 1.0);
assert_eq!(ones.y, 1.0);Sourcepub fn scale(self, scale: T) -> Vector2d<T>
pub fn scale(self, scale: T) -> Vector2d<T>
Scale the vector by a give value.
accept only scalar
The scale method applies scaling to the vector
by multiply its coordinates by the specified value of scale.
§Examples
use vectorlib::math::vector2d_module::Vector2d;
use num_traits::{Float, FromPrimitive};
use std::ops::{Mul, Sub};
let v = Vector2d::new(3.0, 4.0);
let scalaed_vector = v.scale(1.0); // scale by 1 unit
println!("Rotated: {:?}", scalaed_vector);§Parameters
scale: The scale by which to scale the vector.
§Returns
A new vector with the coordinates adjusted based on the scale value.
§Constraints
T: The type of the vector’s coordinates, which must support multiplication and subtraction.
Sourcepub fn magnitude(self) -> T
pub fn magnitude(self) -> T
Magnitude
Computes the magnitude of the vector.
§Concept
The magnitude of a vector is calculated based on the Pythagorean theorem, which states
that the magnitude of a vector (represented as (x, y)) is the square root of the sum of
the squares of its components:
magnitude = sqrt((x * x) + (y * y))
The magnitude value is returned as a type T.
§Constraints
The type T must implement the Float, Mul, Add, and Copy traits.
§Example
use vectorlib::math::vector2d_module::Vector2d;
let v = Vector2d::new(3.0, 4.0);
let magnitude = v.magnitude();
assert_eq!(magnitude, 5.0);Sourcepub fn distance(self, other: Vector2d<T>) -> T
pub fn distance(self, other: Vector2d<T>) -> T
Calculates the Euclidean distance between two 2D vectors.
The Euclidean distance is the straight-line distance between two points in a two-dimensional space.
§Arguments
self- The first vector.other- The second vector to calculate the distance to.
§Returns
The calculated distance as a value of type T.
§Type Constraints
T: Must implementstd::fmt::Display,Copy,Float,AddAssign,Add<T, Output = T>,Sub<T, Output = T>,Mul<T, Output = T>, andDiv<T, Output = T>.
§Examples
use vectorlib::math::vector2d_module::Vector2d;
let v1 = Vector2d::new(1.0, 2.0);
let v2 = Vector2d::new(4.0, 6.0);
let distance = v1.distance(v2);
println!("Distance: {}", distance);Sourcepub fn normalize(&self) -> Result<Self, String>
pub fn normalize(&self) -> Result<Self, String>
Normalize the vector.
§Examples
use vectorlib::math::vector2d_module::Vector2d;
use num_traits::{Float, Zero};
use std::ops::Div;
let v = Vector2d::new(3.0, 4.0);
let normalized = v.normalize();
println!("Normalized: {:#?}", normalized);§Returns
- If the magnitude of the vector is not zero, it returns a
Resultcontaining the normalized vector. - If the magnitude is zero, it returns an
Errwith an error message.
Sourcepub fn rotate(self, theta: T) -> Self
pub fn rotate(self, theta: T) -> Self
Rotate the vector by a given angle.
The rotate method applies a rotation transformation to the vector
by rotating its coordinates by the specified angle theta.
§Examples
use vectorlib::math::vector2d_module::Vector2d;
use num_traits::{Float, FromPrimitive};
use std::ops::{Mul, Sub};
let v = Vector2d::new(3.0, 4.0);
let rotated = v.rotate(1.0); // Rotate by 1 radian
println!("Rotated: {:?}", rotated);§Type Parameters
N: The type of the angletheta.
§Parameters
theta: The angle by which to rotate the vector.
§Returns
The rotated vector with the coordinates adjusted based on the rotation angle.
§Constraints
T: The type of the vector’s coordinates, which must support multiplication and subtraction.N: The type of the angle, which must implement theNum,Float, andSubtraits.
Sourcepub fn dot_product(self, other: Vector2d<T>) -> T
pub fn dot_product(self, other: Vector2d<T>) -> T
Computes the dot product between two vectors.
The dot product of two vectors is calculated by multiplying the corresponding components of the vectors and summing the results.
§Arguments
self- The first vector.other- The second vector.
§Returns
The dot product as a value of type T.
§Constraints
The type T must implement the Mul trait with Output = T and the Add trait
with Output = T. It must also be Copy.
§Example
use vectorlib::math::vector2d_module::Vector2d;
let v1 = Vector2d::new(1.0, 2.0);
let v2 = Vector2d::new(3.0, 4.0);
let dot_product = v1.dot_product(v2);
assert_eq!(dot_product, 11.0);Sourcepub fn projection_onto(self, other: Vector2d<T>) -> Result<Self, String>
pub fn projection_onto(self, other: Vector2d<T>) -> Result<Self, String>
Computes the projection of the vector onto another vector.
The projection of a vector self onto another vector other is calculated
by taking the dot product of the two vectors and dividing it by the square
of the magnitude of other. The resulting value is then multiplied by other
to obtain the projection vector.
§Arguments
self- The vector to be projected.other- The vector onto whichselfis projected.
§Returns
The projected vector as a new Vector2d instance.
§Constraints
The type T must implement the Mul trait with Output = T and be Copy.
§Example
use vectorlib::math::vector2d_module::Vector2d;
let v = Vector2d::new(3.0, 4.0);
let other = Vector2d::new(2.0, 1.0);
let projection = v.projection_onto(other);
//assert_eq!(projection, Vector2d::new(1, 2));Sourcepub fn orthogonal_on(self, other: Vector2d<T>) -> Result<Self, String>
pub fn orthogonal_on(self, other: Vector2d<T>) -> Result<Self, String>
Calculates the orthogonal vector of self with respect to other.
The orthogonal vector is obtained by subtracting the projection of self
onto other from self.
§Arguments
self- The vector for which to calculate the orthogonal vector.other- The vector with respect to which the orthogonal vector is calculated.
§Returns
If the projection of self onto other is successful, it returns Ok(orthogonal),
where orthogonal is the resulting orthogonal vector.
If the projection fails, it returns Err(error), where error is an error message.
§Type Constraints
T: Must implementstd::fmt::Display,Copy,Float,Zero,AddAssign,Add<T, Output = T>,Sub<T, Output = T>,Mul<T, Output = T>, andDiv<T, Output = T>.
§Examples
use vectorlib::math::vector2d_module::Vector2d;
let v1 = Vector2d::new(1.0, 2.0);
let v2 = Vector2d::new(2.0, 1.0);
let result = v1.orthogonal_on(v2);
match result {
Ok(orthogonal) => println!("Orthogonal vector: {:?}", orthogonal),
Err(error) => println!("Error: {}", error),
}Sourcepub fn lerp(
self,
other: Option<Self>,
percentage: T,
slop: Option<bool>,
) -> Self
pub fn lerp( self, other: Option<Self>, percentage: T, slop: Option<bool>, ) -> Self
Linearly interpolates between two vectors.
The lerp function calculates the linear interpolation between self and other
based on a given interpolation factor t. The interpolation factor t should be
in the range [0, 1], where 0 represents self and 1 represents other. The resulting
vector is a blend of the two vectors based on the interpolation factor.
§Arguments
self- The starting vector for the interpolation.other- The target vector for the interpolation.t- The interpolation factor. Should be in the range [0, 1].
§Returns
The resulting vector after the linear interpolation between self and other.
§Type Constraints
T: Must implementstd::fmt::Display,Copy,Add<T, Output = T>,Sub<T, Output = T>, andMul<T, Output = T>.
§Examples
use vectorlib::math::vector2d_module::Vector2d;
let v1 = Vector2d::new(1.0, 2.0);
let v2 = Vector2d::new(4.0, 6.0);
let result = v1.lerp(Some(v2), 0.5, None);
println!("Interpolated vector: {:?}", result);Sourcepub fn round(self) -> Self
pub fn round(self) -> Self
Rounds the vector’s fields to the nearest whole numbers.
The round method rounds the x and y fields of the vector to the nearest
whole numbers. The rounding is done using the standard rounding rules.
§Arguments
self- The vector to be rounded.
§Returns
The resulting vector with x and y rounded to the nearest whole numbers.
§Type Constraints
T: Must implementstd::fmt::Display,Copy,Add<T, Output = T>,Sub<T, Output = T>, andMul<T, Output = T>.
§Examples
use vectorlib::math::vector2d_module::Vector2d;
let v = Vector2d::new(3.4, -2.8);
let rounded = v.round();
println!("Rounded vector: {:?}", rounded);Trait Implementations§
Source§impl<T> AddAssign<T> for Vector2d<T>
impl<T> AddAssign<T> for Vector2d<T>
Source§fn add_assign(&mut self, other: T)
fn add_assign(&mut self, other: T)
+= operation. Read moreSource§impl<T> AddAssign for Vector2d<T>
impl<T> AddAssign for Vector2d<T>
Source§fn add_assign(&mut self, other: Vector2d<T>)
fn add_assign(&mut self, other: Vector2d<T>)
+= operation. Read more