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(
19 all(feature = "bytemuck", not(target_arch = "spirv")),
20 derive(bytemuck::Pod, bytemuck::Zeroable)
21)]
22#[cfg_attr(feature = "cuda", repr(align(8)))]
23#[cfg_attr(not(target_arch = "spirv"), repr(C))]
24#[cfg_attr(target_arch = "spirv", repr(simd))]
25pub struct Vec2 {
26 pub x: f32,
27 pub y: f32,
28}
29
30impl Vec2 {
31 pub const ZERO: Self = Self::splat(0.0);
33
34 pub const ONE: Self = Self::splat(1.0);
36
37 pub const NEG_ONE: Self = Self::splat(-1.0);
39
40 pub const MIN: Self = Self::splat(f32::MIN);
42
43 pub const MAX: Self = Self::splat(f32::MAX);
45
46 pub const NAN: Self = Self::splat(f32::NAN);
48
49 pub const INFINITY: Self = Self::splat(f32::INFINITY);
51
52 pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY);
54
55 pub const X: Self = Self::new(1.0, 0.0);
57
58 pub const Y: Self = Self::new(0.0, 1.0);
60
61 pub const NEG_X: Self = Self::new(-1.0, 0.0);
63
64 pub const NEG_Y: Self = Self::new(0.0, -1.0);
66
67 pub const AXES: [Self; 2] = [Self::X, Self::Y];
69
70 pub const USES_CORE_SIMD: bool = false;
72 pub const USES_NEON: bool = false;
74 pub const USES_SCALAR_MATH: bool = true;
76 pub const USES_SSE2: bool = false;
78 pub const USES_WASM32_SIMD: bool = false;
80
81 #[inline(always)]
83 #[must_use]
84 pub const fn new(x: f32, y: f32) -> Self {
85 Self { x, y }
86 }
87
88 #[inline]
90 #[must_use]
91 pub const fn splat(v: f32) -> Self {
92 Self { x: v, y: v }
93 }
94
95 #[inline]
97 #[must_use]
98 pub fn map<F>(self, f: F) -> Self
99 where
100 F: Fn(f32) -> f32,
101 {
102 Self::new(f(self.x), f(self.y))
103 }
104
105 #[inline]
111 #[must_use]
112 pub fn select(mask: BVec2, if_true: Self, if_false: Self) -> Self {
113 Self {
114 x: if mask.test(0) { if_true.x } else { if_false.x },
115 y: if mask.test(1) { if_true.y } else { if_false.y },
116 }
117 }
118
119 #[inline]
121 #[must_use]
122 pub const fn from_array(a: [f32; 2]) -> Self {
123 Self::new(a[0], a[1])
124 }
125
126 #[inline]
128 #[must_use]
129 pub const fn to_array(&self) -> [f32; 2] {
130 [self.x, self.y]
131 }
132
133 #[inline]
139 #[must_use]
140 pub const fn from_slice(slice: &[f32]) -> Self {
141 assert!(slice.len() >= 2);
142 Self::new(slice[0], slice[1])
143 }
144
145 #[inline]
151 pub fn write_to_slice(self, slice: &mut [f32]) {
152 slice[..2].copy_from_slice(&self.to_array());
153 }
154
155 #[inline]
157 #[must_use]
158 pub const fn extend(self, z: f32) -> Vec3 {
159 Vec3::new(self.x, self.y, z)
160 }
161
162 #[inline]
164 #[must_use]
165 pub fn with_x(mut self, x: f32) -> Self {
166 self.x = x;
167 self
168 }
169
170 #[inline]
172 #[must_use]
173 pub fn with_y(mut self, y: f32) -> Self {
174 self.y = y;
175 self
176 }
177
178 #[inline]
180 #[must_use]
181 pub fn dot(self, rhs: Self) -> f32 {
182 (self.x * rhs.x) + (self.y * rhs.y)
183 }
184
185 #[inline]
187 #[must_use]
188 pub fn dot_into_vec(self, rhs: Self) -> Self {
189 Self::splat(self.dot(rhs))
190 }
191
192 #[inline]
199 #[must_use]
200 pub fn min(self, rhs: Self) -> Self {
201 Self {
202 x: if self.x < rhs.x { self.x } else { rhs.x },
203 y: if self.y < rhs.y { self.y } else { rhs.y },
204 }
205 }
206
207 #[inline]
214 #[must_use]
215 pub fn max(self, rhs: Self) -> Self {
216 Self {
217 x: if self.x > rhs.x { self.x } else { rhs.x },
218 y: if self.y > rhs.y { self.y } else { rhs.y },
219 }
220 }
221
222 #[inline]
233 #[must_use]
234 pub fn clamp(self, min: Self, max: Self) -> Self {
235 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
236 self.max(min).min(max)
237 }
238
239 #[inline]
246 #[must_use]
247 pub fn min_element(self) -> f32 {
248 let min = |a, b| if a < b { a } else { b };
249 min(self.x, self.y)
250 }
251
252 #[inline]
259 #[must_use]
260 pub fn max_element(self) -> f32 {
261 let max = |a, b| if a > b { a } else { b };
262 max(self.x, self.y)
263 }
264
265 #[doc(alias = "argmin")]
267 #[inline]
268 #[must_use]
269 pub fn min_position(self) -> usize {
270 if self.x <= self.y {
271 0
272 } else {
273 1
274 }
275 }
276
277 #[doc(alias = "argmax")]
279 #[inline]
280 #[must_use]
281 pub fn max_position(self) -> usize {
282 if self.x >= self.y {
283 0
284 } else {
285 1
286 }
287 }
288
289 #[inline]
293 #[must_use]
294 pub fn element_sum(self) -> f32 {
295 self.x + self.y
296 }
297
298 #[inline]
302 #[must_use]
303 pub fn element_product(self) -> f32 {
304 self.x * self.y
305 }
306
307 #[inline]
313 #[must_use]
314 pub fn cmpeq(self, rhs: Self) -> BVec2 {
315 BVec2::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y))
316 }
317
318 #[inline]
324 #[must_use]
325 pub fn cmpne(self, rhs: Self) -> BVec2 {
326 BVec2::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y))
327 }
328
329 #[inline]
335 #[must_use]
336 pub fn cmpge(self, rhs: Self) -> BVec2 {
337 BVec2::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y))
338 }
339
340 #[inline]
346 #[must_use]
347 pub fn cmpgt(self, rhs: Self) -> BVec2 {
348 BVec2::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y))
349 }
350
351 #[inline]
357 #[must_use]
358 pub fn cmple(self, rhs: Self) -> BVec2 {
359 BVec2::new(self.x.le(&rhs.x), self.y.le(&rhs.y))
360 }
361
362 #[inline]
368 #[must_use]
369 pub fn cmplt(self, rhs: Self) -> BVec2 {
370 BVec2::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y))
371 }
372
373 #[inline]
375 #[must_use]
376 pub fn abs(self) -> Self {
377 Self {
378 x: math::abs(self.x),
379 y: math::abs(self.y),
380 }
381 }
382
383 #[inline]
389 #[must_use]
390 pub fn signum(self) -> Self {
391 Self {
392 x: math::signum(self.x),
393 y: math::signum(self.y),
394 }
395 }
396
397 #[inline]
399 #[must_use]
400 pub fn copysign(self, rhs: Self) -> Self {
401 Self {
402 x: math::copysign(self.x, rhs.x),
403 y: math::copysign(self.y, rhs.y),
404 }
405 }
406
407 #[inline]
415 #[must_use]
416 pub fn is_negative_bitmask(self) -> u32 {
417 (self.x.is_sign_negative() as u32) | ((self.y.is_sign_negative() as u32) << 1)
418 }
419
420 #[inline]
423 #[must_use]
424 pub fn is_finite(self) -> bool {
425 self.x.is_finite() && self.y.is_finite()
426 }
427
428 #[inline]
432 #[must_use]
433 pub fn is_finite_mask(self) -> BVec2 {
434 BVec2::new(self.x.is_finite(), self.y.is_finite())
435 }
436
437 #[inline]
439 #[must_use]
440 pub fn is_nan(self) -> bool {
441 self.x.is_nan() || self.y.is_nan()
442 }
443
444 #[inline]
448 #[must_use]
449 pub fn is_nan_mask(self) -> BVec2 {
450 BVec2::new(self.x.is_nan(), self.y.is_nan())
451 }
452
453 #[doc(alias = "magnitude")]
455 #[inline]
456 #[must_use]
457 pub fn length(self) -> f32 {
458 math::sqrt(self.dot(self))
459 }
460
461 #[doc(alias = "magnitude2")]
465 #[inline]
466 #[must_use]
467 pub fn length_squared(self) -> f32 {
468 self.dot(self)
469 }
470
471 #[inline]
475 #[must_use]
476 pub fn length_recip(self) -> f32 {
477 self.length().recip()
478 }
479
480 #[inline]
482 #[must_use]
483 pub fn distance(self, rhs: Self) -> f32 {
484 (self - rhs).length()
485 }
486
487 #[inline]
489 #[must_use]
490 pub fn distance_squared(self, rhs: Self) -> f32 {
491 (self - rhs).length_squared()
492 }
493
494 #[inline]
496 #[must_use]
497 pub fn div_euclid(self, rhs: Self) -> Self {
498 Self::new(
499 math::div_euclid(self.x, rhs.x),
500 math::div_euclid(self.y, rhs.y),
501 )
502 }
503
504 #[inline]
508 #[must_use]
509 pub fn rem_euclid(self, rhs: Self) -> Self {
510 Self::new(
511 math::rem_euclid(self.x, rhs.x),
512 math::rem_euclid(self.y, rhs.y),
513 )
514 }
515
516 #[inline]
526 #[must_use]
527 pub fn normalize(self) -> Self {
528 #[allow(clippy::let_and_return)]
529 let normalized = self.mul(self.length_recip());
530 glam_assert!(normalized.is_finite());
531 normalized
532 }
533
534 #[inline]
541 #[must_use]
542 pub fn try_normalize(self) -> Option<Self> {
543 let rcp = self.length_recip();
544 if rcp.is_finite() && rcp > 0.0 {
545 Some(self * rcp)
546 } else {
547 None
548 }
549 }
550
551 #[inline]
559 #[must_use]
560 pub fn normalize_or(self, fallback: Self) -> Self {
561 let rcp = self.length_recip();
562 if rcp.is_finite() && rcp > 0.0 {
563 self * rcp
564 } else {
565 fallback
566 }
567 }
568
569 #[inline]
576 #[must_use]
577 pub fn normalize_or_zero(self) -> Self {
578 self.normalize_or(Self::ZERO)
579 }
580
581 #[inline]
585 #[must_use]
586 pub fn normalize_and_length(self) -> (Self, f32) {
587 let length = self.length();
588 let rcp = 1.0 / length;
589 if rcp.is_finite() && rcp > 0.0 {
590 (self * rcp, length)
591 } else {
592 (Self::X, 0.0)
593 }
594 }
595
596 #[inline]
600 #[must_use]
601 pub fn is_normalized(self) -> bool {
602 math::abs(self.length_squared() - 1.0) <= 2e-4
603 }
604
605 #[inline]
613 #[must_use]
614 pub fn project_onto(self, rhs: Self) -> Self {
615 let other_len_sq_rcp = rhs.dot(rhs).recip();
616 glam_assert!(other_len_sq_rcp.is_finite());
617 rhs * self.dot(rhs) * other_len_sq_rcp
618 }
619
620 #[doc(alias("plane"))]
631 #[inline]
632 #[must_use]
633 pub fn reject_from(self, rhs: Self) -> Self {
634 self - self.project_onto(rhs)
635 }
636
637 #[inline]
645 #[must_use]
646 pub fn project_onto_normalized(self, rhs: Self) -> Self {
647 glam_assert!(rhs.is_normalized());
648 rhs * self.dot(rhs)
649 }
650
651 #[doc(alias("plane"))]
662 #[inline]
663 #[must_use]
664 pub fn reject_from_normalized(self, rhs: Self) -> Self {
665 self - self.project_onto_normalized(rhs)
666 }
667
668 #[inline]
671 #[must_use]
672 pub fn round(self) -> Self {
673 Self {
674 x: math::round(self.x),
675 y: math::round(self.y),
676 }
677 }
678
679 #[inline]
682 #[must_use]
683 pub fn floor(self) -> Self {
684 Self {
685 x: math::floor(self.x),
686 y: math::floor(self.y),
687 }
688 }
689
690 #[inline]
693 #[must_use]
694 pub fn ceil(self) -> Self {
695 Self {
696 x: math::ceil(self.x),
697 y: math::ceil(self.y),
698 }
699 }
700
701 #[inline]
704 #[must_use]
705 pub fn trunc(self) -> Self {
706 Self {
707 x: math::trunc(self.x),
708 y: math::trunc(self.y),
709 }
710 }
711
712 #[inline]
719 #[must_use]
720 pub fn fract(self) -> Self {
721 self - self.trunc()
722 }
723
724 #[inline]
731 #[must_use]
732 pub fn fract_gl(self) -> Self {
733 self - self.floor()
734 }
735
736 #[inline]
739 #[must_use]
740 pub fn exp(self) -> Self {
741 Self::new(math::exp(self.x), math::exp(self.y))
742 }
743
744 #[inline]
746 #[must_use]
747 pub fn powf(self, n: f32) -> Self {
748 Self::new(math::powf(self.x, n), math::powf(self.y, n))
749 }
750
751 #[inline]
753 #[must_use]
754 pub fn recip(self) -> Self {
755 Self {
756 x: 1.0 / self.x,
757 y: 1.0 / self.y,
758 }
759 }
760
761 #[doc(alias = "mix")]
767 #[inline]
768 #[must_use]
769 pub fn lerp(self, rhs: Self, s: f32) -> Self {
770 self * (1.0 - s) + rhs * s
771 }
772
773 #[inline]
778 #[must_use]
779 pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
780 let a = rhs - *self;
781 let len = a.length();
782 if len <= d || len <= 1e-4 {
783 return rhs;
784 }
785 *self + a / len * d
786 }
787
788 #[inline]
794 pub fn midpoint(self, rhs: Self) -> Self {
795 (self + rhs) * 0.5
796 }
797
798 #[inline]
808 #[must_use]
809 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
810 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
811 }
812
813 #[inline]
819 #[must_use]
820 pub fn clamp_length(self, min: f32, max: f32) -> Self {
821 glam_assert!(0.0 <= min);
822 glam_assert!(min <= max);
823 let length_sq = self.length_squared();
824 if length_sq < min * min {
825 min * (self / math::sqrt(length_sq))
826 } else if length_sq > max * max {
827 max * (self / math::sqrt(length_sq))
828 } else {
829 self
830 }
831 }
832
833 #[inline]
839 #[must_use]
840 pub fn clamp_length_max(self, max: f32) -> Self {
841 glam_assert!(0.0 <= max);
842 let length_sq = self.length_squared();
843 if length_sq > max * max {
844 max * (self / math::sqrt(length_sq))
845 } else {
846 self
847 }
848 }
849
850 #[inline]
856 #[must_use]
857 pub fn clamp_length_min(self, min: f32) -> Self {
858 glam_assert!(0.0 <= min);
859 let length_sq = self.length_squared();
860 if length_sq < min * min {
861 min * (self / math::sqrt(length_sq))
862 } else {
863 self
864 }
865 }
866
867 #[inline]
875 #[must_use]
876 pub fn mul_add(self, a: Self, b: Self) -> Self {
877 Self::new(
878 math::mul_add(self.x, a.x, b.x),
879 math::mul_add(self.y, a.y, b.y),
880 )
881 }
882
883 #[inline]
892 #[must_use]
893 pub fn reflect(self, normal: Self) -> Self {
894 glam_assert!(normal.is_normalized());
895 self - 2.0 * self.dot(normal) * normal
896 }
897
898 #[inline]
908 #[must_use]
909 pub fn refract(self, normal: Self, eta: f32) -> Self {
910 glam_assert!(self.is_normalized());
911 glam_assert!(normal.is_normalized());
912 let n_dot_i = normal.dot(self);
913 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
914 if k >= 0.0 {
915 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
916 } else {
917 Self::ZERO
918 }
919 }
920
921 #[inline]
926 #[must_use]
927 pub fn from_angle(angle: f32) -> Self {
928 let (sin, cos) = math::sin_cos(angle);
929 Self { x: cos, y: sin }
930 }
931
932 #[inline]
936 #[must_use]
937 pub fn to_angle(self) -> f32 {
938 math::atan2(self.y, self.x)
939 }
940
941 #[inline]
942 #[must_use]
943 #[deprecated(
944 since = "0.27.0",
945 note = "Use angle_to() instead, the semantics of angle_between will change in the future."
946 )]
947 pub fn angle_between(self, rhs: Self) -> f32 {
948 self.angle_to(rhs)
949 }
950
951 #[inline]
955 #[must_use]
956 pub fn angle_to(self, rhs: Self) -> f32 {
957 let angle = math::acos_approx(
958 self.dot(rhs) / math::sqrt(self.length_squared() * rhs.length_squared()),
959 );
960
961 angle * math::signum(self.perp_dot(rhs))
962 }
963
964 #[inline]
966 #[must_use]
967 pub fn perp(self) -> Self {
968 Self {
969 x: -self.y,
970 y: self.x,
971 }
972 }
973
974 #[doc(alias = "wedge")]
977 #[doc(alias = "cross")]
978 #[doc(alias = "determinant")]
979 #[inline]
980 #[must_use]
981 pub fn perp_dot(self, rhs: Self) -> f32 {
982 (self.x * rhs.y) - (self.y * rhs.x)
983 }
984
985 #[inline]
989 #[must_use]
990 pub fn rotate(self, rhs: Self) -> Self {
991 Self {
992 x: self.x * rhs.x - self.y * rhs.y,
993 y: self.y * rhs.x + self.x * rhs.y,
994 }
995 }
996
997 #[inline]
1003 #[must_use]
1004 pub fn rotate_towards(&self, rhs: Self, max_angle: f32) -> Self {
1005 let a = self.angle_to(rhs);
1006 let abs_a = math::abs(a);
1007 let angle = max_angle.clamp(abs_a - core::f32::consts::PI, abs_a) * math::signum(a);
1009 Self::from_angle(angle).rotate(*self)
1010 }
1011
1012 #[inline]
1014 #[must_use]
1015 pub fn as_dvec2(&self) -> crate::DVec2 {
1016 crate::DVec2::new(self.x as f64, self.y as f64)
1017 }
1018
1019 #[inline]
1021 #[must_use]
1022 pub fn as_i8vec2(&self) -> crate::I8Vec2 {
1023 crate::I8Vec2::new(self.x as i8, self.y as i8)
1024 }
1025
1026 #[inline]
1028 #[must_use]
1029 pub fn as_u8vec2(&self) -> crate::U8Vec2 {
1030 crate::U8Vec2::new(self.x as u8, self.y as u8)
1031 }
1032
1033 #[inline]
1035 #[must_use]
1036 pub fn as_i16vec2(&self) -> crate::I16Vec2 {
1037 crate::I16Vec2::new(self.x as i16, self.y as i16)
1038 }
1039
1040 #[inline]
1042 #[must_use]
1043 pub fn as_u16vec2(&self) -> crate::U16Vec2 {
1044 crate::U16Vec2::new(self.x as u16, self.y as u16)
1045 }
1046
1047 #[inline]
1049 #[must_use]
1050 pub fn as_ivec2(&self) -> crate::IVec2 {
1051 crate::IVec2::new(self.x as i32, self.y as i32)
1052 }
1053
1054 #[inline]
1056 #[must_use]
1057 pub fn as_uvec2(&self) -> crate::UVec2 {
1058 crate::UVec2::new(self.x as u32, self.y as u32)
1059 }
1060
1061 #[inline]
1063 #[must_use]
1064 pub fn as_i64vec2(&self) -> crate::I64Vec2 {
1065 crate::I64Vec2::new(self.x as i64, self.y as i64)
1066 }
1067
1068 #[inline]
1070 #[must_use]
1071 pub fn as_u64vec2(&self) -> crate::U64Vec2 {
1072 crate::U64Vec2::new(self.x as u64, self.y as u64)
1073 }
1074
1075 #[inline]
1077 #[must_use]
1078 pub fn as_usizevec2(&self) -> crate::USizeVec2 {
1079 crate::USizeVec2::new(self.x as usize, self.y as usize)
1080 }
1081}
1082
1083impl Default for Vec2 {
1084 #[inline(always)]
1085 fn default() -> Self {
1086 Self::ZERO
1087 }
1088}
1089
1090impl Div for Vec2 {
1091 type Output = Self;
1092 #[inline]
1093 fn div(self, rhs: Self) -> Self {
1094 Self {
1095 x: self.x.div(rhs.x),
1096 y: self.y.div(rhs.y),
1097 }
1098 }
1099}
1100
1101impl Div<&Self> for Vec2 {
1102 type Output = Self;
1103 #[inline]
1104 fn div(self, rhs: &Self) -> Self {
1105 self.div(*rhs)
1106 }
1107}
1108
1109impl Div<&Vec2> for &Vec2 {
1110 type Output = Vec2;
1111 #[inline]
1112 fn div(self, rhs: &Vec2) -> Vec2 {
1113 (*self).div(*rhs)
1114 }
1115}
1116
1117impl Div<Vec2> for &Vec2 {
1118 type Output = Vec2;
1119 #[inline]
1120 fn div(self, rhs: Vec2) -> Vec2 {
1121 (*self).div(rhs)
1122 }
1123}
1124
1125impl DivAssign for Vec2 {
1126 #[inline]
1127 fn div_assign(&mut self, rhs: Self) {
1128 self.x.div_assign(rhs.x);
1129 self.y.div_assign(rhs.y);
1130 }
1131}
1132
1133impl DivAssign<&Self> for Vec2 {
1134 #[inline]
1135 fn div_assign(&mut self, rhs: &Self) {
1136 self.div_assign(*rhs);
1137 }
1138}
1139
1140impl Div<f32> for Vec2 {
1141 type Output = Self;
1142 #[inline]
1143 fn div(self, rhs: f32) -> Self {
1144 Self {
1145 x: self.x.div(rhs),
1146 y: self.y.div(rhs),
1147 }
1148 }
1149}
1150
1151impl Div<&f32> for Vec2 {
1152 type Output = Self;
1153 #[inline]
1154 fn div(self, rhs: &f32) -> Self {
1155 self.div(*rhs)
1156 }
1157}
1158
1159impl Div<&f32> for &Vec2 {
1160 type Output = Vec2;
1161 #[inline]
1162 fn div(self, rhs: &f32) -> Vec2 {
1163 (*self).div(*rhs)
1164 }
1165}
1166
1167impl Div<f32> for &Vec2 {
1168 type Output = Vec2;
1169 #[inline]
1170 fn div(self, rhs: f32) -> Vec2 {
1171 (*self).div(rhs)
1172 }
1173}
1174
1175impl DivAssign<f32> for Vec2 {
1176 #[inline]
1177 fn div_assign(&mut self, rhs: f32) {
1178 self.x.div_assign(rhs);
1179 self.y.div_assign(rhs);
1180 }
1181}
1182
1183impl DivAssign<&f32> for Vec2 {
1184 #[inline]
1185 fn div_assign(&mut self, rhs: &f32) {
1186 self.div_assign(*rhs);
1187 }
1188}
1189
1190impl Div<Vec2> for f32 {
1191 type Output = Vec2;
1192 #[inline]
1193 fn div(self, rhs: Vec2) -> Vec2 {
1194 Vec2 {
1195 x: self.div(rhs.x),
1196 y: self.div(rhs.y),
1197 }
1198 }
1199}
1200
1201impl Div<&Vec2> for f32 {
1202 type Output = Vec2;
1203 #[inline]
1204 fn div(self, rhs: &Vec2) -> Vec2 {
1205 self.div(*rhs)
1206 }
1207}
1208
1209impl Div<&Vec2> for &f32 {
1210 type Output = Vec2;
1211 #[inline]
1212 fn div(self, rhs: &Vec2) -> Vec2 {
1213 (*self).div(*rhs)
1214 }
1215}
1216
1217impl Div<Vec2> for &f32 {
1218 type Output = Vec2;
1219 #[inline]
1220 fn div(self, rhs: Vec2) -> Vec2 {
1221 (*self).div(rhs)
1222 }
1223}
1224
1225impl Mul for Vec2 {
1226 type Output = Self;
1227 #[inline]
1228 fn mul(self, rhs: Self) -> Self {
1229 Self {
1230 x: self.x.mul(rhs.x),
1231 y: self.y.mul(rhs.y),
1232 }
1233 }
1234}
1235
1236impl Mul<&Self> for Vec2 {
1237 type Output = Self;
1238 #[inline]
1239 fn mul(self, rhs: &Self) -> Self {
1240 self.mul(*rhs)
1241 }
1242}
1243
1244impl Mul<&Vec2> for &Vec2 {
1245 type Output = Vec2;
1246 #[inline]
1247 fn mul(self, rhs: &Vec2) -> Vec2 {
1248 (*self).mul(*rhs)
1249 }
1250}
1251
1252impl Mul<Vec2> for &Vec2 {
1253 type Output = Vec2;
1254 #[inline]
1255 fn mul(self, rhs: Vec2) -> Vec2 {
1256 (*self).mul(rhs)
1257 }
1258}
1259
1260impl MulAssign for Vec2 {
1261 #[inline]
1262 fn mul_assign(&mut self, rhs: Self) {
1263 self.x.mul_assign(rhs.x);
1264 self.y.mul_assign(rhs.y);
1265 }
1266}
1267
1268impl MulAssign<&Self> for Vec2 {
1269 #[inline]
1270 fn mul_assign(&mut self, rhs: &Self) {
1271 self.mul_assign(*rhs);
1272 }
1273}
1274
1275impl Mul<f32> for Vec2 {
1276 type Output = Self;
1277 #[inline]
1278 fn mul(self, rhs: f32) -> Self {
1279 Self {
1280 x: self.x.mul(rhs),
1281 y: self.y.mul(rhs),
1282 }
1283 }
1284}
1285
1286impl Mul<&f32> for Vec2 {
1287 type Output = Self;
1288 #[inline]
1289 fn mul(self, rhs: &f32) -> Self {
1290 self.mul(*rhs)
1291 }
1292}
1293
1294impl Mul<&f32> for &Vec2 {
1295 type Output = Vec2;
1296 #[inline]
1297 fn mul(self, rhs: &f32) -> Vec2 {
1298 (*self).mul(*rhs)
1299 }
1300}
1301
1302impl Mul<f32> for &Vec2 {
1303 type Output = Vec2;
1304 #[inline]
1305 fn mul(self, rhs: f32) -> Vec2 {
1306 (*self).mul(rhs)
1307 }
1308}
1309
1310impl MulAssign<f32> for Vec2 {
1311 #[inline]
1312 fn mul_assign(&mut self, rhs: f32) {
1313 self.x.mul_assign(rhs);
1314 self.y.mul_assign(rhs);
1315 }
1316}
1317
1318impl MulAssign<&f32> for Vec2 {
1319 #[inline]
1320 fn mul_assign(&mut self, rhs: &f32) {
1321 self.mul_assign(*rhs);
1322 }
1323}
1324
1325impl Mul<Vec2> for f32 {
1326 type Output = Vec2;
1327 #[inline]
1328 fn mul(self, rhs: Vec2) -> Vec2 {
1329 Vec2 {
1330 x: self.mul(rhs.x),
1331 y: self.mul(rhs.y),
1332 }
1333 }
1334}
1335
1336impl Mul<&Vec2> for f32 {
1337 type Output = Vec2;
1338 #[inline]
1339 fn mul(self, rhs: &Vec2) -> Vec2 {
1340 self.mul(*rhs)
1341 }
1342}
1343
1344impl Mul<&Vec2> for &f32 {
1345 type Output = Vec2;
1346 #[inline]
1347 fn mul(self, rhs: &Vec2) -> Vec2 {
1348 (*self).mul(*rhs)
1349 }
1350}
1351
1352impl Mul<Vec2> for &f32 {
1353 type Output = Vec2;
1354 #[inline]
1355 fn mul(self, rhs: Vec2) -> Vec2 {
1356 (*self).mul(rhs)
1357 }
1358}
1359
1360impl Add for Vec2 {
1361 type Output = Self;
1362 #[inline]
1363 fn add(self, rhs: Self) -> Self {
1364 Self {
1365 x: self.x.add(rhs.x),
1366 y: self.y.add(rhs.y),
1367 }
1368 }
1369}
1370
1371impl Add<&Self> for Vec2 {
1372 type Output = Self;
1373 #[inline]
1374 fn add(self, rhs: &Self) -> Self {
1375 self.add(*rhs)
1376 }
1377}
1378
1379impl Add<&Vec2> for &Vec2 {
1380 type Output = Vec2;
1381 #[inline]
1382 fn add(self, rhs: &Vec2) -> Vec2 {
1383 (*self).add(*rhs)
1384 }
1385}
1386
1387impl Add<Vec2> for &Vec2 {
1388 type Output = Vec2;
1389 #[inline]
1390 fn add(self, rhs: Vec2) -> Vec2 {
1391 (*self).add(rhs)
1392 }
1393}
1394
1395impl AddAssign for Vec2 {
1396 #[inline]
1397 fn add_assign(&mut self, rhs: Self) {
1398 self.x.add_assign(rhs.x);
1399 self.y.add_assign(rhs.y);
1400 }
1401}
1402
1403impl AddAssign<&Self> for Vec2 {
1404 #[inline]
1405 fn add_assign(&mut self, rhs: &Self) {
1406 self.add_assign(*rhs);
1407 }
1408}
1409
1410impl Add<f32> for Vec2 {
1411 type Output = Self;
1412 #[inline]
1413 fn add(self, rhs: f32) -> Self {
1414 Self {
1415 x: self.x.add(rhs),
1416 y: self.y.add(rhs),
1417 }
1418 }
1419}
1420
1421impl Add<&f32> for Vec2 {
1422 type Output = Self;
1423 #[inline]
1424 fn add(self, rhs: &f32) -> Self {
1425 self.add(*rhs)
1426 }
1427}
1428
1429impl Add<&f32> for &Vec2 {
1430 type Output = Vec2;
1431 #[inline]
1432 fn add(self, rhs: &f32) -> Vec2 {
1433 (*self).add(*rhs)
1434 }
1435}
1436
1437impl Add<f32> for &Vec2 {
1438 type Output = Vec2;
1439 #[inline]
1440 fn add(self, rhs: f32) -> Vec2 {
1441 (*self).add(rhs)
1442 }
1443}
1444
1445impl AddAssign<f32> for Vec2 {
1446 #[inline]
1447 fn add_assign(&mut self, rhs: f32) {
1448 self.x.add_assign(rhs);
1449 self.y.add_assign(rhs);
1450 }
1451}
1452
1453impl AddAssign<&f32> for Vec2 {
1454 #[inline]
1455 fn add_assign(&mut self, rhs: &f32) {
1456 self.add_assign(*rhs);
1457 }
1458}
1459
1460impl Add<Vec2> for f32 {
1461 type Output = Vec2;
1462 #[inline]
1463 fn add(self, rhs: Vec2) -> Vec2 {
1464 Vec2 {
1465 x: self.add(rhs.x),
1466 y: self.add(rhs.y),
1467 }
1468 }
1469}
1470
1471impl Add<&Vec2> for f32 {
1472 type Output = Vec2;
1473 #[inline]
1474 fn add(self, rhs: &Vec2) -> Vec2 {
1475 self.add(*rhs)
1476 }
1477}
1478
1479impl Add<&Vec2> for &f32 {
1480 type Output = Vec2;
1481 #[inline]
1482 fn add(self, rhs: &Vec2) -> Vec2 {
1483 (*self).add(*rhs)
1484 }
1485}
1486
1487impl Add<Vec2> for &f32 {
1488 type Output = Vec2;
1489 #[inline]
1490 fn add(self, rhs: Vec2) -> Vec2 {
1491 (*self).add(rhs)
1492 }
1493}
1494
1495impl Sub for Vec2 {
1496 type Output = Self;
1497 #[inline]
1498 fn sub(self, rhs: Self) -> Self {
1499 Self {
1500 x: self.x.sub(rhs.x),
1501 y: self.y.sub(rhs.y),
1502 }
1503 }
1504}
1505
1506impl Sub<&Self> for Vec2 {
1507 type Output = Self;
1508 #[inline]
1509 fn sub(self, rhs: &Self) -> Self {
1510 self.sub(*rhs)
1511 }
1512}
1513
1514impl Sub<&Vec2> for &Vec2 {
1515 type Output = Vec2;
1516 #[inline]
1517 fn sub(self, rhs: &Vec2) -> Vec2 {
1518 (*self).sub(*rhs)
1519 }
1520}
1521
1522impl Sub<Vec2> for &Vec2 {
1523 type Output = Vec2;
1524 #[inline]
1525 fn sub(self, rhs: Vec2) -> Vec2 {
1526 (*self).sub(rhs)
1527 }
1528}
1529
1530impl SubAssign for Vec2 {
1531 #[inline]
1532 fn sub_assign(&mut self, rhs: Self) {
1533 self.x.sub_assign(rhs.x);
1534 self.y.sub_assign(rhs.y);
1535 }
1536}
1537
1538impl SubAssign<&Self> for Vec2 {
1539 #[inline]
1540 fn sub_assign(&mut self, rhs: &Self) {
1541 self.sub_assign(*rhs);
1542 }
1543}
1544
1545impl Sub<f32> for Vec2 {
1546 type Output = Self;
1547 #[inline]
1548 fn sub(self, rhs: f32) -> Self {
1549 Self {
1550 x: self.x.sub(rhs),
1551 y: self.y.sub(rhs),
1552 }
1553 }
1554}
1555
1556impl Sub<&f32> for Vec2 {
1557 type Output = Self;
1558 #[inline]
1559 fn sub(self, rhs: &f32) -> Self {
1560 self.sub(*rhs)
1561 }
1562}
1563
1564impl Sub<&f32> for &Vec2 {
1565 type Output = Vec2;
1566 #[inline]
1567 fn sub(self, rhs: &f32) -> Vec2 {
1568 (*self).sub(*rhs)
1569 }
1570}
1571
1572impl Sub<f32> for &Vec2 {
1573 type Output = Vec2;
1574 #[inline]
1575 fn sub(self, rhs: f32) -> Vec2 {
1576 (*self).sub(rhs)
1577 }
1578}
1579
1580impl SubAssign<f32> for Vec2 {
1581 #[inline]
1582 fn sub_assign(&mut self, rhs: f32) {
1583 self.x.sub_assign(rhs);
1584 self.y.sub_assign(rhs);
1585 }
1586}
1587
1588impl SubAssign<&f32> for Vec2 {
1589 #[inline]
1590 fn sub_assign(&mut self, rhs: &f32) {
1591 self.sub_assign(*rhs);
1592 }
1593}
1594
1595impl Sub<Vec2> for f32 {
1596 type Output = Vec2;
1597 #[inline]
1598 fn sub(self, rhs: Vec2) -> Vec2 {
1599 Vec2 {
1600 x: self.sub(rhs.x),
1601 y: self.sub(rhs.y),
1602 }
1603 }
1604}
1605
1606impl Sub<&Vec2> for f32 {
1607 type Output = Vec2;
1608 #[inline]
1609 fn sub(self, rhs: &Vec2) -> Vec2 {
1610 self.sub(*rhs)
1611 }
1612}
1613
1614impl Sub<&Vec2> for &f32 {
1615 type Output = Vec2;
1616 #[inline]
1617 fn sub(self, rhs: &Vec2) -> Vec2 {
1618 (*self).sub(*rhs)
1619 }
1620}
1621
1622impl Sub<Vec2> for &f32 {
1623 type Output = Vec2;
1624 #[inline]
1625 fn sub(self, rhs: Vec2) -> Vec2 {
1626 (*self).sub(rhs)
1627 }
1628}
1629
1630impl Rem for Vec2 {
1631 type Output = Self;
1632 #[inline]
1633 fn rem(self, rhs: Self) -> Self {
1634 Self {
1635 x: self.x.rem(rhs.x),
1636 y: self.y.rem(rhs.y),
1637 }
1638 }
1639}
1640
1641impl Rem<&Self> for Vec2 {
1642 type Output = Self;
1643 #[inline]
1644 fn rem(self, rhs: &Self) -> Self {
1645 self.rem(*rhs)
1646 }
1647}
1648
1649impl Rem<&Vec2> for &Vec2 {
1650 type Output = Vec2;
1651 #[inline]
1652 fn rem(self, rhs: &Vec2) -> Vec2 {
1653 (*self).rem(*rhs)
1654 }
1655}
1656
1657impl Rem<Vec2> for &Vec2 {
1658 type Output = Vec2;
1659 #[inline]
1660 fn rem(self, rhs: Vec2) -> Vec2 {
1661 (*self).rem(rhs)
1662 }
1663}
1664
1665impl RemAssign for Vec2 {
1666 #[inline]
1667 fn rem_assign(&mut self, rhs: Self) {
1668 self.x.rem_assign(rhs.x);
1669 self.y.rem_assign(rhs.y);
1670 }
1671}
1672
1673impl RemAssign<&Self> for Vec2 {
1674 #[inline]
1675 fn rem_assign(&mut self, rhs: &Self) {
1676 self.rem_assign(*rhs);
1677 }
1678}
1679
1680impl Rem<f32> for Vec2 {
1681 type Output = Self;
1682 #[inline]
1683 fn rem(self, rhs: f32) -> Self {
1684 Self {
1685 x: self.x.rem(rhs),
1686 y: self.y.rem(rhs),
1687 }
1688 }
1689}
1690
1691impl Rem<&f32> for Vec2 {
1692 type Output = Self;
1693 #[inline]
1694 fn rem(self, rhs: &f32) -> Self {
1695 self.rem(*rhs)
1696 }
1697}
1698
1699impl Rem<&f32> for &Vec2 {
1700 type Output = Vec2;
1701 #[inline]
1702 fn rem(self, rhs: &f32) -> Vec2 {
1703 (*self).rem(*rhs)
1704 }
1705}
1706
1707impl Rem<f32> for &Vec2 {
1708 type Output = Vec2;
1709 #[inline]
1710 fn rem(self, rhs: f32) -> Vec2 {
1711 (*self).rem(rhs)
1712 }
1713}
1714
1715impl RemAssign<f32> for Vec2 {
1716 #[inline]
1717 fn rem_assign(&mut self, rhs: f32) {
1718 self.x.rem_assign(rhs);
1719 self.y.rem_assign(rhs);
1720 }
1721}
1722
1723impl RemAssign<&f32> for Vec2 {
1724 #[inline]
1725 fn rem_assign(&mut self, rhs: &f32) {
1726 self.rem_assign(*rhs);
1727 }
1728}
1729
1730impl Rem<Vec2> for f32 {
1731 type Output = Vec2;
1732 #[inline]
1733 fn rem(self, rhs: Vec2) -> Vec2 {
1734 Vec2 {
1735 x: self.rem(rhs.x),
1736 y: self.rem(rhs.y),
1737 }
1738 }
1739}
1740
1741impl Rem<&Vec2> for f32 {
1742 type Output = Vec2;
1743 #[inline]
1744 fn rem(self, rhs: &Vec2) -> Vec2 {
1745 self.rem(*rhs)
1746 }
1747}
1748
1749impl Rem<&Vec2> for &f32 {
1750 type Output = Vec2;
1751 #[inline]
1752 fn rem(self, rhs: &Vec2) -> Vec2 {
1753 (*self).rem(*rhs)
1754 }
1755}
1756
1757impl Rem<Vec2> for &f32 {
1758 type Output = Vec2;
1759 #[inline]
1760 fn rem(self, rhs: Vec2) -> Vec2 {
1761 (*self).rem(rhs)
1762 }
1763}
1764
1765#[cfg(not(target_arch = "spirv"))]
1766impl AsRef<[f32; 2]> for Vec2 {
1767 #[inline]
1768 fn as_ref(&self) -> &[f32; 2] {
1769 unsafe { &*(self as *const Self as *const [f32; 2]) }
1770 }
1771}
1772
1773#[cfg(not(target_arch = "spirv"))]
1774impl AsMut<[f32; 2]> for Vec2 {
1775 #[inline]
1776 fn as_mut(&mut self) -> &mut [f32; 2] {
1777 unsafe { &mut *(self as *mut Self as *mut [f32; 2]) }
1778 }
1779}
1780
1781impl Sum for Vec2 {
1782 #[inline]
1783 fn sum<I>(iter: I) -> Self
1784 where
1785 I: Iterator<Item = Self>,
1786 {
1787 iter.fold(Self::ZERO, Self::add)
1788 }
1789}
1790
1791impl<'a> Sum<&'a Self> for Vec2 {
1792 #[inline]
1793 fn sum<I>(iter: I) -> Self
1794 where
1795 I: Iterator<Item = &'a Self>,
1796 {
1797 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1798 }
1799}
1800
1801impl Product for Vec2 {
1802 #[inline]
1803 fn product<I>(iter: I) -> Self
1804 where
1805 I: Iterator<Item = Self>,
1806 {
1807 iter.fold(Self::ONE, Self::mul)
1808 }
1809}
1810
1811impl<'a> Product<&'a Self> for Vec2 {
1812 #[inline]
1813 fn product<I>(iter: I) -> Self
1814 where
1815 I: Iterator<Item = &'a Self>,
1816 {
1817 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1818 }
1819}
1820
1821impl Neg for Vec2 {
1822 type Output = Self;
1823 #[inline]
1824 fn neg(self) -> Self {
1825 Self {
1826 x: self.x.neg(),
1827 y: self.y.neg(),
1828 }
1829 }
1830}
1831
1832impl Neg for &Vec2 {
1833 type Output = Vec2;
1834 #[inline]
1835 fn neg(self) -> Vec2 {
1836 (*self).neg()
1837 }
1838}
1839
1840impl Index<usize> for Vec2 {
1841 type Output = f32;
1842 #[inline]
1843 fn index(&self, index: usize) -> &Self::Output {
1844 match index {
1845 0 => &self.x,
1846 1 => &self.y,
1847 _ => panic!("index out of bounds"),
1848 }
1849 }
1850}
1851
1852impl IndexMut<usize> for Vec2 {
1853 #[inline]
1854 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1855 match index {
1856 0 => &mut self.x,
1857 1 => &mut self.y,
1858 _ => panic!("index out of bounds"),
1859 }
1860 }
1861}
1862
1863impl fmt::Display for Vec2 {
1864 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1865 if let Some(p) = f.precision() {
1866 write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y)
1867 } else {
1868 write!(f, "[{}, {}]", self.x, self.y)
1869 }
1870 }
1871}
1872
1873impl fmt::Debug for Vec2 {
1874 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1875 fmt.debug_tuple(stringify!(Vec2))
1876 .field(&self.x)
1877 .field(&self.y)
1878 .finish()
1879 }
1880}
1881
1882impl From<[f32; 2]> for Vec2 {
1883 #[inline]
1884 fn from(a: [f32; 2]) -> Self {
1885 Self::new(a[0], a[1])
1886 }
1887}
1888
1889impl From<Vec2> for [f32; 2] {
1890 #[inline]
1891 fn from(v: Vec2) -> Self {
1892 [v.x, v.y]
1893 }
1894}
1895
1896impl From<(f32, f32)> for Vec2 {
1897 #[inline]
1898 fn from(t: (f32, f32)) -> Self {
1899 Self::new(t.0, t.1)
1900 }
1901}
1902
1903impl From<Vec2> for (f32, f32) {
1904 #[inline]
1905 fn from(v: Vec2) -> Self {
1906 (v.x, v.y)
1907 }
1908}
1909
1910impl From<BVec2> for Vec2 {
1911 #[inline]
1912 fn from(v: BVec2) -> Self {
1913 Self::new(f32::from(v.x), f32::from(v.y))
1914 }
1915}