1use crate::{f32::math, BVec2, Vec3};
4
5use core::fmt;
6use core::iter::{Product, Sum};
7use core::{f32, ops::*};
8
9#[cfg(feature = "zerocopy")]
10use zerocopy_derive::*;
11
12#[inline(always)]
14#[must_use]
15pub const fn vec2(x: f32, y: f32) -> Vec2 {
16 Vec2::new(x, y)
17}
18
19#[derive(Clone, Copy, PartialEq)]
21#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
22#[cfg_attr(
23 feature = "zerocopy",
24 derive(FromBytes, Immutable, IntoBytes, KnownLayout)
25)]
26#[cfg_attr(feature = "cuda", repr(align(8)))]
27#[repr(C)]
28#[cfg_attr(target_arch = "spirv", rust_gpu::vector::v1)]
29pub struct Vec2 {
30 pub x: f32,
31 pub y: f32,
32}
33
34impl Vec2 {
35 pub const ZERO: Self = Self::splat(0.0);
37
38 pub const ONE: Self = Self::splat(1.0);
40
41 pub const NEG_ONE: Self = Self::splat(-1.0);
43
44 pub const MIN: Self = Self::splat(f32::MIN);
46
47 pub const MAX: Self = Self::splat(f32::MAX);
49
50 pub const NAN: Self = Self::splat(f32::NAN);
52
53 pub const INFINITY: Self = Self::splat(f32::INFINITY);
55
56 pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY);
58
59 pub const X: Self = Self::new(1.0, 0.0);
61
62 pub const Y: Self = Self::new(0.0, 1.0);
64
65 pub const NEG_X: Self = Self::new(-1.0, 0.0);
67
68 pub const NEG_Y: Self = Self::new(0.0, -1.0);
70
71 pub const AXES: [Self; 2] = [Self::X, Self::Y];
73
74 pub const USES_CORE_SIMD: bool = false;
76 pub const USES_NEON: bool = false;
78 pub const USES_SCALAR_MATH: bool = true;
80 pub const USES_SSE2: bool = false;
82 pub const USES_WASM32_SIMD: bool = false;
84
85 #[inline(always)]
87 #[must_use]
88 pub const fn new(x: f32, y: f32) -> Self {
89 Self { x, y }
90 }
91
92 #[inline]
94 #[must_use]
95 pub const fn splat(v: f32) -> Self {
96 Self { x: v, y: v }
97 }
98
99 #[inline]
101 #[must_use]
102 pub fn map<F>(self, f: F) -> Self
103 where
104 F: Fn(f32) -> f32,
105 {
106 Self::new(f(self.x), f(self.y))
107 }
108
109 #[inline]
115 #[must_use]
116 pub fn select(mask: BVec2, if_true: Self, if_false: Self) -> Self {
117 Self {
118 x: if mask.test(0) { if_true.x } else { if_false.x },
119 y: if mask.test(1) { if_true.y } else { if_false.y },
120 }
121 }
122
123 #[inline]
125 #[must_use]
126 pub const fn from_array(a: [f32; 2]) -> Self {
127 Self::new(a[0], a[1])
128 }
129
130 #[inline]
132 #[must_use]
133 pub const fn to_array(&self) -> [f32; 2] {
134 [self.x, self.y]
135 }
136
137 #[inline]
143 #[must_use]
144 pub const fn from_slice(slice: &[f32]) -> Self {
145 assert!(slice.len() >= 2);
146 Self::new(slice[0], slice[1])
147 }
148
149 #[inline]
155 pub fn write_to_slice(self, slice: &mut [f32]) {
156 slice[..2].copy_from_slice(&self.to_array());
157 }
158
159 #[inline]
161 #[must_use]
162 pub const fn extend(self, z: f32) -> Vec3 {
163 Vec3::new(self.x, self.y, z)
164 }
165
166 #[inline]
168 #[must_use]
169 pub fn with_x(mut self, x: f32) -> Self {
170 self.x = x;
171 self
172 }
173
174 #[inline]
176 #[must_use]
177 pub fn with_y(mut self, y: f32) -> Self {
178 self.y = y;
179 self
180 }
181
182 #[inline]
184 #[must_use]
185 pub fn dot(self, rhs: Self) -> f32 {
186 (self.x * rhs.x) + (self.y * rhs.y)
187 }
188
189 #[inline]
191 #[must_use]
192 pub fn dot_into_vec(self, rhs: Self) -> Self {
193 Self::splat(self.dot(rhs))
194 }
195
196 #[inline]
203 #[must_use]
204 pub fn min(self, rhs: Self) -> Self {
205 Self {
206 x: if self.x < rhs.x { self.x } else { rhs.x },
207 y: if self.y < rhs.y { self.y } else { rhs.y },
208 }
209 }
210
211 #[inline]
218 #[must_use]
219 pub fn max(self, rhs: Self) -> Self {
220 Self {
221 x: if self.x > rhs.x { self.x } else { rhs.x },
222 y: if self.y > rhs.y { self.y } else { rhs.y },
223 }
224 }
225
226 #[inline]
237 #[must_use]
238 pub fn clamp(self, min: Self, max: Self) -> Self {
239 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
240 self.max(min).min(max)
241 }
242
243 #[inline]
250 #[must_use]
251 pub fn min_element(self) -> f32 {
252 let min = |a, b| if a < b { a } else { b };
253 min(self.x, self.y)
254 }
255
256 #[inline]
263 #[must_use]
264 pub fn max_element(self) -> f32 {
265 let max = |a, b| if a > b { a } else { b };
266 max(self.x, self.y)
267 }
268
269 #[doc(alias = "argmin")]
271 #[inline]
272 #[must_use]
273 pub fn min_position(self) -> usize {
274 if self.x <= self.y {
275 0
276 } else {
277 1
278 }
279 }
280
281 #[doc(alias = "argmax")]
283 #[inline]
284 #[must_use]
285 pub fn max_position(self) -> usize {
286 if self.x >= self.y {
287 0
288 } else {
289 1
290 }
291 }
292
293 #[inline]
297 #[must_use]
298 pub fn element_sum(self) -> f32 {
299 self.x + self.y
300 }
301
302 #[inline]
306 #[must_use]
307 pub fn element_product(self) -> f32 {
308 self.x * self.y
309 }
310
311 #[inline]
317 #[must_use]
318 pub fn cmpeq(self, rhs: Self) -> BVec2 {
319 BVec2::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y))
320 }
321
322 #[inline]
328 #[must_use]
329 pub fn cmpne(self, rhs: Self) -> BVec2 {
330 BVec2::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y))
331 }
332
333 #[inline]
339 #[must_use]
340 pub fn cmpge(self, rhs: Self) -> BVec2 {
341 BVec2::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y))
342 }
343
344 #[inline]
350 #[must_use]
351 pub fn cmpgt(self, rhs: Self) -> BVec2 {
352 BVec2::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y))
353 }
354
355 #[inline]
361 #[must_use]
362 pub fn cmple(self, rhs: Self) -> BVec2 {
363 BVec2::new(self.x.le(&rhs.x), self.y.le(&rhs.y))
364 }
365
366 #[inline]
372 #[must_use]
373 pub fn cmplt(self, rhs: Self) -> BVec2 {
374 BVec2::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y))
375 }
376
377 #[inline]
379 #[must_use]
380 pub fn abs(self) -> Self {
381 Self {
382 x: math::abs(self.x),
383 y: math::abs(self.y),
384 }
385 }
386
387 #[inline]
393 #[must_use]
394 pub fn signum(self) -> Self {
395 Self {
396 x: math::signum(self.x),
397 y: math::signum(self.y),
398 }
399 }
400
401 #[inline]
403 #[must_use]
404 pub fn copysign(self, rhs: Self) -> Self {
405 Self {
406 x: math::copysign(self.x, rhs.x),
407 y: math::copysign(self.y, rhs.y),
408 }
409 }
410
411 #[inline]
419 #[must_use]
420 pub fn is_negative_bitmask(self) -> u32 {
421 (self.x.is_sign_negative() as u32) | ((self.y.is_sign_negative() as u32) << 1)
422 }
423
424 #[inline]
427 #[must_use]
428 pub fn is_finite(self) -> bool {
429 self.x.is_finite() && self.y.is_finite()
430 }
431
432 #[inline]
436 #[must_use]
437 pub fn is_finite_mask(self) -> BVec2 {
438 BVec2::new(self.x.is_finite(), self.y.is_finite())
439 }
440
441 #[inline]
443 #[must_use]
444 pub fn is_nan(self) -> bool {
445 self.x.is_nan() || self.y.is_nan()
446 }
447
448 #[inline]
452 #[must_use]
453 pub fn is_nan_mask(self) -> BVec2 {
454 BVec2::new(self.x.is_nan(), self.y.is_nan())
455 }
456
457 #[doc(alias = "magnitude")]
459 #[inline]
460 #[must_use]
461 pub fn length(self) -> f32 {
462 math::sqrt(self.dot(self))
463 }
464
465 #[doc(alias = "magnitude2")]
469 #[inline]
470 #[must_use]
471 pub fn length_squared(self) -> f32 {
472 self.dot(self)
473 }
474
475 #[inline]
479 #[must_use]
480 pub fn length_recip(self) -> f32 {
481 self.length().recip()
482 }
483
484 #[inline]
486 #[must_use]
487 pub fn distance(self, rhs: Self) -> f32 {
488 (self - rhs).length()
489 }
490
491 #[inline]
493 #[must_use]
494 pub fn distance_squared(self, rhs: Self) -> f32 {
495 (self - rhs).length_squared()
496 }
497
498 #[inline]
500 #[must_use]
501 pub fn div_euclid(self, rhs: Self) -> Self {
502 Self::new(
503 math::div_euclid(self.x, rhs.x),
504 math::div_euclid(self.y, rhs.y),
505 )
506 }
507
508 #[inline]
512 #[must_use]
513 pub fn rem_euclid(self, rhs: Self) -> Self {
514 Self::new(
515 math::rem_euclid(self.x, rhs.x),
516 math::rem_euclid(self.y, rhs.y),
517 )
518 }
519
520 #[inline]
530 #[must_use]
531 pub fn normalize(self) -> Self {
532 #[allow(clippy::let_and_return)]
533 let normalized = self.mul(self.length_recip());
534 glam_assert!(normalized.is_finite());
535 normalized
536 }
537
538 #[inline]
545 #[must_use]
546 pub fn try_normalize(self) -> Option<Self> {
547 let rcp = self.length_recip();
548 if rcp.is_finite() && rcp > 0.0 {
549 Some(self * rcp)
550 } else {
551 None
552 }
553 }
554
555 #[inline]
563 #[must_use]
564 pub fn normalize_or(self, fallback: Self) -> Self {
565 let rcp = self.length_recip();
566 if rcp.is_finite() && rcp > 0.0 {
567 self * rcp
568 } else {
569 fallback
570 }
571 }
572
573 #[inline]
580 #[must_use]
581 pub fn normalize_or_zero(self) -> Self {
582 self.normalize_or(Self::ZERO)
583 }
584
585 #[inline]
589 #[must_use]
590 pub fn normalize_and_length(self) -> (Self, f32) {
591 let length = self.length();
592 let rcp = 1.0 / length;
593 if rcp.is_finite() && rcp > 0.0 {
594 (self * rcp, length)
595 } else {
596 (Self::X, 0.0)
597 }
598 }
599
600 #[inline]
604 #[must_use]
605 pub fn is_normalized(self) -> bool {
606 math::abs(self.length_squared() - 1.0) <= 2e-4
607 }
608
609 #[inline]
617 #[must_use]
618 pub fn project_onto(self, rhs: Self) -> Self {
619 let other_len_sq_rcp = rhs.dot(rhs).recip();
620 glam_assert!(other_len_sq_rcp.is_finite());
621 rhs * self.dot(rhs) * other_len_sq_rcp
622 }
623
624 #[doc(alias("plane"))]
635 #[inline]
636 #[must_use]
637 pub fn reject_from(self, rhs: Self) -> Self {
638 self - self.project_onto(rhs)
639 }
640
641 #[inline]
649 #[must_use]
650 pub fn project_onto_normalized(self, rhs: Self) -> Self {
651 glam_assert!(rhs.is_normalized());
652 rhs * self.dot(rhs)
653 }
654
655 #[doc(alias("plane"))]
666 #[inline]
667 #[must_use]
668 pub fn reject_from_normalized(self, rhs: Self) -> Self {
669 self - self.project_onto_normalized(rhs)
670 }
671
672 #[inline]
675 #[must_use]
676 pub fn round(self) -> Self {
677 Self {
678 x: math::round(self.x),
679 y: math::round(self.y),
680 }
681 }
682
683 #[inline]
686 #[must_use]
687 pub fn floor(self) -> Self {
688 Self {
689 x: math::floor(self.x),
690 y: math::floor(self.y),
691 }
692 }
693
694 #[inline]
697 #[must_use]
698 pub fn ceil(self) -> Self {
699 Self {
700 x: math::ceil(self.x),
701 y: math::ceil(self.y),
702 }
703 }
704
705 #[inline]
708 #[must_use]
709 pub fn trunc(self) -> Self {
710 Self {
711 x: math::trunc(self.x),
712 y: math::trunc(self.y),
713 }
714 }
715
716 #[inline]
723 #[must_use]
724 pub fn fract(self) -> Self {
725 self - self.trunc()
726 }
727
728 #[inline]
735 #[must_use]
736 pub fn fract_gl(self) -> Self {
737 self - self.floor()
738 }
739
740 #[inline]
743 #[must_use]
744 pub fn exp(self) -> Self {
745 Self::new(math::exp(self.x), math::exp(self.y))
746 }
747
748 #[inline]
750 #[must_use]
751 pub fn exp2(self) -> Self {
752 Self::new(math::exp2(self.x), math::exp2(self.y))
753 }
754
755 #[inline]
758 #[must_use]
759 pub fn ln(self) -> Self {
760 Self::new(math::ln(self.x), math::ln(self.y))
761 }
762
763 #[inline]
766 #[must_use]
767 pub fn log2(self) -> Self {
768 Self::new(math::log2(self.x), math::log2(self.y))
769 }
770
771 #[inline]
773 #[must_use]
774 pub fn powf(self, n: f32) -> Self {
775 Self::new(math::powf(self.x, n), math::powf(self.y, n))
776 }
777
778 #[inline]
780 #[must_use]
781 pub fn recip(self) -> Self {
782 Self {
783 x: 1.0 / self.x,
784 y: 1.0 / self.y,
785 }
786 }
787
788 #[doc(alias = "mix")]
794 #[inline]
795 #[must_use]
796 pub fn lerp(self, rhs: Self, s: f32) -> Self {
797 self * (1.0 - s) + rhs * s
798 }
799
800 #[inline]
805 #[must_use]
806 pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
807 let a = rhs - *self;
808 let len = a.length();
809 if len <= d || len <= 1e-4 {
810 return rhs;
811 }
812 *self + a / len * d
813 }
814
815 #[inline]
821 pub fn midpoint(self, rhs: Self) -> Self {
822 (self + rhs) * 0.5
823 }
824
825 #[inline]
835 #[must_use]
836 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
837 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
838 }
839
840 #[inline]
846 #[must_use]
847 pub fn clamp_length(self, min: f32, max: f32) -> Self {
848 glam_assert!(0.0 <= min);
849 glam_assert!(min <= max);
850 let length_sq = self.length_squared();
851 if length_sq < min * min {
852 min * (self / math::sqrt(length_sq))
853 } else if length_sq > max * max {
854 max * (self / math::sqrt(length_sq))
855 } else {
856 self
857 }
858 }
859
860 #[inline]
866 #[must_use]
867 pub fn clamp_length_max(self, max: f32) -> Self {
868 glam_assert!(0.0 <= max);
869 let length_sq = self.length_squared();
870 if length_sq > max * max {
871 max * (self / math::sqrt(length_sq))
872 } else {
873 self
874 }
875 }
876
877 #[inline]
883 #[must_use]
884 pub fn clamp_length_min(self, min: f32) -> Self {
885 glam_assert!(0.0 <= min);
886 let length_sq = self.length_squared();
887 if length_sq < min * min {
888 min * (self / math::sqrt(length_sq))
889 } else {
890 self
891 }
892 }
893
894 #[inline]
902 #[must_use]
903 pub fn mul_add(self, a: Self, b: Self) -> Self {
904 Self::new(
905 math::mul_add(self.x, a.x, b.x),
906 math::mul_add(self.y, a.y, b.y),
907 )
908 }
909
910 #[inline]
919 #[must_use]
920 pub fn reflect(self, normal: Self) -> Self {
921 glam_assert!(normal.is_normalized());
922 self - 2.0 * self.dot(normal) * normal
923 }
924
925 #[inline]
935 #[must_use]
936 pub fn refract(self, normal: Self, eta: f32) -> Self {
937 glam_assert!(self.is_normalized());
938 glam_assert!(normal.is_normalized());
939 let n_dot_i = normal.dot(self);
940 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
941 if k >= 0.0 {
942 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
943 } else {
944 Self::ZERO
945 }
946 }
947
948 #[inline]
953 #[must_use]
954 pub fn from_angle(angle: f32) -> Self {
955 let (sin, cos) = math::sin_cos(angle);
956 Self { x: cos, y: sin }
957 }
958
959 #[inline]
963 #[must_use]
964 pub fn to_angle(self) -> f32 {
965 math::atan2(self.y, self.x)
966 }
967
968 #[inline]
969 #[must_use]
970 #[deprecated(
971 since = "0.27.0",
972 note = "Use angle_to() instead, the semantics of angle_between will change in the future."
973 )]
974 pub fn angle_between(self, rhs: Self) -> f32 {
975 self.angle_to(rhs)
976 }
977
978 #[inline]
982 #[must_use]
983 pub fn angle_to(self, rhs: Self) -> f32 {
984 let angle = math::acos_approx(
985 self.dot(rhs) / math::sqrt(self.length_squared() * rhs.length_squared()),
986 );
987
988 angle * math::signum(self.perp_dot(rhs))
989 }
990
991 #[inline]
993 #[must_use]
994 pub fn perp(self) -> Self {
995 Self {
996 x: -self.y,
997 y: self.x,
998 }
999 }
1000
1001 #[doc(alias = "wedge")]
1004 #[doc(alias = "cross")]
1005 #[doc(alias = "determinant")]
1006 #[inline]
1007 #[must_use]
1008 pub fn perp_dot(self, rhs: Self) -> f32 {
1009 (self.x * rhs.y) - (self.y * rhs.x)
1010 }
1011
1012 #[inline]
1020 #[must_use]
1021 pub fn rotate(self, rhs: Self) -> Self {
1022 Self {
1023 x: self.x * rhs.x - self.y * rhs.y,
1024 y: self.y * rhs.x + self.x * rhs.y,
1025 }
1026 }
1027
1028 #[inline]
1034 #[must_use]
1035 pub fn rotate_towards(&self, rhs: Self, max_angle: f32) -> Self {
1036 let a = self.angle_to(rhs);
1037 let abs_a = math::abs(a);
1038 let angle = max_angle.clamp(abs_a - core::f32::consts::PI, abs_a) * math::signum(a);
1040 Self::from_angle(angle).rotate(*self)
1041 }
1042
1043 #[inline]
1045 #[must_use]
1046 pub fn as_dvec2(&self) -> crate::DVec2 {
1047 crate::DVec2::new(self.x as f64, self.y as f64)
1048 }
1049
1050 #[inline]
1052 #[must_use]
1053 pub fn as_i8vec2(&self) -> crate::I8Vec2 {
1054 crate::I8Vec2::new(self.x as i8, self.y as i8)
1055 }
1056
1057 #[inline]
1059 #[must_use]
1060 pub fn as_u8vec2(&self) -> crate::U8Vec2 {
1061 crate::U8Vec2::new(self.x as u8, self.y as u8)
1062 }
1063
1064 #[inline]
1066 #[must_use]
1067 pub fn as_i16vec2(&self) -> crate::I16Vec2 {
1068 crate::I16Vec2::new(self.x as i16, self.y as i16)
1069 }
1070
1071 #[inline]
1073 #[must_use]
1074 pub fn as_u16vec2(&self) -> crate::U16Vec2 {
1075 crate::U16Vec2::new(self.x as u16, self.y as u16)
1076 }
1077
1078 #[inline]
1080 #[must_use]
1081 pub fn as_ivec2(&self) -> crate::IVec2 {
1082 crate::IVec2::new(self.x as i32, self.y as i32)
1083 }
1084
1085 #[inline]
1087 #[must_use]
1088 pub fn as_uvec2(&self) -> crate::UVec2 {
1089 crate::UVec2::new(self.x as u32, self.y as u32)
1090 }
1091
1092 #[inline]
1094 #[must_use]
1095 pub fn as_i64vec2(&self) -> crate::I64Vec2 {
1096 crate::I64Vec2::new(self.x as i64, self.y as i64)
1097 }
1098
1099 #[inline]
1101 #[must_use]
1102 pub fn as_u64vec2(&self) -> crate::U64Vec2 {
1103 crate::U64Vec2::new(self.x as u64, self.y as u64)
1104 }
1105
1106 #[inline]
1108 #[must_use]
1109 pub fn as_usizevec2(&self) -> crate::USizeVec2 {
1110 crate::USizeVec2::new(self.x as usize, self.y as usize)
1111 }
1112}
1113
1114impl Default for Vec2 {
1115 #[inline(always)]
1116 fn default() -> Self {
1117 Self::ZERO
1118 }
1119}
1120
1121impl Div for Vec2 {
1122 type Output = Self;
1123 #[inline]
1124 fn div(self, rhs: Self) -> Self {
1125 Self {
1126 x: self.x.div(rhs.x),
1127 y: self.y.div(rhs.y),
1128 }
1129 }
1130}
1131
1132impl Div<&Self> for Vec2 {
1133 type Output = Self;
1134 #[inline]
1135 fn div(self, rhs: &Self) -> Self {
1136 self.div(*rhs)
1137 }
1138}
1139
1140impl Div<&Vec2> for &Vec2 {
1141 type Output = Vec2;
1142 #[inline]
1143 fn div(self, rhs: &Vec2) -> Vec2 {
1144 (*self).div(*rhs)
1145 }
1146}
1147
1148impl Div<Vec2> for &Vec2 {
1149 type Output = Vec2;
1150 #[inline]
1151 fn div(self, rhs: Vec2) -> Vec2 {
1152 (*self).div(rhs)
1153 }
1154}
1155
1156impl DivAssign for Vec2 {
1157 #[inline]
1158 fn div_assign(&mut self, rhs: Self) {
1159 self.x.div_assign(rhs.x);
1160 self.y.div_assign(rhs.y);
1161 }
1162}
1163
1164impl DivAssign<&Self> for Vec2 {
1165 #[inline]
1166 fn div_assign(&mut self, rhs: &Self) {
1167 self.div_assign(*rhs);
1168 }
1169}
1170
1171impl Div<f32> for Vec2 {
1172 type Output = Self;
1173 #[inline]
1174 fn div(self, rhs: f32) -> Self {
1175 Self {
1176 x: self.x.div(rhs),
1177 y: self.y.div(rhs),
1178 }
1179 }
1180}
1181
1182impl Div<&f32> for Vec2 {
1183 type Output = Self;
1184 #[inline]
1185 fn div(self, rhs: &f32) -> Self {
1186 self.div(*rhs)
1187 }
1188}
1189
1190impl Div<&f32> for &Vec2 {
1191 type Output = Vec2;
1192 #[inline]
1193 fn div(self, rhs: &f32) -> Vec2 {
1194 (*self).div(*rhs)
1195 }
1196}
1197
1198impl Div<f32> for &Vec2 {
1199 type Output = Vec2;
1200 #[inline]
1201 fn div(self, rhs: f32) -> Vec2 {
1202 (*self).div(rhs)
1203 }
1204}
1205
1206impl DivAssign<f32> for Vec2 {
1207 #[inline]
1208 fn div_assign(&mut self, rhs: f32) {
1209 self.x.div_assign(rhs);
1210 self.y.div_assign(rhs);
1211 }
1212}
1213
1214impl DivAssign<&f32> for Vec2 {
1215 #[inline]
1216 fn div_assign(&mut self, rhs: &f32) {
1217 self.div_assign(*rhs);
1218 }
1219}
1220
1221impl Div<Vec2> for f32 {
1222 type Output = Vec2;
1223 #[inline]
1224 fn div(self, rhs: Vec2) -> Vec2 {
1225 Vec2 {
1226 x: self.div(rhs.x),
1227 y: self.div(rhs.y),
1228 }
1229 }
1230}
1231
1232impl Div<&Vec2> for f32 {
1233 type Output = Vec2;
1234 #[inline]
1235 fn div(self, rhs: &Vec2) -> Vec2 {
1236 self.div(*rhs)
1237 }
1238}
1239
1240impl Div<&Vec2> for &f32 {
1241 type Output = Vec2;
1242 #[inline]
1243 fn div(self, rhs: &Vec2) -> Vec2 {
1244 (*self).div(*rhs)
1245 }
1246}
1247
1248impl Div<Vec2> for &f32 {
1249 type Output = Vec2;
1250 #[inline]
1251 fn div(self, rhs: Vec2) -> Vec2 {
1252 (*self).div(rhs)
1253 }
1254}
1255
1256impl Mul for Vec2 {
1257 type Output = Self;
1258 #[inline]
1259 fn mul(self, rhs: Self) -> Self {
1260 Self {
1261 x: self.x.mul(rhs.x),
1262 y: self.y.mul(rhs.y),
1263 }
1264 }
1265}
1266
1267impl Mul<&Self> for Vec2 {
1268 type Output = Self;
1269 #[inline]
1270 fn mul(self, rhs: &Self) -> Self {
1271 self.mul(*rhs)
1272 }
1273}
1274
1275impl Mul<&Vec2> for &Vec2 {
1276 type Output = Vec2;
1277 #[inline]
1278 fn mul(self, rhs: &Vec2) -> Vec2 {
1279 (*self).mul(*rhs)
1280 }
1281}
1282
1283impl Mul<Vec2> for &Vec2 {
1284 type Output = Vec2;
1285 #[inline]
1286 fn mul(self, rhs: Vec2) -> Vec2 {
1287 (*self).mul(rhs)
1288 }
1289}
1290
1291impl MulAssign for Vec2 {
1292 #[inline]
1293 fn mul_assign(&mut self, rhs: Self) {
1294 self.x.mul_assign(rhs.x);
1295 self.y.mul_assign(rhs.y);
1296 }
1297}
1298
1299impl MulAssign<&Self> for Vec2 {
1300 #[inline]
1301 fn mul_assign(&mut self, rhs: &Self) {
1302 self.mul_assign(*rhs);
1303 }
1304}
1305
1306impl Mul<f32> for Vec2 {
1307 type Output = Self;
1308 #[inline]
1309 fn mul(self, rhs: f32) -> Self {
1310 Self {
1311 x: self.x.mul(rhs),
1312 y: self.y.mul(rhs),
1313 }
1314 }
1315}
1316
1317impl Mul<&f32> for Vec2 {
1318 type Output = Self;
1319 #[inline]
1320 fn mul(self, rhs: &f32) -> Self {
1321 self.mul(*rhs)
1322 }
1323}
1324
1325impl Mul<&f32> for &Vec2 {
1326 type Output = Vec2;
1327 #[inline]
1328 fn mul(self, rhs: &f32) -> Vec2 {
1329 (*self).mul(*rhs)
1330 }
1331}
1332
1333impl Mul<f32> for &Vec2 {
1334 type Output = Vec2;
1335 #[inline]
1336 fn mul(self, rhs: f32) -> Vec2 {
1337 (*self).mul(rhs)
1338 }
1339}
1340
1341impl MulAssign<f32> for Vec2 {
1342 #[inline]
1343 fn mul_assign(&mut self, rhs: f32) {
1344 self.x.mul_assign(rhs);
1345 self.y.mul_assign(rhs);
1346 }
1347}
1348
1349impl MulAssign<&f32> for Vec2 {
1350 #[inline]
1351 fn mul_assign(&mut self, rhs: &f32) {
1352 self.mul_assign(*rhs);
1353 }
1354}
1355
1356impl Mul<Vec2> for f32 {
1357 type Output = Vec2;
1358 #[inline]
1359 fn mul(self, rhs: Vec2) -> Vec2 {
1360 Vec2 {
1361 x: self.mul(rhs.x),
1362 y: self.mul(rhs.y),
1363 }
1364 }
1365}
1366
1367impl Mul<&Vec2> for f32 {
1368 type Output = Vec2;
1369 #[inline]
1370 fn mul(self, rhs: &Vec2) -> Vec2 {
1371 self.mul(*rhs)
1372 }
1373}
1374
1375impl Mul<&Vec2> for &f32 {
1376 type Output = Vec2;
1377 #[inline]
1378 fn mul(self, rhs: &Vec2) -> Vec2 {
1379 (*self).mul(*rhs)
1380 }
1381}
1382
1383impl Mul<Vec2> for &f32 {
1384 type Output = Vec2;
1385 #[inline]
1386 fn mul(self, rhs: Vec2) -> Vec2 {
1387 (*self).mul(rhs)
1388 }
1389}
1390
1391impl Add for Vec2 {
1392 type Output = Self;
1393 #[inline]
1394 fn add(self, rhs: Self) -> Self {
1395 Self {
1396 x: self.x.add(rhs.x),
1397 y: self.y.add(rhs.y),
1398 }
1399 }
1400}
1401
1402impl Add<&Self> for Vec2 {
1403 type Output = Self;
1404 #[inline]
1405 fn add(self, rhs: &Self) -> Self {
1406 self.add(*rhs)
1407 }
1408}
1409
1410impl Add<&Vec2> for &Vec2 {
1411 type Output = Vec2;
1412 #[inline]
1413 fn add(self, rhs: &Vec2) -> Vec2 {
1414 (*self).add(*rhs)
1415 }
1416}
1417
1418impl Add<Vec2> for &Vec2 {
1419 type Output = Vec2;
1420 #[inline]
1421 fn add(self, rhs: Vec2) -> Vec2 {
1422 (*self).add(rhs)
1423 }
1424}
1425
1426impl AddAssign for Vec2 {
1427 #[inline]
1428 fn add_assign(&mut self, rhs: Self) {
1429 self.x.add_assign(rhs.x);
1430 self.y.add_assign(rhs.y);
1431 }
1432}
1433
1434impl AddAssign<&Self> for Vec2 {
1435 #[inline]
1436 fn add_assign(&mut self, rhs: &Self) {
1437 self.add_assign(*rhs);
1438 }
1439}
1440
1441impl Add<f32> for Vec2 {
1442 type Output = Self;
1443 #[inline]
1444 fn add(self, rhs: f32) -> Self {
1445 Self {
1446 x: self.x.add(rhs),
1447 y: self.y.add(rhs),
1448 }
1449 }
1450}
1451
1452impl Add<&f32> for Vec2 {
1453 type Output = Self;
1454 #[inline]
1455 fn add(self, rhs: &f32) -> Self {
1456 self.add(*rhs)
1457 }
1458}
1459
1460impl Add<&f32> for &Vec2 {
1461 type Output = Vec2;
1462 #[inline]
1463 fn add(self, rhs: &f32) -> Vec2 {
1464 (*self).add(*rhs)
1465 }
1466}
1467
1468impl Add<f32> for &Vec2 {
1469 type Output = Vec2;
1470 #[inline]
1471 fn add(self, rhs: f32) -> Vec2 {
1472 (*self).add(rhs)
1473 }
1474}
1475
1476impl AddAssign<f32> for Vec2 {
1477 #[inline]
1478 fn add_assign(&mut self, rhs: f32) {
1479 self.x.add_assign(rhs);
1480 self.y.add_assign(rhs);
1481 }
1482}
1483
1484impl AddAssign<&f32> for Vec2 {
1485 #[inline]
1486 fn add_assign(&mut self, rhs: &f32) {
1487 self.add_assign(*rhs);
1488 }
1489}
1490
1491impl Add<Vec2> for f32 {
1492 type Output = Vec2;
1493 #[inline]
1494 fn add(self, rhs: Vec2) -> Vec2 {
1495 Vec2 {
1496 x: self.add(rhs.x),
1497 y: self.add(rhs.y),
1498 }
1499 }
1500}
1501
1502impl Add<&Vec2> for f32 {
1503 type Output = Vec2;
1504 #[inline]
1505 fn add(self, rhs: &Vec2) -> Vec2 {
1506 self.add(*rhs)
1507 }
1508}
1509
1510impl Add<&Vec2> for &f32 {
1511 type Output = Vec2;
1512 #[inline]
1513 fn add(self, rhs: &Vec2) -> Vec2 {
1514 (*self).add(*rhs)
1515 }
1516}
1517
1518impl Add<Vec2> for &f32 {
1519 type Output = Vec2;
1520 #[inline]
1521 fn add(self, rhs: Vec2) -> Vec2 {
1522 (*self).add(rhs)
1523 }
1524}
1525
1526impl Sub for Vec2 {
1527 type Output = Self;
1528 #[inline]
1529 fn sub(self, rhs: Self) -> Self {
1530 Self {
1531 x: self.x.sub(rhs.x),
1532 y: self.y.sub(rhs.y),
1533 }
1534 }
1535}
1536
1537impl Sub<&Self> for Vec2 {
1538 type Output = Self;
1539 #[inline]
1540 fn sub(self, rhs: &Self) -> Self {
1541 self.sub(*rhs)
1542 }
1543}
1544
1545impl Sub<&Vec2> for &Vec2 {
1546 type Output = Vec2;
1547 #[inline]
1548 fn sub(self, rhs: &Vec2) -> Vec2 {
1549 (*self).sub(*rhs)
1550 }
1551}
1552
1553impl Sub<Vec2> for &Vec2 {
1554 type Output = Vec2;
1555 #[inline]
1556 fn sub(self, rhs: Vec2) -> Vec2 {
1557 (*self).sub(rhs)
1558 }
1559}
1560
1561impl SubAssign for Vec2 {
1562 #[inline]
1563 fn sub_assign(&mut self, rhs: Self) {
1564 self.x.sub_assign(rhs.x);
1565 self.y.sub_assign(rhs.y);
1566 }
1567}
1568
1569impl SubAssign<&Self> for Vec2 {
1570 #[inline]
1571 fn sub_assign(&mut self, rhs: &Self) {
1572 self.sub_assign(*rhs);
1573 }
1574}
1575
1576impl Sub<f32> for Vec2 {
1577 type Output = Self;
1578 #[inline]
1579 fn sub(self, rhs: f32) -> Self {
1580 Self {
1581 x: self.x.sub(rhs),
1582 y: self.y.sub(rhs),
1583 }
1584 }
1585}
1586
1587impl Sub<&f32> for Vec2 {
1588 type Output = Self;
1589 #[inline]
1590 fn sub(self, rhs: &f32) -> Self {
1591 self.sub(*rhs)
1592 }
1593}
1594
1595impl Sub<&f32> for &Vec2 {
1596 type Output = Vec2;
1597 #[inline]
1598 fn sub(self, rhs: &f32) -> Vec2 {
1599 (*self).sub(*rhs)
1600 }
1601}
1602
1603impl Sub<f32> for &Vec2 {
1604 type Output = Vec2;
1605 #[inline]
1606 fn sub(self, rhs: f32) -> Vec2 {
1607 (*self).sub(rhs)
1608 }
1609}
1610
1611impl SubAssign<f32> for Vec2 {
1612 #[inline]
1613 fn sub_assign(&mut self, rhs: f32) {
1614 self.x.sub_assign(rhs);
1615 self.y.sub_assign(rhs);
1616 }
1617}
1618
1619impl SubAssign<&f32> for Vec2 {
1620 #[inline]
1621 fn sub_assign(&mut self, rhs: &f32) {
1622 self.sub_assign(*rhs);
1623 }
1624}
1625
1626impl Sub<Vec2> for f32 {
1627 type Output = Vec2;
1628 #[inline]
1629 fn sub(self, rhs: Vec2) -> Vec2 {
1630 Vec2 {
1631 x: self.sub(rhs.x),
1632 y: self.sub(rhs.y),
1633 }
1634 }
1635}
1636
1637impl Sub<&Vec2> for f32 {
1638 type Output = Vec2;
1639 #[inline]
1640 fn sub(self, rhs: &Vec2) -> Vec2 {
1641 self.sub(*rhs)
1642 }
1643}
1644
1645impl Sub<&Vec2> for &f32 {
1646 type Output = Vec2;
1647 #[inline]
1648 fn sub(self, rhs: &Vec2) -> Vec2 {
1649 (*self).sub(*rhs)
1650 }
1651}
1652
1653impl Sub<Vec2> for &f32 {
1654 type Output = Vec2;
1655 #[inline]
1656 fn sub(self, rhs: Vec2) -> Vec2 {
1657 (*self).sub(rhs)
1658 }
1659}
1660
1661impl Rem for Vec2 {
1662 type Output = Self;
1663 #[inline]
1664 fn rem(self, rhs: Self) -> Self {
1665 Self {
1666 x: self.x.rem(rhs.x),
1667 y: self.y.rem(rhs.y),
1668 }
1669 }
1670}
1671
1672impl Rem<&Self> for Vec2 {
1673 type Output = Self;
1674 #[inline]
1675 fn rem(self, rhs: &Self) -> Self {
1676 self.rem(*rhs)
1677 }
1678}
1679
1680impl Rem<&Vec2> for &Vec2 {
1681 type Output = Vec2;
1682 #[inline]
1683 fn rem(self, rhs: &Vec2) -> Vec2 {
1684 (*self).rem(*rhs)
1685 }
1686}
1687
1688impl Rem<Vec2> for &Vec2 {
1689 type Output = Vec2;
1690 #[inline]
1691 fn rem(self, rhs: Vec2) -> Vec2 {
1692 (*self).rem(rhs)
1693 }
1694}
1695
1696impl RemAssign for Vec2 {
1697 #[inline]
1698 fn rem_assign(&mut self, rhs: Self) {
1699 self.x.rem_assign(rhs.x);
1700 self.y.rem_assign(rhs.y);
1701 }
1702}
1703
1704impl RemAssign<&Self> for Vec2 {
1705 #[inline]
1706 fn rem_assign(&mut self, rhs: &Self) {
1707 self.rem_assign(*rhs);
1708 }
1709}
1710
1711impl Rem<f32> for Vec2 {
1712 type Output = Self;
1713 #[inline]
1714 fn rem(self, rhs: f32) -> Self {
1715 Self {
1716 x: self.x.rem(rhs),
1717 y: self.y.rem(rhs),
1718 }
1719 }
1720}
1721
1722impl Rem<&f32> for Vec2 {
1723 type Output = Self;
1724 #[inline]
1725 fn rem(self, rhs: &f32) -> Self {
1726 self.rem(*rhs)
1727 }
1728}
1729
1730impl Rem<&f32> for &Vec2 {
1731 type Output = Vec2;
1732 #[inline]
1733 fn rem(self, rhs: &f32) -> Vec2 {
1734 (*self).rem(*rhs)
1735 }
1736}
1737
1738impl Rem<f32> for &Vec2 {
1739 type Output = Vec2;
1740 #[inline]
1741 fn rem(self, rhs: f32) -> Vec2 {
1742 (*self).rem(rhs)
1743 }
1744}
1745
1746impl RemAssign<f32> for Vec2 {
1747 #[inline]
1748 fn rem_assign(&mut self, rhs: f32) {
1749 self.x.rem_assign(rhs);
1750 self.y.rem_assign(rhs);
1751 }
1752}
1753
1754impl RemAssign<&f32> for Vec2 {
1755 #[inline]
1756 fn rem_assign(&mut self, rhs: &f32) {
1757 self.rem_assign(*rhs);
1758 }
1759}
1760
1761impl Rem<Vec2> for f32 {
1762 type Output = Vec2;
1763 #[inline]
1764 fn rem(self, rhs: Vec2) -> Vec2 {
1765 Vec2 {
1766 x: self.rem(rhs.x),
1767 y: self.rem(rhs.y),
1768 }
1769 }
1770}
1771
1772impl Rem<&Vec2> for f32 {
1773 type Output = Vec2;
1774 #[inline]
1775 fn rem(self, rhs: &Vec2) -> Vec2 {
1776 self.rem(*rhs)
1777 }
1778}
1779
1780impl Rem<&Vec2> for &f32 {
1781 type Output = Vec2;
1782 #[inline]
1783 fn rem(self, rhs: &Vec2) -> Vec2 {
1784 (*self).rem(*rhs)
1785 }
1786}
1787
1788impl Rem<Vec2> for &f32 {
1789 type Output = Vec2;
1790 #[inline]
1791 fn rem(self, rhs: Vec2) -> Vec2 {
1792 (*self).rem(rhs)
1793 }
1794}
1795
1796impl AsRef<[f32; 2]> for Vec2 {
1797 #[inline]
1798 fn as_ref(&self) -> &[f32; 2] {
1799 unsafe { &*(self as *const Self as *const [f32; 2]) }
1800 }
1801}
1802
1803impl AsMut<[f32; 2]> for Vec2 {
1804 #[inline]
1805 fn as_mut(&mut self) -> &mut [f32; 2] {
1806 unsafe { &mut *(self as *mut Self as *mut [f32; 2]) }
1807 }
1808}
1809
1810impl Sum for Vec2 {
1811 #[inline]
1812 fn sum<I>(iter: I) -> Self
1813 where
1814 I: Iterator<Item = Self>,
1815 {
1816 iter.fold(Self::ZERO, Self::add)
1817 }
1818}
1819
1820impl<'a> Sum<&'a Self> for Vec2 {
1821 #[inline]
1822 fn sum<I>(iter: I) -> Self
1823 where
1824 I: Iterator<Item = &'a Self>,
1825 {
1826 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1827 }
1828}
1829
1830impl Product for Vec2 {
1831 #[inline]
1832 fn product<I>(iter: I) -> Self
1833 where
1834 I: Iterator<Item = Self>,
1835 {
1836 iter.fold(Self::ONE, Self::mul)
1837 }
1838}
1839
1840impl<'a> Product<&'a Self> for Vec2 {
1841 #[inline]
1842 fn product<I>(iter: I) -> Self
1843 where
1844 I: Iterator<Item = &'a Self>,
1845 {
1846 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1847 }
1848}
1849
1850impl Neg for Vec2 {
1851 type Output = Self;
1852 #[inline]
1853 fn neg(self) -> Self {
1854 Self {
1855 x: self.x.neg(),
1856 y: self.y.neg(),
1857 }
1858 }
1859}
1860
1861impl Neg for &Vec2 {
1862 type Output = Vec2;
1863 #[inline]
1864 fn neg(self) -> Vec2 {
1865 (*self).neg()
1866 }
1867}
1868
1869impl Index<usize> for Vec2 {
1870 type Output = f32;
1871 #[inline]
1872 fn index(&self, index: usize) -> &Self::Output {
1873 match index {
1874 0 => &self.x,
1875 1 => &self.y,
1876 _ => panic!("index out of bounds"),
1877 }
1878 }
1879}
1880
1881impl IndexMut<usize> for Vec2 {
1882 #[inline]
1883 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1884 match index {
1885 0 => &mut self.x,
1886 1 => &mut self.y,
1887 _ => panic!("index out of bounds"),
1888 }
1889 }
1890}
1891
1892impl fmt::Display for Vec2 {
1893 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1894 if let Some(p) = f.precision() {
1895 write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y)
1896 } else {
1897 write!(f, "[{}, {}]", self.x, self.y)
1898 }
1899 }
1900}
1901
1902impl fmt::Debug for Vec2 {
1903 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1904 fmt.debug_tuple(stringify!(Vec2))
1905 .field(&self.x)
1906 .field(&self.y)
1907 .finish()
1908 }
1909}
1910
1911impl From<[f32; 2]> for Vec2 {
1912 #[inline]
1913 fn from(a: [f32; 2]) -> Self {
1914 Self::new(a[0], a[1])
1915 }
1916}
1917
1918impl From<Vec2> for [f32; 2] {
1919 #[inline]
1920 fn from(v: Vec2) -> Self {
1921 [v.x, v.y]
1922 }
1923}
1924
1925impl From<(f32, f32)> for Vec2 {
1926 #[inline]
1927 fn from(t: (f32, f32)) -> Self {
1928 Self::new(t.0, t.1)
1929 }
1930}
1931
1932impl From<Vec2> for (f32, f32) {
1933 #[inline]
1934 fn from(v: Vec2) -> Self {
1935 (v.x, v.y)
1936 }
1937}
1938
1939impl From<BVec2> for Vec2 {
1940 #[inline]
1941 fn from(v: BVec2) -> Self {
1942 Self::new(f32::from(v.x), f32::from(v.y))
1943 }
1944}