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