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