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