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