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