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