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 pub const USES_CORE_SIMD: bool = false;
68 pub const USES_NEON: bool = false;
70 pub const USES_SCALAR_MATH: bool = true;
72 pub const USES_SSE2: bool = false;
74 pub const USES_WASM32_SIMD: bool = false;
76
77 #[inline(always)]
79 #[must_use]
80 pub const fn new(x: f32, y: f32) -> Self {
81 Self { x, y }
82 }
83
84 #[inline]
86 #[must_use]
87 pub const fn splat(v: f32) -> Self {
88 Self { x: v, y: v }
89 }
90
91 #[inline]
93 #[must_use]
94 pub fn map<F>(self, f: F) -> Self
95 where
96 F: Fn(f32) -> f32,
97 {
98 Self::new(f(self.x), f(self.y))
99 }
100
101 #[inline]
107 #[must_use]
108 pub fn select(mask: BVec2, if_true: Self, if_false: Self) -> Self {
109 Self {
110 x: if mask.test(0) { if_true.x } else { if_false.x },
111 y: if mask.test(1) { if_true.y } else { if_false.y },
112 }
113 }
114
115 #[inline]
117 #[must_use]
118 pub const fn from_array(a: [f32; 2]) -> Self {
119 Self::new(a[0], a[1])
120 }
121
122 #[inline]
124 #[must_use]
125 pub const fn to_array(&self) -> [f32; 2] {
126 [self.x, self.y]
127 }
128
129 #[inline]
135 #[must_use]
136 pub const fn from_slice(slice: &[f32]) -> Self {
137 assert!(slice.len() >= 2);
138 Self::new(slice[0], slice[1])
139 }
140
141 #[inline]
147 pub fn write_to_slice(self, slice: &mut [f32]) {
148 slice[..2].copy_from_slice(&self.to_array());
149 }
150
151 #[inline]
153 #[must_use]
154 pub const fn extend(self, z: f32) -> Vec3 {
155 Vec3::new(self.x, self.y, z)
156 }
157
158 #[inline]
160 #[must_use]
161 pub fn with_x(mut self, x: f32) -> Self {
162 self.x = x;
163 self
164 }
165
166 #[inline]
168 #[must_use]
169 pub fn with_y(mut self, y: f32) -> Self {
170 self.y = y;
171 self
172 }
173
174 #[inline]
176 #[must_use]
177 pub fn dot(self, rhs: Self) -> f32 {
178 (self.x * rhs.x) + (self.y * rhs.y)
179 }
180
181 #[inline]
183 #[must_use]
184 pub fn dot_into_vec(self, rhs: Self) -> Self {
185 Self::splat(self.dot(rhs))
186 }
187
188 #[inline]
195 #[must_use]
196 pub fn min(self, rhs: Self) -> Self {
197 Self {
198 x: if self.x < rhs.x { self.x } else { rhs.x },
199 y: if self.y < rhs.y { self.y } else { rhs.y },
200 }
201 }
202
203 #[inline]
210 #[must_use]
211 pub fn max(self, rhs: Self) -> Self {
212 Self {
213 x: if self.x > rhs.x { self.x } else { rhs.x },
214 y: if self.y > rhs.y { self.y } else { rhs.y },
215 }
216 }
217
218 #[inline]
229 #[must_use]
230 pub fn clamp(self, min: Self, max: Self) -> Self {
231 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
232 self.max(min).min(max)
233 }
234
235 #[inline]
242 #[must_use]
243 pub fn min_element(self) -> f32 {
244 let min = |a, b| if a < b { a } else { b };
245 min(self.x, self.y)
246 }
247
248 #[inline]
255 #[must_use]
256 pub fn max_element(self) -> f32 {
257 let max = |a, b| if a > b { a } else { b };
258 max(self.x, self.y)
259 }
260
261 #[doc(alias = "argmin")]
263 #[inline]
264 #[must_use]
265 pub fn min_position(self) -> usize {
266 if self.x <= self.y {
267 0
268 } else {
269 1
270 }
271 }
272
273 #[doc(alias = "argmax")]
275 #[inline]
276 #[must_use]
277 pub fn max_position(self) -> usize {
278 if self.x >= self.y {
279 0
280 } else {
281 1
282 }
283 }
284
285 #[inline]
289 #[must_use]
290 pub fn element_sum(self) -> f32 {
291 self.x + self.y
292 }
293
294 #[inline]
298 #[must_use]
299 pub fn element_product(self) -> f32 {
300 self.x * self.y
301 }
302
303 #[inline]
309 #[must_use]
310 pub fn cmpeq(self, rhs: Self) -> BVec2 {
311 BVec2::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y))
312 }
313
314 #[inline]
320 #[must_use]
321 pub fn cmpne(self, rhs: Self) -> BVec2 {
322 BVec2::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y))
323 }
324
325 #[inline]
331 #[must_use]
332 pub fn cmpge(self, rhs: Self) -> BVec2 {
333 BVec2::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y))
334 }
335
336 #[inline]
342 #[must_use]
343 pub fn cmpgt(self, rhs: Self) -> BVec2 {
344 BVec2::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y))
345 }
346
347 #[inline]
353 #[must_use]
354 pub fn cmple(self, rhs: Self) -> BVec2 {
355 BVec2::new(self.x.le(&rhs.x), self.y.le(&rhs.y))
356 }
357
358 #[inline]
364 #[must_use]
365 pub fn cmplt(self, rhs: Self) -> BVec2 {
366 BVec2::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y))
367 }
368
369 #[inline]
371 #[must_use]
372 pub fn abs(self) -> Self {
373 Self {
374 x: math::abs(self.x),
375 y: math::abs(self.y),
376 }
377 }
378
379 #[inline]
385 #[must_use]
386 pub fn signum(self) -> Self {
387 Self {
388 x: math::signum(self.x),
389 y: math::signum(self.y),
390 }
391 }
392
393 #[inline]
395 #[must_use]
396 pub fn copysign(self, rhs: Self) -> Self {
397 Self {
398 x: math::copysign(self.x, rhs.x),
399 y: math::copysign(self.y, rhs.y),
400 }
401 }
402
403 #[inline]
411 #[must_use]
412 pub fn is_negative_bitmask(self) -> u32 {
413 (self.x.is_sign_negative() as u32) | ((self.y.is_sign_negative() as u32) << 1)
414 }
415
416 #[inline]
419 #[must_use]
420 pub fn is_finite(self) -> bool {
421 self.x.is_finite() && self.y.is_finite()
422 }
423
424 pub fn is_finite_mask(self) -> BVec2 {
428 BVec2::new(self.x.is_finite(), self.y.is_finite())
429 }
430
431 #[inline]
433 #[must_use]
434 pub fn is_nan(self) -> bool {
435 self.x.is_nan() || self.y.is_nan()
436 }
437
438 #[inline]
442 #[must_use]
443 pub fn is_nan_mask(self) -> BVec2 {
444 BVec2::new(self.x.is_nan(), self.y.is_nan())
445 }
446
447 #[doc(alias = "magnitude")]
449 #[inline]
450 #[must_use]
451 pub fn length(self) -> f32 {
452 math::sqrt(self.dot(self))
453 }
454
455 #[doc(alias = "magnitude2")]
459 #[inline]
460 #[must_use]
461 pub fn length_squared(self) -> f32 {
462 self.dot(self)
463 }
464
465 #[inline]
469 #[must_use]
470 pub fn length_recip(self) -> f32 {
471 self.length().recip()
472 }
473
474 #[inline]
476 #[must_use]
477 pub fn distance(self, rhs: Self) -> f32 {
478 (self - rhs).length()
479 }
480
481 #[inline]
483 #[must_use]
484 pub fn distance_squared(self, rhs: Self) -> f32 {
485 (self - rhs).length_squared()
486 }
487
488 #[inline]
490 #[must_use]
491 pub fn div_euclid(self, rhs: Self) -> Self {
492 Self::new(
493 math::div_euclid(self.x, rhs.x),
494 math::div_euclid(self.y, rhs.y),
495 )
496 }
497
498 #[inline]
502 #[must_use]
503 pub fn rem_euclid(self, rhs: Self) -> Self {
504 Self::new(
505 math::rem_euclid(self.x, rhs.x),
506 math::rem_euclid(self.y, rhs.y),
507 )
508 }
509
510 #[inline]
520 #[must_use]
521 pub fn normalize(self) -> Self {
522 #[allow(clippy::let_and_return)]
523 let normalized = self.mul(self.length_recip());
524 glam_assert!(normalized.is_finite());
525 normalized
526 }
527
528 #[inline]
535 #[must_use]
536 pub fn try_normalize(self) -> Option<Self> {
537 let rcp = self.length_recip();
538 if rcp.is_finite() && rcp > 0.0 {
539 Some(self * rcp)
540 } else {
541 None
542 }
543 }
544
545 #[inline]
553 #[must_use]
554 pub fn normalize_or(self, fallback: Self) -> Self {
555 let rcp = self.length_recip();
556 if rcp.is_finite() && rcp > 0.0 {
557 self * rcp
558 } else {
559 fallback
560 }
561 }
562
563 #[inline]
570 #[must_use]
571 pub fn normalize_or_zero(self) -> Self {
572 self.normalize_or(Self::ZERO)
573 }
574
575 #[inline]
579 #[must_use]
580 pub fn normalize_and_length(self) -> (Self, f32) {
581 let length = self.length();
582 let rcp = 1.0 / length;
583 if rcp.is_finite() && rcp > 0.0 {
584 (self * rcp, length)
585 } else {
586 (Self::X, 0.0)
587 }
588 }
589
590 #[inline]
594 #[must_use]
595 pub fn is_normalized(self) -> bool {
596 math::abs(self.length_squared() - 1.0) <= 2e-4
597 }
598
599 #[inline]
607 #[must_use]
608 pub fn project_onto(self, rhs: Self) -> Self {
609 let other_len_sq_rcp = rhs.dot(rhs).recip();
610 glam_assert!(other_len_sq_rcp.is_finite());
611 rhs * self.dot(rhs) * other_len_sq_rcp
612 }
613
614 #[doc(alias("plane"))]
625 #[inline]
626 #[must_use]
627 pub fn reject_from(self, rhs: Self) -> Self {
628 self - self.project_onto(rhs)
629 }
630
631 #[inline]
639 #[must_use]
640 pub fn project_onto_normalized(self, rhs: Self) -> Self {
641 glam_assert!(rhs.is_normalized());
642 rhs * self.dot(rhs)
643 }
644
645 #[doc(alias("plane"))]
656 #[inline]
657 #[must_use]
658 pub fn reject_from_normalized(self, rhs: Self) -> Self {
659 self - self.project_onto_normalized(rhs)
660 }
661
662 #[inline]
665 #[must_use]
666 pub fn round(self) -> Self {
667 Self {
668 x: math::round(self.x),
669 y: math::round(self.y),
670 }
671 }
672
673 #[inline]
676 #[must_use]
677 pub fn floor(self) -> Self {
678 Self {
679 x: math::floor(self.x),
680 y: math::floor(self.y),
681 }
682 }
683
684 #[inline]
687 #[must_use]
688 pub fn ceil(self) -> Self {
689 Self {
690 x: math::ceil(self.x),
691 y: math::ceil(self.y),
692 }
693 }
694
695 #[inline]
698 #[must_use]
699 pub fn trunc(self) -> Self {
700 Self {
701 x: math::trunc(self.x),
702 y: math::trunc(self.y),
703 }
704 }
705
706 #[inline]
713 #[must_use]
714 pub fn fract(self) -> Self {
715 self - self.trunc()
716 }
717
718 #[inline]
725 #[must_use]
726 pub fn fract_gl(self) -> Self {
727 self - self.floor()
728 }
729
730 #[inline]
733 #[must_use]
734 pub fn exp(self) -> Self {
735 Self::new(math::exp(self.x), math::exp(self.y))
736 }
737
738 #[inline]
740 #[must_use]
741 pub fn powf(self, n: f32) -> Self {
742 Self::new(math::powf(self.x, n), math::powf(self.y, n))
743 }
744
745 #[inline]
747 #[must_use]
748 pub fn recip(self) -> Self {
749 Self {
750 x: 1.0 / self.x,
751 y: 1.0 / self.y,
752 }
753 }
754
755 #[doc(alias = "mix")]
761 #[inline]
762 #[must_use]
763 pub fn lerp(self, rhs: Self, s: f32) -> Self {
764 self * (1.0 - s) + rhs * s
765 }
766
767 #[inline]
772 #[must_use]
773 pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
774 let a = rhs - *self;
775 let len = a.length();
776 if len <= d || len <= 1e-4 {
777 return rhs;
778 }
779 *self + a / len * d
780 }
781
782 #[inline]
788 pub fn midpoint(self, rhs: Self) -> Self {
789 (self + rhs) * 0.5
790 }
791
792 #[inline]
802 #[must_use]
803 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
804 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
805 }
806
807 #[inline]
813 #[must_use]
814 pub fn clamp_length(self, min: f32, max: f32) -> Self {
815 glam_assert!(0.0 <= min);
816 glam_assert!(min <= max);
817 let length_sq = self.length_squared();
818 if length_sq < min * min {
819 min * (self / math::sqrt(length_sq))
820 } else if length_sq > max * max {
821 max * (self / math::sqrt(length_sq))
822 } else {
823 self
824 }
825 }
826
827 #[inline]
833 #[must_use]
834 pub fn clamp_length_max(self, max: f32) -> Self {
835 glam_assert!(0.0 <= max);
836 let length_sq = self.length_squared();
837 if length_sq > max * max {
838 max * (self / math::sqrt(length_sq))
839 } else {
840 self
841 }
842 }
843
844 #[inline]
850 #[must_use]
851 pub fn clamp_length_min(self, min: f32) -> Self {
852 glam_assert!(0.0 <= min);
853 let length_sq = self.length_squared();
854 if length_sq < min * min {
855 min * (self / math::sqrt(length_sq))
856 } else {
857 self
858 }
859 }
860
861 #[inline]
869 #[must_use]
870 pub fn mul_add(self, a: Self, b: Self) -> Self {
871 Self::new(
872 math::mul_add(self.x, a.x, b.x),
873 math::mul_add(self.y, a.y, b.y),
874 )
875 }
876
877 #[inline]
886 #[must_use]
887 pub fn reflect(self, normal: Self) -> Self {
888 glam_assert!(normal.is_normalized());
889 self - 2.0 * self.dot(normal) * normal
890 }
891
892 #[inline]
902 #[must_use]
903 pub fn refract(self, normal: Self, eta: f32) -> Self {
904 glam_assert!(self.is_normalized());
905 glam_assert!(normal.is_normalized());
906 let n_dot_i = normal.dot(self);
907 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
908 if k >= 0.0 {
909 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
910 } else {
911 Self::ZERO
912 }
913 }
914
915 #[inline]
920 #[must_use]
921 pub fn from_angle(angle: f32) -> Self {
922 let (sin, cos) = math::sin_cos(angle);
923 Self { x: cos, y: sin }
924 }
925
926 #[inline]
930 #[must_use]
931 pub fn to_angle(self) -> f32 {
932 math::atan2(self.y, self.x)
933 }
934
935 #[inline]
936 #[must_use]
937 #[deprecated(
938 since = "0.27.0",
939 note = "Use angle_to() instead, the semantics of angle_between will change in the future."
940 )]
941 pub fn angle_between(self, rhs: Self) -> f32 {
942 self.angle_to(rhs)
943 }
944
945 #[inline]
949 #[must_use]
950 pub fn angle_to(self, rhs: Self) -> f32 {
951 let angle = math::acos_approx(
952 self.dot(rhs) / math::sqrt(self.length_squared() * rhs.length_squared()),
953 );
954
955 angle * math::signum(self.perp_dot(rhs))
956 }
957
958 #[inline]
960 #[must_use]
961 pub fn perp(self) -> Self {
962 Self {
963 x: -self.y,
964 y: self.x,
965 }
966 }
967
968 #[doc(alias = "wedge")]
971 #[doc(alias = "cross")]
972 #[doc(alias = "determinant")]
973 #[inline]
974 #[must_use]
975 pub fn perp_dot(self, rhs: Self) -> f32 {
976 (self.x * rhs.y) - (self.y * rhs.x)
977 }
978
979 #[inline]
983 #[must_use]
984 pub fn rotate(self, rhs: Self) -> Self {
985 Self {
986 x: self.x * rhs.x - self.y * rhs.y,
987 y: self.y * rhs.x + self.x * rhs.y,
988 }
989 }
990
991 #[inline]
997 #[must_use]
998 pub fn rotate_towards(&self, rhs: Self, max_angle: f32) -> Self {
999 let a = self.angle_to(rhs);
1000 let abs_a = math::abs(a);
1001 let angle = max_angle.clamp(abs_a - core::f32::consts::PI, abs_a) * math::signum(a);
1003 Self::from_angle(angle).rotate(*self)
1004 }
1005
1006 #[inline]
1008 #[must_use]
1009 pub fn as_dvec2(&self) -> crate::DVec2 {
1010 crate::DVec2::new(self.x as f64, self.y as f64)
1011 }
1012
1013 #[inline]
1015 #[must_use]
1016 pub fn as_i8vec2(&self) -> crate::I8Vec2 {
1017 crate::I8Vec2::new(self.x as i8, self.y as i8)
1018 }
1019
1020 #[inline]
1022 #[must_use]
1023 pub fn as_u8vec2(&self) -> crate::U8Vec2 {
1024 crate::U8Vec2::new(self.x as u8, self.y as u8)
1025 }
1026
1027 #[inline]
1029 #[must_use]
1030 pub fn as_i16vec2(&self) -> crate::I16Vec2 {
1031 crate::I16Vec2::new(self.x as i16, self.y as i16)
1032 }
1033
1034 #[inline]
1036 #[must_use]
1037 pub fn as_u16vec2(&self) -> crate::U16Vec2 {
1038 crate::U16Vec2::new(self.x as u16, self.y as u16)
1039 }
1040
1041 #[inline]
1043 #[must_use]
1044 pub fn as_ivec2(&self) -> crate::IVec2 {
1045 crate::IVec2::new(self.x as i32, self.y as i32)
1046 }
1047
1048 #[inline]
1050 #[must_use]
1051 pub fn as_uvec2(&self) -> crate::UVec2 {
1052 crate::UVec2::new(self.x as u32, self.y as u32)
1053 }
1054
1055 #[inline]
1057 #[must_use]
1058 pub fn as_i64vec2(&self) -> crate::I64Vec2 {
1059 crate::I64Vec2::new(self.x as i64, self.y as i64)
1060 }
1061
1062 #[inline]
1064 #[must_use]
1065 pub fn as_u64vec2(&self) -> crate::U64Vec2 {
1066 crate::U64Vec2::new(self.x as u64, self.y as u64)
1067 }
1068
1069 #[inline]
1071 #[must_use]
1072 pub fn as_usizevec2(&self) -> crate::USizeVec2 {
1073 crate::USizeVec2::new(self.x as usize, self.y as usize)
1074 }
1075}
1076
1077impl Default for Vec2 {
1078 #[inline(always)]
1079 fn default() -> Self {
1080 Self::ZERO
1081 }
1082}
1083
1084impl Div<Vec2> for Vec2 {
1085 type Output = Self;
1086 #[inline]
1087 fn div(self, rhs: Self) -> Self {
1088 Self {
1089 x: self.x.div(rhs.x),
1090 y: self.y.div(rhs.y),
1091 }
1092 }
1093}
1094
1095impl Div<&Vec2> for Vec2 {
1096 type Output = Vec2;
1097 #[inline]
1098 fn div(self, rhs: &Vec2) -> Vec2 {
1099 self.div(*rhs)
1100 }
1101}
1102
1103impl Div<&Vec2> for &Vec2 {
1104 type Output = Vec2;
1105 #[inline]
1106 fn div(self, rhs: &Vec2) -> Vec2 {
1107 (*self).div(*rhs)
1108 }
1109}
1110
1111impl Div<Vec2> for &Vec2 {
1112 type Output = Vec2;
1113 #[inline]
1114 fn div(self, rhs: Vec2) -> Vec2 {
1115 (*self).div(rhs)
1116 }
1117}
1118
1119impl DivAssign<Vec2> for Vec2 {
1120 #[inline]
1121 fn div_assign(&mut self, rhs: Self) {
1122 self.x.div_assign(rhs.x);
1123 self.y.div_assign(rhs.y);
1124 }
1125}
1126
1127impl DivAssign<&Vec2> for Vec2 {
1128 #[inline]
1129 fn div_assign(&mut self, rhs: &Vec2) {
1130 self.div_assign(*rhs)
1131 }
1132}
1133
1134impl Div<f32> for Vec2 {
1135 type Output = Self;
1136 #[inline]
1137 fn div(self, rhs: f32) -> Self {
1138 Self {
1139 x: self.x.div(rhs),
1140 y: self.y.div(rhs),
1141 }
1142 }
1143}
1144
1145impl Div<&f32> for Vec2 {
1146 type Output = Vec2;
1147 #[inline]
1148 fn div(self, rhs: &f32) -> Vec2 {
1149 self.div(*rhs)
1150 }
1151}
1152
1153impl Div<&f32> for &Vec2 {
1154 type Output = Vec2;
1155 #[inline]
1156 fn div(self, rhs: &f32) -> Vec2 {
1157 (*self).div(*rhs)
1158 }
1159}
1160
1161impl Div<f32> for &Vec2 {
1162 type Output = Vec2;
1163 #[inline]
1164 fn div(self, rhs: f32) -> Vec2 {
1165 (*self).div(rhs)
1166 }
1167}
1168
1169impl DivAssign<f32> for Vec2 {
1170 #[inline]
1171 fn div_assign(&mut self, rhs: f32) {
1172 self.x.div_assign(rhs);
1173 self.y.div_assign(rhs);
1174 }
1175}
1176
1177impl DivAssign<&f32> for Vec2 {
1178 #[inline]
1179 fn div_assign(&mut self, rhs: &f32) {
1180 self.div_assign(*rhs)
1181 }
1182}
1183
1184impl Div<Vec2> for f32 {
1185 type Output = Vec2;
1186 #[inline]
1187 fn div(self, rhs: Vec2) -> Vec2 {
1188 Vec2 {
1189 x: self.div(rhs.x),
1190 y: self.div(rhs.y),
1191 }
1192 }
1193}
1194
1195impl Div<&Vec2> for f32 {
1196 type Output = Vec2;
1197 #[inline]
1198 fn div(self, rhs: &Vec2) -> Vec2 {
1199 self.div(*rhs)
1200 }
1201}
1202
1203impl Div<&Vec2> for &f32 {
1204 type Output = Vec2;
1205 #[inline]
1206 fn div(self, rhs: &Vec2) -> Vec2 {
1207 (*self).div(*rhs)
1208 }
1209}
1210
1211impl Div<Vec2> for &f32 {
1212 type Output = Vec2;
1213 #[inline]
1214 fn div(self, rhs: Vec2) -> Vec2 {
1215 (*self).div(rhs)
1216 }
1217}
1218
1219impl Mul<Vec2> for Vec2 {
1220 type Output = Self;
1221 #[inline]
1222 fn mul(self, rhs: Self) -> Self {
1223 Self {
1224 x: self.x.mul(rhs.x),
1225 y: self.y.mul(rhs.y),
1226 }
1227 }
1228}
1229
1230impl Mul<&Vec2> for Vec2 {
1231 type Output = Vec2;
1232 #[inline]
1233 fn mul(self, rhs: &Vec2) -> Vec2 {
1234 self.mul(*rhs)
1235 }
1236}
1237
1238impl Mul<&Vec2> for &Vec2 {
1239 type Output = Vec2;
1240 #[inline]
1241 fn mul(self, rhs: &Vec2) -> Vec2 {
1242 (*self).mul(*rhs)
1243 }
1244}
1245
1246impl Mul<Vec2> for &Vec2 {
1247 type Output = Vec2;
1248 #[inline]
1249 fn mul(self, rhs: Vec2) -> Vec2 {
1250 (*self).mul(rhs)
1251 }
1252}
1253
1254impl MulAssign<Vec2> for Vec2 {
1255 #[inline]
1256 fn mul_assign(&mut self, rhs: Self) {
1257 self.x.mul_assign(rhs.x);
1258 self.y.mul_assign(rhs.y);
1259 }
1260}
1261
1262impl MulAssign<&Vec2> for Vec2 {
1263 #[inline]
1264 fn mul_assign(&mut self, rhs: &Vec2) {
1265 self.mul_assign(*rhs)
1266 }
1267}
1268
1269impl Mul<f32> for Vec2 {
1270 type Output = Self;
1271 #[inline]
1272 fn mul(self, rhs: f32) -> Self {
1273 Self {
1274 x: self.x.mul(rhs),
1275 y: self.y.mul(rhs),
1276 }
1277 }
1278}
1279
1280impl Mul<&f32> for Vec2 {
1281 type Output = Vec2;
1282 #[inline]
1283 fn mul(self, rhs: &f32) -> Vec2 {
1284 self.mul(*rhs)
1285 }
1286}
1287
1288impl Mul<&f32> for &Vec2 {
1289 type Output = Vec2;
1290 #[inline]
1291 fn mul(self, rhs: &f32) -> Vec2 {
1292 (*self).mul(*rhs)
1293 }
1294}
1295
1296impl Mul<f32> for &Vec2 {
1297 type Output = Vec2;
1298 #[inline]
1299 fn mul(self, rhs: f32) -> Vec2 {
1300 (*self).mul(rhs)
1301 }
1302}
1303
1304impl MulAssign<f32> for Vec2 {
1305 #[inline]
1306 fn mul_assign(&mut self, rhs: f32) {
1307 self.x.mul_assign(rhs);
1308 self.y.mul_assign(rhs);
1309 }
1310}
1311
1312impl MulAssign<&f32> for Vec2 {
1313 #[inline]
1314 fn mul_assign(&mut self, rhs: &f32) {
1315 self.mul_assign(*rhs)
1316 }
1317}
1318
1319impl Mul<Vec2> for f32 {
1320 type Output = Vec2;
1321 #[inline]
1322 fn mul(self, rhs: Vec2) -> Vec2 {
1323 Vec2 {
1324 x: self.mul(rhs.x),
1325 y: self.mul(rhs.y),
1326 }
1327 }
1328}
1329
1330impl Mul<&Vec2> for f32 {
1331 type Output = Vec2;
1332 #[inline]
1333 fn mul(self, rhs: &Vec2) -> Vec2 {
1334 self.mul(*rhs)
1335 }
1336}
1337
1338impl Mul<&Vec2> for &f32 {
1339 type Output = Vec2;
1340 #[inline]
1341 fn mul(self, rhs: &Vec2) -> Vec2 {
1342 (*self).mul(*rhs)
1343 }
1344}
1345
1346impl Mul<Vec2> for &f32 {
1347 type Output = Vec2;
1348 #[inline]
1349 fn mul(self, rhs: Vec2) -> Vec2 {
1350 (*self).mul(rhs)
1351 }
1352}
1353
1354impl Add<Vec2> for Vec2 {
1355 type Output = Self;
1356 #[inline]
1357 fn add(self, rhs: Self) -> Self {
1358 Self {
1359 x: self.x.add(rhs.x),
1360 y: self.y.add(rhs.y),
1361 }
1362 }
1363}
1364
1365impl Add<&Vec2> for Vec2 {
1366 type Output = Vec2;
1367 #[inline]
1368 fn add(self, rhs: &Vec2) -> Vec2 {
1369 self.add(*rhs)
1370 }
1371}
1372
1373impl Add<&Vec2> for &Vec2 {
1374 type Output = Vec2;
1375 #[inline]
1376 fn add(self, rhs: &Vec2) -> Vec2 {
1377 (*self).add(*rhs)
1378 }
1379}
1380
1381impl Add<Vec2> for &Vec2 {
1382 type Output = Vec2;
1383 #[inline]
1384 fn add(self, rhs: Vec2) -> Vec2 {
1385 (*self).add(rhs)
1386 }
1387}
1388
1389impl AddAssign<Vec2> for Vec2 {
1390 #[inline]
1391 fn add_assign(&mut self, rhs: Self) {
1392 self.x.add_assign(rhs.x);
1393 self.y.add_assign(rhs.y);
1394 }
1395}
1396
1397impl AddAssign<&Vec2> for Vec2 {
1398 #[inline]
1399 fn add_assign(&mut self, rhs: &Vec2) {
1400 self.add_assign(*rhs)
1401 }
1402}
1403
1404impl Add<f32> for Vec2 {
1405 type Output = Self;
1406 #[inline]
1407 fn add(self, rhs: f32) -> Self {
1408 Self {
1409 x: self.x.add(rhs),
1410 y: self.y.add(rhs),
1411 }
1412 }
1413}
1414
1415impl Add<&f32> for Vec2 {
1416 type Output = Vec2;
1417 #[inline]
1418 fn add(self, rhs: &f32) -> Vec2 {
1419 self.add(*rhs)
1420 }
1421}
1422
1423impl Add<&f32> for &Vec2 {
1424 type Output = Vec2;
1425 #[inline]
1426 fn add(self, rhs: &f32) -> Vec2 {
1427 (*self).add(*rhs)
1428 }
1429}
1430
1431impl Add<f32> for &Vec2 {
1432 type Output = Vec2;
1433 #[inline]
1434 fn add(self, rhs: f32) -> Vec2 {
1435 (*self).add(rhs)
1436 }
1437}
1438
1439impl AddAssign<f32> for Vec2 {
1440 #[inline]
1441 fn add_assign(&mut self, rhs: f32) {
1442 self.x.add_assign(rhs);
1443 self.y.add_assign(rhs);
1444 }
1445}
1446
1447impl AddAssign<&f32> for Vec2 {
1448 #[inline]
1449 fn add_assign(&mut self, rhs: &f32) {
1450 self.add_assign(*rhs)
1451 }
1452}
1453
1454impl Add<Vec2> for f32 {
1455 type Output = Vec2;
1456 #[inline]
1457 fn add(self, rhs: Vec2) -> Vec2 {
1458 Vec2 {
1459 x: self.add(rhs.x),
1460 y: self.add(rhs.y),
1461 }
1462 }
1463}
1464
1465impl Add<&Vec2> for f32 {
1466 type Output = Vec2;
1467 #[inline]
1468 fn add(self, rhs: &Vec2) -> Vec2 {
1469 self.add(*rhs)
1470 }
1471}
1472
1473impl Add<&Vec2> for &f32 {
1474 type Output = Vec2;
1475 #[inline]
1476 fn add(self, rhs: &Vec2) -> Vec2 {
1477 (*self).add(*rhs)
1478 }
1479}
1480
1481impl Add<Vec2> for &f32 {
1482 type Output = Vec2;
1483 #[inline]
1484 fn add(self, rhs: Vec2) -> Vec2 {
1485 (*self).add(rhs)
1486 }
1487}
1488
1489impl Sub<Vec2> for Vec2 {
1490 type Output = Self;
1491 #[inline]
1492 fn sub(self, rhs: Self) -> Self {
1493 Self {
1494 x: self.x.sub(rhs.x),
1495 y: self.y.sub(rhs.y),
1496 }
1497 }
1498}
1499
1500impl Sub<&Vec2> for Vec2 {
1501 type Output = Vec2;
1502 #[inline]
1503 fn sub(self, rhs: &Vec2) -> Vec2 {
1504 self.sub(*rhs)
1505 }
1506}
1507
1508impl Sub<&Vec2> for &Vec2 {
1509 type Output = Vec2;
1510 #[inline]
1511 fn sub(self, rhs: &Vec2) -> Vec2 {
1512 (*self).sub(*rhs)
1513 }
1514}
1515
1516impl Sub<Vec2> for &Vec2 {
1517 type Output = Vec2;
1518 #[inline]
1519 fn sub(self, rhs: Vec2) -> Vec2 {
1520 (*self).sub(rhs)
1521 }
1522}
1523
1524impl SubAssign<Vec2> for Vec2 {
1525 #[inline]
1526 fn sub_assign(&mut self, rhs: Vec2) {
1527 self.x.sub_assign(rhs.x);
1528 self.y.sub_assign(rhs.y);
1529 }
1530}
1531
1532impl SubAssign<&Vec2> for Vec2 {
1533 #[inline]
1534 fn sub_assign(&mut self, rhs: &Vec2) {
1535 self.sub_assign(*rhs)
1536 }
1537}
1538
1539impl Sub<f32> for Vec2 {
1540 type Output = Self;
1541 #[inline]
1542 fn sub(self, rhs: f32) -> Self {
1543 Self {
1544 x: self.x.sub(rhs),
1545 y: self.y.sub(rhs),
1546 }
1547 }
1548}
1549
1550impl Sub<&f32> for Vec2 {
1551 type Output = Vec2;
1552 #[inline]
1553 fn sub(self, rhs: &f32) -> Vec2 {
1554 self.sub(*rhs)
1555 }
1556}
1557
1558impl Sub<&f32> for &Vec2 {
1559 type Output = Vec2;
1560 #[inline]
1561 fn sub(self, rhs: &f32) -> Vec2 {
1562 (*self).sub(*rhs)
1563 }
1564}
1565
1566impl Sub<f32> for &Vec2 {
1567 type Output = Vec2;
1568 #[inline]
1569 fn sub(self, rhs: f32) -> Vec2 {
1570 (*self).sub(rhs)
1571 }
1572}
1573
1574impl SubAssign<f32> for Vec2 {
1575 #[inline]
1576 fn sub_assign(&mut self, rhs: f32) {
1577 self.x.sub_assign(rhs);
1578 self.y.sub_assign(rhs);
1579 }
1580}
1581
1582impl SubAssign<&f32> for Vec2 {
1583 #[inline]
1584 fn sub_assign(&mut self, rhs: &f32) {
1585 self.sub_assign(*rhs)
1586 }
1587}
1588
1589impl Sub<Vec2> for f32 {
1590 type Output = Vec2;
1591 #[inline]
1592 fn sub(self, rhs: Vec2) -> Vec2 {
1593 Vec2 {
1594 x: self.sub(rhs.x),
1595 y: self.sub(rhs.y),
1596 }
1597 }
1598}
1599
1600impl Sub<&Vec2> for f32 {
1601 type Output = Vec2;
1602 #[inline]
1603 fn sub(self, rhs: &Vec2) -> Vec2 {
1604 self.sub(*rhs)
1605 }
1606}
1607
1608impl Sub<&Vec2> for &f32 {
1609 type Output = Vec2;
1610 #[inline]
1611 fn sub(self, rhs: &Vec2) -> Vec2 {
1612 (*self).sub(*rhs)
1613 }
1614}
1615
1616impl Sub<Vec2> for &f32 {
1617 type Output = Vec2;
1618 #[inline]
1619 fn sub(self, rhs: Vec2) -> Vec2 {
1620 (*self).sub(rhs)
1621 }
1622}
1623
1624impl Rem<Vec2> for Vec2 {
1625 type Output = Self;
1626 #[inline]
1627 fn rem(self, rhs: Self) -> Self {
1628 Self {
1629 x: self.x.rem(rhs.x),
1630 y: self.y.rem(rhs.y),
1631 }
1632 }
1633}
1634
1635impl Rem<&Vec2> for Vec2 {
1636 type Output = Vec2;
1637 #[inline]
1638 fn rem(self, rhs: &Vec2) -> Vec2 {
1639 self.rem(*rhs)
1640 }
1641}
1642
1643impl Rem<&Vec2> for &Vec2 {
1644 type Output = Vec2;
1645 #[inline]
1646 fn rem(self, rhs: &Vec2) -> Vec2 {
1647 (*self).rem(*rhs)
1648 }
1649}
1650
1651impl Rem<Vec2> for &Vec2 {
1652 type Output = Vec2;
1653 #[inline]
1654 fn rem(self, rhs: Vec2) -> Vec2 {
1655 (*self).rem(rhs)
1656 }
1657}
1658
1659impl RemAssign<Vec2> for Vec2 {
1660 #[inline]
1661 fn rem_assign(&mut self, rhs: Self) {
1662 self.x.rem_assign(rhs.x);
1663 self.y.rem_assign(rhs.y);
1664 }
1665}
1666
1667impl RemAssign<&Vec2> for Vec2 {
1668 #[inline]
1669 fn rem_assign(&mut self, rhs: &Vec2) {
1670 self.rem_assign(*rhs)
1671 }
1672}
1673
1674impl Rem<f32> for Vec2 {
1675 type Output = Self;
1676 #[inline]
1677 fn rem(self, rhs: f32) -> Self {
1678 Self {
1679 x: self.x.rem(rhs),
1680 y: self.y.rem(rhs),
1681 }
1682 }
1683}
1684
1685impl Rem<&f32> for Vec2 {
1686 type Output = Vec2;
1687 #[inline]
1688 fn rem(self, rhs: &f32) -> Vec2 {
1689 self.rem(*rhs)
1690 }
1691}
1692
1693impl Rem<&f32> for &Vec2 {
1694 type Output = Vec2;
1695 #[inline]
1696 fn rem(self, rhs: &f32) -> Vec2 {
1697 (*self).rem(*rhs)
1698 }
1699}
1700
1701impl Rem<f32> for &Vec2 {
1702 type Output = Vec2;
1703 #[inline]
1704 fn rem(self, rhs: f32) -> Vec2 {
1705 (*self).rem(rhs)
1706 }
1707}
1708
1709impl RemAssign<f32> for Vec2 {
1710 #[inline]
1711 fn rem_assign(&mut self, rhs: f32) {
1712 self.x.rem_assign(rhs);
1713 self.y.rem_assign(rhs);
1714 }
1715}
1716
1717impl RemAssign<&f32> for Vec2 {
1718 #[inline]
1719 fn rem_assign(&mut self, rhs: &f32) {
1720 self.rem_assign(*rhs)
1721 }
1722}
1723
1724impl Rem<Vec2> for f32 {
1725 type Output = Vec2;
1726 #[inline]
1727 fn rem(self, rhs: Vec2) -> Vec2 {
1728 Vec2 {
1729 x: self.rem(rhs.x),
1730 y: self.rem(rhs.y),
1731 }
1732 }
1733}
1734
1735impl Rem<&Vec2> for f32 {
1736 type Output = Vec2;
1737 #[inline]
1738 fn rem(self, rhs: &Vec2) -> Vec2 {
1739 self.rem(*rhs)
1740 }
1741}
1742
1743impl Rem<&Vec2> for &f32 {
1744 type Output = Vec2;
1745 #[inline]
1746 fn rem(self, rhs: &Vec2) -> Vec2 {
1747 (*self).rem(*rhs)
1748 }
1749}
1750
1751impl Rem<Vec2> for &f32 {
1752 type Output = Vec2;
1753 #[inline]
1754 fn rem(self, rhs: Vec2) -> Vec2 {
1755 (*self).rem(rhs)
1756 }
1757}
1758
1759#[cfg(not(target_arch = "spirv"))]
1760impl AsRef<[f32; 2]> for Vec2 {
1761 #[inline]
1762 fn as_ref(&self) -> &[f32; 2] {
1763 unsafe { &*(self as *const Vec2 as *const [f32; 2]) }
1764 }
1765}
1766
1767#[cfg(not(target_arch = "spirv"))]
1768impl AsMut<[f32; 2]> for Vec2 {
1769 #[inline]
1770 fn as_mut(&mut self) -> &mut [f32; 2] {
1771 unsafe { &mut *(self as *mut Vec2 as *mut [f32; 2]) }
1772 }
1773}
1774
1775impl Sum for Vec2 {
1776 #[inline]
1777 fn sum<I>(iter: I) -> Self
1778 where
1779 I: Iterator<Item = Self>,
1780 {
1781 iter.fold(Self::ZERO, Self::add)
1782 }
1783}
1784
1785impl<'a> Sum<&'a Self> for Vec2 {
1786 #[inline]
1787 fn sum<I>(iter: I) -> Self
1788 where
1789 I: Iterator<Item = &'a Self>,
1790 {
1791 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1792 }
1793}
1794
1795impl Product for Vec2 {
1796 #[inline]
1797 fn product<I>(iter: I) -> Self
1798 where
1799 I: Iterator<Item = Self>,
1800 {
1801 iter.fold(Self::ONE, Self::mul)
1802 }
1803}
1804
1805impl<'a> Product<&'a Self> for Vec2 {
1806 #[inline]
1807 fn product<I>(iter: I) -> Self
1808 where
1809 I: Iterator<Item = &'a Self>,
1810 {
1811 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1812 }
1813}
1814
1815impl Neg for Vec2 {
1816 type Output = Self;
1817 #[inline]
1818 fn neg(self) -> Self {
1819 Self {
1820 x: self.x.neg(),
1821 y: self.y.neg(),
1822 }
1823 }
1824}
1825
1826impl Neg for &Vec2 {
1827 type Output = Vec2;
1828 #[inline]
1829 fn neg(self) -> Vec2 {
1830 (*self).neg()
1831 }
1832}
1833
1834impl Index<usize> for Vec2 {
1835 type Output = f32;
1836 #[inline]
1837 fn index(&self, index: usize) -> &Self::Output {
1838 match index {
1839 0 => &self.x,
1840 1 => &self.y,
1841 _ => panic!("index out of bounds"),
1842 }
1843 }
1844}
1845
1846impl IndexMut<usize> for Vec2 {
1847 #[inline]
1848 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1849 match index {
1850 0 => &mut self.x,
1851 1 => &mut self.y,
1852 _ => panic!("index out of bounds"),
1853 }
1854 }
1855}
1856
1857impl fmt::Display for Vec2 {
1858 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1859 if let Some(p) = f.precision() {
1860 write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y)
1861 } else {
1862 write!(f, "[{}, {}]", self.x, self.y)
1863 }
1864 }
1865}
1866
1867impl fmt::Debug for Vec2 {
1868 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1869 fmt.debug_tuple(stringify!(Vec2))
1870 .field(&self.x)
1871 .field(&self.y)
1872 .finish()
1873 }
1874}
1875
1876impl From<[f32; 2]> for Vec2 {
1877 #[inline]
1878 fn from(a: [f32; 2]) -> Self {
1879 Self::new(a[0], a[1])
1880 }
1881}
1882
1883impl From<Vec2> for [f32; 2] {
1884 #[inline]
1885 fn from(v: Vec2) -> Self {
1886 [v.x, v.y]
1887 }
1888}
1889
1890impl From<(f32, f32)> for Vec2 {
1891 #[inline]
1892 fn from(t: (f32, f32)) -> Self {
1893 Self::new(t.0, t.1)
1894 }
1895}
1896
1897impl From<Vec2> for (f32, f32) {
1898 #[inline]
1899 fn from(v: Vec2) -> Self {
1900 (v.x, v.y)
1901 }
1902}
1903
1904impl From<BVec2> for Vec2 {
1905 #[inline]
1906 fn from(v: BVec2) -> Self {
1907 Self::new(f32::from(v.x), f32::from(v.y))
1908 }
1909}