1use crate::{BVec2, Vec3, f32::math};
4
5use core::fmt;
6use core::iter::{Product, Sum};
7use core::ops::*;
8
9#[cfg(feature = "zerocopy-08")]
10use zerocopy_derive_08::*;
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-08",
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_WASM_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::new(v, v)
97 }
98
99 #[inline]
101 #[must_use]
102 pub fn map<F>(self, mut f: F) -> Self
103 where
104 F: FnMut(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::new(
118 if mask.test(0) { if_true.x } else { if_false.x },
119 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 #[track_caller]
145 pub const fn from_slice(slice: &[f32]) -> Self {
146 assert!(slice.len() >= 2);
147 Self::new(slice[0], slice[1])
148 }
149
150 #[inline]
156 #[track_caller]
157 pub fn write_to_slice(self, slice: &mut [f32]) {
158 slice[..2].copy_from_slice(&self.to_array());
159 }
160
161 #[inline]
163 #[must_use]
164 pub const fn extend(self, z: f32) -> Vec3 {
165 Vec3::new(self.x, self.y, z)
166 }
167
168 #[inline]
170 #[must_use]
171 pub fn with_x(mut self, x: f32) -> Self {
172 self.x = x;
173 self
174 }
175
176 #[inline]
178 #[must_use]
179 pub fn with_y(mut self, y: f32) -> Self {
180 self.y = y;
181 self
182 }
183
184 #[inline]
186 #[must_use]
187 pub fn dot(self, rhs: Self) -> f32 {
188 (self.x * rhs.x) + (self.y * rhs.y)
189 }
190
191 #[inline]
193 #[must_use]
194 pub fn dot_into_vec(self, rhs: Self) -> Self {
195 Self::splat(self.dot(rhs))
196 }
197
198 #[inline]
206 #[must_use]
207 pub fn min(self, rhs: Self) -> Self {
208 Self::new(
209 if self.x < rhs.x { self.x } else { rhs.x },
210 if self.y < rhs.y { self.y } else { rhs.y },
211 )
212 }
213
214 #[inline]
222 #[must_use]
223 pub fn max(self, rhs: Self) -> Self {
224 Self::new(
225 if self.x > rhs.x { self.x } else { rhs.x },
226 if self.y > rhs.y { self.y } else { rhs.y },
227 )
228 }
229
230 #[inline]
240 #[must_use]
241 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
242 pub fn clamp(self, min: Self, max: Self) -> Self {
243 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
244 self.max(min).min(max)
245 }
246
247 #[inline]
253 #[must_use]
254 pub fn min_element(self) -> f32 {
255 let min = |a, b| if a < b { a } else { b };
256 min(self.x, self.y)
257 }
258
259 #[inline]
265 #[must_use]
266 pub fn max_element(self) -> f32 {
267 let max = |a, b| if a > b { a } else { b };
268 max(self.x, self.y)
269 }
270
271 #[doc(alias = "argmin")]
273 #[inline]
274 #[must_use]
275 pub fn min_position(self) -> usize {
276 if self.x <= self.y { 0 } else { 1 }
277 }
278
279 #[doc(alias = "argmax")]
281 #[inline]
282 #[must_use]
283 pub fn max_position(self) -> usize {
284 if self.x >= self.y { 0 } else { 1 }
285 }
286
287 #[inline]
291 #[must_use]
292 pub fn element_sum(self) -> f32 {
293 self.x + self.y
294 }
295
296 #[inline]
300 #[must_use]
301 pub fn element_product(self) -> f32 {
302 self.x * self.y
303 }
304
305 #[inline]
311 #[must_use]
312 pub fn cmpeq(self, rhs: Self) -> BVec2 {
313 BVec2::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y))
314 }
315
316 #[inline]
322 #[must_use]
323 pub fn cmpne(self, rhs: Self) -> BVec2 {
324 BVec2::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y))
325 }
326
327 #[inline]
333 #[must_use]
334 pub fn cmpge(self, rhs: Self) -> BVec2 {
335 BVec2::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y))
336 }
337
338 #[inline]
344 #[must_use]
345 pub fn cmpgt(self, rhs: Self) -> BVec2 {
346 BVec2::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y))
347 }
348
349 #[inline]
355 #[must_use]
356 pub fn cmple(self, rhs: Self) -> BVec2 {
357 BVec2::new(self.x.le(&rhs.x), self.y.le(&rhs.y))
358 }
359
360 #[inline]
366 #[must_use]
367 pub fn cmplt(self, rhs: Self) -> BVec2 {
368 BVec2::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y))
369 }
370
371 #[inline]
373 #[must_use]
374 pub fn abs(self) -> Self {
375 Self::new(self.x.abs(), self.y.abs())
376 }
377
378 #[inline]
384 #[must_use]
385 pub fn signum(self) -> Self {
386 Self::new(self.x.signum(), self.y.signum())
387 }
388
389 #[inline]
391 #[must_use]
392 pub fn copysign(self, rhs: Self) -> Self {
393 Self::new(self.x.copysign(rhs.x), self.y.copysign(rhs.y))
394 }
395
396 #[inline]
404 #[must_use]
405 pub fn is_negative_bitmask(self) -> u32 {
406 (self.x.is_sign_negative() as u32) | ((self.y.is_sign_negative() as u32) << 1)
407 }
408
409 #[inline]
414 #[must_use]
415 pub fn is_negative_mask(self) -> BVec2 {
416 BVec2::new(self.x.is_sign_negative(), self.y.is_sign_negative())
417 }
418
419 #[inline]
422 #[must_use]
423 pub fn is_finite(self) -> bool {
424 self.x.is_finite() && self.y.is_finite()
425 }
426
427 #[inline]
431 #[must_use]
432 pub fn is_finite_mask(self) -> BVec2 {
433 BVec2::new(self.x.is_finite(), self.y.is_finite())
434 }
435
436 #[inline]
438 #[must_use]
439 pub fn is_nan(self) -> bool {
440 self.x.is_nan() || self.y.is_nan()
441 }
442
443 #[inline]
447 #[must_use]
448 pub fn is_nan_mask(self) -> BVec2 {
449 BVec2::new(self.x.is_nan(), self.y.is_nan())
450 }
451
452 #[doc(alias = "magnitude")]
454 #[inline]
455 #[must_use]
456 pub fn length(self) -> f32 {
457 math::sqrt(self.dot(self))
458 }
459
460 #[allow(dead_code)]
462 fn is_non_zero(self) -> bool {
463 self.length_squared() > 0.0
464 }
465
466 #[doc(alias = "magnitude2")]
470 #[inline]
471 #[must_use]
472 pub fn length_squared(self) -> f32 {
473 self.dot(self)
474 }
475
476 #[inline]
480 #[must_use]
481 pub fn length_recip(self) -> f32 {
482 1.0 / self.length()
483 }
484
485 #[inline]
487 #[must_use]
488 pub fn distance(self, rhs: Self) -> f32 {
489 (self - rhs).length()
490 }
491
492 #[inline]
494 #[must_use]
495 pub fn distance_squared(self, rhs: Self) -> f32 {
496 (self - rhs).length_squared()
497 }
498
499 #[inline]
501 #[must_use]
502 pub fn div_euclid(self, rhs: Self) -> Self {
503 Self::new(
504 math::div_euclid(self.x, rhs.x),
505 math::div_euclid(self.y, rhs.y),
506 )
507 }
508
509 #[inline]
513 #[must_use]
514 pub fn rem_euclid(self, rhs: Self) -> Self {
515 Self::new(
516 math::rem_euclid(self.x, rhs.x),
517 math::rem_euclid(self.y, rhs.y),
518 )
519 }
520
521 #[inline]
531 #[must_use]
532 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
533 pub fn normalize(self) -> Self {
534 #[allow(clippy::let_and_return)]
535 let normalized = self.mul(self.length_recip());
536 glam_assert!(normalized.is_finite());
537 normalized
538 }
539
540 #[inline]
547 #[must_use]
548 pub fn try_normalize(self) -> Option<Self> {
549 let rcp = self.length_recip();
550 if rcp.is_finite() && rcp > 0.0 {
551 Some(self * rcp)
552 } else {
553 None
554 }
555 }
556
557 #[inline]
565 #[must_use]
566 pub fn normalize_or(self, fallback: Self) -> Self {
567 let rcp = self.length_recip();
568 if rcp.is_finite() && rcp > 0.0 {
569 self * rcp
570 } else {
571 fallback
572 }
573 }
574
575 #[inline]
582 #[must_use]
583 pub fn normalize_or_zero(self) -> Self {
584 self.normalize_or(Self::ZERO)
585 }
586
587 #[inline]
591 #[must_use]
592 pub fn normalize_and_length(self) -> (Self, f32) {
593 let length = self.length();
594 let rcp = 1.0 / length;
595 if rcp.is_finite() && rcp > 0.0 {
596 (self * rcp, length)
597 } else {
598 (Self::X, 0.0)
599 }
600 }
601
602 #[inline]
606 #[must_use]
607 pub fn is_normalized(self) -> bool {
608 (self.length_squared() - 1.0).abs() <= 2e-4
609 }
610
611 #[inline]
619 #[must_use]
620 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
621 pub fn project_onto(self, rhs: Self) -> Self {
622 let rhs_len_sq = rhs.dot(rhs);
623 glam_assert!(rhs_len_sq != 0.0);
624 rhs * (self.dot(rhs) / rhs_len_sq)
625 }
626
627 #[doc(alias("plane"))]
638 #[inline]
639 #[must_use]
640 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
641 pub fn reject_from(self, rhs: Self) -> Self {
642 self - self.project_onto(rhs)
643 }
644
645 #[inline]
653 #[must_use]
654 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
655 pub fn project_onto_normalized(self, rhs: Self) -> Self {
656 glam_assert!(rhs.is_normalized());
657 rhs * self.dot(rhs)
658 }
659
660 #[doc(alias("plane"))]
671 #[inline]
672 #[must_use]
673 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
674 pub fn reject_from_normalized(self, rhs: Self) -> Self {
675 self - self.project_onto_normalized(rhs)
676 }
677
678 #[inline]
681 #[must_use]
682 pub fn round(self) -> Self {
683 Self::new(math::round(self.x), math::round(self.y))
684 }
685
686 #[inline]
689 #[must_use]
690 pub fn floor(self) -> Self {
691 Self::new(math::floor(self.x), math::floor(self.y))
692 }
693
694 #[inline]
697 #[must_use]
698 pub fn ceil(self) -> Self {
699 Self::new(math::ceil(self.x), math::ceil(self.y))
700 }
701
702 #[inline]
705 #[must_use]
706 pub fn trunc(self) -> Self {
707 Self::new(math::trunc(self.x), math::trunc(self.y))
708 }
709
710 #[inline]
714 #[must_use]
715 pub fn step(self, rhs: Self) -> Self {
716 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
717 }
718
719 #[inline]
730 #[must_use]
731 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
732 pub fn smoothstep(self, edge0: Self, edge1: Self) -> Self {
733 glam_assert!(edge0.cmplt(edge1).all());
734 let t = ((self - edge0) / (edge1 - edge0)).saturate();
735 t * t * (Self::splat(3.0) - Self::splat(2.0) * t)
736 }
737
738 #[inline]
740 #[must_use]
741 pub fn saturate(self) -> Self {
742 self.clamp(Self::ZERO, Self::ONE)
743 }
744
745 #[inline]
752 #[must_use]
753 pub fn fract(self) -> Self {
754 self - self.trunc()
755 }
756
757 #[inline]
764 #[must_use]
765 pub fn fract_gl(self) -> Self {
766 self - self.floor()
767 }
768
769 #[inline]
772 #[must_use]
773 pub fn exp(self) -> Self {
774 Self::new(math::exp(self.x), math::exp(self.y))
775 }
776
777 #[inline]
779 #[must_use]
780 pub fn exp2(self) -> Self {
781 Self::new(math::exp2(self.x), math::exp2(self.y))
782 }
783
784 #[inline]
787 #[must_use]
788 pub fn ln(self) -> Self {
789 Self::new(math::ln(self.x), math::ln(self.y))
790 }
791
792 #[inline]
795 #[must_use]
796 pub fn log2(self) -> Self {
797 Self::new(math::log2(self.x), math::log2(self.y))
798 }
799
800 #[inline]
802 #[must_use]
803 pub fn powf(self, n: f32) -> Self {
804 Self::new(math::powf(self.x, n), math::powf(self.y, n))
805 }
806
807 #[inline]
810 #[must_use]
811 pub fn sqrt(self) -> Self {
812 Self::new(math::sqrt(self.x), math::sqrt(self.y))
813 }
814
815 #[inline]
817 #[must_use]
818 pub fn cos(self) -> Self {
819 Self::new(math::cos(self.x), math::cos(self.y))
820 }
821
822 #[inline]
824 #[must_use]
825 pub fn sin(self) -> Self {
826 Self::new(math::sin(self.x), math::sin(self.y))
827 }
828
829 #[inline]
831 #[must_use]
832 pub fn sin_cos(self) -> (Self, Self) {
833 let (sin_x, cos_x) = math::sin_cos(self.x);
834 let (sin_y, cos_y) = math::sin_cos(self.y);
835
836 (Self::new(sin_x, sin_y), Self::new(cos_x, cos_y))
837 }
838
839 #[inline]
841 #[must_use]
842 pub fn recip(self) -> Self {
843 Self::new(1.0 / self.x, 1.0 / self.y)
844 }
845
846 #[doc(alias = "mix")]
858 #[inline]
859 #[must_use]
860 pub fn lerp(self, rhs: Self, s: f32) -> Self {
861 self * (1.0 - s) + rhs * s
862 }
863
864 #[doc(alias = "mix")]
881 #[inline]
882 #[must_use]
883 pub fn lerp_monotonic(self, rhs: Self, s: f32) -> Self {
884 self + (rhs - self) * s
885 }
886
887 #[inline]
892 #[must_use]
893 pub fn move_towards(self, rhs: Self, d: f32) -> Self {
894 let a = rhs - self;
895 let len = a.length();
896 if len <= d || len <= 1e-4 {
897 return rhs;
898 }
899 self + a / len * d
900 }
901
902 #[inline]
908 pub fn midpoint(self, rhs: Self) -> Self {
909 (self + rhs) * 0.5
910 }
911
912 #[inline]
922 #[must_use]
923 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
924 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
925 }
926
927 #[inline]
933 #[must_use]
934 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
935 pub fn clamp_length(self, min: f32, max: f32) -> Self {
936 glam_assert!(0.0 <= min);
937 glam_assert!(min <= max);
938 let length_sq = self.length_squared();
939 if length_sq < min * min {
940 min * (self / math::sqrt(length_sq))
941 } else if length_sq > max * max {
942 max * (self / math::sqrt(length_sq))
943 } else {
944 self
945 }
946 }
947
948 #[inline]
954 #[must_use]
955 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
956 pub fn clamp_length_max(self, max: f32) -> Self {
957 glam_assert!(0.0 <= max);
958 let length_sq = self.length_squared();
959 if length_sq > max * max {
960 max * (self / math::sqrt(length_sq))
961 } else {
962 self
963 }
964 }
965
966 #[inline]
972 #[must_use]
973 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
974 pub fn clamp_length_min(self, min: f32) -> Self {
975 glam_assert!(0.0 <= min);
976 let length_sq = self.length_squared();
977 if length_sq < min * min {
978 min * (self / math::sqrt(length_sq))
979 } else {
980 self
981 }
982 }
983
984 #[inline]
992 #[must_use]
993 pub fn mul_add(self, a: Self, b: Self) -> Self {
994 Self::new(
995 math::mul_add(self.x, a.x, b.x),
996 math::mul_add(self.y, a.y, b.y),
997 )
998 }
999
1000 #[inline]
1009 #[must_use]
1010 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1011 pub fn reflect(self, normal: Self) -> Self {
1012 glam_assert!(normal.is_normalized());
1013 self - 2.0 * self.dot(normal) * normal
1014 }
1015
1016 #[inline]
1026 #[must_use]
1027 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1028 pub fn refract(self, normal: Self, eta: f32) -> Self {
1029 glam_assert!(self.is_normalized());
1030 glam_assert!(normal.is_normalized());
1031 let n_dot_i = normal.dot(self);
1032 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1033 if k >= 0.0 {
1034 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1035 } else {
1036 Self::ZERO
1037 }
1038 }
1039
1040 #[inline]
1045 #[must_use]
1046 pub fn from_angle(angle: f32) -> Self {
1047 let (sin, cos) = math::sin_cos(angle);
1048 Self::new(cos, sin)
1049 }
1050
1051 #[inline]
1055 #[must_use]
1056 pub fn to_angle(self) -> f32 {
1057 math::atan2(self.y, self.x)
1058 }
1059
1060 #[inline]
1071 #[must_use]
1072 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1073 pub fn angle_to(self, rhs: Self) -> f32 {
1074 glam_assert!(self.is_non_zero());
1075 glam_assert!(rhs.is_non_zero());
1076 let angle = math::acos_approx(
1077 self.dot(rhs) / math::sqrt(self.length_squared() * rhs.length_squared()),
1078 );
1079
1080 angle * self.perp_dot(rhs).signum()
1081 }
1082
1083 #[inline]
1085 #[must_use]
1086 pub fn perp(self) -> Self {
1087 Self::new(-self.y, self.x)
1088 }
1089
1090 #[doc(alias = "wedge")]
1093 #[doc(alias = "cross")]
1094 #[doc(alias = "determinant")]
1095 #[inline]
1096 #[must_use]
1097 pub fn perp_dot(self, rhs: Self) -> f32 {
1098 (self.x * rhs.y) - (self.y * rhs.x)
1099 }
1100
1101 #[inline]
1109 #[must_use]
1110 pub fn rotate(self, rhs: Self) -> Self {
1111 Self::new(
1112 self.x * rhs.x - self.y * rhs.y,
1113 self.y * rhs.x + self.x * rhs.y,
1114 )
1115 }
1116
1117 #[inline]
1120 #[must_use]
1121 pub fn rotate_angle(self, angle: f32) -> Self {
1122 self.rotate(Self::from_angle(angle))
1123 }
1124
1125 #[inline]
1135 #[must_use]
1136 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1137 pub fn rotate_towards(self, rhs: Self, max_angle: f32) -> Self {
1138 let a = self.angle_to(rhs);
1139 let abs_a = a.abs();
1140 let angle = max_angle.clamp(abs_a - core::f32::consts::PI, abs_a) * a.signum();
1142 Self::from_angle(angle).rotate(self)
1143 }
1144
1145 #[cfg(feature = "f64")]
1147 #[inline]
1148 #[must_use]
1149 pub fn as_dvec2(self) -> crate::DVec2 {
1150 crate::DVec2::new(self.x as f64, self.y as f64)
1151 }
1152
1153 #[cfg(feature = "i8")]
1155 #[inline]
1156 #[must_use]
1157 pub fn as_i8vec2(self) -> crate::I8Vec2 {
1158 crate::I8Vec2::new(self.x as i8, self.y as i8)
1159 }
1160
1161 #[cfg(feature = "u8")]
1163 #[inline]
1164 #[must_use]
1165 pub fn as_u8vec2(self) -> crate::U8Vec2 {
1166 crate::U8Vec2::new(self.x as u8, self.y as u8)
1167 }
1168
1169 #[cfg(feature = "i16")]
1171 #[inline]
1172 #[must_use]
1173 pub fn as_i16vec2(self) -> crate::I16Vec2 {
1174 crate::I16Vec2::new(self.x as i16, self.y as i16)
1175 }
1176
1177 #[cfg(feature = "u16")]
1179 #[inline]
1180 #[must_use]
1181 pub fn as_u16vec2(self) -> crate::U16Vec2 {
1182 crate::U16Vec2::new(self.x as u16, self.y as u16)
1183 }
1184
1185 #[cfg(feature = "i32")]
1187 #[inline]
1188 #[must_use]
1189 pub fn as_ivec2(self) -> crate::IVec2 {
1190 crate::IVec2::new(self.x as i32, self.y as i32)
1191 }
1192
1193 #[cfg(feature = "u32")]
1195 #[inline]
1196 #[must_use]
1197 pub fn as_uvec2(self) -> crate::UVec2 {
1198 crate::UVec2::new(self.x as u32, self.y as u32)
1199 }
1200
1201 #[cfg(feature = "i64")]
1203 #[inline]
1204 #[must_use]
1205 pub fn as_i64vec2(self) -> crate::I64Vec2 {
1206 crate::I64Vec2::new(self.x as i64, self.y as i64)
1207 }
1208
1209 #[cfg(feature = "u64")]
1211 #[inline]
1212 #[must_use]
1213 pub fn as_u64vec2(self) -> crate::U64Vec2 {
1214 crate::U64Vec2::new(self.x as u64, self.y as u64)
1215 }
1216
1217 #[cfg(feature = "isize")]
1219 #[inline]
1220 #[must_use]
1221 pub fn as_isizevec2(self) -> crate::ISizeVec2 {
1222 crate::ISizeVec2::new(self.x as isize, self.y as isize)
1223 }
1224
1225 #[cfg(feature = "usize")]
1227 #[inline]
1228 #[must_use]
1229 pub fn as_usizevec2(self) -> crate::USizeVec2 {
1230 crate::USizeVec2::new(self.x as usize, self.y as usize)
1231 }
1232}
1233
1234impl Default for Vec2 {
1235 #[inline(always)]
1236 fn default() -> Self {
1237 Self::ZERO
1238 }
1239}
1240
1241impl Div for Vec2 {
1242 type Output = Self;
1243 #[inline]
1244 fn div(self, rhs: Self) -> Self {
1245 Self::new(self.x.div(rhs.x), self.y.div(rhs.y))
1246 }
1247}
1248
1249impl Div<&Self> for Vec2 {
1250 type Output = Self;
1251 #[inline]
1252 fn div(self, rhs: &Self) -> Self {
1253 self.div(*rhs)
1254 }
1255}
1256
1257impl Div<&Vec2> for &Vec2 {
1258 type Output = Vec2;
1259 #[inline]
1260 fn div(self, rhs: &Vec2) -> Vec2 {
1261 (*self).div(*rhs)
1262 }
1263}
1264
1265impl Div<Vec2> for &Vec2 {
1266 type Output = Vec2;
1267 #[inline]
1268 fn div(self, rhs: Vec2) -> Vec2 {
1269 (*self).div(rhs)
1270 }
1271}
1272
1273impl DivAssign for Vec2 {
1274 #[inline]
1275 fn div_assign(&mut self, rhs: Self) {
1276 self.x.div_assign(rhs.x);
1277 self.y.div_assign(rhs.y);
1278 }
1279}
1280
1281impl DivAssign<&Self> for Vec2 {
1282 #[inline]
1283 fn div_assign(&mut self, rhs: &Self) {
1284 self.div_assign(*rhs);
1285 }
1286}
1287
1288impl Div<f32> for Vec2 {
1289 type Output = Self;
1290 #[inline]
1291 fn div(self, rhs: f32) -> Self {
1292 Self::new(self.x.div(rhs), self.y.div(rhs))
1293 }
1294}
1295
1296impl Div<&f32> for Vec2 {
1297 type Output = Self;
1298 #[inline]
1299 fn div(self, rhs: &f32) -> Self {
1300 self.div(*rhs)
1301 }
1302}
1303
1304impl Div<&f32> for &Vec2 {
1305 type Output = Vec2;
1306 #[inline]
1307 fn div(self, rhs: &f32) -> Vec2 {
1308 (*self).div(*rhs)
1309 }
1310}
1311
1312impl Div<f32> for &Vec2 {
1313 type Output = Vec2;
1314 #[inline]
1315 fn div(self, rhs: f32) -> Vec2 {
1316 (*self).div(rhs)
1317 }
1318}
1319
1320impl DivAssign<f32> for Vec2 {
1321 #[inline]
1322 fn div_assign(&mut self, rhs: f32) {
1323 self.x.div_assign(rhs);
1324 self.y.div_assign(rhs);
1325 }
1326}
1327
1328impl DivAssign<&f32> for Vec2 {
1329 #[inline]
1330 fn div_assign(&mut self, rhs: &f32) {
1331 self.div_assign(*rhs);
1332 }
1333}
1334
1335impl Div<Vec2> for f32 {
1336 type Output = Vec2;
1337 #[inline]
1338 fn div(self, rhs: Vec2) -> Vec2 {
1339 Vec2::new(self.div(rhs.x), self.div(rhs.y))
1340 }
1341}
1342
1343impl Div<&Vec2> for f32 {
1344 type Output = Vec2;
1345 #[inline]
1346 fn div(self, rhs: &Vec2) -> Vec2 {
1347 self.div(*rhs)
1348 }
1349}
1350
1351impl Div<&Vec2> for &f32 {
1352 type Output = Vec2;
1353 #[inline]
1354 fn div(self, rhs: &Vec2) -> Vec2 {
1355 (*self).div(*rhs)
1356 }
1357}
1358
1359impl Div<Vec2> for &f32 {
1360 type Output = Vec2;
1361 #[inline]
1362 fn div(self, rhs: Vec2) -> Vec2 {
1363 (*self).div(rhs)
1364 }
1365}
1366
1367impl Mul for Vec2 {
1368 type Output = Self;
1369 #[inline]
1370 fn mul(self, rhs: Self) -> Self {
1371 Self::new(self.x.mul(rhs.x), self.y.mul(rhs.y))
1372 }
1373}
1374
1375impl Mul<&Self> for Vec2 {
1376 type Output = Self;
1377 #[inline]
1378 fn mul(self, rhs: &Self) -> Self {
1379 self.mul(*rhs)
1380 }
1381}
1382
1383impl Mul<&Vec2> for &Vec2 {
1384 type Output = Vec2;
1385 #[inline]
1386 fn mul(self, rhs: &Vec2) -> Vec2 {
1387 (*self).mul(*rhs)
1388 }
1389}
1390
1391impl Mul<Vec2> for &Vec2 {
1392 type Output = Vec2;
1393 #[inline]
1394 fn mul(self, rhs: Vec2) -> Vec2 {
1395 (*self).mul(rhs)
1396 }
1397}
1398
1399impl MulAssign for Vec2 {
1400 #[inline]
1401 fn mul_assign(&mut self, rhs: Self) {
1402 self.x.mul_assign(rhs.x);
1403 self.y.mul_assign(rhs.y);
1404 }
1405}
1406
1407impl MulAssign<&Self> for Vec2 {
1408 #[inline]
1409 fn mul_assign(&mut self, rhs: &Self) {
1410 self.mul_assign(*rhs);
1411 }
1412}
1413
1414impl Mul<f32> for Vec2 {
1415 type Output = Self;
1416 #[inline]
1417 fn mul(self, rhs: f32) -> Self {
1418 Self::new(self.x.mul(rhs), self.y.mul(rhs))
1419 }
1420}
1421
1422impl Mul<&f32> for Vec2 {
1423 type Output = Self;
1424 #[inline]
1425 fn mul(self, rhs: &f32) -> Self {
1426 self.mul(*rhs)
1427 }
1428}
1429
1430impl Mul<&f32> for &Vec2 {
1431 type Output = Vec2;
1432 #[inline]
1433 fn mul(self, rhs: &f32) -> Vec2 {
1434 (*self).mul(*rhs)
1435 }
1436}
1437
1438impl Mul<f32> for &Vec2 {
1439 type Output = Vec2;
1440 #[inline]
1441 fn mul(self, rhs: f32) -> Vec2 {
1442 (*self).mul(rhs)
1443 }
1444}
1445
1446impl MulAssign<f32> for Vec2 {
1447 #[inline]
1448 fn mul_assign(&mut self, rhs: f32) {
1449 self.x.mul_assign(rhs);
1450 self.y.mul_assign(rhs);
1451 }
1452}
1453
1454impl MulAssign<&f32> for Vec2 {
1455 #[inline]
1456 fn mul_assign(&mut self, rhs: &f32) {
1457 self.mul_assign(*rhs);
1458 }
1459}
1460
1461impl Mul<Vec2> for f32 {
1462 type Output = Vec2;
1463 #[inline]
1464 fn mul(self, rhs: Vec2) -> Vec2 {
1465 Vec2::new(self.mul(rhs.x), self.mul(rhs.y))
1466 }
1467}
1468
1469impl Mul<&Vec2> for f32 {
1470 type Output = Vec2;
1471 #[inline]
1472 fn mul(self, rhs: &Vec2) -> Vec2 {
1473 self.mul(*rhs)
1474 }
1475}
1476
1477impl Mul<&Vec2> for &f32 {
1478 type Output = Vec2;
1479 #[inline]
1480 fn mul(self, rhs: &Vec2) -> Vec2 {
1481 (*self).mul(*rhs)
1482 }
1483}
1484
1485impl Mul<Vec2> for &f32 {
1486 type Output = Vec2;
1487 #[inline]
1488 fn mul(self, rhs: Vec2) -> Vec2 {
1489 (*self).mul(rhs)
1490 }
1491}
1492
1493impl Add for Vec2 {
1494 type Output = Self;
1495 #[inline]
1496 fn add(self, rhs: Self) -> Self {
1497 Self::new(self.x.add(rhs.x), self.y.add(rhs.y))
1498 }
1499}
1500
1501impl Add<&Self> for Vec2 {
1502 type Output = Self;
1503 #[inline]
1504 fn add(self, rhs: &Self) -> Self {
1505 self.add(*rhs)
1506 }
1507}
1508
1509impl Add<&Vec2> for &Vec2 {
1510 type Output = Vec2;
1511 #[inline]
1512 fn add(self, rhs: &Vec2) -> Vec2 {
1513 (*self).add(*rhs)
1514 }
1515}
1516
1517impl Add<Vec2> for &Vec2 {
1518 type Output = Vec2;
1519 #[inline]
1520 fn add(self, rhs: Vec2) -> Vec2 {
1521 (*self).add(rhs)
1522 }
1523}
1524
1525impl AddAssign for Vec2 {
1526 #[inline]
1527 fn add_assign(&mut self, rhs: Self) {
1528 self.x.add_assign(rhs.x);
1529 self.y.add_assign(rhs.y);
1530 }
1531}
1532
1533impl AddAssign<&Self> for Vec2 {
1534 #[inline]
1535 fn add_assign(&mut self, rhs: &Self) {
1536 self.add_assign(*rhs);
1537 }
1538}
1539
1540impl Add<f32> for Vec2 {
1541 type Output = Self;
1542 #[inline]
1543 fn add(self, rhs: f32) -> Self {
1544 Self::new(self.x.add(rhs), self.y.add(rhs))
1545 }
1546}
1547
1548impl Add<&f32> for Vec2 {
1549 type Output = Self;
1550 #[inline]
1551 fn add(self, rhs: &f32) -> Self {
1552 self.add(*rhs)
1553 }
1554}
1555
1556impl Add<&f32> for &Vec2 {
1557 type Output = Vec2;
1558 #[inline]
1559 fn add(self, rhs: &f32) -> Vec2 {
1560 (*self).add(*rhs)
1561 }
1562}
1563
1564impl Add<f32> for &Vec2 {
1565 type Output = Vec2;
1566 #[inline]
1567 fn add(self, rhs: f32) -> Vec2 {
1568 (*self).add(rhs)
1569 }
1570}
1571
1572impl AddAssign<f32> for Vec2 {
1573 #[inline]
1574 fn add_assign(&mut self, rhs: f32) {
1575 self.x.add_assign(rhs);
1576 self.y.add_assign(rhs);
1577 }
1578}
1579
1580impl AddAssign<&f32> for Vec2 {
1581 #[inline]
1582 fn add_assign(&mut self, rhs: &f32) {
1583 self.add_assign(*rhs);
1584 }
1585}
1586
1587impl Add<Vec2> for f32 {
1588 type Output = Vec2;
1589 #[inline]
1590 fn add(self, rhs: Vec2) -> Vec2 {
1591 Vec2::new(self.add(rhs.x), self.add(rhs.y))
1592 }
1593}
1594
1595impl Add<&Vec2> for f32 {
1596 type Output = Vec2;
1597 #[inline]
1598 fn add(self, rhs: &Vec2) -> Vec2 {
1599 self.add(*rhs)
1600 }
1601}
1602
1603impl Add<&Vec2> for &f32 {
1604 type Output = Vec2;
1605 #[inline]
1606 fn add(self, rhs: &Vec2) -> Vec2 {
1607 (*self).add(*rhs)
1608 }
1609}
1610
1611impl Add<Vec2> for &f32 {
1612 type Output = Vec2;
1613 #[inline]
1614 fn add(self, rhs: Vec2) -> Vec2 {
1615 (*self).add(rhs)
1616 }
1617}
1618
1619impl Sub for Vec2 {
1620 type Output = Self;
1621 #[inline]
1622 fn sub(self, rhs: Self) -> Self {
1623 Self::new(self.x.sub(rhs.x), self.y.sub(rhs.y))
1624 }
1625}
1626
1627impl Sub<&Self> for Vec2 {
1628 type Output = Self;
1629 #[inline]
1630 fn sub(self, rhs: &Self) -> Self {
1631 self.sub(*rhs)
1632 }
1633}
1634
1635impl Sub<&Vec2> for &Vec2 {
1636 type Output = Vec2;
1637 #[inline]
1638 fn sub(self, rhs: &Vec2) -> Vec2 {
1639 (*self).sub(*rhs)
1640 }
1641}
1642
1643impl Sub<Vec2> for &Vec2 {
1644 type Output = Vec2;
1645 #[inline]
1646 fn sub(self, rhs: Vec2) -> Vec2 {
1647 (*self).sub(rhs)
1648 }
1649}
1650
1651impl SubAssign for Vec2 {
1652 #[inline]
1653 fn sub_assign(&mut self, rhs: Self) {
1654 self.x.sub_assign(rhs.x);
1655 self.y.sub_assign(rhs.y);
1656 }
1657}
1658
1659impl SubAssign<&Self> for Vec2 {
1660 #[inline]
1661 fn sub_assign(&mut self, rhs: &Self) {
1662 self.sub_assign(*rhs);
1663 }
1664}
1665
1666impl Sub<f32> for Vec2 {
1667 type Output = Self;
1668 #[inline]
1669 fn sub(self, rhs: f32) -> Self {
1670 Self::new(self.x.sub(rhs), self.y.sub(rhs))
1671 }
1672}
1673
1674impl Sub<&f32> for Vec2 {
1675 type Output = Self;
1676 #[inline]
1677 fn sub(self, rhs: &f32) -> Self {
1678 self.sub(*rhs)
1679 }
1680}
1681
1682impl Sub<&f32> for &Vec2 {
1683 type Output = Vec2;
1684 #[inline]
1685 fn sub(self, rhs: &f32) -> Vec2 {
1686 (*self).sub(*rhs)
1687 }
1688}
1689
1690impl Sub<f32> for &Vec2 {
1691 type Output = Vec2;
1692 #[inline]
1693 fn sub(self, rhs: f32) -> Vec2 {
1694 (*self).sub(rhs)
1695 }
1696}
1697
1698impl SubAssign<f32> for Vec2 {
1699 #[inline]
1700 fn sub_assign(&mut self, rhs: f32) {
1701 self.x.sub_assign(rhs);
1702 self.y.sub_assign(rhs);
1703 }
1704}
1705
1706impl SubAssign<&f32> for Vec2 {
1707 #[inline]
1708 fn sub_assign(&mut self, rhs: &f32) {
1709 self.sub_assign(*rhs);
1710 }
1711}
1712
1713impl Sub<Vec2> for f32 {
1714 type Output = Vec2;
1715 #[inline]
1716 fn sub(self, rhs: Vec2) -> Vec2 {
1717 Vec2::new(self.sub(rhs.x), self.sub(rhs.y))
1718 }
1719}
1720
1721impl Sub<&Vec2> for f32 {
1722 type Output = Vec2;
1723 #[inline]
1724 fn sub(self, rhs: &Vec2) -> Vec2 {
1725 self.sub(*rhs)
1726 }
1727}
1728
1729impl Sub<&Vec2> for &f32 {
1730 type Output = Vec2;
1731 #[inline]
1732 fn sub(self, rhs: &Vec2) -> Vec2 {
1733 (*self).sub(*rhs)
1734 }
1735}
1736
1737impl Sub<Vec2> for &f32 {
1738 type Output = Vec2;
1739 #[inline]
1740 fn sub(self, rhs: Vec2) -> Vec2 {
1741 (*self).sub(rhs)
1742 }
1743}
1744
1745impl Rem for Vec2 {
1746 type Output = Self;
1747 #[inline]
1748 fn rem(self, rhs: Self) -> Self {
1749 Self::new(self.x.rem(rhs.x), self.y.rem(rhs.y))
1750 }
1751}
1752
1753impl Rem<&Self> for Vec2 {
1754 type Output = Self;
1755 #[inline]
1756 fn rem(self, rhs: &Self) -> Self {
1757 self.rem(*rhs)
1758 }
1759}
1760
1761impl Rem<&Vec2> for &Vec2 {
1762 type Output = Vec2;
1763 #[inline]
1764 fn rem(self, rhs: &Vec2) -> Vec2 {
1765 (*self).rem(*rhs)
1766 }
1767}
1768
1769impl Rem<Vec2> for &Vec2 {
1770 type Output = Vec2;
1771 #[inline]
1772 fn rem(self, rhs: Vec2) -> Vec2 {
1773 (*self).rem(rhs)
1774 }
1775}
1776
1777impl RemAssign for Vec2 {
1778 #[inline]
1779 fn rem_assign(&mut self, rhs: Self) {
1780 self.x.rem_assign(rhs.x);
1781 self.y.rem_assign(rhs.y);
1782 }
1783}
1784
1785impl RemAssign<&Self> for Vec2 {
1786 #[inline]
1787 fn rem_assign(&mut self, rhs: &Self) {
1788 self.rem_assign(*rhs);
1789 }
1790}
1791
1792impl Rem<f32> for Vec2 {
1793 type Output = Self;
1794 #[inline]
1795 fn rem(self, rhs: f32) -> Self {
1796 Self::new(self.x.rem(rhs), self.y.rem(rhs))
1797 }
1798}
1799
1800impl Rem<&f32> for Vec2 {
1801 type Output = Self;
1802 #[inline]
1803 fn rem(self, rhs: &f32) -> Self {
1804 self.rem(*rhs)
1805 }
1806}
1807
1808impl Rem<&f32> for &Vec2 {
1809 type Output = Vec2;
1810 #[inline]
1811 fn rem(self, rhs: &f32) -> Vec2 {
1812 (*self).rem(*rhs)
1813 }
1814}
1815
1816impl Rem<f32> for &Vec2 {
1817 type Output = Vec2;
1818 #[inline]
1819 fn rem(self, rhs: f32) -> Vec2 {
1820 (*self).rem(rhs)
1821 }
1822}
1823
1824impl RemAssign<f32> for Vec2 {
1825 #[inline]
1826 fn rem_assign(&mut self, rhs: f32) {
1827 self.x.rem_assign(rhs);
1828 self.y.rem_assign(rhs);
1829 }
1830}
1831
1832impl RemAssign<&f32> for Vec2 {
1833 #[inline]
1834 fn rem_assign(&mut self, rhs: &f32) {
1835 self.rem_assign(*rhs);
1836 }
1837}
1838
1839impl Rem<Vec2> for f32 {
1840 type Output = Vec2;
1841 #[inline]
1842 fn rem(self, rhs: Vec2) -> Vec2 {
1843 Vec2::new(self.rem(rhs.x), self.rem(rhs.y))
1844 }
1845}
1846
1847impl Rem<&Vec2> for f32 {
1848 type Output = Vec2;
1849 #[inline]
1850 fn rem(self, rhs: &Vec2) -> Vec2 {
1851 self.rem(*rhs)
1852 }
1853}
1854
1855impl Rem<&Vec2> for &f32 {
1856 type Output = Vec2;
1857 #[inline]
1858 fn rem(self, rhs: &Vec2) -> Vec2 {
1859 (*self).rem(*rhs)
1860 }
1861}
1862
1863impl Rem<Vec2> for &f32 {
1864 type Output = Vec2;
1865 #[inline]
1866 fn rem(self, rhs: Vec2) -> Vec2 {
1867 (*self).rem(rhs)
1868 }
1869}
1870
1871impl AsRef<[f32; 2]> for Vec2 {
1872 #[inline]
1873 fn as_ref(&self) -> &[f32; 2] {
1874 unsafe { &*(self as *const Self as *const [f32; 2]) }
1875 }
1876}
1877
1878impl AsMut<[f32; 2]> for Vec2 {
1879 #[inline]
1880 fn as_mut(&mut self) -> &mut [f32; 2] {
1881 unsafe { &mut *(self as *mut Self as *mut [f32; 2]) }
1882 }
1883}
1884
1885impl Sum for Vec2 {
1886 #[inline]
1887 fn sum<I>(iter: I) -> Self
1888 where
1889 I: Iterator<Item = Self>,
1890 {
1891 iter.fold(Self::ZERO, Self::add)
1892 }
1893}
1894
1895impl<'a> Sum<&'a Self> for Vec2 {
1896 #[inline]
1897 fn sum<I>(iter: I) -> Self
1898 where
1899 I: Iterator<Item = &'a Self>,
1900 {
1901 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1902 }
1903}
1904
1905impl Product for Vec2 {
1906 #[inline]
1907 fn product<I>(iter: I) -> Self
1908 where
1909 I: Iterator<Item = Self>,
1910 {
1911 iter.fold(Self::ONE, Self::mul)
1912 }
1913}
1914
1915impl<'a> Product<&'a Self> for Vec2 {
1916 #[inline]
1917 fn product<I>(iter: I) -> Self
1918 where
1919 I: Iterator<Item = &'a Self>,
1920 {
1921 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1922 }
1923}
1924
1925impl Neg for Vec2 {
1926 type Output = Self;
1927 #[inline]
1928 fn neg(self) -> Self {
1929 Self::new(self.x.neg(), self.y.neg())
1930 }
1931}
1932
1933impl Neg for &Vec2 {
1934 type Output = Vec2;
1935 #[inline]
1936 fn neg(self) -> Vec2 {
1937 (*self).neg()
1938 }
1939}
1940
1941impl Index<usize> for Vec2 {
1942 type Output = f32;
1943 #[inline]
1944 #[track_caller]
1945 fn index(&self, index: usize) -> &Self::Output {
1946 match index {
1947 0 => &self.x,
1948 1 => &self.y,
1949 _ => panic!("index out of bounds"),
1950 }
1951 }
1952}
1953
1954impl IndexMut<usize> for Vec2 {
1955 #[inline]
1956 #[track_caller]
1957 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1958 match index {
1959 0 => &mut self.x,
1960 1 => &mut self.y,
1961 _ => panic!("index out of bounds"),
1962 }
1963 }
1964}
1965
1966impl fmt::Display for Vec2 {
1967 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1968 if let Some(p) = f.precision() {
1969 write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y)
1970 } else {
1971 write!(f, "[{}, {}]", self.x, self.y)
1972 }
1973 }
1974}
1975
1976impl fmt::Debug for Vec2 {
1977 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1978 fmt.debug_tuple(stringify!(Vec2))
1979 .field(&self.x)
1980 .field(&self.y)
1981 .finish()
1982 }
1983}
1984
1985impl From<[f32; 2]> for Vec2 {
1986 #[inline]
1987 fn from(a: [f32; 2]) -> Self {
1988 Self::new(a[0], a[1])
1989 }
1990}
1991
1992impl From<Vec2> for [f32; 2] {
1993 #[inline]
1994 fn from(v: Vec2) -> Self {
1995 [v.x, v.y]
1996 }
1997}
1998
1999impl From<(f32, f32)> for Vec2 {
2000 #[inline]
2001 fn from(t: (f32, f32)) -> Self {
2002 Self::new(t.0, t.1)
2003 }
2004}
2005
2006impl From<Vec2> for (f32, f32) {
2007 #[inline]
2008 fn from(v: Vec2) -> Self {
2009 (v.x, v.y)
2010 }
2011}
2012
2013impl From<BVec2> for Vec2 {
2014 #[inline]
2015 fn from(v: BVec2) -> Self {
2016 Self::new(f32::from(v.x), f32::from(v.y))
2017 }
2018}