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