[go: up one dir, main page]

glam/f32/
vec2.rs

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