Vec2

Struct Vec2 

Source
pub struct Vec2 {
    pub x: f64,
    pub y: f64,
}
Expand description

A 2-dimensional vector type

Provides common vector operations such as addition, subtraction, scalar and component-wise multiplication, normalization, dot and cross products.

§Examples


use lars::Vec2;
let a = Vec2::new(3.0, 4.0);
assert_eq!(a.mag(), 5.0);

Fields§

§x: f64

X component of the vector.

§y: f64

Y component of the vector.

Implementations§

Source§

impl Vec2

Source

pub const fn new(x: f64, y: f64) -> Vec2

Source§

impl Vec2

Source

pub const ZERO: Vec2

A zero Vector (0.0, 0.0, 0.0)

Source

pub const ONE: Vec2

A one Vector (1.0, 1.0, 1.0)

Source

pub const UNIT_X: Vec2

A Unit Vector in X (1.0, 0.0, 0.0)

Source

pub const UNIT_Y: Vec2

A Unit Vector in Y (0.0, 1.0, 0.0)

Source

pub fn mag(&self) -> f64

Returns the magnitude (length) of the vector.

§Examples

use lars::Vec2;
let v = Vec2::new(3.0, 4.0);
assert_eq!(v.mag(), 5.0);
Source

pub fn dot(&self, other: &Vec2) -> f64

Returns the dot product between self and another Vec2.

The dot product measures the cosine of the angle between two vectors.

§Examples

use lars::Vec2;
let a = Vec2::new(1.0, 2.0);
let b = Vec2::new(3.0, 4.0);
assert_eq!(a.dot(&b), 11.0);
Source

pub fn cross(&self, other: &Vec2) -> f64

Returns the scalar 2D cross product between self and another Vec2.

Unlike the 3D cross product, the 2D version returns a scalar equal to the signed area of the parallelogram formed by the two vectors.

§Examples

use lars::Vec2;
let a = Vec2::new(1.0, 0.0);
let b = Vec2::new(0.0, 1.0);
assert_eq!(a.cross(&b), 1.0);
Source

pub fn map<F>(&self, f: F) -> Vec2
where F: Fn(f64) -> f64,

Applies a function f to each component (x, y) of the vector.

§Examples

use lars::Vec2;
let v = Vec2::new(1.0, 2.0);
let squared = v.map(|x| x * x);
assert_eq!(squared, Vec2::new(1.0, 4.0));
Source

pub fn normalize(&self) -> Vec2

Returns a normalized (unit-length) version of the vector.

§Panics

Panics if the vector has zero magnitude (division by zero).

§Examples

use lars::Vec2;
let v = Vec2::new(3.0, 0.0);
assert_eq!(v.normalize(), Vec2::new(1.0, 0.0));
Source

pub fn mag_sq(&self) -> f64

Returns the magnitude of the vector, squared.

§Examples

use lars::Vec2;
let v = Vec2::new(3.0, 4.0);
assert_eq!(v.mag_sq(), 25.0);
Source§

impl Vec2

Source

pub fn dist(&self, other: &Point2D) -> f64

Finds the unsigned distance between self and another 3D point Other.

#examples

use lars::Point2D;
let a = Point2D::new(1.0, 0.0);
let b = Point2D::new(0.0, 0.0);
assert_eq!(a.dist(&b), 1.0)



Source

pub fn dist_sq(&self, other: &Point2D) -> f64

Finds the unsigned distance between self and another 3D point Other, squared

#examples


use lars::Point2D;
let a = Point2D::new(2.0, 0.0);
let b = Point2D::new(0.0, 0.0);
assert_eq!(a.dist_sq(&b), 4.0)

Trait Implementations§

Source§

impl Add for Vec2

Source§

type Output = Vec2

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Vec2) -> Vec2

Performs the + operation. Read more
Source§

impl Clone for Vec2

Source§

fn clone(&self) -> Vec2

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 Debug for Vec2

Source§

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

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

impl Default for Vec2

Returns (0.0, 0.0)

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Vec2

displays the vector in the form (X, Y)

Source§

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

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

impl<__RhsT: Copy> Div<__RhsT> for Vec2
where f64: Div<__RhsT, Output = f64>,

Source§

type Output = Vec2

The resulting type after applying the / operator.
Source§

fn div(self, rhs: __RhsT) -> Vec2

Performs the / operation. Read more
Source§

impl Mul<Mat2> for Vec2

Source§

type Output = Vec2

The resulting type after applying the * operator.
Source§

fn mul(self, m: Mat2) -> Vec2

Performs the * operation. Read more
Source§

impl Mul<Vec2> for Mat2

Implements matrix–vector multiplication (Mat2 * Vec2).

Performs the linear transformation of the vector by the matrix.

§[ \begin{bmatrix} a & b \ c & d \end{bmatrix} \begin{bmatrix} x \ y \end{bmatrix}

\begin{bmatrix} ax + by \ cx + dy \end{bmatrix} ]

§Examples

use lars::{Mat2, Vec2};
let m = Mat2::new(1.0, 2.0, 3.0, 4.0);
let v = Vec2::new(1.0, 1.0);
assert_eq!(m * v, Vec2::new(3.0, 7.0));
Source§

type Output = Vec2

The resulting type after applying the * operator.
Source§

fn mul(self, v: Vec2) -> Vec2

Performs the * operation. Read more
Source§

impl Mul<Vec2> for f64

Implements scalar multiplication for f64 * Vec2.

§Examples


use lars::Vec2;
let v = Vec2::new(1.0, 2.0);
let scaled = 2.0 * v;
assert_eq!(scaled, Vec2::new(2.0, 4.0));
Source§

type Output = Vec2

The resulting type after applying the * operator.
Source§

fn mul(self, vector: Vec2) -> Vec2

Performs the * operation. Read more
Source§

impl<__RhsT: Copy> Mul<__RhsT> for Vec2
where f64: Mul<__RhsT, Output = f64>,

Source§

type Output = Vec2

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: __RhsT) -> Vec2

Performs the * operation. Read more
Source§

impl Mul for Vec2

Implements component-wise multiplication between two Vec2s.

§Examples


use lars::Vec2;
let a = Vec2::new(1.0, 2.0);
let b = Vec2::new(3.0, 4.0);
assert_eq!(a * b, Vec2::new(3.0, 8.0));
Source§

type Output = Vec2

The resulting type after applying the * operator.
Source§

fn mul(self, vector: Vec2) -> Vec2

Performs the * operation. Read more
Source§

impl Neg for Vec2

Source§

type Output = Vec2

The resulting type after applying the - operator.
Source§

fn neg(self) -> Vec2

Performs the unary - operation. Read more
Source§

impl PartialEq for Vec2

Source§

fn eq(&self, other: &Self) -> 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 PartialOrd for Vec2

Source§

fn partial_cmp(&self, other: &Vec2) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Sub for Vec2

Source§

type Output = Vec2

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Vec2) -> Vec2

Performs the - operation. Read more
Source§

impl Copy for Vec2

Auto Trait Implementations§

§

impl Freeze for Vec2

§

impl RefUnwindSafe for Vec2

§

impl Send for Vec2

§

impl Sync for Vec2

§

impl Unpin for Vec2

§

impl UnwindSafe for Vec2

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.