Vector2d

Struct Vector2d 

Source
pub struct Vector2d<T>
where T: Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = T> + AddAssign + Copy + Display,
{ pub x: T, pub y: T, }

Fields§

§x: T§y: T

Implementations§

Source§

impl<T> Vector2d<T>
where T: Display + Copy + AddAssign + Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = T>,

Source

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);
Source

pub fn zero_vector() -> Self
where T: NumAssign + Zero + Display + Copy + AddAssign + Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = T>,

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);
Source

pub fn ones_vector() -> Self
where T: NumAssign + One + Display + Copy + AddAssign + Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = T>,

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);
Source

pub fn scale(self, scale: T) -> Vector2d<T>
where T: NumAssign + One + Display + Copy + AddAssign + Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = 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.
Source

pub fn magnitude(self) -> T
where T: Display + Copy + Float + AddAssign + Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = 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);
Source

pub fn distance(self, other: Vector2d<T>) -> T
where T: Display + Copy + Float + AddAssign + Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = 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 implement std::fmt::Display, Copy, Float, AddAssign, Add<T, Output = T>, Sub<T, Output = T>, Mul<T, Output = T>, and Div<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);
Source

pub fn normalize(&self) -> Result<Self, String>
where T: Display + Copy + Float + Zero + AddAssign + Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = T>,

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 Result containing the normalized vector.
  • If the magnitude is zero, it returns an Err with an error message.
Source

pub fn rotate(self, theta: T) -> Self
where T: Display + Copy + Float + Zero + AddAssign + Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = T>,

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 angle theta.
§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 the Num, Float, and Sub traits.
Source

pub fn dot_product(self, other: Vector2d<T>) -> T
where T: Display + Copy + Float + Zero + AddAssign + Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = 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);
Source

pub fn projection_onto(self, other: Vector2d<T>) -> Result<Self, String>
where T: Display + Copy + Float + Zero + AddAssign + Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = T>,

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 which self is 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));
Source

pub fn orthogonal_on(self, other: Vector2d<T>) -> Result<Self, String>
where T: Display + Copy + Float + Zero + AddAssign + Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = T>,

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 implement std::fmt::Display, Copy, Float, Zero, AddAssign, Add<T, Output = T>, Sub<T, Output = T>, Mul<T, Output = T>, and Div<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),
}
Source

pub fn lerp( self, other: Option<Self>, percentage: T, slop: Option<bool>, ) -> Self
where T: Display + Copy + Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = T> + Float,

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 implement std::fmt::Display, Copy, Add<T, Output = T>, Sub<T, Output = T>, and Mul<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);
Source

pub fn round(self) -> Self
where T: Display + Copy + Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Float,

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 implement std::fmt::Display, Copy, Add<T, Output = T>, Sub<T, Output = T>, and Mul<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> Add<T> for Vector2d<T>
where T: Add<T, Output = T> + Copy + AddAssign + Display + Sub<T, Output = T> + Div<T, Output = T> + Mul<T, Output = T>,

Source§

type Output = Vector2d<T>

The resulting type after applying the + operator.
Source§

fn add(self, scalar: T) -> Vector2d<T>

Performs the + operation. Read more
Source§

impl<T> Add for Vector2d<T>
where T: Add<T, Output = T> + Mul<T, Output = T> + Copy + AddAssign + Display + Sub<T, Output = T> + Div<T, Output = T>,

Source§

type Output = Vector2d<T>

The resulting type after applying the + operator.
Source§

fn add(self, other: Vector2d<T>) -> Vector2d<T>

Performs the + operation. Read more
Source§

impl<T> AddAssign<T> for Vector2d<T>
where T: Add<T, Output = T> + Mul<T, Output = T> + Copy + AddAssign + Display + Sub<T, Output = T> + Div<T, Output = T>,

Source§

fn add_assign(&mut self, other: T)

Performs the += operation. Read more
Source§

impl<T> AddAssign for Vector2d<T>
where T: Add<T, Output = T> + Mul<T, Output = T> + Copy + AddAssign + Display + Sub<T, Output = T> + Div<T, Output = T>,

