Skip to main content

glam/f64/
dvec2.rs

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