[go: up one dir, main page]

glam/f32/
vec2.rs

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