1use crate::{f32::math, BVec2, Vec3};
4
5use core::fmt;
6use core::iter::{Product, Sum};
7use core::{f32, ops::*};
8
9#[inline(always)]
11#[must_use]
12pub const fn vec2(x: f32, y: f32) -> Vec2 {
13    Vec2::new(x, y)
14}
15
16#[derive(Clone, Copy, PartialEq)]
18#[cfg_attr(feature = "cuda", repr(align(8)))]
19#[cfg_attr(not(target_arch = "spirv"), repr(C))]
20#[cfg_attr(target_arch = "spirv", repr(simd))]
21pub struct Vec2 {
22    pub x: f32,
23    pub y: f32,
24}
25
26impl Vec2 {
27    pub const ZERO: Self = Self::splat(0.0);
29
30    pub const ONE: Self = Self::splat(1.0);
32
33    pub const NEG_ONE: Self = Self::splat(-1.0);
35
36    pub const MIN: Self = Self::splat(f32::MIN);
38
39    pub const MAX: Self = Self::splat(f32::MAX);
41
42    pub const NAN: Self = Self::splat(f32::NAN);
44
45    pub const INFINITY: Self = Self::splat(f32::INFINITY);
47
48    pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY);
50
51    pub const X: Self = Self::new(1.0, 0.0);
53
54    pub const Y: Self = Self::new(0.0, 1.0);
56
57    pub const NEG_X: Self = Self::new(-1.0, 0.0);
59
60    pub const NEG_Y: Self = Self::new(0.0, -1.0);
62
63    pub const AXES: [Self; 2] = [Self::X, Self::Y];
65
66    #[inline(always)]
68    #[must_use]
69    pub const fn new(x: f32, y: f32) -> Self {
70        Self { x, y }
71    }
72
73    #[inline]
75    #[must_use]
76    pub const fn splat(v: f32) -> Self {
77        Self { x: v, y: v }
78    }
79
80    #[inline]
82    #[must_use]
83    pub fn map<F>(self, f: F) -> Self
84    where
85        F: Fn(f32) -> f32,
86    {
87        Self::new(f(self.x), f(self.y))
88    }
89
90    #[inline]
96    #[must_use]
97    pub fn select(mask: BVec2, if_true: Self, if_false: Self) -> Self {
98        Self {
99            x: if mask.test(0) { if_true.x } else { if_false.x },
100            y: if mask.test(1) { if_true.y } else { if_false.y },
101        }
102    }
103
104    #[inline]
106    #[must_use]
107    pub const fn from_array(a: [f32; 2]) -> Self {
108        Self::new(a[0], a[1])
109    }
110
111    #[inline]
113    #[must_use]
114    pub const fn to_array(&self) -> [f32; 2] {
115        [self.x, self.y]
116    }
117
118    #[inline]
124    #[must_use]
125    pub const fn from_slice(slice: &[f32]) -> Self {
126        assert!(slice.len() >= 2);
127        Self::new(slice[0], slice[1])
128    }
129
130    #[inline]
136    pub fn write_to_slice(self, slice: &mut [f32]) {
137        slice[..2].copy_from_slice(&self.to_array());
138    }
139
140    #[inline]
142    #[must_use]
143    pub const fn extend(self, z: f32) -> Vec3 {
144        Vec3::new(self.x, self.y, z)
145    }
146
147    #[inline]
149    #[must_use]
150    pub fn with_x(mut self, x: f32) -> Self {
151        self.x = x;
152        self
153    }
154
155    #[inline]
157    #[must_use]
158    pub fn with_y(mut self, y: f32) -> Self {
159        self.y = y;
160        self
161    }
162
163    #[inline]
165    #[must_use]
166    pub fn dot(self, rhs: Self) -> f32 {
167        (self.x * rhs.x) + (self.y * rhs.y)
168    }
169
170    #[inline]
172    #[must_use]
173    pub fn dot_into_vec(self, rhs: Self) -> Self {
174        Self::splat(self.dot(rhs))
175    }
176
177    #[inline]
181    #[must_use]
182    pub fn min(self, rhs: Self) -> Self {
183        Self {
184            x: self.x.min(rhs.x),
185            y: self.y.min(rhs.y),
186        }
187    }
188
189    #[inline]
193    #[must_use]
194    pub fn max(self, rhs: Self) -> Self {
195        Self {
196            x: self.x.max(rhs.x),
197            y: self.y.max(rhs.y),
198        }
199    }
200
201    #[inline]
209    #[must_use]
210    pub fn clamp(self, min: Self, max: Self) -> Self {
211        glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
212        self.max(min).min(max)
213    }
214
215    #[inline]
219    #[must_use]
220    pub fn min_element(self) -> f32 {
221        self.x.min(self.y)
222    }
223
224    #[inline]
228    #[must_use]
229    pub fn max_element(self) -> f32 {
230        self.x.max(self.y)
231    }
232
233    #[inline]
237    #[must_use]
238    pub fn element_sum(self) -> f32 {
239        self.x + self.y
240    }
241
242    #[inline]
246    #[must_use]
247    pub fn element_product(self) -> f32 {
248        self.x * self.y
249    }
250
251    #[inline]
257    #[must_use]
258    pub fn cmpeq(self, rhs: Self) -> BVec2 {
259        BVec2::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y))
260    }
261
262    #[inline]
268    #[must_use]
269    pub fn cmpne(self, rhs: Self) -> BVec2 {
270        BVec2::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y))
271    }
272
273    #[inline]
279    #[must_use]
280    pub fn cmpge(self, rhs: Self) -> BVec2 {
281        BVec2::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y))
282    }
283
284    #[inline]
290    #[must_use]
291    pub fn cmpgt(self, rhs: Self) -> BVec2 {
292        BVec2::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y))
293    }
294
295    #[inline]
301    #[must_use]
302    pub fn cmple(self, rhs: Self) -> BVec2 {
303        BVec2::new(self.x.le(&rhs.x), self.y.le(&rhs.y))
304    }
305
306    #[inline]
312    #[must_use]
313    pub fn cmplt(self, rhs: Self) -> BVec2 {
314        BVec2::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y))
315    }
316
317    #[inline]
319    #[must_use]
320    pub fn abs(self) -> Self {
321        Self {
322            x: math::abs(self.x),
323            y: math::abs(self.y),
324        }
325    }
326
327    #[inline]
333    #[must_use]
334    pub fn signum(self) -> Self {
335        Self {
336            x: math::signum(self.x),
337            y: math::signum(self.y),
338        }
339    }
340
341    #[inline]
343    #[must_use]
344    pub fn copysign(self, rhs: Self) -> Self {
345        Self {
346            x: math::copysign(self.x, rhs.x),
347            y: math::copysign(self.y, rhs.y),
348        }
349    }
350
351    #[inline]
356    #[must_use]
357    pub fn is_negative_bitmask(self) -> u32 {
358        (self.x.is_sign_negative() as u32) | (self.y.is_sign_negative() as u32) << 1
359    }
360
361    #[inline]
364    #[must_use]
365    pub fn is_finite(self) -> bool {
366        self.x.is_finite() && self.y.is_finite()
367    }
368
369    pub fn is_finite_mask(self) -> BVec2 {
373        BVec2::new(self.x.is_finite(), self.y.is_finite())
374    }
375
376    #[inline]
378    #[must_use]
379    pub fn is_nan(self) -> bool {
380        self.x.is_nan() || self.y.is_nan()
381    }
382
383    #[inline]
387    #[must_use]
388    pub fn is_nan_mask(self) -> BVec2 {
389        BVec2::new(self.x.is_nan(), self.y.is_nan())
390    }
391
392    #[doc(alias = "magnitude")]
394    #[inline]
395    #[must_use]
396    pub fn length(self) -> f32 {
397        math::sqrt(self.dot(self))
398    }
399
400    #[doc(alias = "magnitude2")]
404    #[inline]
405    #[must_use]
406    pub fn length_squared(self) -> f32 {
407        self.dot(self)
408    }
409
410    #[inline]
414    #[must_use]
415    pub fn length_recip(self) -> f32 {
416        self.length().recip()
417    }
418
419    #[inline]
421    #[must_use]
422    pub fn distance(self, rhs: Self) -> f32 {
423        (self - rhs).length()
424    }
425
426    #[inline]
428    #[must_use]
429    pub fn distance_squared(self, rhs: Self) -> f32 {
430        (self - rhs).length_squared()
431    }
432
433    #[inline]
435    #[must_use]
436    pub fn div_euclid(self, rhs: Self) -> Self {
437        Self::new(
438            math::div_euclid(self.x, rhs.x),
439            math::div_euclid(self.y, rhs.y),
440        )
441    }
442
443    #[inline]
447    #[must_use]
448    pub fn rem_euclid(self, rhs: Self) -> Self {
449        Self::new(
450            math::rem_euclid(self.x, rhs.x),
451            math::rem_euclid(self.y, rhs.y),
452        )
453    }
454
455    #[inline]
465    #[must_use]
466    pub fn normalize(self) -> Self {
467        #[allow(clippy::let_and_return)]
468        let normalized = self.mul(self.length_recip());
469        glam_assert!(normalized.is_finite());
470        normalized
471    }
472
473    #[inline]
480    #[must_use]
481    pub fn try_normalize(self) -> Option<Self> {
482        let rcp = self.length_recip();
483        if rcp.is_finite() && rcp > 0.0 {
484            Some(self * rcp)
485        } else {
486            None
487        }
488    }
489
490    #[inline]
498    #[must_use]
499    pub fn normalize_or(self, fallback: Self) -> Self {
500        let rcp = self.length_recip();
501        if rcp.is_finite() && rcp > 0.0 {
502            self * rcp
503        } else {
504            fallback
505        }
506    }
507
508    #[inline]
515    #[must_use]
516    pub fn normalize_or_zero(self) -> Self {
517        self.normalize_or(Self::ZERO)
518    }
519
520    #[inline]
524    #[must_use]
525    pub fn is_normalized(self) -> bool {
526        math::abs(self.length_squared() - 1.0) <= 2e-4
527    }
528
529    #[inline]
537    #[must_use]
538    pub fn project_onto(self, rhs: Self) -> Self {
539        let other_len_sq_rcp = rhs.dot(rhs).recip();
540        glam_assert!(other_len_sq_rcp.is_finite());
541        rhs * self.dot(rhs) * other_len_sq_rcp
542    }
543
544    #[doc(alias("plane"))]
555    #[inline]
556    #[must_use]
557    pub fn reject_from(self, rhs: Self) -> Self {
558        self - self.project_onto(rhs)
559    }
560
561    #[inline]
569    #[must_use]
570    pub fn project_onto_normalized(self, rhs: Self) -> Self {
571        glam_assert!(rhs.is_normalized());
572        rhs * self.dot(rhs)
573    }
574
575    #[doc(alias("plane"))]
586    #[inline]
587    #[must_use]
588    pub fn reject_from_normalized(self, rhs: Self) -> Self {
589        self - self.project_onto_normalized(rhs)
590    }
591
592    #[inline]
595    #[must_use]
596    pub fn round(self) -> Self {
597        Self {
598            x: math::round(self.x),
599            y: math::round(self.y),
600        }
601    }
602
603    #[inline]
606    #[must_use]
607    pub fn floor(self) -> Self {
608        Self {
609            x: math::floor(self.x),
610            y: math::floor(self.y),
611        }
612    }
613
614    #[inline]
617    #[must_use]
618    pub fn ceil(self) -> Self {
619        Self {
620            x: math::ceil(self.x),
621            y: math::ceil(self.y),
622        }
623    }
624
625    #[inline]
628    #[must_use]
629    pub fn trunc(self) -> Self {
630        Self {
631            x: math::trunc(self.x),
632            y: math::trunc(self.y),
633        }
634    }
635
636    #[inline]
643    #[must_use]
644    pub fn fract(self) -> Self {
645        self - self.trunc()
646    }
647
648    #[inline]
655    #[must_use]
656    pub fn fract_gl(self) -> Self {
657        self - self.floor()
658    }
659
660    #[inline]
663    #[must_use]
664    pub fn exp(self) -> Self {
665        Self::new(math::exp(self.x), math::exp(self.y))
666    }
667
668    #[inline]
670    #[must_use]
671    pub fn powf(self, n: f32) -> Self {
672        Self::new(math::powf(self.x, n), math::powf(self.y, n))
673    }
674
675    #[inline]
677    #[must_use]
678    pub fn recip(self) -> Self {
679        Self {
680            x: 1.0 / self.x,
681            y: 1.0 / self.y,
682        }
683    }
684
685    #[doc(alias = "mix")]
691    #[inline]
692    #[must_use]
693    pub fn lerp(self, rhs: Self, s: f32) -> Self {
694        self * (1.0 - s) + rhs * s
695    }
696
697    #[inline]
702    #[must_use]
703    pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
704        let a = rhs - *self;
705        let len = a.length();
706        if len <= d || len <= 1e-4 {
707            return rhs;
708        }
709        *self + a / len * d
710    }
711
712    #[inline]
718    pub fn midpoint(self, rhs: Self) -> Self {
719        (self + rhs) * 0.5
720    }
721
722    #[inline]
732    #[must_use]
733    pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
734        self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
735    }
736
737    #[inline]
743    #[must_use]
744    pub fn clamp_length(self, min: f32, max: f32) -> Self {
745        glam_assert!(0.0 <= min);
746        glam_assert!(min <= max);
747        let length_sq = self.length_squared();
748        if length_sq < min * min {
749            min * (self / math::sqrt(length_sq))
750        } else if length_sq > max * max {
751            max * (self / math::sqrt(length_sq))
752        } else {
753            self
754        }
755    }
756
757    #[inline]
763    #[must_use]
764    pub fn clamp_length_max(self, max: f32) -> Self {
765        glam_assert!(0.0 <= max);
766        let length_sq = self.length_squared();
767        if length_sq > max * max {
768            max * (self / math::sqrt(length_sq))
769        } else {
770            self
771        }
772    }
773
774    #[inline]
780    #[must_use]
781    pub fn clamp_length_min(self, min: f32) -> Self {
782        glam_assert!(0.0 <= min);
783        let length_sq = self.length_squared();
784        if length_sq < min * min {
785            min * (self / math::sqrt(length_sq))
786        } else {
787            self
788        }
789    }
790
791    #[inline]
799    #[must_use]
800    pub fn mul_add(self, a: Self, b: Self) -> Self {
801        Self::new(
802            math::mul_add(self.x, a.x, b.x),
803            math::mul_add(self.y, a.y, b.y),
804        )
805    }
806
807    #[inline]
816    #[must_use]
817    pub fn reflect(self, normal: Self) -> Self {
818        glam_assert!(normal.is_normalized());
819        self - 2.0 * self.dot(normal) * normal
820    }
821
822    #[inline]
832    #[must_use]
833    pub fn refract(self, normal: Self, eta: f32) -> Self {
834        glam_assert!(self.is_normalized());
835        glam_assert!(normal.is_normalized());
836        let n_dot_i = normal.dot(self);
837        let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
838        if k >= 0.0 {
839            eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
840        } else {
841            Self::ZERO
842        }
843    }
844
845    #[inline]
850    #[must_use]
851    pub fn from_angle(angle: f32) -> Self {
852        let (sin, cos) = math::sin_cos(angle);
853        Self { x: cos, y: sin }
854    }
855
856    #[inline]
860    #[must_use]
861    pub fn to_angle(self) -> f32 {
862        math::atan2(self.y, self.x)
863    }
864
865    #[inline]
866    #[must_use]
867    #[deprecated(
868        since = "0.27.0",
869        note = "Use angle_to() instead, the semantics of angle_between will change in the future."
870    )]
871    pub fn angle_between(self, rhs: Self) -> f32 {
872        self.angle_to(rhs)
873    }
874
875    #[inline]
879    #[must_use]
880    pub fn angle_to(self, rhs: Self) -> f32 {
881        let angle = math::acos_approx(
882            self.dot(rhs) / math::sqrt(self.length_squared() * rhs.length_squared()),
883        );
884
885        angle * math::signum(self.perp_dot(rhs))
886    }
887
888    #[inline]
890    #[must_use]
891    pub fn perp(self) -> Self {
892        Self {
893            x: -self.y,
894            y: self.x,
895        }
896    }
897
898    #[doc(alias = "wedge")]
901    #[doc(alias = "cross")]
902    #[doc(alias = "determinant")]
903    #[inline]
904    #[must_use]
905    pub fn perp_dot(self, rhs: Self) -> f32 {
906        (self.x * rhs.y) - (self.y * rhs.x)
907    }
908
909    #[inline]
913    #[must_use]
914    pub fn rotate(self, rhs: Self) -> Self {
915        Self {
916            x: self.x * rhs.x - self.y * rhs.y,
917            y: self.y * rhs.x + self.x * rhs.y,
918        }
919    }
920
921    #[inline]
927    #[must_use]
928    pub fn rotate_towards(&self, rhs: Self, max_angle: f32) -> Self {
929        let a = self.angle_to(rhs);
930        let abs_a = math::abs(a);
931        if abs_a <= 1e-4 {
932            return rhs;
933        }
934        let angle = max_angle.clamp(abs_a - core::f32::consts::PI, abs_a) * math::signum(a);
936        Self::from_angle(angle).rotate(*self)
937    }
938
939    #[inline]
941    #[must_use]
942    pub fn as_dvec2(&self) -> crate::DVec2 {
943        crate::DVec2::new(self.x as f64, self.y as f64)
944    }
945
946    #[inline]
948    #[must_use]
949    pub fn as_i8vec2(&self) -> crate::I8Vec2 {
950        crate::I8Vec2::new(self.x as i8, self.y as i8)
951    }
952
953    #[inline]
955    #[must_use]
956    pub fn as_u8vec2(&self) -> crate::U8Vec2 {
957        crate::U8Vec2::new(self.x as u8, self.y as u8)
958    }
959
960    #[inline]
962    #[must_use]
963    pub fn as_i16vec2(&self) -> crate::I16Vec2 {
964        crate::I16Vec2::new(self.x as i16, self.y as i16)
965    }
966
967    #[inline]
969    #[must_use]
970    pub fn as_u16vec2(&self) -> crate::U16Vec2 {
971        crate::U16Vec2::new(self.x as u16, self.y as u16)
972    }
973
974    #[inline]
976    #[must_use]
977    pub fn as_ivec2(&self) -> crate::IVec2 {
978        crate::IVec2::new(self.x as i32, self.y as i32)
979    }
980
981    #[inline]
983    #[must_use]
984    pub fn as_uvec2(&self) -> crate::UVec2 {
985        crate::UVec2::new(self.x as u32, self.y as u32)
986    }
987
988    #[inline]
990    #[must_use]
991    pub fn as_i64vec2(&self) -> crate::I64Vec2 {
992        crate::I64Vec2::new(self.x as i64, self.y as i64)
993    }
994
995    #[inline]
997    #[must_use]
998    pub fn as_u64vec2(&self) -> crate::U64Vec2 {
999        crate::U64Vec2::new(self.x as u64, self.y as u64)
1000    }
1001}
1002
1003impl Default for Vec2 {
1004    #[inline(always)]
1005    fn default() -> Self {
1006        Self::ZERO
1007    }
1008}
1009
1010impl Div<Vec2> for Vec2 {
1011    type Output = Self;
1012    #[inline]
1013    fn div(self, rhs: Self) -> Self {
1014        Self {
1015            x: self.x.div(rhs.x),
1016            y: self.y.div(rhs.y),
1017        }
1018    }
1019}
1020
1021impl Div<&Vec2> for Vec2 {
1022    type Output = Vec2;
1023    #[inline]
1024    fn div(self, rhs: &Vec2) -> Vec2 {
1025        self.div(*rhs)
1026    }
1027}
1028
1029impl Div<&Vec2> for &Vec2 {
1030    type Output = Vec2;
1031    #[inline]
1032    fn div(self, rhs: &Vec2) -> Vec2 {
1033        (*self).div(*rhs)
1034    }
1035}
1036
1037impl Div<Vec2> for &Vec2 {
1038    type Output = Vec2;
1039    #[inline]
1040    fn div(self, rhs: Vec2) -> Vec2 {
1041        (*self).div(rhs)
1042    }
1043}
1044
1045impl DivAssign<Vec2> for Vec2 {
1046    #[inline]
1047    fn div_assign(&mut self, rhs: Self) {
1048        self.x.div_assign(rhs.x);
1049        self.y.div_assign(rhs.y);
1050    }
1051}
1052
1053impl DivAssign<&Self> for Vec2 {
1054    #[inline]
1055    fn div_assign(&mut self, rhs: &Self) {
1056        self.div_assign(*rhs)
1057    }
1058}
1059
1060impl Div<f32> for Vec2 {
1061    type Output = Self;
1062    #[inline]
1063    fn div(self, rhs: f32) -> Self {
1064        Self {
1065            x: self.x.div(rhs),
1066            y: self.y.div(rhs),
1067        }
1068    }
1069}
1070
1071impl Div<&f32> for Vec2 {
1072    type Output = Vec2;
1073    #[inline]
1074    fn div(self, rhs: &f32) -> Vec2 {
1075        self.div(*rhs)
1076    }
1077}
1078
1079impl Div<&f32> for &Vec2 {
1080    type Output = Vec2;
1081    #[inline]
1082    fn div(self, rhs: &f32) -> Vec2 {
1083        (*self).div(*rhs)
1084    }
1085}
1086
1087impl Div<f32> for &Vec2 {
1088    type Output = Vec2;
1089    #[inline]
1090    fn div(self, rhs: f32) -> Vec2 {
1091        (*self).div(rhs)
1092    }
1093}
1094
1095impl DivAssign<f32> for Vec2 {
1096    #[inline]
1097    fn div_assign(&mut self, rhs: f32) {
1098        self.x.div_assign(rhs);
1099        self.y.div_assign(rhs);
1100    }
1101}
1102
1103impl DivAssign<&f32> for Vec2 {
1104    #[inline]
1105    fn div_assign(&mut self, rhs: &f32) {
1106        self.div_assign(*rhs)
1107    }
1108}
1109
1110impl Div<Vec2> for f32 {
1111    type Output = Vec2;
1112    #[inline]
1113    fn div(self, rhs: Vec2) -> Vec2 {
1114        Vec2 {
1115            x: self.div(rhs.x),
1116            y: self.div(rhs.y),
1117        }
1118    }
1119}
1120
1121impl Div<&Vec2> for f32 {
1122    type Output = Vec2;
1123    #[inline]
1124    fn div(self, rhs: &Vec2) -> Vec2 {
1125        self.div(*rhs)
1126    }
1127}
1128
1129impl Div<&Vec2> for &f32 {
1130    type Output = Vec2;
1131    #[inline]
1132    fn div(self, rhs: &Vec2) -> Vec2 {
1133        (*self).div(*rhs)
1134    }
1135}
1136
1137impl Div<Vec2> for &f32 {
1138    type Output = Vec2;
1139    #[inline]
1140    fn div(self, rhs: Vec2) -> Vec2 {
1141        (*self).div(rhs)
1142    }
1143}
1144
1145impl Mul<Vec2> for Vec2 {
1146    type Output = Self;
1147    #[inline]
1148    fn mul(self, rhs: Self) -> Self {
1149        Self {
1150            x: self.x.mul(rhs.x),
1151            y: self.y.mul(rhs.y),
1152        }
1153    }
1154}
1155
1156impl Mul<&Vec2> for Vec2 {
1157    type Output = Vec2;
1158    #[inline]
1159    fn mul(self, rhs: &Vec2) -> Vec2 {
1160        self.mul(*rhs)
1161    }
1162}
1163
1164impl Mul<&Vec2> for &Vec2 {
1165    type Output = Vec2;
1166    #[inline]
1167    fn mul(self, rhs: &Vec2) -> Vec2 {
1168        (*self).mul(*rhs)
1169    }
1170}
1171
1172impl Mul<Vec2> for &Vec2 {
1173    type Output = Vec2;
1174    #[inline]
1175    fn mul(self, rhs: Vec2) -> Vec2 {
1176        (*self).mul(rhs)
1177    }
1178}
1179
1180impl MulAssign<Vec2> for Vec2 {
1181    #[inline]
1182    fn mul_assign(&mut self, rhs: Self) {
1183        self.x.mul_assign(rhs.x);
1184        self.y.mul_assign(rhs.y);
1185    }
1186}
1187
1188impl MulAssign<&Self> for Vec2 {
1189    #[inline]
1190    fn mul_assign(&mut self, rhs: &Self) {
1191        self.mul_assign(*rhs)
1192    }
1193}
1194
1195impl Mul<f32> for Vec2 {
1196    type Output = Self;
1197    #[inline]
1198    fn mul(self, rhs: f32) -> Self {
1199        Self {
1200            x: self.x.mul(rhs),
1201            y: self.y.mul(rhs),
1202        }
1203    }
1204}
1205
1206impl Mul<&f32> for Vec2 {
1207    type Output = Vec2;
1208    #[inline]
1209    fn mul(self, rhs: &f32) -> Vec2 {
1210        self.mul(*rhs)
1211    }
1212}
1213
1214impl Mul<&f32> for &Vec2 {
1215    type Output = Vec2;
1216    #[inline]
1217    fn mul(self, rhs: &f32) -> Vec2 {
1218        (*self).mul(*rhs)
1219    }
1220}
1221
1222impl Mul<f32> for &Vec2 {
1223    type Output = Vec2;
1224    #[inline]
1225    fn mul(self, rhs: f32) -> Vec2 {
1226        (*self).mul(rhs)
1227    }
1228}
1229
1230impl MulAssign<f32> for Vec2 {
1231    #[inline]
1232    fn mul_assign(&mut self, rhs: f32) {
1233        self.x.mul_assign(rhs);
1234        self.y.mul_assign(rhs);
1235    }
1236}
1237
1238impl MulAssign<&f32> for Vec2 {
1239    #[inline]
1240    fn mul_assign(&mut self, rhs: &f32) {
1241        self.mul_assign(*rhs)
1242    }
1243}
1244
1245impl Mul<Vec2> for f32 {
1246    type Output = Vec2;
1247    #[inline]
1248    fn mul(self, rhs: Vec2) -> Vec2 {
1249        Vec2 {
1250            x: self.mul(rhs.x),
1251            y: self.mul(rhs.y),
1252        }
1253    }
1254}
1255
1256impl Mul<&Vec2> for f32 {
1257    type Output = Vec2;
1258    #[inline]
1259    fn mul(self, rhs: &Vec2) -> Vec2 {
1260        self.mul(*rhs)
1261    }
1262}
1263
1264impl Mul<&Vec2> for &f32 {
1265    type Output = Vec2;
1266    #[inline]
1267    fn mul(self, rhs: &Vec2) -> Vec2 {
1268        (*self).mul(*rhs)
1269    }
1270}
1271
1272impl Mul<Vec2> for &f32 {
1273    type Output = Vec2;
1274    #[inline]
1275    fn mul(self, rhs: Vec2) -> Vec2 {
1276        (*self).mul(rhs)
1277    }
1278}
1279
1280impl Add<Vec2> for Vec2 {
1281    type Output = Self;
1282    #[inline]
1283    fn add(self, rhs: Self) -> Self {
1284        Self {
1285            x: self.x.add(rhs.x),
1286            y: self.y.add(rhs.y),
1287        }
1288    }
1289}
1290
1291impl Add<&Vec2> for Vec2 {
1292    type Output = Vec2;
1293    #[inline]
1294    fn add(self, rhs: &Vec2) -> Vec2 {
1295        self.add(*rhs)
1296    }
1297}
1298
1299impl Add<&Vec2> for &Vec2 {
1300    type Output = Vec2;
1301    #[inline]
1302    fn add(self, rhs: &Vec2) -> Vec2 {
1303        (*self).add(*rhs)
1304    }
1305}
1306
1307impl Add<Vec2> for &Vec2 {
1308    type Output = Vec2;
1309    #[inline]
1310    fn add(self, rhs: Vec2) -> Vec2 {
1311        (*self).add(rhs)
1312    }
1313}
1314
1315impl AddAssign<Vec2> for Vec2 {
1316    #[inline]
1317    fn add_assign(&mut self, rhs: Self) {
1318        self.x.add_assign(rhs.x);
1319        self.y.add_assign(rhs.y);
1320    }
1321}
1322
1323impl AddAssign<&Self> for Vec2 {
1324    #[inline]
1325    fn add_assign(&mut self, rhs: &Self) {
1326        self.add_assign(*rhs)
1327    }
1328}
1329
1330impl Add<f32> for Vec2 {
1331    type Output = Self;
1332    #[inline]
1333    fn add(self, rhs: f32) -> Self {
1334        Self {
1335            x: self.x.add(rhs),
1336            y: self.y.add(rhs),
1337        }
1338    }
1339}
1340
1341impl Add<&f32> for Vec2 {
1342    type Output = Vec2;
1343    #[inline]
1344    fn add(self, rhs: &f32) -> Vec2 {
1345        self.add(*rhs)
1346    }
1347}
1348
1349impl Add<&f32> for &Vec2 {
1350    type Output = Vec2;
1351    #[inline]
1352    fn add(self, rhs: &f32) -> Vec2 {
1353        (*self).add(*rhs)
1354    }
1355}
1356
1357impl Add<f32> for &Vec2 {
1358    type Output = Vec2;
1359    #[inline]
1360    fn add(self, rhs: f32) -> Vec2 {
1361        (*self).add(rhs)
1362    }
1363}
1364
1365impl AddAssign<f32> for Vec2 {
1366    #[inline]
1367    fn add_assign(&mut self, rhs: f32) {
1368        self.x.add_assign(rhs);
1369        self.y.add_assign(rhs);
1370    }
1371}
1372
1373impl AddAssign<&f32> for Vec2 {
1374    #[inline]
1375    fn add_assign(&mut self, rhs: &f32) {
1376        self.add_assign(*rhs)
1377    }
1378}
1379
1380impl Add<Vec2> for f32 {
1381    type Output = Vec2;
1382    #[inline]
1383    fn add(self, rhs: Vec2) -> Vec2 {
1384        Vec2 {
1385            x: self.add(rhs.x),
1386            y: self.add(rhs.y),
1387        }
1388    }
1389}
1390
1391impl Add<&Vec2> for f32 {
1392    type Output = Vec2;
1393    #[inline]
1394    fn add(self, rhs: &Vec2) -> Vec2 {
1395        self.add(*rhs)
1396    }
1397}
1398
1399impl Add<&Vec2> for &f32 {
1400    type Output = Vec2;
1401    #[inline]
1402    fn add(self, rhs: &Vec2) -> Vec2 {
1403        (*self).add(*rhs)
1404    }
1405}
1406
1407impl Add<Vec2> for &f32 {
1408    type Output = Vec2;
1409    #[inline]
1410    fn add(self, rhs: Vec2) -> Vec2 {
1411        (*self).add(rhs)
1412    }
1413}
1414
1415impl Sub<Vec2> for Vec2 {
1416    type Output = Self;
1417    #[inline]
1418    fn sub(self, rhs: Self) -> Self {
1419        Self {
1420            x: self.x.sub(rhs.x),
1421            y: self.y.sub(rhs.y),
1422        }
1423    }
1424}
1425
1426impl Sub<&Vec2> for Vec2 {
1427    type Output = Vec2;
1428    #[inline]
1429    fn sub(self, rhs: &Vec2) -> Vec2 {
1430        self.sub(*rhs)
1431    }
1432}
1433
1434impl Sub<&Vec2> for &Vec2 {
1435    type Output = Vec2;
1436    #[inline]
1437    fn sub(self, rhs: &Vec2) -> Vec2 {
1438        (*self).sub(*rhs)
1439    }
1440}
1441
1442impl Sub<Vec2> for &Vec2 {
1443    type Output = Vec2;
1444    #[inline]
1445    fn sub(self, rhs: Vec2) -> Vec2 {
1446        (*self).sub(rhs)
1447    }
1448}
1449
1450impl SubAssign<Vec2> for Vec2 {
1451    #[inline]
1452    fn sub_assign(&mut self, rhs: Vec2) {
1453        self.x.sub_assign(rhs.x);
1454        self.y.sub_assign(rhs.y);
1455    }
1456}
1457
1458impl SubAssign<&Self> for Vec2 {
1459    #[inline]
1460    fn sub_assign(&mut self, rhs: &Self) {
1461        self.sub_assign(*rhs)
1462    }
1463}
1464
1465impl Sub<f32> for Vec2 {
1466    type Output = Self;
1467    #[inline]
1468    fn sub(self, rhs: f32) -> Self {
1469        Self {
1470            x: self.x.sub(rhs),
1471            y: self.y.sub(rhs),
1472        }
1473    }
1474}
1475
1476impl Sub<&f32> for Vec2 {
1477    type Output = Vec2;
1478    #[inline]
1479    fn sub(self, rhs: &f32) -> Vec2 {
1480        self.sub(*rhs)
1481    }
1482}
1483
1484impl Sub<&f32> for &Vec2 {
1485    type Output = Vec2;
1486    #[inline]
1487    fn sub(self, rhs: &f32) -> Vec2 {
1488        (*self).sub(*rhs)
1489    }
1490}
1491
1492impl Sub<f32> for &Vec2 {
1493    type Output = Vec2;
1494    #[inline]
1495    fn sub(self, rhs: f32) -> Vec2 {
1496        (*self).sub(rhs)
1497    }
1498}
1499
1500impl SubAssign<f32> for Vec2 {
1501    #[inline]
1502    fn sub_assign(&mut self, rhs: f32) {
1503        self.x.sub_assign(rhs);
1504        self.y.sub_assign(rhs);
1505    }
1506}
1507
1508impl SubAssign<&f32> for Vec2 {
1509    #[inline]
1510    fn sub_assign(&mut self, rhs: &f32) {
1511        self.sub_assign(*rhs)
1512    }
1513}
1514
1515impl Sub<Vec2> for f32 {
1516    type Output = Vec2;
1517    #[inline]
1518    fn sub(self, rhs: Vec2) -> Vec2 {
1519        Vec2 {
1520            x: self.sub(rhs.x),
1521            y: self.sub(rhs.y),
1522        }
1523    }
1524}
1525
1526impl Sub<&Vec2> for f32 {
1527    type Output = Vec2;
1528    #[inline]
1529    fn sub(self, rhs: &Vec2) -> Vec2 {
1530        self.sub(*rhs)
1531    }
1532}
1533
1534impl Sub<&Vec2> for &f32 {
1535    type Output = Vec2;
1536    #[inline]
1537    fn sub(self, rhs: &Vec2) -> Vec2 {
1538        (*self).sub(*rhs)
1539    }
1540}
1541
1542impl Sub<Vec2> for &f32 {
1543    type Output = Vec2;
1544    #[inline]
1545    fn sub(self, rhs: Vec2) -> Vec2 {
1546        (*self).sub(rhs)
1547    }
1548}
1549
1550impl Rem<Vec2> for Vec2 {
1551    type Output = Self;
1552    #[inline]
1553    fn rem(self, rhs: Self) -> Self {
1554        Self {
1555            x: self.x.rem(rhs.x),
1556            y: self.y.rem(rhs.y),
1557        }
1558    }
1559}
1560
1561impl Rem<&Vec2> for Vec2 {
1562    type Output = Vec2;
1563    #[inline]
1564    fn rem(self, rhs: &Vec2) -> Vec2 {
1565        self.rem(*rhs)
1566    }
1567}
1568
1569impl Rem<&Vec2> for &Vec2 {
1570    type Output = Vec2;
1571    #[inline]
1572    fn rem(self, rhs: &Vec2) -> Vec2 {
1573        (*self).rem(*rhs)
1574    }
1575}
1576
1577impl Rem<Vec2> for &Vec2 {
1578    type Output = Vec2;
1579    #[inline]
1580    fn rem(self, rhs: Vec2) -> Vec2 {
1581        (*self).rem(rhs)
1582    }
1583}
1584
1585impl RemAssign<Vec2> for Vec2 {
1586    #[inline]
1587    fn rem_assign(&mut self, rhs: Self) {
1588        self.x.rem_assign(rhs.x);
1589        self.y.rem_assign(rhs.y);
1590    }
1591}
1592
1593impl RemAssign<&Self> for Vec2 {
1594    #[inline]
1595    fn rem_assign(&mut self, rhs: &Self) {
1596        self.rem_assign(*rhs)
1597    }
1598}
1599
1600impl Rem<f32> for Vec2 {
1601    type Output = Self;
1602    #[inline]
1603    fn rem(self, rhs: f32) -> Self {
1604        Self {
1605            x: self.x.rem(rhs),
1606            y: self.y.rem(rhs),
1607        }
1608    }
1609}
1610
1611impl Rem<&f32> for Vec2 {
1612    type Output = Vec2;
1613    #[inline]
1614    fn rem(self, rhs: &f32) -> Vec2 {
1615        self.rem(*rhs)
1616    }
1617}
1618
1619impl Rem<&f32> for &Vec2 {
1620    type Output = Vec2;
1621    #[inline]
1622    fn rem(self, rhs: &f32) -> Vec2 {
1623        (*self).rem(*rhs)
1624    }
1625}
1626
1627impl Rem<f32> for &Vec2 {
1628    type Output = Vec2;
1629    #[inline]
1630    fn rem(self, rhs: f32) -> Vec2 {
1631        (*self).rem(rhs)
1632    }
1633}
1634
1635impl RemAssign<f32> for Vec2 {
1636    #[inline]
1637    fn rem_assign(&mut self, rhs: f32) {
1638        self.x.rem_assign(rhs);
1639        self.y.rem_assign(rhs);
1640    }
1641}
1642
1643impl RemAssign<&f32> for Vec2 {
1644    #[inline]
1645    fn rem_assign(&mut self, rhs: &f32) {
1646        self.rem_assign(*rhs)
1647    }
1648}
1649
1650impl Rem<Vec2> for f32 {
1651    type Output = Vec2;
1652    #[inline]
1653    fn rem(self, rhs: Vec2) -> Vec2 {
1654        Vec2 {
1655            x: self.rem(rhs.x),
1656            y: self.rem(rhs.y),
1657        }
1658    }
1659}
1660
1661impl Rem<&Vec2> for f32 {
1662    type Output = Vec2;
1663    #[inline]
1664    fn rem(self, rhs: &Vec2) -> Vec2 {
1665        self.rem(*rhs)
1666    }
1667}
1668
1669impl Rem<&Vec2> for &f32 {
1670    type Output = Vec2;
1671    #[inline]
1672    fn rem(self, rhs: &Vec2) -> Vec2 {
1673        (*self).rem(*rhs)
1674    }
1675}
1676
1677impl Rem<Vec2> for &f32 {
1678    type Output = Vec2;
1679    #[inline]
1680    fn rem(self, rhs: Vec2) -> Vec2 {
1681        (*self).rem(rhs)
1682    }
1683}
1684
1685#[cfg(not(target_arch = "spirv"))]
1686impl AsRef<[f32; 2]> for Vec2 {
1687    #[inline]
1688    fn as_ref(&self) -> &[f32; 2] {
1689        unsafe { &*(self as *const Vec2 as *const [f32; 2]) }
1690    }
1691}
1692
1693#[cfg(not(target_arch = "spirv"))]
1694impl AsMut<[f32; 2]> for Vec2 {
1695    #[inline]
1696    fn as_mut(&mut self) -> &mut [f32; 2] {
1697        unsafe { &mut *(self as *mut Vec2 as *mut [f32; 2]) }
1698    }
1699}
1700
1701impl Sum for Vec2 {
1702    #[inline]
1703    fn sum<I>(iter: I) -> Self
1704    where
1705        I: Iterator<Item = Self>,
1706    {
1707        iter.fold(Self::ZERO, Self::add)
1708    }
1709}
1710
1711impl<'a> Sum<&'a Self> for Vec2 {
1712    #[inline]
1713    fn sum<I>(iter: I) -> Self
1714    where
1715        I: Iterator<Item = &'a Self>,
1716    {
1717        iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1718    }
1719}
1720
1721impl Product for Vec2 {
1722    #[inline]
1723    fn product<I>(iter: I) -> Self
1724    where
1725        I: Iterator<Item = Self>,
1726    {
1727        iter.fold(Self::ONE, Self::mul)
1728    }
1729}
1730
1731impl<'a> Product<&'a Self> for Vec2 {
1732    #[inline]
1733    fn product<I>(iter: I) -> Self
1734    where
1735        I: Iterator<Item = &'a Self>,
1736    {
1737        iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1738    }
1739}
1740
1741impl Neg for Vec2 {
1742    type Output = Self;
1743    #[inline]
1744    fn neg(self) -> Self {
1745        Self {
1746            x: self.x.neg(),
1747            y: self.y.neg(),
1748        }
1749    }
1750}
1751
1752impl Neg for &Vec2 {
1753    type Output = Vec2;
1754    #[inline]
1755    fn neg(self) -> Vec2 {
1756        (*self).neg()
1757    }
1758}
1759
1760impl Index<usize> for Vec2 {
1761    type Output = f32;
1762    #[inline]
1763    fn index(&self, index: usize) -> &Self::Output {
1764        match index {
1765            0 => &self.x,
1766            1 => &self.y,
1767            _ => panic!("index out of bounds"),
1768        }
1769    }
1770}
1771
1772impl IndexMut<usize> for Vec2 {
1773    #[inline]
1774    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1775        match index {
1776            0 => &mut self.x,
1777            1 => &mut self.y,
1778            _ => panic!("index out of bounds"),
1779        }
1780    }
1781}
1782
1783impl fmt::Display for Vec2 {
1784    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1785        if let Some(p) = f.precision() {
1786            write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y)
1787        } else {
1788            write!(f, "[{}, {}]", self.x, self.y)
1789        }
1790    }
1791}
1792
1793impl fmt::Debug for Vec2 {
1794    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1795        fmt.debug_tuple(stringify!(Vec2))
1796            .field(&self.x)
1797            .field(&self.y)
1798            .finish()
1799    }
1800}
1801
1802impl From<[f32; 2]> for Vec2 {
1803    #[inline]
1804    fn from(a: [f32; 2]) -> Self {
1805        Self::new(a[0], a[1])
1806    }
1807}
1808
1809impl From<Vec2> for [f32; 2] {
1810    #[inline]
1811    fn from(v: Vec2) -> Self {
1812        [v.x, v.y]
1813    }
1814}
1815
1816impl From<(f32, f32)> for Vec2 {
1817    #[inline]
1818    fn from(t: (f32, f32)) -> Self {
1819        Self::new(t.0, t.1)
1820    }
1821}
1822
1823impl From<Vec2> for (f32, f32) {
1824    #[inline]
1825    fn from(v: Vec2) -> Self {
1826        (v.x, v.y)
1827    }
1828}
1829
1830impl From<BVec2> for Vec2 {
1831    #[inline]
1832    fn from(v: BVec2) -> Self {
1833        Self::new(f32::from(v.x), f32::from(v.y))
1834    }
1835}