packages

spatial::Vec2

On this page

Vec2Source

pub struct Vec2<T = f32>

A 2-component vector. Vec2 alone is Vec2<f32>; DVec2, IVec2 and UVec2 are the f64, i32 and u32 forms. #[repr(C)]: two Ts in order, no padding; Vec2 (f32) is 8 bytes, aligned to 4, as a vec2<f32> in WGSL.

nabi
use spatial::Vec2;

let v = Vec2::new(1.0, 2.0);
assert(v.x == 1.0 && v.y == 2.0);
assert(Vec2::<f32>::size() == 8);
  • pub x: T§

    Source
  • pub y: T§

    Source
  • pub const fn new(x: T, y: T) -> Vec2<T>§

    Source

    The vector of its components.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(1.0, 2.0).y == 2.0);
    
  • pub const fn splat(v: T) -> Vec2<T>§

    Source

    Every component v.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::splat(2.0) == Vec2::new(2.0, 2.0));
    
  • pub fn zero() -> Vec2<T>§

    Source

    All zero.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::<f32>::zero() == Vec2::splat(0.0));
    
  • pub fn one() -> Vec2<T>§

    Source

    All one.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::<f32>::one() == Vec2::splat(1.0));
    
  • pub fn unit_x() -> Vec2<T>§

    Source

    The unit vector along x.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::<f32>::unit_x().x == 1.0);
    
  • pub fn unit_y() -> Vec2<T>§

    Source

    The unit vector along y.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::<f32>::unit_y().y == 1.0);
    
  • pub const fn from_array(a: [T; 2]) -> Vec2<T>§

    Source

    From [x, y].

    nabi
    use spatial::Vec2;
    
    assert(Vec2::from_array([1.0, 2.0]) == Vec2::new(1.0, 2.0));
    
  • pub const fn to_array(self) -> [T; 2]§

    Source

    [x, y].

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(1.0, 2.0).to_array() == [1.0, 2.0]);
    
  • pub fn dot(self, o: Vec2<T>) -> T§

    Source

    The dot product.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(1.0, 2.0).dot(Vec2::new(3.0, 4.0)) == 11.0);
    
  • pub fn length_squared(self) -> T§

    Source

    self.dot(self).

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(1.0, 2.0).length_squared() == Vec2::new(1.0, 2.0).dot(Vec2::new(1.0, 2.0)));
    
  • pub fn distance_squared(self, o: Vec2<T>) -> T§

    Source

    The squared distance to o.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::<f32>::zero().distance_squared(Vec2::splat(1.0)) == 2.0);
    
  • pub fn min(self, o: Vec2<T>) -> Vec2<T>§

    Source

    The smaller of each pair of components.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(1.0, 2.0).min(Vec2::splat(1.5)).y == 1.5);
    
  • pub fn max(self, o: Vec2<T>) -> Vec2<T>§

    Source

    The larger of each pair of components.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(1.0, 2.0).max(Vec2::splat(1.5)).x == 1.5);
    
  • pub fn clamp(self, lo: Vec2<T>, hi: Vec2<T>) -> Vec2<T>§

    Source

    Each component limited to lo..=hi.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(1.0, 2.0).clamp(Vec2::splat(1.5), Vec2::splat(1.5)) == Vec2::splat(1.5));
    
  • pub fn min_element(self) -> T§

    Source

    The smallest component.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(1.0, 2.0).min_element() == 1.0);
    
  • pub fn max_element(self) -> T§

    Source

    The largest component.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(1.0, 2.0).max_element() == 2.0);
    
  • pub fn element_sum(self) -> T§

    Source

    The sum of the components.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(1.0, 2.0).element_sum() == 3.0);
    
  • pub fn element_product(self) -> T§

    Source

    The product of the components.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(1.0, 2.0).element_product() == 2.0);
    
  • pub fn map<U>(self, f: fn(T) -> U) -> Vec2<U>§

    Source

    Each component through f.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(1.0, 2.0).map(|c: f64| -> f64 { c * 2.0 }) == Vec2::new(1.0, 2.0) * 2.0);
    
  • pub const fn yx(self) -> Vec2<T>§

    Source

    (y, x).

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(1.0, 2.0).yx() == Vec2::new(2.0, 1.0));
    
  • pub const fn extend(self, z: T) -> Vec3<T>§

    Source

    (x, y, z).

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(1.0, 2.0).extend(3.0).z == 3.0);
    
  • pub fn length(self) -> T§

    Source

    The length.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(3.0, 4.0).length() == 5.0);
    
  • pub fn length_recip(self) -> T§

    Source

    1 / length.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(0.0, 4.0).length_recip() == 0.25);
    
  • pub fn distance(self, o: Vec2<T>) -> T§

    Source

    The distance to o.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::<f32>::zero().distance(Vec2::new(3.0, 4.0)) == 5.0);
    
  • pub fn normalize(self) -> Vec2<T>§

    Source

    The vector of length 1 in the same direction. A zero vector gives NaN components; try_normalize or normalize_or_zero handle it.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(0.0, 2.0).normalize() == Vec2::new(0.0, 1.0));
    
  • pub fn try_normalize(self) -> Vec2<T> | none§

    Source

    The unit vector, or none for a zero, tiny or non-finite one.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::<f32>::zero().try_normalize() == none);
    
  • pub fn normalize_or_zero(self) -> Vec2<T>§

    Source

    The unit vector, or zero where try_normalize gives none.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::<f32>::zero().normalize_or_zero() == Vec2::zero());
    
  • pub fn is_normalized(self) -> bool§

    Source

    Whether the length is 1, within 1e-4 of the squared length.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::<f32>::unit_x().is_normalized());
    
  • pub fn lerp(self, o: Vec2<T>, t: T) -> Vec2<T>§

    Source

    self + (o - self) * t: t = 0 gives self, t = 1 gives o.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::<f32>::zero().lerp(Vec2::splat(2.0), 0.5) == Vec2::one());
    
  • pub fn midpoint(self, o: Vec2<T>) -> Vec2<T>§

    Source

    The midpoint of self and o.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::<f32>::zero().midpoint(Vec2::splat(2.0)) == Vec2::one());
    
  • pub fn approx_eq(self, o: Vec2<T>, eps: T) -> bool§

    Source

    Whether every component is within eps of o's.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(1.0, 2.0).approx_eq(Vec2::new(1.0, 2.0) + Vec2::splat(1e-7), 1e-6));
    
  • pub fn abs(self) -> Vec2<T>§

    Source

    Each component's absolute value.

    nabi
    use spatial::Vec2;
    
    assert((-Vec2::<f32>::one()).abs() == Vec2::one());
    
  • pub fn floor(self) -> Vec2<T>§

    Source

    Each component rounded down.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::splat(1.5).floor() == Vec2::one());
    
  • pub fn ceil(self) -> Vec2<T>§

    Source

    Each component rounded up.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::splat(0.5).ceil() == Vec2::one());
    
  • pub fn round(self) -> Vec2<T>§

    Source

    Each component rounded to the nearest integer, half away from zero.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::splat(0.5).round() == Vec2::one());
    
  • pub fn recip(self) -> Vec2<T>§

    Source

    1 / c of each component.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::splat(4.0).recip() == Vec2::splat(0.25));
    
  • pub fn is_finite(self) -> bool§

    Source

    Whether no component is NaN or infinite.

    nabi
    use spatial::Vec2;
    
    assert(!Vec2::splat(1.0 / 0.0).is_finite());
    
  • pub fn project_onto(self, onto: Vec2<T>) -> Vec2<T>§

    Source

    The part of self along onto (which need not be unit length).

    nabi
    use spatial::Vec2;
    
    assert(Vec2::splat(2.0).project_onto(Vec2::unit_x()) == Vec2::unit_x() * 2.0);
    
  • pub fn reject_from(self, from: Vec2<T>) -> Vec2<T>§

    Source

    The part of self at right angles to from.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::splat(2.0).reject_from(Vec2::unit_x()).x == 0.0);
    
  • pub fn reflect(self, n: Vec2<T>) -> Vec2<T>§

    Source

    self mirrored by the plane through 0 with unit normal n.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::splat(1.0).reflect(Vec2::unit_x()).x == -1.0);
    
  • pub fn angle_between(self, o: Vec2<T>) -> T§

    Source

    The angle to o in radians, in 0..=pi.

    nabi
    use spatial::Vec2;
    
    assert((Vec2::<f64>::unit_x().angle_between(Vec2::unit_y()) - f64::PI / 2.0).abs() < 1e-12);
    
  • pub fn from_angle(angle: T) -> Vec2<T>§

    Source

    The unit vector at angle radians from +x, counterclockwise.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::<f64>::from_angle(0.0) == Vec2::unit_x());
    
  • pub fn to_angle(self) -> T§

    Source

    The angle from +x in radians, in -pi..=pi (atan2(y, x)).

    nabi
    use spatial::Vec2;
    
    assert(Vec2::<f64>::unit_x().to_angle() == 0.0);
    
  • pub fn perp(self) -> Vec2<T>§

    Source

    self turned 90 degrees counterclockwise, (-y, x).

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(1.0, 0.0).perp() == Vec2::new(0.0, 1.0));
    
  • pub fn perp_dot(self, o: Vec2<T>) -> T§

    Source

    The 2D cross product x * o.y - y * o.x: positive when o is counterclockwise of self.

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(1.0, 0.0).perp_dot(Vec2::new(0.0, 1.0)) == 1.0);
    
  • pub fn rotate(self, o: Vec2<T>) -> Vec2<T>§

    Source

    o rotated by the unit vector self (complex multiplication).

    nabi
    use spatial::Vec2;
    
    assert(Vec2::new(0.0, 1.0).rotate(Vec2::new(1.0, 0.0)) == Vec2::new(0.0, 1.0));
    