Source§

fn add_assign(&mut self, other: Vector2d<T>)

Performs the += operation. Read more
Source§

impl<T> Clone for Vector2d<T>
where T: Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = T> + AddAssign + Copy + Display + Clone,

Source§

fn clone(&self) -> Vector2d<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Vector2d<T>
where T: Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = T> + AddAssign + Copy + Display + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Display for Vector2d<T>
where T: Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = T> + AddAssign + Copy + Display,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Div<T> for Vector2d<T>
where T: Add<T, Output = T> + Copy + AddAssign + Default + PartialEq + Display + Sub<T, Output = T> + Div<T, Output = T> + Mul<T, Output = T>,

Source§

type Output = Result<Vector2d<T>, String>

The resulting type after applying the / operator.
Source§

fn div(self, scalar: T) -> Result<Vector2d<T>, String>

Performs the / operation. Read more
Source§

impl<T> Div for Vector2d<T>
where T: Add<T, Output = T> + Copy + AddAssign + Display + Default + PartialEq + Sub<T, Output = T> + Div<T, Output = T> + Mul<T, Output = T>,

Source§

type Output = Result<Vector2d<T>, String>

The resulting type after applying the / operator.
Source§

fn div(self, other: Vector2d<T>) -> Result<Vector2d<T>, String>

Performs the / operation. Read more
Source§

impl<T> Index<usize> for Vector2d<T>
where T: Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = T> + AddAssign + Copy + Display,

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<usize> for Vector2d<T>
where T: Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = T> + AddAssign + Copy + Display,

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<T> Mul<T> for Vector2d<T>
where T: Add<T, Output = T> + Copy + AddAssign + Display + Sub<T, Output = T> + Div<T, Output = T> + Mul<T, Output = T>,

Source§

type Output = Vector2d<T>

The resulting type after applying the * operator.
Source§

fn mul(self, scalar: T) -> Vector2d<T>

Performs the * operation. Read more
Source§

impl<T> Mul for Vector2d<T>
where T: Add<T, Output = T> + Copy + AddAssign + Display + Sub<T, Output = T> + Div<T, Output = T> + Mul<T, Output = T>,

Source§

type Output = Vector2d<T>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Vector2d<T>) -> Vector2d<T>

Performs the * operation. Read more
Source§

impl<T> PartialEq for Vector2d<T>
where T: Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = T> + AddAssign + Copy + Display + PartialEq,

Source§

fn eq(&self, other: &Vector2d<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Sub<T> for Vector2d<T>
where T: Add<T, Output = T> + Copy + AddAssign + Display + Sub<T, Output = T> + Div<T, Output = T> + Mul<T, Output = T>,

Source§

type Output = Vector2d<T>

The resulting type after applying the - operator.
Source§

fn sub(self, scalar: T) -> Vector2d<T>

Performs the - operation. Read more
Source§

impl<T> Sub for Vector2d<T>
where T: Add<T, Output = T> + Copy + AddAssign + Display + Sub<T, Output = T> + Div<T, Output = T> + Mul<T, Output = T>,

Source§

type Output = Vector2d<T>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Vector2d<T>) -> Vector2d<T>

Performs the - operation. Read more
Source§

impl<T> Copy for Vector2d<T>
where T: Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = T> + AddAssign + Copy + Display + Copy,

Source§

impl<T> StructuralPartialEq for Vector2d<T>
where T: Add<T, Output = T> + Sub<T, Output = T> + Mul<T, Output = T> + Div<T, Output = T> + AddAssign + Copy + Display,

Auto Trait Implementations§

§

impl<T> Freeze for Vector2d<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Vector2d<T>
where T: RefUnwindSafe,

§

impl<T> Send for Vector2d<T>
where T: Send,

§

impl<T> Sync for Vector2d<T>
where T: Sync,

§

impl<T> Unpin for Vector2d<T>
where T: Unpin,

§

impl<T> UnwindSafe for Vector2d<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.