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