glam/f64/
dvec2.rs

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