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