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: f64X component of the vector.
y: f64Y component of the vector.
Implementations§
Source§impl Vec2
impl Vec2
Sourcepub fn mag(&self) -> f64
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);Sourcepub fn cross(&self, other: &Vec2) -> f64
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);Sourcepub fn map<F>(&self, f: F) -> Vec2
pub fn map<F>(&self, f: F) -> Vec2
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));Trait Implementations§
Source§impl Mul<Vec2> for Mat2
Implements matrix–vector multiplication (Mat2 * Vec2).
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§impl Mul<Vec2> for f64
Implements scalar multiplication for f64 * Vec2.
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§impl Mul for Vec2
Implements component-wise multiplication between two Vec2s.
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));