Trait implementations 19
  • impl<T: Scalar> Add for Vec2<T>
  • impl<T: Scalar> Add<T> for Vec2<T>
  • impl<T: Scalar> AddAssign for Vec2<T>
  • impl<T: Scalar> AddAssign<T> for Vec2<T>
  • impl<T: Scalar> Sub for Vec2<T>
  • impl<T: Scalar> Sub<T> for Vec2<T>
  • impl<T: Scalar> SubAssign for Vec2<T>
  • impl<T: Scalar> SubAssign<T> for Vec2<T>
  • impl<T: Scalar> Mul for Vec2<T>
  • impl<T: Scalar> Mul<T> for Vec2<T>
  • impl<T: Scalar> MulAssign for Vec2<T>
  • impl<T: Scalar> MulAssign<T> for Vec2<T>
  • impl<T: Scalar> Div for Vec2<T>
  • impl<T: Scalar> Div<T> for Vec2<T>
  • impl<T: Scalar> DivAssign for Vec2<T>
  • impl<T: Scalar> DivAssign<T> for Vec2<T>
  • impl<T: Scalar + Neg<Output = T>> Neg for Vec2<T>
  • impl<T: Scalar> Index<usize> for Vec2<T>
  • impl<T: Scalar> Display for Vec2<T>