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