Skip to main content

glam/f32/
vec2.rs

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