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