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