Skip to main content

glam/f32/
vec2.rs

1// Generated from vec.rs.tera template. Edit the template, not the generated file.
2
3use crate::{f32::math, BVec2, Vec3};
4
5use core::fmt;
6use core::iter::{Product, Sum};
7use core::ops::*;
8
9#[cfg(feature = "zerocopy-08")]
10use zerocopy_derive_08::*;
11
12/// Creates a 2-dimensional vector.
13#[inline(always)]
14#[must_use]
15pub const fn vec2(x: f32, y: f32) -> Vec2 {
16    Vec2::new(x, y)
17}
18
19/// A 2-dimensional vector.
20#[derive(Clone, Copy, PartialEq)]
21#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
22#[cfg_attr(
23    feature = "zerocopy-08",
24    derive(FromBytes, Immutable, IntoBytes, KnownLayout)
25)]
26#[cfg_attr(feature = "cuda", repr(align(8)))]
27#[repr(C)]
28#[cfg_attr(target_arch = "spirv", rust_gpu::vector::v1)]
29pub struct Vec2 {
30    pub x: f32,
31    pub y: f32,
32}
33
34impl Vec2 {
35    /// All zeroes.
36    pub const ZERO: Self = Self::splat(0.0);
37
38    /// All ones.
39    pub const ONE: Self = Self::splat(1.0);
40
41    /// All negative ones.
42    pub const NEG_ONE: Self = Self::splat(-1.0);
43
44    /// All `f32::MIN`.
45    pub const MIN: Self = Self::splat(f32::MIN);
46
47    /// All `f32::MAX`.
48    pub const MAX: Self = Self::splat(f32::MAX);
49
50    /// All `f32::NAN`.
51    pub const NAN: Self = Self::splat(f32::NAN);
52
53    /// All `f32::INFINITY`.
54    pub const INFINITY: Self = Self::splat(f32::INFINITY);
55
56    /// All `f32::NEG_INFINITY`.
57    pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY);
58
59    /// A unit vector pointing along the positive X axis.
60    pub const X: Self = Self::new(1.0, 0.0);
61
62    /// A unit vector pointing along the positive Y axis.
63    pub const Y: Self = Self::new(0.0, 1.0);
64
65    /// A unit vector pointing along the negative X axis.
66    pub const NEG_X: Self = Self::new(-1.0, 0.0);
67
68    /// A unit vector pointing along the negative Y axis.
69    pub const NEG_Y: Self = Self::new(0.0, -1.0);
70
71    /// The unit axes.
72    pub const AXES: [Self; 2] = [Self::X, Self::Y];
73
74    /// Vec2 uses Rust Portable SIMD
75    pub const USES_CORE_SIMD: bool = false;
76    /// Vec2 uses Arm NEON
77    pub const USES_NEON: bool = false;
78    /// Vec2 uses scalar math
79    pub const USES_SCALAR_MATH: bool = true;
80    /// Vec2 uses Intel SSE2
81    pub const USES_SSE2: bool = false;
82    /// Vec2 uses WebAssembly 128-bit SIMD
83    pub const USES_WASM_SIMD: bool = false;
84    #[deprecated(since = "0.31.0", note = "Renamed to USES_WASM_SIMD")]
85    pub const USES_WASM32_SIMD: bool = false;
86
87    /// Creates a new vector.
88    #[inline(always)]
89    #[must_use]
90    pub const fn new(x: f32, y: f32) -> Self {
91        Self { x, y }
92    }
93
94    /// Creates a vector with all elements set to `v`.
95    #[inline]
96    #[must_use]
97    pub const fn splat(v: f32) -> Self {
98        Self::new(v, v)
99    }
100
101    /// Returns a vector containing each element of `self` modified by a mapping function `f`.
102    #[inline]
103    #[must_use]
104    pub fn map<F>(self, mut f: F) -> Self
105    where
106        F: FnMut(f32) -> f32,
107    {
108        Self::new(f(self.x), f(self.y))
109    }
110
111    /// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
112    /// for each element of `self`.
113    ///
114    /// A true element in the mask uses the corresponding element from `if_true`, and false
115    /// uses the element from `if_false`.
116    #[inline]
117    #[must_use]
118    pub fn select(mask: BVec2, if_true: Self, if_false: Self) -> Self {
119        Self::new(
120            if mask.test(0) { if_true.x } else { if_false.x },
121            if mask.test(1) { if_true.y } else { if_false.y },
122        )
123    }
124
125    /// Creates a new vector from an array.
126    #[inline]
127    #[must_use]
128    pub const fn from_array(a: [f32; 2]) -> Self {
129        Self::new(a[0], a[1])
130    }
131
132    /// Converts `self` to `[x, y]`
133    #[inline]
134    #[must_use]
135    pub const fn to_array(&self) -> [f32; 2] {
136        [self.x, self.y]
137    }
138
139    /// Creates a vector from the first 2 values in `slice`.
140    ///
141    /// # Panics
142    ///
143    /// Panics if `slice` is less than 2 elements long.
144    #[inline]
145    #[must_use]
146    pub const fn from_slice(slice: &[f32]) -> Self {
147        assert!(slice.len() >= 2);
148        Self::new(slice[0], slice[1])
149    }
150
151    /// Writes the elements of `self` to the first 2 elements in `slice`.
152    ///
153    /// # Panics
154    ///
155    /// Panics if `slice` is less than 2 elements long.
156    #[inline]
157    pub fn write_to_slice(self, slice: &mut [f32]) {
158        slice[..2].copy_from_slice(&self.to_array());
159    }
160
161    /// Creates a 3D vector from `self` and the given `z` value.
162    #[inline]
163    #[must_use]
164    pub const fn extend(self, z: f32) -> Vec3 {
165        Vec3::new(self.x, self.y, z)
166    }
167
168    /// Creates a 2D vector from `self` with the given value of `x`.
169    #[inline]
170    #[must_use]
171    pub fn with_x(mut self, x: f32) -> Self {
172        self.x = x;
173        self
174    }
175
176    /// Creates a 2D vector from `self` with the given value of `y`.
177    #[inline]
178    #[must_use]
179    pub fn with_y(mut self, y: f32) -> Self {
180        self.y = y;
181        self
182    }
183
184    /// Computes the dot product of `self` and `rhs`.
185    #[inline]
186    #[must_use]
187    pub fn dot(self, rhs: Self) -> f32 {
188        (self.x * rhs.x) + (self.y * rhs.y)
189    }
190
191    /// Returns a vector where every component is the dot product of `self` and `rhs`.
192    #[inline]
193    #[must_use]
194    pub fn dot_into_vec(self, rhs: Self) -> Self {
195        Self::splat(self.dot(rhs))
196    }
197
198    /// Returns a vector containing the minimum values for each element of `self` and `rhs`.
199    ///
200    /// In other words this computes `[min(x, rhs.x), min(self.y, rhs.y), ..]`.
201    ///
202    /// NaN propogation does not follow IEEE 754-2008 semantics for minNum and may differ on
203    /// different SIMD architectures.
204    #[inline]
205    #[must_use]
206    pub fn min(self, rhs: Self) -> Self {
207        Self::new(
208            if self.x < rhs.x { self.x } else { rhs.x },
209            if self.y < rhs.y { self.y } else { rhs.y },
210        )
211    }
212
213    /// Returns a vector containing the maximum values for each element of `self` and `rhs`.
214    ///
215    /// In other words this computes `[max(self.x, rhs.x), max(self.y, rhs.y), ..]`.
216    ///
217    /// NaN propogation does not follow IEEE 754-2008 semantics for maxNum and may differ on
218    /// different SIMD architectures.
219    #[inline]
220    #[must_use]
221    pub fn max(self, rhs: Self) -> Self {
222        Self::new(
223            if self.x > rhs.x { self.x } else { rhs.x },
224            if self.y > rhs.y { self.y } else { rhs.y },
225        )
226    }
227
228    /// Component-wise clamping of values, similar to [`f32::clamp`].
229    ///
230    /// Each element in `min` must be less-or-equal to the corresponding element in `max`.
231    ///
232    /// NaN propogation does not follow IEEE 754-2008 semantics and may differ on
233    /// different SIMD architectures.
234    ///
235    /// # Panics
236    ///
237    /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
238    #[inline]
239    #[must_use]
240    pub fn clamp(self, min: Self, max: Self) -> Self {
241        glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
242        self.max(min).min(max)
243    }
244
245    /// Returns the horizontal minimum of `self`.
246    ///
247    /// In other words this computes `min(x, y, ..)`.
248    ///
249    /// NaN propogation does not follow IEEE 754-2008 semantics and may differ on
250    /// different SIMD architectures.
251    #[inline]
252    #[must_use]
253    pub fn min_element(self) -> f32 {
254        let min = |a, b| if a < b { a } else { b };
255        min(self.x, self.y)
256    }
257
258    /// Returns the horizontal maximum of `self`.
259    ///
260    /// In other words this computes `max(x, y, ..)`.
261    ///
262    /// NaN propogation does not follow IEEE 754-2008 semantics and may differ on
263    /// different SIMD architectures.
264    #[inline]
265    #[must_use]
266    pub fn max_element(self) -> f32 {
267        let max = |a, b| if a > b { a } else { b };
268        max(self.x, self.y)
269    }
270
271    /// Returns the index of the first minimum element of `self`.
272    #[doc(alias = "argmin")]
273    #[inline]
274    #[must_use]
275    pub fn min_position(self) -> usize {
276        if self.x <= self.y {
277            0
278        } else {
279            1
280        }
281    }
282
283    /// Returns the index of the first maximum element of `self`.
284    #[doc(alias = "argmax")]
285    #[inline]
286    #[must_use]
287    pub fn max_position(self) -> usize {
288        if self.x >= self.y {
289            0
290        } else {
291            1
292        }
293    }
294
295    /// Returns the sum of all elements of `self`.
296    ///
297    /// In other words, this computes `self.x + self.y + ..`.
298    #[inline]
299    #[must_use]
300    pub fn element_sum(self) -> f32 {
301        self.x + self.y
302    }
303
304    /// Returns the product of all elements of `self`.
305    ///
306    /// In other words, this computes `self.x * self.y * ..`.
307    #[inline]
308    #[must_use]
309    pub fn element_product(self) -> f32 {
310        self.x * self.y
311    }
312
313    /// Returns a vector mask containing the result of a `==` comparison for each element of
314    /// `self` and `rhs`.
315    ///
316    /// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all
317    /// elements.
318    #[inline]
319    #[must_use]
320    pub fn cmpeq(self, rhs: Self) -> BVec2 {
321        BVec2::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y))
322    }
323
324    /// Returns a vector mask containing the result of a `!=` comparison for each element of
325    /// `self` and `rhs`.
326    ///
327    /// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all
328    /// elements.
329    #[inline]
330    #[must_use]
331    pub fn cmpne(self, rhs: Self) -> BVec2 {
332        BVec2::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y))
333    }
334
335    /// Returns a vector mask containing the result of a `>=` comparison for each element of
336    /// `self` and `rhs`.
337    ///
338    /// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all
339    /// elements.
340    #[inline]
341    #[must_use]
342    pub fn cmpge(self, rhs: Self) -> BVec2 {
343        BVec2::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y))
344    }
345
346    /// Returns a vector mask containing the result of a `>` comparison for each element of
347    /// `self` and `rhs`.
348    ///
349    /// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all
350    /// elements.
351    #[inline]
352    #[must_use]
353    pub fn cmpgt(self, rhs: Self) -> BVec2 {
354        BVec2::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y))
355    }
356
357    /// Returns a vector mask containing the result of a `<=` comparison for each element of
358    /// `self` and `rhs`.
359    ///
360    /// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all
361    /// elements.
362    #[inline]
363    #[must_use]
364    pub fn cmple(self, rhs: Self) -> BVec2 {
365        BVec2::new(self.x.le(&rhs.x), self.y.le(&rhs.y))
366    }
367
368    /// Returns a vector mask containing the result of a `<` comparison for each element of
369    /// `self` and `rhs`.
370    ///
371    /// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all
372    /// elements.
373    #[inline]
374    #[must_use]
375    pub fn cmplt(self, rhs: Self) -> BVec2 {
376        BVec2::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y))
377    }
378
379    /// Returns a vector containing the absolute value of each element of `self`.
380    #[inline]
381    #[must_use]
382    pub fn abs(self) -> Self {
383        Self::new(math::abs(self.x), math::abs(self.y))
384    }
385
386    /// Returns a vector with elements representing the sign of `self`.
387    ///
388    /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
389    /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
390    /// - `NAN` if the number is `NAN`
391    #[inline]
392    #[must_use]
393    pub fn signum(self) -> Self {
394        Self::new(math::signum(self.x), math::signum(self.y))
395    }
396
397    /// Returns a vector with signs of `rhs` and the magnitudes of `self`.
398    #[inline]
399    #[must_use]
400    pub fn copysign(self, rhs: Self) -> Self {
401        Self::new(math::copysign(self.x, rhs.x), math::copysign(self.y, rhs.y))
402    }
403
404    /// Returns a bitmask with the lowest 2 bits set to the sign bits from the elements of `self`.
405    ///
406    /// A negative element results in a `1` bit and a positive element in a `0` bit.  Element `x` goes
407    /// into the first lowest bit, element `y` into the second, etc.
408    ///
409    /// An element is negative if it has a negative sign, including -0.0, NaNs with negative sign
410    /// bit and negative infinity.
411    #[inline]
412    #[must_use]
413    pub fn is_negative_bitmask(self) -> u32 {
414        (self.x.is_sign_negative() as u32) | ((self.y.is_sign_negative() as u32) << 1)
415    }
416
417    /// Returns a mask indicating which components are negative.
418    ///
419    /// An element is negative if it has a negative sign, including -0.0, NaNs with negative sign
420    /// bit and negative infinity.
421    #[inline]
422    #[must_use]
423    pub fn is_negative_mask(self) -> BVec2 {
424        BVec2::new(self.x.is_sign_negative(), self.y.is_sign_negative())
425    }
426
427    /// Returns `true` if, and only if, all elements are finite.  If any element is either
428    /// `NaN`, positive or negative infinity, this will return `false`.
429    #[inline]
430    #[must_use]
431    pub fn is_finite(self) -> bool {
432        self.x.is_finite() && self.y.is_finite()
433    }
434
435    /// Performs `is_finite` on each element of self, returning a vector mask of the results.
436    ///
437    /// In other words, this computes `[x.is_finite(), y.is_finite(), ...]`.
438    #[inline]
439    #[must_use]
440    pub fn is_finite_mask(self) -> BVec2 {
441        BVec2::new(self.x.is_finite(), self.y.is_finite())
442    }
443
444    /// Returns `true` if any elements are `NaN`.
445    #[inline]
446    #[must_use]
447    pub fn is_nan(self) -> bool {
448        self.x.is_nan() || self.y.is_nan()
449    }
450
451    /// Performs `is_nan` on each element of self, returning a vector mask of the results.
452    ///
453    /// In other words, this computes `[x.is_nan(), y.is_nan(), ...]`.
454    #[inline]
455    #[must_use]
456    pub fn is_nan_mask(self) -> BVec2 {
457        BVec2::new(self.x.is_nan(), self.y.is_nan())
458    }
459
460    /// Computes the length of `self`.
461    #[doc(alias = "magnitude")]
462    #[inline]
463    #[must_use]
464    pub fn length(self) -> f32 {
465        math::sqrt(self.dot(self))
466    }
467
468    /// Returns `true` if the vector is not the zero vector (also rejects NaN).
469    #[allow(dead_code)]
470    fn is_non_zero(self) -> bool {
471        self.length_squared() > 0.0
472    }
473
474    /// Computes the squared length of `self`.
475    ///
476    /// This is faster than `length()` as it avoids a square root operation.
477    #[doc(alias = "magnitude2")]
478    #[inline]
479    #[must_use]
480    pub fn length_squared(self) -> f32 {
481        self.dot(self)
482    }
483
484    /// Computes `1.0 / length()`.
485    ///
486    /// For valid results, `self` must _not_ be of length zero.
487    #[inline]
488    #[must_use]
489    pub fn length_recip(self) -> f32 {
490        1.0 / self.length()
491    }
492
493    /// Computes the Euclidean distance between two points in space.
494    #[inline]
495    #[must_use]
496    pub fn distance(self, rhs: Self) -> f32 {
497        (self - rhs).length()
498    }
499
500    /// Compute the squared euclidean distance between two points in space.
501    #[inline]
502    #[must_use]
503    pub fn distance_squared(self, rhs: Self) -> f32 {
504        (self - rhs).length_squared()
505    }
506
507    /// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
508    #[inline]
509    #[must_use]
510    pub fn div_euclid(self, rhs: Self) -> Self {
511        Self::new(
512            math::div_euclid(self.x, rhs.x),
513            math::div_euclid(self.y, rhs.y),
514        )
515    }
516
517    /// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
518    ///
519    /// [Euclidean division]: f32::rem_euclid
520    #[inline]
521    #[must_use]
522    pub fn rem_euclid(self, rhs: Self) -> Self {
523        Self::new(
524            math::rem_euclid(self.x, rhs.x),
525            math::rem_euclid(self.y, rhs.y),
526        )
527    }
528
529    /// Returns `self` normalized to length 1.0.
530    ///
531    /// For valid results, `self` must be finite and _not_ of length zero, nor very close to zero.
532    ///
533    /// See also [`Self::try_normalize()`] and [`Self::normalize_or_zero()`].
534    ///
535    /// # Panics
536    ///
537    /// Will panic if the resulting normalized vector is not finite when `glam_assert` is enabled.
538    #[inline]
539    #[must_use]
540    pub fn normalize(self) -> Self {
541        #[allow(clippy::let_and_return)]
542        let normalized = self.mul(self.length_recip());
543        glam_assert!(normalized.is_finite());
544        normalized
545    }
546
547    /// Returns `self` normalized to length 1.0 if possible, else returns `None`.
548    ///
549    /// In particular, if the input is zero (or very close to zero), or non-finite,
550    /// the result of this operation will be `None`.
551    ///
552    /// See also [`Self::normalize_or_zero()`].
553    #[inline]
554    #[must_use]
555    pub fn try_normalize(self) -> Option<Self> {
556        let rcp = self.length_recip();
557        if rcp.is_finite() && rcp > 0.0 {
558            Some(self * rcp)
559        } else {
560            None
561        }
562    }
563
564    /// Returns `self` normalized to length 1.0 if possible, else returns a
565    /// fallback value.
566    ///
567    /// In particular, if the input is zero (or very close to zero), or non-finite,
568    /// the result of this operation will be the fallback value.
569    ///
570    /// See also [`Self::try_normalize()`].
571    #[inline]
572    #[must_use]
573    pub fn normalize_or(self, fallback: Self) -> Self {
574        let rcp = self.length_recip();
575        if rcp.is_finite() && rcp > 0.0 {
576            self * rcp
577        } else {
578            fallback
579        }
580    }
581
582    /// Returns `self` normalized to length 1.0 if possible, else returns zero.
583    ///
584    /// In particular, if the input is zero (or very close to zero), or non-finite,
585    /// the result of this operation will be zero.
586    ///
587    /// See also [`Self::try_normalize()`].
588    #[inline]
589    #[must_use]
590    pub fn normalize_or_zero(self) -> Self {
591        self.normalize_or(Self::ZERO)
592    }
593
594    /// Returns `self` normalized to length 1.0 and the length of `self`.
595    ///
596    /// If `self` is zero length then `(Self::X, 0.0)` is returned.
597    #[inline]
598    #[must_use]
599    pub fn normalize_and_length(self) -> (Self, f32) {
600        let length = self.length();
601        let rcp = 1.0 / length;
602        if rcp.is_finite() && rcp > 0.0 {
603            (self * rcp, length)
604        } else {
605            (Self::X, 0.0)
606        }
607    }
608
609    /// Returns whether `self` is length `1.0` or not.
610    ///
611    /// Uses a precision threshold of approximately `1e-4`.
612    #[inline]
613    #[must_use]
614    pub fn is_normalized(self) -> bool {
615        math::abs(self.length_squared() - 1.0) <= 2e-4
616    }
617
618    /// Returns the vector projection of `self` onto `rhs`.
619    ///
620    /// `rhs` must be of non-zero length.
621    ///
622    /// # Panics
623    ///
624    /// Will panic if `rhs` is zero length when `glam_assert` is enabled.
625    #[inline]
626    #[must_use]
627    pub fn project_onto(self, rhs: Self) -> Self {
628        let other_len_sq_rcp = 1.0 / rhs.dot(rhs);
629        glam_assert!(other_len_sq_rcp.is_finite());
630        rhs * self.dot(rhs) * other_len_sq_rcp
631    }
632
633    /// Returns the vector rejection of `self` from `rhs`.
634    ///
635    /// The vector rejection is the vector perpendicular to the projection of `self` onto
636    /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
637    ///
638    /// `rhs` must be of non-zero length.
639    ///
640    /// # Panics
641    ///
642    /// Will panic if `rhs` has a length of zero when `glam_assert` is enabled.
643    #[doc(alias("plane"))]
644    #[inline]
645    #[must_use]
646    pub fn reject_from(self, rhs: Self) -> Self {
647        self - self.project_onto(rhs)
648    }
649
650    /// Returns the vector projection of `self` onto `rhs`.
651    ///
652    /// `rhs` must be normalized.
653    ///
654    /// # Panics
655    ///
656    /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
657    #[inline]
658    #[must_use]
659    pub fn project_onto_normalized(self, rhs: Self) -> Self {
660        glam_assert!(rhs.is_normalized());
661        rhs * self.dot(rhs)
662    }
663
664    /// Returns the vector rejection of `self` from `rhs`.
665    ///
666    /// The vector rejection is the vector perpendicular to the projection of `self` onto
667    /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
668    ///
669    /// `rhs` must be normalized.
670    ///
671    /// # Panics
672    ///
673    /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
674    #[doc(alias("plane"))]
675    #[inline]
676    #[must_use]
677    pub fn reject_from_normalized(self, rhs: Self) -> Self {
678        self - self.project_onto_normalized(rhs)
679    }
680
681    /// Returns a vector containing the nearest integer to a number for each element of `self`.
682    /// Round half-way cases away from 0.0.
683    #[inline]
684    #[must_use]
685    pub fn round(self) -> Self {
686        Self::new(math::round(self.x), math::round(self.y))
687    }
688
689    /// Returns a vector containing the largest integer less than or equal to a number for each
690    /// element of `self`.
691    #[inline]
692    #[must_use]
693    pub fn floor(self) -> Self {
694        Self::new(math::floor(self.x), math::floor(self.y))
695    }
696
697    /// Returns a vector containing the smallest integer greater than or equal to a number for
698    /// each element of `self`.
699    #[inline]
700    #[must_use]
701    pub fn ceil(self) -> Self {
702        Self::new(math::ceil(self.x), math::ceil(self.y))
703    }
704
705    /// Returns a vector containing the integer part each element of `self`. This means numbers are
706    /// always truncated towards zero.
707    #[inline]
708    #[must_use]
709    pub fn trunc(self) -> Self {
710        Self::new(math::trunc(self.x), math::trunc(self.y))
711    }
712
713    /// Returns a vector containing `0.0` if `rhs < self` and 1.0 otherwise.
714    ///
715    /// Similar to glsl's step(edge, x), which translates into edge.step(x)
716    #[inline]
717    #[must_use]
718    pub fn step(self, rhs: Self) -> Self {
719        Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
720    }
721
722    /// Performs Hermite interpolation between `0.0` and `1.0` using `x` normalized to `[edge0, edge1]`.
723    ///
724    /// This is equivalent to `t * t * (3.0 - 2.0 * t)`, where `t` is clamped to `[0.0, 1.0]`.
725    /// Results are undefined if any element of `edge0` is greater than or equal to the corresponding
726    /// element of `edge1`.
727    ///
728    /// # Panics
729    ///
730    /// Will panic if any element of `edge0` is greater than or equal to the corresponding element
731    /// of `edge1`, when `glam_assert` is enabled.
732    #[inline]
733    #[must_use]
734    pub fn smoothstep(self, edge0: Self, edge1: Self) -> Self {
735        glam_assert!(edge0.cmplt(edge1).all());
736        let t = ((self - edge0) / (edge1 - edge0)).saturate();
737        t * t * (Self::splat(3.0) - Self::splat(2.0) * t)
738    }
739
740    /// Returns a vector containing all elements of `self` clamped to the range of `[0, 1]`.
741    #[inline]
742    #[must_use]
743    pub fn saturate(self) -> Self {
744        self.clamp(Self::ZERO, Self::ONE)
745    }
746
747    /// Returns a vector containing the fractional part of the vector as `self - self.trunc()`.
748    ///
749    /// Note that this differs from the GLSL implementation of `fract` which returns
750    /// `self - self.floor()`.
751    ///
752    /// Note that this is fast but not precise for large numbers.
753    #[inline]
754    #[must_use]
755    pub fn fract(self) -> Self {
756        self - self.trunc()
757    }
758
759    /// Returns a vector containing the fractional part of the vector as `self - self.floor()`.
760    ///
761    /// Note that this differs from the Rust implementation of `fract` which returns
762    /// `self - self.trunc()`.
763    ///
764    /// Note that this is fast but not precise for large numbers.
765    #[inline]
766    #[must_use]
767    pub fn fract_gl(self) -> Self {
768        self - self.floor()
769    }
770
771    /// Returns a vector containing `e^self` (the exponential function) for each element of
772    /// `self`.
773    #[inline]
774    #[must_use]
775    pub fn exp(self) -> Self {
776        Self::new(math::exp(self.x), math::exp(self.y))
777    }
778
779    /// Returns a vector containing `2^self` for each element of `self`.
780    #[inline]
781    #[must_use]
782    pub fn exp2(self) -> Self {
783        Self::new(math::exp2(self.x), math::exp2(self.y))
784    }
785
786    /// Returns a vector containing the natural logarithm for each element of `self`.
787    /// This returns NaN when the element is negative and negative infinity when the element is zero.
788    #[inline]
789    #[must_use]
790    pub fn ln(self) -> Self {
791        Self::new(math::ln(self.x), math::ln(self.y))
792    }
793
794    /// Returns a vector containing the base 2 logarithm for each element of `self`.
795    /// This returns NaN when the element is negative and negative infinity when the element is zero.
796    #[inline]
797    #[must_use]
798    pub fn log2(self) -> Self {
799        Self::new(math::log2(self.x), math::log2(self.y))
800    }
801
802    /// Returns a vector containing each element of `self` raised to the power of `n`.
803    #[inline]
804    #[must_use]
805    pub fn powf(self, n: f32) -> Self {
806        Self::new(math::powf(self.x, n), math::powf(self.y, n))
807    }
808
809    /// Returns a vector containing the square root for each element of `self`.
810    /// This returns NaN when the element is negative.
811    #[inline]
812    #[must_use]
813    pub fn sqrt(self) -> Self {
814        Self::new(math::sqrt(self.x), math::sqrt(self.y))
815    }
816
817    /// Returns a vector containing the cosine for each element of `self`.
818    #[inline]
819    #[must_use]
820    pub fn cos(self) -> Self {
821        Self::new(math::cos(self.x), math::cos(self.y))
822    }
823
824    /// Returns a vector containing the sine for each element of `self`.
825    #[inline]
826    #[must_use]
827    pub fn sin(self) -> Self {
828        Self::new(math::sin(self.x), math::sin(self.y))
829    }
830
831    /// Returns a tuple of two vectors containing the sine and cosine for each element of `self`.
832    #[inline]
833    #[must_use]
834    pub fn sin_cos(self) -> (Self, Self) {
835        let (sin_x, cos_x) = math::sin_cos(self.x);
836        let (sin_y, cos_y) = math::sin_cos(self.y);
837
838        (Self::new(sin_x, sin_y), Self::new(cos_x, cos_y))
839    }
840
841    /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
842    #[inline]
843    #[must_use]
844    pub fn recip(self) -> Self {
845        Self::new(1.0 / self.x, 1.0 / self.y)
846    }
847
848    /// Performs a linear interpolation between `self` and `rhs` based on the value `s`, using the
849    /// form `self * (1.0 - s) + rhs * s`.
850    ///
851    /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result will
852    /// be equal to `rhs`. When `s` is outside of the range `[0, 1]`, the result is linearly
853    /// extrapolated.
854    ///
855    /// The result is guaranteed to be `self` at `s == 0.0` and `rhs` at `s == 1.0`, even when the
856    /// values differ greatly in magnitude, but it is not monotonic in `s` for nearly equal inputs
857    /// and may not preserve equal inputs exactly. Consider [`lerp_monotonic`](Self::lerp_monotonic)
858    /// instead when interpolating between values that may be equal or nearly equal.
859    #[doc(alias = "mix")]
860    #[inline]
861    #[must_use]
862    pub fn lerp(self, rhs: Self, s: f32) -> Self {
863        self * (1.0 - s) + rhs * s
864    }
865
866    /// Performs a linear interpolation between `self` and `rhs` based on the value `s`, using the
867    /// monotonic form `self + (rhs - self) * s`.
868    ///
869    /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result will
870    /// be equal to `rhs`. When `s` is outside of the range `[0, 1]`, the result is linearly
871    /// extrapolated.
872    ///
873    /// Prefer this over [`lerp`](Self::lerp) when interpolating between values that may be equal or
874    /// nearly equal: the result is monotonic in `s` and equal inputs are preserved exactly, avoiding
875    /// the rounding jitter that [`lerp`](Self::lerp) can introduce. The tradeoff is that
876    /// `rhs - self` is evaluated first, so this is less accurate than [`lerp`](Self::lerp) when
877    /// `self` and `rhs` differ greatly in magnitude, and overflows to infinity when they have
878    /// opposite signs and large magnitudes.
879    ///
880    /// On SIMD back-ends the multiply and add are fused when the target supports it, which has a
881    /// single rounding step and can be more accurate than a separate multiply and add.
882    #[doc(alias = "mix")]
883    #[inline]
884    #[must_use]
885    pub fn lerp_monotonic(self, rhs: Self, s: f32) -> Self {
886        self + (rhs - self) * s
887    }
888
889    /// Moves towards `rhs` based on the value `d`.
890    ///
891    /// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
892    /// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
893    #[inline]
894    #[must_use]
895    pub fn move_towards(self, rhs: Self, d: f32) -> Self {
896        let a = rhs - self;
897        let len = a.length();
898        if len <= d || len <= 1e-4 {
899            return rhs;
900        }
901        self + a / len * d
902    }
903
904    /// Calculates the midpoint between `self` and `rhs`.
905    ///
906    /// The midpoint is the average of, or halfway point between, two vectors.
907    /// `a.midpoint(b)` should yield the same result as `a.lerp(b, 0.5)`
908    /// while being slightly cheaper to compute.
909    #[inline]
910    pub fn midpoint(self, rhs: Self) -> Self {
911        (self + rhs) * 0.5
912    }
913
914    /// Returns true if the absolute difference of all elements between `self` and `rhs` is
915    /// less than or equal to `max_abs_diff`.
916    ///
917    /// This can be used to compare if two vectors contain similar elements. It works best when
918    /// comparing with a known value. The `max_abs_diff` that should be used used depends on
919    /// the values being compared against.
920    ///
921    /// For more see
922    /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
923    #[inline]
924    #[must_use]
925    pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
926        self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
927    }
928
929    /// Returns a vector with a length no less than `min` and no more than `max`.
930    ///
931    /// # Panics
932    ///
933    /// Will panic if `min` is greater than `max`, or if either `min` or `max` is negative, when `glam_assert` is enabled.
934    #[inline]
935    #[must_use]
936    pub fn clamp_length(self, min: f32, max: f32) -> Self {
937        glam_assert!(0.0 <= min);
938        glam_assert!(min <= max);
939        let length_sq = self.length_squared();
940        if length_sq < min * min {
941            min * (self / math::sqrt(length_sq))
942        } else if length_sq > max * max {
943            max * (self / math::sqrt(length_sq))
944        } else {
945            self
946        }
947    }
948
949    /// Returns a vector with a length no more than `max`.
950    ///
951    /// # Panics
952    ///
953    /// Will panic if `max` is negative when `glam_assert` is enabled.
954    #[inline]
955    #[must_use]
956    pub fn clamp_length_max(self, max: f32) -> Self {
957        glam_assert!(0.0 <= max);
958        let length_sq = self.length_squared();
959        if length_sq > max * max {
960            max * (self / math::sqrt(length_sq))
961        } else {
962            self
963        }
964    }
965
966    /// Returns a vector with a length no less than `min`.
967    ///
968    /// # Panics
969    ///
970    /// Will panic if `min` is negative when `glam_assert` is enabled.
971    #[inline]
972    #[must_use]
973    pub fn clamp_length_min(self, min: f32) -> Self {
974        glam_assert!(0.0 <= min);
975        let length_sq = self.length_squared();
976        if length_sq < min * min {
977            min * (self / math::sqrt(length_sq))
978        } else {
979            self
980        }
981    }
982
983    /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding
984    /// error, yielding a more accurate result than an unfused multiply-add.
985    ///
986    /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
987    /// architecture has a dedicated fma CPU instruction. However, this is not always true,
988    /// and will be heavily dependant on designing algorithms with specific target hardware in
989    /// mind.
990    #[inline]
991    #[must_use]
992    pub fn mul_add(self, a: Self, b: Self) -> Self {
993        Self::new(
994            math::mul_add(self.x, a.x, b.x),
995            math::mul_add(self.y, a.y, b.y),
996        )
997    }
998
999    /// Returns the reflection vector for a given incident vector `self` and surface normal
1000    /// `normal`.
1001    ///
1002    /// `normal` must be normalized.
1003    ///
1004    /// # Panics
1005    ///
1006    /// Will panic if `normal` is not normalized when `glam_assert` is enabled.
1007    #[inline]
1008    #[must_use]
1009    pub fn reflect(self, normal: Self) -> Self {
1010        glam_assert!(normal.is_normalized());
1011        self - 2.0 * self.dot(normal) * normal
1012    }
1013
1014    /// Returns the refraction direction for a given incident vector `self`, surface normal
1015    /// `normal` and ratio of indices of refraction, `eta`. When total internal reflection occurs,
1016    /// a zero vector will be returned.
1017    ///
1018    /// `self` and `normal` must be normalized.
1019    ///
1020    /// # Panics
1021    ///
1022    /// Will panic if `self` or `normal` is not normalized when `glam_assert` is enabled.
1023    #[inline]
1024    #[must_use]
1025    pub fn refract(self, normal: Self, eta: f32) -> Self {
1026        glam_assert!(self.is_normalized());
1027        glam_assert!(normal.is_normalized());
1028        let n_dot_i = normal.dot(self);
1029        let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1030        if k >= 0.0 {
1031            eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1032        } else {
1033            Self::ZERO
1034        }
1035    }
1036
1037    /// Creates a 2D vector containing `[angle.cos(), angle.sin()]`. This can be used in
1038    /// conjunction with the [`rotate()`][Self::rotate()] method, e.g.
1039    /// `Vec2::from_angle(PI).rotate(Vec2::Y)` will create the vector `[-1, 0]`
1040    /// and rotate [`Vec2::Y`] around it returning `-Vec2::Y`.
1041    #[inline]
1042    #[must_use]
1043    pub fn from_angle(angle: f32) -> Self {
1044        let (sin, cos) = math::sin_cos(angle);
1045        Self::new(cos, sin)
1046    }
1047
1048    /// Returns the angle (in radians) of this vector in the range `[-Ï€, +Ï€]`.
1049    ///
1050    /// The input does not need to be a unit vector however it must be non-zero.
1051    #[inline]
1052    #[must_use]
1053    pub fn to_angle(self) -> f32 {
1054        math::atan2(self.y, self.x)
1055    }
1056
1057    /// Returns the angle of rotation (in radians) from `self` to `rhs` in the range `[-Ï€, +Ï€]`.
1058    ///
1059    /// The inputs do not need to be unit vectors however they must be non-zero.
1060    ///
1061    /// The returned angle can be used with [`rotate_angle()`][Self::rotate_angle], e.g.
1062    /// `self.rotate_angle(self.angle_to(rhs))` will be equal to `rhs`.
1063    ///
1064    /// # Panics
1065    ///
1066    /// Will panic if `self` or `rhs` has zero length when `glam_assert` is enabled.
1067    #[inline]
1068    #[must_use]
1069    pub fn angle_to(self, rhs: Self) -> f32 {
1070        glam_assert!(self.is_non_zero());
1071        glam_assert!(rhs.is_non_zero());
1072        let angle = math::acos_approx(
1073            self.dot(rhs) / math::sqrt(self.length_squared() * rhs.length_squared()),
1074        );
1075
1076        angle * math::signum(self.perp_dot(rhs))
1077    }
1078
1079    /// Returns a vector that is equal to `self` rotated by 90 degrees.
1080    #[inline]
1081    #[must_use]
1082    pub fn perp(self) -> Self {
1083        Self::new(-self.y, self.x)
1084    }
1085
1086    /// The perpendicular dot product of `self` and `rhs`.
1087    /// Also known as the wedge product, 2D cross product, and determinant.
1088    #[doc(alias = "wedge")]
1089    #[doc(alias = "cross")]
1090    #[doc(alias = "determinant")]
1091    #[inline]
1092    #[must_use]
1093    pub fn perp_dot(self, rhs: Self) -> f32 {
1094        (self.x * rhs.y) - (self.y * rhs.x)
1095    }
1096
1097    /// Returns `rhs` rotated by the angle of `self`. If `self` is normalized,
1098    /// then this just rotation. This is what you usually want. Otherwise,
1099    /// it will be like a rotation with a multiplication by `self`'s length.
1100    ///
1101    /// This can be used in conjunction with the [`from_angle()`][Self::from_angle()] method, e.g.
1102    /// `Vec2::from_angle(PI).rotate(Vec2::Y)` will create the vector `[-1, 0]`
1103    /// and rotate [`Vec2::Y`] around it returning `-Vec2::Y`.
1104    #[inline]
1105    #[must_use]
1106    pub fn rotate(self, rhs: Self) -> Self {
1107        Self::new(
1108            self.x * rhs.x - self.y * rhs.y,
1109            self.y * rhs.x + self.x * rhs.y,
1110        )
1111    }
1112
1113    /// Rotates `self` by `angle` (in radians), equivalent to
1114    /// `self.rotate(Vec2::from_angle(angle))`.
1115    #[inline]
1116    #[must_use]
1117    pub fn rotate_angle(self, angle: f32) -> Self {
1118        self.rotate(Self::from_angle(angle))
1119    }
1120
1121    /// Rotates towards `rhs` up to `max_angle` (in radians).
1122    ///
1123    /// When `max_angle` is `0.0`, the result will be equal to `self`. When `max_angle` is equal to
1124    /// `self.angle_between(rhs)`, the result will be parallel to `rhs`. If `max_angle` is negative,
1125    /// rotates towards the exact opposite of `rhs`. Will not go past the target.
1126    #[inline]
1127    #[must_use]
1128    pub fn rotate_towards(self, rhs: Self, max_angle: f32) -> Self {
1129        let a = self.angle_to(rhs);
1130        let abs_a = math::abs(a);
1131        // When `max_angle < 0`, rotate no further than `PI` radians away
1132        let angle = max_angle.clamp(abs_a - core::f32::consts::PI, abs_a) * math::signum(a);
1133        Self::from_angle(angle).rotate(self)
1134    }
1135
1136    /// Casts all elements of `self` to `f64`.
1137    #[cfg(feature = "f64")]
1138    #[inline]
1139    #[must_use]
1140    pub fn as_dvec2(self) -> crate::DVec2 {
1141        crate::DVec2::new(self.x as f64, self.y as f64)
1142    }
1143
1144    /// Casts all elements of `self` to `i8`.
1145    #[cfg(feature = "i8")]
1146    #[inline]
1147    #[must_use]
1148    pub fn as_i8vec2(self) -> crate::I8Vec2 {
1149        crate::I8Vec2::new(self.x as i8, self.y as i8)
1150    }
1151
1152    /// Casts all elements of `self` to `u8`.
1153    #[cfg(feature = "u8")]
1154    #[inline]
1155    #[must_use]
1156    pub fn as_u8vec2(self) -> crate::U8Vec2 {
1157        crate::U8Vec2::new(self.x as u8, self.y as u8)
1158    }
1159
1160    /// Casts all elements of `self` to `i16`.
1161    #[cfg(feature = "i16")]
1162    #[inline]
1163    #[must_use]
1164    pub fn as_i16vec2(self) -> crate::I16Vec2 {
1165        crate::I16Vec2::new(self.x as i16, self.y as i16)
1166    }
1167
1168    /// Casts all elements of `self` to `u16`.
1169    #[cfg(feature = "u16")]
1170    #[inline]
1171    #[must_use]
1172    pub fn as_u16vec2(self) -> crate::U16Vec2 {
1173        crate::U16Vec2::new(self.x as u16, self.y as u16)
1174    }
1175
1176    /// Casts all elements of `self` to `i32`.
1177    #[cfg(feature = "i32")]
1178    #[inline]
1179    #[must_use]
1180    pub fn as_ivec2(self) -> crate::IVec2 {
1181        crate::IVec2::new(self.x as i32, self.y as i32)
1182    }
1183
1184    /// Casts all elements of `self` to `u32`.
1185    #[cfg(feature = "u32")]
1186    #[inline]
1187    #[must_use]
1188    pub fn as_uvec2(self) -> crate::UVec2 {
1189        crate::UVec2::new(self.x as u32, self.y as u32)
1190    }
1191
1192    /// Casts all elements of `self` to `i64`.
1193    #[cfg(feature = "i64")]
1194    #[inline]
1195    #[must_use]
1196    pub fn as_i64vec2(self) -> crate::I64Vec2 {
1197        crate::I64Vec2::new(self.x as i64, self.y as i64)
1198    }
1199
1200    /// Casts all elements of `self` to `u64`.
1201    #[cfg(feature = "u64")]
1202    #[inline]
1203    #[must_use]
1204    pub fn as_u64vec2(self) -> crate::U64Vec2 {
1205        crate::U64Vec2::new(self.x as u64, self.y as u64)
1206    }
1207
1208    /// Casts all elements of `self` to `isize`.
1209    #[cfg(feature = "isize")]
1210    #[inline]
1211    #[must_use]
1212    pub fn as_isizevec2(self) -> crate::ISizeVec2 {
1213        crate::ISizeVec2::new(self.x as isize, self.y as isize)
1214    }
1215
1216    /// Casts all elements of `self` to `usize`.
1217    #[cfg(feature = "usize")]
1218    #[inline]
1219    #[must_use]
1220    pub fn as_usizevec2(self) -> crate::USizeVec2 {
1221        crate::USizeVec2::new(self.x as usize, self.y as usize)
1222    }
1223}
1224
1225impl Default for Vec2 {
1226    #[inline(always)]
1227    fn default() -> Self {
1228        Self::ZERO
1229    }
1230}
1231
1232impl Div for Vec2 {
1233    type Output = Self;
1234    #[inline]
1235    fn div(self, rhs: Self) -> Self {
1236        Self::new(self.x.div(rhs.x), self.y.div(rhs.y))
1237    }
1238}
1239
1240impl Div<&Self> for Vec2 {
1241    type Output = Self;
1242    #[inline]
1243    fn div(self, rhs: &Self) -> Self {
1244        self.div(*rhs)
1245    }
1246}
1247
1248impl Div<&Vec2> for &Vec2 {
1249    type Output = Vec2;
1250    #[inline]
1251    fn div(self, rhs: &Vec2) -> Vec2 {
1252        (*self).div(*rhs)
1253    }
1254}
1255
1256impl Div<Vec2> for &Vec2 {
1257    type Output = Vec2;
1258    #[inline]
1259    fn div(self, rhs: Vec2) -> Vec2 {
1260        (*self).div(rhs)
1261    }
1262}
1263
1264impl DivAssign for Vec2 {
1265    #[inline]
1266    fn div_assign(&mut self, rhs: Self) {
1267        self.x.div_assign(rhs.x);
1268        self.y.div_assign(rhs.y);
1269    }
1270}
1271
1272impl DivAssign<&Self> for Vec2 {
1273    #[inline]
1274    fn div_assign(&mut self, rhs: &Self) {
1275        self.div_assign(*rhs);
1276    }
1277}
1278
1279impl Div<f32> for Vec2 {
1280    type Output = Self;
1281    #[inline]
1282    fn div(self, rhs: f32) -> Self {
1283        Self::new(self.x.div(rhs), self.y.div(rhs))
1284    }
1285}
1286
1287impl Div<&f32> for Vec2 {
1288    type Output = Self;
1289    #[inline]
1290    fn div(self, rhs: &f32) -> Self {
1291        self.div(*rhs)
1292    }
1293}
1294
1295impl Div<&f32> for &Vec2 {
1296    type Output = Vec2;
1297    #[inline]
1298    fn div(self, rhs: &f32) -> Vec2 {
1299        (*self).div(*rhs)
1300    }
1301}
1302
1303impl Div<f32> for &Vec2 {
1304    type Output = Vec2;
1305    #[inline]
1306    fn div(self, rhs: f32) -> Vec2 {
1307        (*self).div(rhs)
1308    }
1309}
1310
1311impl DivAssign<f32> for Vec2 {
1312    #[inline]
1313    fn div_assign(&mut self, rhs: f32) {
1314        self.x.div_assign(rhs);
1315        self.y.div_assign(rhs);
1316    }
1317}
1318
1319impl DivAssign<&f32> for Vec2 {
1320    #[inline]
1321    fn div_assign(&mut self, rhs: &f32) {
1322        self.div_assign(*rhs);
1323    }
1324}
1325
1326impl Div<Vec2> for f32 {
1327    type Output = Vec2;
1328    #[inline]
1329    fn div(self, rhs: Vec2) -> Vec2 {
1330        Vec2::new(self.div(rhs.x), self.div(rhs.y))
1331    }
1332}
1333
1334impl Div<&Vec2> for f32 {
1335    type Output = Vec2;
1336    #[inline]
1337    fn div(self, rhs: &Vec2) -> Vec2 {
1338        self.div(*rhs)
1339    }
1340}
1341
1342impl Div<&Vec2> for &f32 {
1343    type Output = Vec2;
1344    #[inline]
1345    fn div(self, rhs: &Vec2) -> Vec2 {
1346        (*self).div(*rhs)
1347    }
1348}
1349
1350impl Div<Vec2> for &f32 {
1351    type Output = Vec2;
1352    #[inline]
1353    fn div(self, rhs: Vec2) -> Vec2 {
1354        (*self).div(rhs)
1355    }
1356}
1357
1358impl Mul for Vec2 {
1359    type Output = Self;
1360    #[inline]
1361    fn mul(self, rhs: Self) -> Self {
1362        Self::new(self.x.mul(rhs.x), self.y.mul(rhs.y))
1363    }
1364}
1365
1366impl Mul<&Self> for Vec2 {
1367    type Output = Self;
1368    #[inline]
1369    fn mul(self, rhs: &Self) -> Self {
1370        self.mul(*rhs)
1371    }
1372}
1373
1374impl Mul<&Vec2> for &Vec2 {
1375    type Output = Vec2;
1376    #[inline]
1377    fn mul(self, rhs: &Vec2) -> Vec2 {
1378        (*self).mul(*rhs)
1379    }
1380}
1381
1382impl Mul<Vec2> for &Vec2 {
1383    type Output = Vec2;
1384    #[inline]
1385    fn mul(self, rhs: Vec2) -> Vec2 {
1386        (*self).mul(rhs)
1387    }
1388}
1389
1390impl MulAssign for Vec2 {
1391    #[inline]
1392    fn mul_assign(&mut self, rhs: Self) {
1393        self.x.mul_assign(rhs.x);
1394        self.y.mul_assign(rhs.y);
1395    }
1396}
1397
1398impl MulAssign<&Self> for Vec2 {
1399    #[inline]
1400    fn mul_assign(&mut self, rhs: &Self) {
1401        self.mul_assign(*rhs);
1402    }
1403}
1404
1405impl Mul<f32> for Vec2 {
1406    type Output = Self;
1407    #[inline]
1408    fn mul(self, rhs: f32) -> Self {
1409        Self::new(self.x.mul(rhs), self.y.mul(rhs))
1410    }
1411}
1412
1413impl Mul<&f32> for Vec2 {
1414    type Output = Self;
1415    #[inline]
1416    fn mul(self, rhs: &f32) -> Self {
1417        self.mul(*rhs)
1418    }
1419}
1420
1421impl Mul<&f32> for &Vec2 {
1422    type Output = Vec2;
1423    #[inline]
1424    fn mul(self, rhs: &f32) -> Vec2 {
1425        (*self).mul(*rhs)
1426    }
1427}
1428
1429impl Mul<f32> for &Vec2 {
1430    type Output = Vec2;
1431    #[inline]
1432    fn mul(self, rhs: f32) -> Vec2 {
1433        (*self).mul(rhs)
1434    }
1435}
1436
1437impl MulAssign<f32> for Vec2 {
1438    #[inline]
1439    fn mul_assign(&mut self, rhs: f32) {
1440        self.x.mul_assign(rhs);
1441        self.y.mul_assign(rhs);
1442    }
1443}
1444
1445impl MulAssign<&f32> for Vec2 {
1446    #[inline]
1447    fn mul_assign(&mut self, rhs: &f32) {
1448        self.mul_assign(*rhs);
1449    }
1450}
1451
1452impl Mul<Vec2> for f32 {
1453    type Output = Vec2;
1454    #[inline]
1455    fn mul(self, rhs: Vec2) -> Vec2 {
1456        Vec2::new(self.mul(rhs.x), self.mul(rhs.y))
1457    }
1458}
1459
1460impl Mul<&Vec2> for f32 {
1461    type Output = Vec2;
1462    #[inline]
1463    fn mul(self, rhs: &Vec2) -> Vec2 {
1464        self.mul(*rhs)
1465    }
1466}
1467
1468impl Mul<&Vec2> for &f32 {
1469    type Output = Vec2;
1470    #[inline]
1471    fn mul(self, rhs: &Vec2) -> Vec2 {
1472        (*self).mul(*rhs)
1473    }
1474}
1475
1476impl Mul<Vec2> for &f32 {
1477    type Output = Vec2;
1478    #[inline]
1479    fn mul(self, rhs: Vec2) -> Vec2 {
1480        (*self).mul(rhs)
1481    }
1482}
1483
1484impl Add for Vec2 {
1485    type Output = Self;
1486    #[inline]
1487    fn add(self, rhs: Self) -> Self {
1488        Self::new(self.x.add(rhs.x), self.y.add(rhs.y))
1489    }
1490}
1491
1492impl Add<&Self> for Vec2 {
1493    type Output = Self;
1494    #[inline]
1495    fn add(self, rhs: &Self) -> Self {
1496        self.add(*rhs)
1497    }
1498}
1499
1500impl Add<&Vec2> for &Vec2 {
1501    type Output = Vec2;
1502    #[inline]
1503    fn add(self, rhs: &Vec2) -> Vec2 {
1504        (*self).add(*rhs)
1505    }
1506}
1507
1508impl Add<Vec2> for &Vec2 {
1509    type Output = Vec2;
1510    #[inline]
1511    fn add(self, rhs: Vec2) -> Vec2 {
1512        (*self).add(rhs)
1513    }
1514}
1515
1516impl AddAssign for Vec2 {
1517    #[inline]
1518    fn add_assign(&mut self, rhs: Self) {
1519        self.x.add_assign(rhs.x);
1520        self.y.add_assign(rhs.y);
1521    }
1522}
1523
1524impl AddAssign<&Self> for Vec2 {
1525    #[inline]
1526    fn add_assign(&mut self, rhs: &Self) {
1527        self.add_assign(*rhs);
1528    }
1529}
1530
1531impl Add<f32> for Vec2 {
1532    type Output = Self;
1533    #[inline]
1534    fn add(self, rhs: f32) -> Self {
1535        Self::new(self.x.add(rhs), self.y.add(rhs))
1536    }
1537}
1538
1539impl Add<&f32> for Vec2 {
1540    type Output = Self;
1541    #[inline]
1542    fn add(self, rhs: &f32) -> Self {
1543        self.add(*rhs)
1544    }
1545}
1546
1547impl Add<&f32> for &Vec2 {
1548    type Output = Vec2;
1549    #[inline]
1550    fn add(self, rhs: &f32) -> Vec2 {
1551        (*self).add(*rhs)
1552    }
1553}
1554
1555impl Add<f32> for &Vec2 {
1556    type Output = Vec2;
1557    #[inline]
1558    fn add(self, rhs: f32) -> Vec2 {
1559        (*self).add(rhs)
1560    }
1561}
1562
1563impl AddAssign<f32> for Vec2 {
1564    #[inline]
1565    fn add_assign(&mut self, rhs: f32) {
1566        self.x.add_assign(rhs);
1567        self.y.add_assign(rhs);
1568    }
1569}
1570
1571impl AddAssign<&f32> for Vec2 {
1572    #[inline]
1573    fn add_assign(&mut self, rhs: &f32) {
1574        self.add_assign(*rhs);
1575    }
1576}
1577
1578impl Add<Vec2> for f32 {
1579    type Output = Vec2;
1580    #[inline]
1581    fn add(self, rhs: Vec2) -> Vec2 {
1582        Vec2::new(self.add(rhs.x), self.add(rhs.y))
1583    }
1584}
1585
1586impl Add<&Vec2> for f32 {
1587    type Output = Vec2;
1588    #[inline]
1589    fn add(self, rhs: &Vec2) -> Vec2 {
1590        self.add(*rhs)
1591    }
1592}
1593
1594impl Add<&Vec2> for &f32 {
1595    type Output = Vec2;
1596    #[inline]
1597    fn add(self, rhs: &Vec2) -> Vec2 {
1598        (*self).add(*rhs)
1599    }
1600}
1601
1602impl Add<Vec2> for &f32 {
1603    type Output = Vec2;
1604    #[inline]
1605    fn add(self, rhs: Vec2) -> Vec2 {
1606        (*self).add(rhs)
1607    }
1608}
1609
1610impl Sub for Vec2 {
1611    type Output = Self;
1612    #[inline]
1613    fn sub(self, rhs: Self) -> Self {
1614        Self::new(self.x.sub(rhs.x), self.y.sub(rhs.y))
1615    }
1616}
1617
1618impl Sub<&Self> for Vec2 {
1619    type Output = Self;
1620    #[inline]
1621    fn sub(self, rhs: &Self) -> Self {
1622        self.sub(*rhs)
1623    }
1624}
1625
1626impl Sub<&Vec2> for &Vec2 {
1627    type Output = Vec2;
1628    #[inline]
1629    fn sub(self, rhs: &Vec2) -> Vec2 {
1630        (*self).sub(*rhs)
1631    }
1632}
1633
1634impl Sub<Vec2> for &Vec2 {
1635    type Output = Vec2;
1636    #[inline]
1637    fn sub(self, rhs: Vec2) -> Vec2 {
1638        (*self).sub(rhs)
1639    }
1640}
1641
1642impl SubAssign for Vec2 {
1643    #[inline]
1644    fn sub_assign(&mut self, rhs: Self) {
1645        self.x.sub_assign(rhs.x);
1646        self.y.sub_assign(rhs.y);
1647    }
1648}
1649
1650impl SubAssign<&Self> for Vec2 {
1651    #[inline]
1652    fn sub_assign(&mut self, rhs: &Self) {
1653        self.sub_assign(*rhs);
1654    }
1655}
1656
1657impl Sub<f32> for Vec2 {
1658    type Output = Self;
1659    #[inline]
1660    fn sub(self, rhs: f32) -> Self {
1661        Self::new(self.x.sub(rhs), self.y.sub(rhs))
1662    }
1663}
1664
1665impl Sub<&f32> for Vec2 {
1666    type Output = Self;
1667    #[inline]
1668    fn sub(self, rhs: &f32) -> Self {
1669        self.sub(*rhs)
1670    }
1671}
1672
1673impl Sub<&f32> for &Vec2 {
1674    type Output = Vec2;
1675    #[inline]
1676    fn sub(self, rhs: &f32) -> Vec2 {
1677        (*self).sub(*rhs)
1678    }
1679}
1680
1681impl Sub<f32> for &Vec2 {
1682    type Output = Vec2;
1683    #[inline]
1684    fn sub(self, rhs: f32) -> Vec2 {
1685        (*self).sub(rhs)
1686    }
1687}
1688
1689impl SubAssign<f32> for Vec2 {
1690    #[inline]
1691    fn sub_assign(&mut self, rhs: f32) {
1692        self.x.sub_assign(rhs);
1693        self.y.sub_assign(rhs);
1694    }
1695}
1696
1697impl SubAssign<&f32> for Vec2 {
1698    #[inline]
1699    fn sub_assign(&mut self, rhs: &f32) {
1700        self.sub_assign(*rhs);
1701    }
1702}
1703
1704impl Sub<Vec2> for f32 {
1705    type Output = Vec2;
1706    #[inline]
1707    fn sub(self, rhs: Vec2) -> Vec2 {
1708        Vec2::new(self.sub(rhs.x), self.sub(rhs.y))
1709    }
1710}
1711
1712impl Sub<&Vec2> for f32 {
1713    type Output = Vec2;
1714    #[inline]
1715    fn sub(self, rhs: &Vec2) -> Vec2 {
1716        self.sub(*rhs)
1717    }
1718}
1719
1720impl Sub<&Vec2> for &f32 {
1721    type Output = Vec2;
1722    #[inline]
1723    fn sub(self, rhs: &Vec2) -> Vec2 {
1724        (*self).sub(*rhs)
1725    }
1726}
1727
1728impl Sub<Vec2> for &f32 {
1729    type Output = Vec2;
1730    #[inline]
1731    fn sub(self, rhs: Vec2) -> Vec2 {
1732        (*self).sub(rhs)
1733    }
1734}
1735
1736impl Rem for Vec2 {
1737    type Output = Self;
1738    #[inline]
1739    fn rem(self, rhs: Self) -> Self {
1740        Self::new(self.x.rem(rhs.x), self.y.rem(rhs.y))
1741    }
1742}
1743
1744impl Rem<&Self> for Vec2 {
1745    type Output = Self;
1746    #[inline]
1747    fn rem(self, rhs: &Self) -> Self {
1748        self.rem(*rhs)
1749    }
1750}
1751
1752impl Rem<&Vec2> for &Vec2 {
1753    type Output = Vec2;
1754    #[inline]
1755    fn rem(self, rhs: &Vec2) -> Vec2 {
1756        (*self).rem(*rhs)
1757    }
1758}
1759
1760impl Rem<Vec2> for &Vec2 {
1761    type Output = Vec2;
1762    #[inline]
1763    fn rem(self, rhs: Vec2) -> Vec2 {
1764        (*self).rem(rhs)
1765    }
1766}
1767
1768impl RemAssign for Vec2 {
1769    #[inline]
1770    fn rem_assign(&mut self, rhs: Self) {
1771        self.x.rem_assign(rhs.x);
1772        self.y.rem_assign(rhs.y);
1773    }
1774}
1775
1776impl RemAssign<&Self> for Vec2 {
1777    #[inline]
1778    fn rem_assign(&mut self, rhs: &Self) {
1779        self.rem_assign(*rhs);
1780    }
1781}
1782
1783impl Rem<f32> for Vec2 {
1784    type Output = Self;
1785    #[inline]
1786    fn rem(self, rhs: f32) -> Self {
1787        Self::new(self.x.rem(rhs), self.y.rem(rhs))
1788    }
1789}
1790
1791impl Rem<&f32> for Vec2 {
1792    type Output = Self;
1793    #[inline]
1794    fn rem(self, rhs: &f32) -> Self {
1795        self.rem(*rhs)
1796    }
1797}
1798
1799impl Rem<&f32> for &Vec2 {
1800    type Output = Vec2;
1801    #[inline]
1802    fn rem(self, rhs: &f32) -> Vec2 {
1803        (*self).rem(*rhs)
1804    }
1805}
1806
1807impl Rem<f32> for &Vec2 {
1808    type Output = Vec2;
1809    #[inline]
1810    fn rem(self, rhs: f32) -> Vec2 {
1811        (*self).rem(rhs)
1812    }
1813}
1814
1815impl RemAssign<f32> for Vec2 {
1816    #[inline]
1817    fn rem_assign(&mut self, rhs: f32) {
1818        self.x.rem_assign(rhs);
1819        self.y.rem_assign(rhs);
1820    }
1821}
1822
1823impl RemAssign<&f32> for Vec2 {
1824    #[inline]
1825    fn rem_assign(&mut self, rhs: &f32) {
1826        self.rem_assign(*rhs);
1827    }
1828}
1829
1830impl Rem<Vec2> for f32 {
1831    type Output = Vec2;
1832    #[inline]
1833    fn rem(self, rhs: Vec2) -> Vec2 {
1834        Vec2::new(self.rem(rhs.x), self.rem(rhs.y))
1835    }
1836}
1837
1838impl Rem<&Vec2> for f32 {
1839    type Output = Vec2;
1840    #[inline]
1841    fn rem(self, rhs: &Vec2) -> Vec2 {
1842        self.rem(*rhs)
1843    }
1844}
1845
1846impl Rem<&Vec2> for &f32 {
1847    type Output = Vec2;
1848    #[inline]
1849    fn rem(self, rhs: &Vec2) -> Vec2 {
1850        (*self).rem(*rhs)
1851    }
1852}
1853
1854impl Rem<Vec2> for &f32 {
1855    type Output = Vec2;
1856    #[inline]
1857    fn rem(self, rhs: Vec2) -> Vec2 {
1858        (*self).rem(rhs)
1859    }
1860}
1861
1862impl AsRef<[f32; 2]> for Vec2 {
1863    #[inline]
1864    fn as_ref(&self) -> &[f32; 2] {
1865        unsafe { &*(self as *const Self as *const [f32; 2]) }
1866    }
1867}
1868
1869impl AsMut<[f32; 2]> for Vec2 {
1870    #[inline]
1871    fn as_mut(&mut self) -> &mut [f32; 2] {
1872        unsafe { &mut *(self as *mut Self as *mut [f32; 2]) }
1873    }
1874}
1875
1876impl Sum for Vec2 {
1877    #[inline]
1878    fn sum<I>(iter: I) -> Self
1879    where
1880        I: Iterator<Item = Self>,
1881    {
1882        iter.fold(Self::ZERO, Self::add)
1883    }
1884}
1885
1886impl<'a> Sum<&'a Self> for Vec2 {
1887    #[inline]
1888    fn sum<I>(iter: I) -> Self
1889    where
1890        I: Iterator<Item = &'a Self>,
1891    {
1892        iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1893    }
1894}
1895
1896impl Product for Vec2 {
1897    #[inline]
1898    fn product<I>(iter: I) -> Self
1899    where
1900        I: Iterator<Item = Self>,
1901    {
1902        iter.fold(Self::ONE, Self::mul)
1903    }
1904}
1905
1906impl<'a> Product<&'a Self> for Vec2 {
1907    #[inline]
1908    fn product<I>(iter: I) -> Self
1909    where
1910        I: Iterator<Item = &'a Self>,
1911    {
1912        iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1913    }
1914}
1915
1916impl Neg for Vec2 {
1917    type Output = Self;
1918    #[inline]
1919    fn neg(self) -> Self {
1920        Self::new(self.x.neg(), self.y.neg())
1921    }
1922}
1923
1924impl Neg for &Vec2 {
1925    type Output = Vec2;
1926    #[inline]
1927    fn neg(self) -> Vec2 {
1928        (*self).neg()
1929    }
1930}
1931
1932impl Index<usize> for Vec2 {
1933    type Output = f32;
1934    #[inline]
1935    fn index(&self, index: usize) -> &Self::Output {
1936        match index {
1937            0 => &self.x,
1938            1 => &self.y,
1939            _ => panic!("index out of bounds"),
1940        }
1941    }
1942}
1943
1944impl IndexMut<usize> for Vec2 {
1945    #[inline]
1946    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1947        match index {
1948            0 => &mut self.x,
1949            1 => &mut self.y,
1950            _ => panic!("index out of bounds"),
1951        }
1952    }
1953}
1954
1955impl fmt::Display for Vec2 {
1956    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1957        if let Some(p) = f.precision() {
1958            write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y)
1959        } else {
1960            write!(f, "[{}, {}]", self.x, self.y)
1961        }
1962    }
1963}
1964
1965impl fmt::Debug for Vec2 {
1966    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1967        fmt.debug_tuple(stringify!(Vec2))
1968            .field(&self.x)
1969            .field(&self.y)
1970            .finish()
1971    }
1972}
1973
1974impl From<[f32; 2]> for Vec2 {
1975    #[inline]
1976    fn from(a: [f32; 2]) -> Self {
1977        Self::new(a[0], a[1])
1978    }
1979}
1980
1981impl From<Vec2> for [f32; 2] {
1982    #[inline]
1983    fn from(v: Vec2) -> Self {
1984        [v.x, v.y]
1985    }
1986}
1987
1988impl From<(f32, f32)> for Vec2 {
1989    #[inline]
1990    fn from(t: (f32, f32)) -> Self {
1991        Self::new(t.0, t.1)
1992    }
1993}
1994
1995impl From<Vec2> for (f32, f32) {
1996    #[inline]
1997    fn from(v: Vec2) -> Self {
1998        (v.x, v.y)
1999    }
2000}
2001
2002impl From<BVec2> for Vec2 {
2003    #[inline]
2004    fn from(v: BVec2) -> Self {
2005        Self::new(f32::from(v.x), f32::from(v.y))
2006    }
2007}