Skip to main content

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