spatial::Vec2
On this page
46absangle_betweenapprox_eqceilclampdistancedistance_squareddotelement_productelement_sumextendfloorfrom_anglefrom_arrayis_finiteis_normalizedlengthlength_reciplength_squaredlerpmapmaxmax_elementmidpointminmin_elementnewnormalizenormalize_or_zerooneperpperp_dotproject_ontorecipreflectreject_fromrotateroundsplatto_angleto_arraytry_normalizeunit_xunit_yyxzero
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.
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);
The vector of its components.
nabiuse spatial::Vec2; assert(Vec2::new(1.0, 2.0).y == 2.0);Every component
v.nabiuse spatial::Vec2; assert(Vec2::splat(2.0) == Vec2::new(2.0, 2.0));All zero.
nabiuse spatial::Vec2; assert(Vec2::<f32>::zero() == Vec2::splat(0.0));All one.
nabiuse spatial::Vec2; assert(Vec2::<f32>::one() == Vec2::splat(1.0));The unit vector along
x.nabiuse spatial::Vec2; assert(Vec2::<f32>::unit_x().x == 1.0);The unit vector along
y.nabiuse spatial::Vec2; assert(Vec2::<f32>::unit_y().y == 1.0);From
[x, y].nabiuse spatial::Vec2; assert(Vec2::from_array([1.0, 2.0]) == Vec2::new(1.0, 2.0));[x, y].nabiuse spatial::Vec2; assert(Vec2::new(1.0, 2.0).to_array() == [1.0, 2.0]);The dot product.
nabiuse spatial::Vec2; assert(Vec2::new(1.0, 2.0).dot(Vec2::new(3.0, 4.0)) == 11.0);self.dot(self).nabiuse spatial::Vec2; assert(Vec2::new(1.0, 2.0).length_squared() == Vec2::new(1.0, 2.0).dot(Vec2::new(1.0, 2.0)));The squared distance to
o.nabiuse spatial::Vec2; assert(Vec2::<f32>::zero().distance_squared(Vec2::splat(1.0)) == 2.0);The smaller of each pair of components.
nabiuse spatial::Vec2; assert(Vec2::new(1.0, 2.0).min(Vec2::splat(1.5)).y == 1.5);The larger of each pair of components.
nabiuse spatial::Vec2; assert(Vec2::new(1.0, 2.0).max(Vec2::splat(1.5)).x == 1.5);Each component limited to
lo..=hi.nabiuse spatial::Vec2; assert(Vec2::new(1.0, 2.0).clamp(Vec2::splat(1.5), Vec2::splat(1.5)) == Vec2::splat(1.5));The smallest component.
nabiuse spatial::Vec2; assert(Vec2::new(1.0, 2.0).min_element() == 1.0);The largest component.
nabiuse spatial::Vec2; assert(Vec2::new(1.0, 2.0).max_element() == 2.0);The sum of the components.
nabiuse spatial::Vec2; assert(Vec2::new(1.0, 2.0).element_sum() == 3.0);The product of the components.
nabiuse spatial::Vec2; assert(Vec2::new(1.0, 2.0).element_product() == 2.0);Each component through
f.nabiuse spatial::Vec2; assert(Vec2::new(1.0, 2.0).map(|c: f64| -> f64 { c * 2.0 }) == Vec2::new(1.0, 2.0) * 2.0);(y, x).nabiuse spatial::Vec2; assert(Vec2::new(1.0, 2.0).yx() == Vec2::new(2.0, 1.0));(x, y, z).nabiuse spatial::Vec2; assert(Vec2::new(1.0, 2.0).extend(3.0).z == 3.0);The length.
nabiuse spatial::Vec2; assert(Vec2::new(3.0, 4.0).length() == 5.0);1 / length.nabiuse spatial::Vec2; assert(Vec2::new(0.0, 4.0).length_recip() == 0.25);The distance to
o.nabiuse spatial::Vec2; assert(Vec2::<f32>::zero().distance(Vec2::new(3.0, 4.0)) == 5.0);The vector of length 1 in the same direction. A zero vector gives NaN components;
try_normalizeornormalize_or_zerohandle it.nabiuse spatial::Vec2; assert(Vec2::new(0.0, 2.0).normalize() == Vec2::new(0.0, 1.0));The unit vector, or
nonefor a zero, tiny or non-finite one.nabiuse spatial::Vec2; assert(Vec2::<f32>::zero().try_normalize() == none);The unit vector, or zero where
try_normalizegivesnone.nabiuse spatial::Vec2; assert(Vec2::<f32>::zero().normalize_or_zero() == Vec2::zero());Whether the length is 1, within
1e-4of the squared length.nabiuse spatial::Vec2; assert(Vec2::<f32>::unit_x().is_normalized());self + (o - self) * t:t = 0givesself,t = 1giveso.nabiuse spatial::Vec2; assert(Vec2::<f32>::zero().lerp(Vec2::splat(2.0), 0.5) == Vec2::one());The midpoint of
selfando.nabiuse spatial::Vec2; assert(Vec2::<f32>::zero().midpoint(Vec2::splat(2.0)) == Vec2::one());Whether every component is within
epsofo's.nabiuse spatial::Vec2; assert(Vec2::new(1.0, 2.0).approx_eq(Vec2::new(1.0, 2.0) + Vec2::splat(1e-7), 1e-6));Each component's absolute value.
nabiuse spatial::Vec2; assert((-Vec2::<f32>::one()).abs() == Vec2::one());Each component rounded down.
nabiuse spatial::Vec2; assert(Vec2::splat(1.5).floor() == Vec2::one());Each component rounded up.
nabiuse spatial::Vec2; assert(Vec2::splat(0.5).ceil() == Vec2::one());Each component rounded to the nearest integer, half away from zero.
nabiuse spatial::Vec2; assert(Vec2::splat(0.5).round() == Vec2::one());1 / cof each component.nabiuse spatial::Vec2; assert(Vec2::splat(4.0).recip() == Vec2::splat(0.25));Whether no component is NaN or infinite.
nabiuse spatial::Vec2; assert(!Vec2::splat(1.0 / 0.0).is_finite());The part of
selfalongonto(which need not be unit length).nabiuse spatial::Vec2; assert(Vec2::splat(2.0).project_onto(Vec2::unit_x()) == Vec2::unit_x() * 2.0);The part of
selfat right angles tofrom.nabiuse spatial::Vec2; assert(Vec2::splat(2.0).reject_from(Vec2::unit_x()).x == 0.0);selfmirrored by the plane through 0 with unit normaln.nabiuse spatial::Vec2; assert(Vec2::splat(1.0).reflect(Vec2::unit_x()).x == -1.0);The angle to
oin radians, in0..=pi.nabiuse spatial::Vec2; assert((Vec2::<f64>::unit_x().angle_between(Vec2::unit_y()) - f64::PI / 2.0).abs() < 1e-12);The unit vector at
angleradians from +x, counterclockwise.nabiuse spatial::Vec2; assert(Vec2::<f64>::from_angle(0.0) == Vec2::unit_x());The angle from +x in radians, in
-pi..=pi(atan2(y, x)).nabiuse spatial::Vec2; assert(Vec2::<f64>::unit_x().to_angle() == 0.0);selfturned 90 degrees counterclockwise,(-y, x).nabiuse spatial::Vec2; assert(Vec2::new(1.0, 0.0).perp() == Vec2::new(0.0, 1.0));The 2D cross product
x * o.y - y * o.x: positive whenois counterclockwise ofself.nabiuse spatial::Vec2; assert(Vec2::new(1.0, 0.0).perp_dot(Vec2::new(0.0, 1.0)) == 1.0);orotated by the unit vectorself(complex multiplication).nabiuse 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>