1use crate::{f32::math, BVec2, Vec3};
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 #[deprecated(since = "0.31.0", note = "Renamed to USES_WASM_SIMD")]
85 pub const USES_WASM32_SIMD: bool = false;
86
87 #[inline(always)]
89 #[must_use]
90 pub const fn new(x: f32, y: f32) -> Self {
91 Self { x, y }
92 }
93
94 #[inline]
96 #[must_use]
97 pub const fn splat(v: f32) -> Self {
98 Self::new(v, v)
99 }
100
101 #[inline]
103 #[must_use]
104 pub fn map<F>(self, mut f: F) -> Self
105 where
106 F: FnMut(f32) -> f32,
107 {
108 Self::new(f(self.x), f(self.y))
109 }
110
111 #[inline]
117 #[must_use]
118 pub fn select(mask: BVec2, if_true: Self, if_false: Self) -> Self {
119 Self::new(
120 if mask.test(0) { if_true.x } else { if_false.x },
121 if mask.test(1) { if_true.y } else { if_false.y },
122 )
123 }
124
125 #[inline]
127 #[must_use]
128 pub const fn from_array(a: [f32; 2]) -> Self {
129 Self::new(a[0], a[1])
130 }
131
132 #[inline]
134 #[must_use]
135 pub const fn to_array(&self) -> [f32; 2] {
136 [self.x, self.y]
137 }
138
139 #[inline]
145 #[must_use]
146 #[track_caller]
147 pub const fn from_slice(slice: &[f32]) -> Self {
148 assert!(slice.len() >= 2);
149 Self::new(slice[0], slice[1])
150 }
151
152 #[inline]
158 #[track_caller]
159 pub fn write_to_slice(self, slice: &mut [f32]) {
160 slice[..2].copy_from_slice(&self.to_array());
161 }
162
163 #[inline]
165 #[must_use]
166 pub const fn extend(self, z: f32) -> Vec3 {
167 Vec3::new(self.x, self.y, z)
168 }
169
170 #[inline]
172 #[must_use]
173 pub fn with_x(mut self, x: f32) -> Self {
174 self.x = x;
175 self
176 }
177
178 #[inline]
180 #[must_use]
181 pub fn with_y(mut self, y: f32) -> Self {
182 self.y = y;
183 self
184 }
185
186 #[inline]
188 #[must_use]
189 pub fn dot(self, rhs: Self) -> f32 {
190 (self.x * rhs.x) + (self.y * rhs.y)
191 }
192
193 #[inline]
195 #[must_use]
196 pub fn dot_into_vec(self, rhs: Self) -> Self {
197 Self::splat(self.dot(rhs))
198 }
199
200 #[inline]
207 #[must_use]
208 pub fn min(self, rhs: Self) -> Self {
209 Self::new(
210 if self.x < rhs.x { self.x } else { rhs.x },
211 if self.y < rhs.y { self.y } else { rhs.y },
212 )
213 }
214
215 #[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]
241 #[must_use]
242 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
243 pub fn clamp(self, min: Self, max: Self) -> Self {
244 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
245 self.max(min).min(max)
246 }
247
248 #[inline]
255 #[must_use]
256 pub fn min_element(self) -> f32 {
257 let min = |a, b| if a < b { a } else { b };
258 min(self.x, self.y)
259 }
260
261 #[inline]
268 #[must_use]
269 pub fn max_element(self) -> f32 {
270 let max = |a, b| if a > b { a } else { b };
271 max(self.x, self.y)
272 }
273
274 #[doc(alias = "argmin")]
276 #[inline]
277 #[must_use]
278 pub fn min_position(self) -> usize {
279 if self.x <= self.y {
280 0
281 } else {
282 1
283 }
284 }
285
286 #[doc(alias = "argmax")]
288 #[inline]
289 #[must_use]
290 pub fn max_position(self) -> usize {
291 if self.x >= self.y {
292 0
293 } else {
294 1
295 }
296 }
297
298 #[inline]
302 #[must_use]
303 pub fn element_sum(self) -> f32 {
304 self.x + self.y
305 }
306
307 #[inline]
311 #[must_use]
312 pub fn element_product(self) -> f32 {
313 self.x * self.y
314 }
315
316 #[inline]
322 #[must_use]
323 pub fn cmpeq(self, rhs: Self) -> BVec2 {
324 BVec2::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y))
325 }
326
327 #[inline]
333 #[must_use]
334 pub fn cmpne(self, rhs: Self) -> BVec2 {
335 BVec2::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y))
336 }
337
338 #[inline]
344 #[must_use]
345 pub fn cmpge(self, rhs: Self) -> BVec2 {
346 BVec2::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y))
347 }
348
349 #[inline]
355 #[must_use]
356 pub fn cmpgt(self, rhs: Self) -> BVec2 {
357 BVec2::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y))
358 }
359
360 #[inline]
366 #[must_use]
367 pub fn cmple(self, rhs: Self) -> BVec2 {
368 BVec2::new(self.x.le(&rhs.x), self.y.le(&rhs.y))
369 }
370
371 #[inline]
377 #[must_use]
378 pub fn cmplt(self, rhs: Self) -> BVec2 {
379 BVec2::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y))
380 }
381
382 #[inline]
384 #[must_use]
385 pub fn abs(self) -> Self {
386 Self::new(math::abs(self.x), math::abs(self.y))
387 }
388
389 #[inline]
395 #[must_use]
396 pub fn signum(self) -> Self {
397 Self::new(math::signum(self.x), math::signum(self.y))
398 }
399
400 #[inline]
402 #[must_use]
403 pub fn copysign(self, rhs: Self) -> Self {
404 Self::new(math::copysign(self.x, rhs.x), math::copysign(self.y, rhs.y))
405 }
406
407 #[inline]
415 #[must_use]
416 pub fn is_negative_bitmask(self) -> u32 {
417 (self.x.is_sign_negative() as u32) | ((self.y.is_sign_negative() as u32) << 1)
418 }
419
420 #[inline]
425 #[must_use]
426 pub fn is_negative_mask(self) -> BVec2 {
427 BVec2::new(self.x.is_sign_negative(), self.y.is_sign_negative())
428 }
429
430 #[inline]
433 #[must_use]
434 pub fn is_finite(self) -> bool {
435 self.x.is_finite() && self.y.is_finite()
436 }
437
438 #[inline]
442 #[must_use]
443 pub fn is_finite_mask(self) -> BVec2 {
444 BVec2::new(self.x.is_finite(), self.y.is_finite())
445 }
446
447 #[inline]
449 #[must_use]
450 pub fn is_nan(self) -> bool {
451 self.x.is_nan() || self.y.is_nan()
452 }
453
454 #[inline]
458 #[must_use]
459 pub fn is_nan_mask(self) -> BVec2 {
460 BVec2::new(self.x.is_nan(), self.y.is_nan())
461 }
462
463 #[doc(alias = "magnitude")]
465 #[inline]
466 #[must_use]
467 pub fn length(self) -> f32 {
468 math::sqrt(self.dot(self))
469 }
470
471 #[allow(dead_code)]
473 fn is_non_zero(self) -> bool {
474 self.length_squared() > 0.0
475 }
476
477 #[doc(alias = "magnitude2")]
481 #[inline]
482 #[must_use]
483 pub fn length_squared(self) -> f32 {
484 self.dot(self)
485 }
486
487 #[inline]
491 #[must_use]
492 pub fn length_recip(self) -> f32 {
493 1.0 / self.length()
494 }
495
496 #[inline]
498 #[must_use]
499 pub fn distance(self, rhs: Self) -> f32 {
500 (self - rhs).length()
501 }
502
503 #[inline]
505 #[must_use]
506 pub fn distance_squared(self, rhs: Self) -> f32 {
507 (self - rhs).length_squared()
508 }
509
510 #[inline]
512 #[must_use]
513 pub fn div_euclid(self, rhs: Self) -> Self {
514 Self::new(
515 math::div_euclid(self.x, rhs.x),
516 math::div_euclid(self.y, rhs.y),
517 )
518 }
519
520 #[inline]
524 #[must_use]
525 pub fn rem_euclid(self, rhs: Self) -> Self {
526 Self::new(
527 math::rem_euclid(self.x, rhs.x),
528 math::rem_euclid(self.y, rhs.y),
529 )
530 }
531
532 #[inline]
542 #[must_use]
543 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
544 pub fn normalize(self) -> Self {
545 #[allow(clippy::let_and_return)]
546 let normalized = self.mul(self.length_recip());
547 glam_assert!(normalized.is_finite());
548 normalized
549 }
550
551 #[inline]
558 #[must_use]
559 pub fn try_normalize(self) -> Option<Self> {
560 let rcp = self.length_recip();
561 if rcp.is_finite() && rcp > 0.0 {
562 Some(self * rcp)
563 } else {
564 None
565 }
566 }
567
568 #[inline]
576 #[must_use]
577 pub fn normalize_or(self, fallback: Self) -> Self {
578 let rcp = self.length_recip();
579 if rcp.is_finite() && rcp > 0.0 {
580 self * rcp
581 } else {
582 fallback
583 }
584 }
585
586 #[inline]
593 #[must_use]
594 pub fn normalize_or_zero(self) -> Self {
595 self.normalize_or(Self::ZERO)
596 }
597
598 #[inline]
602 #[must_use]
603 pub fn normalize_and_length(self) -> (Self, f32) {
604 let length = self.length();
605 let rcp = 1.0 / length;
606 if rcp.is_finite() && rcp > 0.0 {
607 (self * rcp, length)
608 } else {
609 (Self::X, 0.0)
610 }
611 }
612
613 #[inline]
617 #[must_use]
618 pub fn is_normalized(self) -> bool {
619 math::abs(self.length_squared() - 1.0) <= 2e-4
620 }
621
622 #[inline]
630 #[must_use]
631 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
632 pub fn project_onto(self, rhs: Self) -> Self {
633 let rhs_len_sq = rhs.dot(rhs);
634 glam_assert!(rhs_len_sq != 0.0);
635 rhs * (self.dot(rhs) / rhs_len_sq)
636 }
637
638 #[doc(alias("plane"))]
649 #[inline]
650 #[must_use]
651 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
652 pub fn reject_from(self, rhs: Self) -> Self {
653 self - self.project_onto(rhs)
654 }
655
656 #[inline]
664 #[must_use]
665 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
666 pub fn project_onto_normalized(self, rhs: Self) -> Self {
667 glam_assert!(rhs.is_normalized());
668 rhs * self.dot(rhs)
669 }
670
671 #[doc(alias("plane"))]
682 #[inline]
683 #[must_use]
684 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
685 pub fn reject_from_normalized(self, rhs: Self) -> Self {
686 self - self.project_onto_normalized(rhs)
687 }
688
689 #[inline]
692 #[must_use]
693 pub fn round(self) -> Self {
694 Self::new(math::round(self.x), math::round(self.y))
695 }
696
697 #[inline]
700 #[must_use]
701 pub fn floor(self) -> Self {
702 Self::new(math::floor(self.x), math::floor(self.y))
703 }
704
705 #[inline]
708 #[must_use]
709 pub fn ceil(self) -> Self {
710 Self::new(math::ceil(self.x), math::ceil(self.y))
711 }
712
713 #[inline]
716 #[must_use]
717 pub fn trunc(self) -> Self {
718 Self::new(math::trunc(self.x), math::trunc(self.y))
719 }
720
721 #[inline]
725 #[must_use]
726 pub fn step(self, rhs: Self) -> Self {
727 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
728 }
729
730 #[inline]
741 #[must_use]
742 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
743 pub fn smoothstep(self, edge0: Self, edge1: Self) -> Self {
744 glam_assert!(edge0.cmplt(edge1).all());
745 let t = ((self - edge0) / (edge1 - edge0)).saturate();
746 t * t * (Self::splat(3.0) - Self::splat(2.0) * t)
747 }
748
749 #[inline]
751 #[must_use]
752 pub fn saturate(self) -> Self {
753 self.clamp(Self::ZERO, Self::ONE)
754 }
755
756 #[inline]
763 #[must_use]
764 pub fn fract(self) -> Self {
765 self - self.trunc()
766 }
767
768 #[inline]
775 #[must_use]
776 pub fn fract_gl(self) -> Self {
777 self - self.floor()
778 }
779
780 #[inline]
783 #[must_use]
784 pub fn exp(self) -> Self {
785 Self::new(math::exp(self.x), math::exp(self.y))
786 }
787
788 #[inline]
790 #[must_use]
791 pub fn exp2(self) -> Self {
792 Self::new(math::exp2(self.x), math::exp2(self.y))
793 }
794
795 #[inline]
798 #[must_use]
799 pub fn ln(self) -> Self {
800 Self::new(math::ln(self.x), math::ln(self.y))
801 }
802
803 #[inline]
806 #[must_use]
807 pub fn log2(self) -> Self {
808 Self::new(math::log2(self.x), math::log2(self.y))
809 }
810
811 #[inline]
813 #[must_use]
814 pub fn powf(self, n: f32) -> Self {
815 Self::new(math::powf(self.x, n), math::powf(self.y, n))
816 }
817
818 #[inline]
821 #[must_use]
822 pub fn sqrt(self) -> Self {
823 Self::new(math::sqrt(self.x), math::sqrt(self.y))
824 }
825
826 #[inline]
828 #[must_use]
829 pub fn cos(self) -> Self {
830 Self::new(math::cos(self.x), math::cos(self.y))
831 }
832
833 #[inline]
835 #[must_use]
836 pub fn sin(self) -> Self {
837 Self::new(math::sin(self.x), math::sin(self.y))
838 }
839
840 #[inline]
842 #[must_use]
843 pub fn sin_cos(self) -> (Self, Self) {
844 let (sin_x, cos_x) = math::sin_cos(self.x);
845 let (sin_y, cos_y) = math::sin_cos(self.y);
846
847 (Self::new(sin_x, sin_y), Self::new(cos_x, cos_y))
848 }
849
850 #[inline]
852 #[must_use]
853 pub fn recip(self) -> Self {
854 Self::new(1.0 / self.x, 1.0 / self.y)
855 }
856
857 #[doc(alias = "mix")]
869 #[inline]
870 #[must_use]
871 pub fn lerp(self, rhs: Self, s: f32) -> Self {
872 self * (1.0 - s) + rhs * s
873 }
874
875 #[doc(alias = "mix")]
892 #[inline]
893 #[must_use]
894 pub fn lerp_monotonic(self, rhs: Self, s: f32) -> Self {
895 self + (rhs - self) * s
896 }
897
898 #[inline]
903 #[must_use]
904 pub fn move_towards(self, rhs: Self, d: f32) -> Self {
905 let a = rhs - self;
906 let len = a.length();
907 if len <= d || len <= 1e-4 {
908 return rhs;
909 }
910 self + a / len * d
911 }
912
913 #[inline]
919 pub fn midpoint(self, rhs: Self) -> Self {
920 (self + rhs) * 0.5
921 }
922
923 #[inline]
933 #[must_use]
934 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
935 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
936 }
937
938 #[inline]
944 #[must_use]
945 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
946 pub fn clamp_length(self, min: f32, max: f32) -> Self {
947 glam_assert!(0.0 <= min);
948 glam_assert!(min <= max);
949 let length_sq = self.length_squared();
950 if length_sq < min * min {
951 min * (self / math::sqrt(length_sq))
952 } else if length_sq > max * max {
953 max * (self / math::sqrt(length_sq))
954 } else {
955 self
956 }
957 }
958
959 #[inline]
965 #[must_use]
966 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
967 pub fn clamp_length_max(self, max: f32) -> Self {
968 glam_assert!(0.0 <= max);
969 let length_sq = self.length_squared();
970 if length_sq > max * max {
971 max * (self / math::sqrt(length_sq))
972 } else {
973 self
974 }
975 }
976
977 #[inline]
983 #[must_use]
984 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
985 pub fn clamp_length_min(self, min: f32) -> Self {
986 glam_assert!(0.0 <= min);
987 let length_sq = self.length_squared();
988 if length_sq < min * min {
989 min * (self / math::sqrt(length_sq))
990 } else {
991 self
992 }
993 }
994
995 #[inline]
1003 #[must_use]
1004 pub fn mul_add(self, a: Self, b: Self) -> Self {
1005 Self::new(
1006 math::mul_add(self.x, a.x, b.x),
1007 math::mul_add(self.y, a.y, b.y),
1008 )
1009 }
1010
1011 #[inline]
1020 #[must_use]
1021 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1022 pub fn reflect(self, normal: Self) -> Self {
1023 glam_assert!(normal.is_normalized());
1024 self - 2.0 * self.dot(normal) * normal
1025 }
1026
1027 #[inline]
1037 #[must_use]
1038 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1039 pub fn refract(self, normal: Self, eta: f32) -> Self {
1040 glam_assert!(self.is_normalized());
1041 glam_assert!(normal.is_normalized());
1042 let n_dot_i = normal.dot(self);
1043 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1044 if k >= 0.0 {
1045 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1046 } else {
1047 Self::ZERO
1048 }
1049 }
1050
1051 #[inline]
1056 #[must_use]
1057 pub fn from_angle(angle: f32) -> Self {
1058 let (sin, cos) = math::sin_cos(angle);
1059 Self::new(cos, sin)
1060 }
1061
1062 #[inline]
1066 #[must_use]
1067 pub fn to_angle(self) -> f32 {
1068 math::atan2(self.y, self.x)
1069 }
1070
1071 #[inline]
1082 #[must_use]
1083 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1084 pub fn angle_to(self, rhs: Self) -> f32 {
1085 glam_assert!(self.is_non_zero());
1086 glam_assert!(rhs.is_non_zero());
1087 let angle = math::acos_approx(
1088 self.dot(rhs) / math::sqrt(self.length_squared() * rhs.length_squared()),
1089 );
1090
1091 angle * math::signum(self.perp_dot(rhs))
1092 }
1093
1094 #[inline]
1096 #[must_use]
1097 pub fn perp(self) -> Self {
1098 Self::new(-self.y, self.x)
1099 }
1100
1101 #[doc(alias = "wedge")]
1104 #[doc(alias = "cross")]
1105 #[doc(alias = "determinant")]
1106 #[inline]
1107 #[must_use]
1108 pub fn perp_dot(self, rhs: Self) -> f32 {
1109 (self.x * rhs.y) - (self.y * rhs.x)
1110 }
1111
1112 #[inline]
1120 #[must_use]
1121 pub fn rotate(self, rhs: Self) -> Self {
1122 Self::new(
1123 self.x * rhs.x - self.y * rhs.y,
1124 self.y * rhs.x + self.x * rhs.y,
1125 )
1126 }
1127
1128 #[inline]
1131 #[must_use]
1132 pub fn rotate_angle(self, angle: f32) -> Self {
1133 self.rotate(Self::from_angle(angle))
1134 }
1135
1136 #[inline]
1146 #[must_use]
1147 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1148 pub fn rotate_towards(self, rhs: Self, max_angle: f32) -> Self {
1149 let a = self.angle_to(rhs);
1150 let abs_a = math::abs(a);
1151 let angle = max_angle.clamp(abs_a - core::f32::consts::PI, abs_a) * math::signum(a);
1153 Self::from_angle(angle).rotate(self)
1154 }
1155
1156 #[cfg(feature = "f64")]
1158 #[inline]
1159 #[must_use]
1160 pub fn as_dvec2(self) -> crate::DVec2 {
1161 crate::DVec2::new(self.x as f64, self.y as f64)
1162 }
1163
1164 #[cfg(feature = "i8")]
1166 #[inline]
1167 #[must_use]
1168 pub fn as_i8vec2(self) -> crate::I8Vec2 {
1169 crate::I8Vec2::new(self.x as i8, self.y as i8)
1170 }
1171
1172 #[cfg(feature = "u8")]
1174 #[inline]
1175 #[must_use]
1176 pub fn as_u8vec2(self) -> crate::U8Vec2 {
1177 crate::U8Vec2::new(self.x as u8, self.y as u8)
1178 }
1179
1180 #[cfg(feature = "i16")]
1182 #[inline]
1183 #[must_use]
1184 pub fn as_i16vec2(self) -> crate::I16Vec2 {
1185 crate::I16Vec2::new(self.x as i16, self.y as i16)
1186 }
1187
1188 #[cfg(feature = "u16")]
1190 #[inline]
1191 #[must_use]
1192 pub fn as_u16vec2(self) -> crate::U16Vec2 {
1193 crate::U16Vec2::new(self.x as u16, self.y as u16)
1194 }
1195
1196 #[cfg(feature = "i32")]
1198 #[inline]
1199 #[must_use]
1200 pub fn as_ivec2(self) -> crate::IVec2 {
1201 crate::IVec2::new(self.x as i32, self.y as i32)
1202 }
1203
1204 #[cfg(feature = "u32")]
1206 #[inline]
1207 #[must_use]
1208 pub fn as_uvec2(self) -> crate::UVec2 {
1209 crate::UVec2::new(self.x as u32, self.y as u32)
1210 }
1211
1212 #[cfg(feature = "i64")]
1214 #[inline]
1215 #[must_use]
1216 pub fn as_i64vec2(self) -> crate::I64Vec2 {
1217 crate::I64Vec2::new(self.x as i64, self.y as i64)
1218 }
1219
1220 #[cfg(feature = "u64")]
1222 #[inline]
1223 #[must_use]
1224 pub fn as_u64vec2(self) -> crate::U64Vec2 {
1225 crate::U64Vec2::new(self.x as u64, self.y as u64)
1226 }
1227
1228 #[cfg(feature = "isize")]
1230 #[inline]
1231 #[must_use]
1232 pub fn as_isizevec2(self) -> crate::ISizeVec2 {
1233 crate::ISizeVec2::new(self.x as isize, self.y as isize)
1234 }
1235
1236 #[cfg(feature = "usize")]
1238 #[inline]
1239 #[must_use]
1240 pub fn as_usizevec2(self) -> crate::USizeVec2 {
1241 crate::USizeVec2::new(self.x as usize, self.y as usize)
1242 }
1243}
1244
1245impl Default for Vec2 {
1246 #[inline(always)]
1247 fn default() -> Self {
1248 Self::ZERO
1249 }
1250}
1251
1252impl Div for Vec2 {
1253 type Output = Self;
1254 #[inline]
1255 fn div(self, rhs: Self) -> Self {
1256 Self::new(self.x.div(rhs.x), self.y.div(rhs.y))
1257 }
1258}
1259
1260impl Div<&Self> for Vec2 {
1261 type Output = Self;
1262 #[inline]
1263 fn div(self, rhs: &Self) -> Self {
1264 self.div(*rhs)
1265 }
1266}
1267
1268impl Div<&Vec2> for &Vec2 {
1269 type Output = Vec2;
1270 #[inline]
1271 fn div(self, rhs: &Vec2) -> Vec2 {
1272 (*self).div(*rhs)
1273 }
1274}
1275
1276impl Div<Vec2> for &Vec2 {
1277 type Output = Vec2;
1278 #[inline]
1279 fn div(self, rhs: Vec2) -> Vec2 {
1280 (*self).div(rhs)
1281 }
1282}
1283
1284impl DivAssign for Vec2 {
1285 #[inline]
1286 fn div_assign(&mut self, rhs: Self) {
1287 self.x.div_assign(rhs.x);
1288 self.y.div_assign(rhs.y);
1289 }
1290}
1291
1292impl DivAssign<&Self> for Vec2 {
1293 #[inline]
1294 fn div_assign(&mut self, rhs: &Self) {
1295 self.div_assign(*rhs);
1296 }
1297}
1298
1299impl Div<f32> for Vec2 {
1300 type Output = Self;
1301 #[inline]
1302 fn div(self, rhs: f32) -> Self {
1303 Self::new(self.x.div(rhs), self.y.div(rhs))
1304 }
1305}
1306
1307impl Div<&f32> for Vec2 {
1308 type Output = Self;
1309 #[inline]
1310 fn div(self, rhs: &f32) -> Self {
1311 self.div(*rhs)
1312 }
1313}
1314
1315impl Div<&f32> for &Vec2 {
1316 type Output = Vec2;
1317 #[inline]
1318 fn div(self, rhs: &f32) -> Vec2 {
1319 (*self).div(*rhs)
1320 }
1321}
1322
1323impl Div<f32> for &Vec2 {
1324 type Output = Vec2;
1325 #[inline]
1326 fn div(self, rhs: f32) -> Vec2 {
1327 (*self).div(rhs)
1328 }
1329}
1330
1331impl DivAssign<f32> for Vec2 {
1332 #[inline]
1333 fn div_assign(&mut self, rhs: f32) {
1334 self.x.div_assign(rhs);
1335 self.y.div_assign(rhs);
1336 }
1337}
1338
1339impl DivAssign<&f32> for Vec2 {
1340 #[inline]
1341 fn div_assign(&mut self, rhs: &f32) {
1342 self.div_assign(*rhs);
1343 }
1344}
1345
1346impl Div<Vec2> for f32 {
1347 type Output = Vec2;
1348 #[inline]
1349 fn div(self, rhs: Vec2) -> Vec2 {
1350 Vec2::new(self.div(rhs.x), self.div(rhs.y))
1351 }
1352}
1353
1354impl Div<&Vec2> for f32 {
1355 type Output = Vec2;
1356 #[inline]
1357 fn div(self, rhs: &Vec2) -> Vec2 {
1358 self.div(*rhs)
1359 }
1360}
1361
1362impl Div<&Vec2> for &f32 {
1363 type Output = Vec2;
1364 #[inline]
1365 fn div(self, rhs: &Vec2) -> Vec2 {
1366 (*self).div(*rhs)
1367 }
1368}
1369
1370impl Div<Vec2> for &f32 {
1371 type Output = Vec2;
1372 #[inline]
1373 fn div(self, rhs: Vec2) -> Vec2 {
1374 (*self).div(rhs)
1375 }
1376}
1377
1378impl Mul for Vec2 {
1379 type Output = Self;
1380 #[inline]
1381 fn mul(self, rhs: Self) -> Self {
1382 Self::new(self.x.mul(rhs.x), self.y.mul(rhs.y))
1383 }
1384}
1385
1386impl Mul<&Self> for Vec2 {
1387 type Output = Self;
1388 #[inline]
1389 fn mul(self, rhs: &Self) -> Self {
1390 self.mul(*rhs)
1391 }
1392}
1393
1394impl Mul<&Vec2> for &Vec2 {
1395 type Output = Vec2;
1396 #[inline]
1397 fn mul(self, rhs: &Vec2) -> Vec2 {
1398 (*self).mul(*rhs)
1399 }
1400}
1401
1402impl Mul<Vec2> for &Vec2 {
1403 type Output = Vec2;
1404 #[inline]
1405 fn mul(self, rhs: Vec2) -> Vec2 {
1406 (*self).mul(rhs)
1407 }
1408}
1409
1410impl MulAssign for Vec2 {
1411 #[inline]
1412 fn mul_assign(&mut self, rhs: Self) {
1413 self.x.mul_assign(rhs.x);
1414 self.y.mul_assign(rhs.y);
1415 }
1416}
1417
1418impl MulAssign<&Self> for Vec2 {
1419 #[inline]
1420 fn mul_assign(&mut self, rhs: &Self) {
1421 self.mul_assign(*rhs);
1422 }
1423}
1424
1425impl Mul<f32> for Vec2 {
1426 type Output = Self;
1427 #[inline]
1428 fn mul(self, rhs: f32) -> Self {
1429 Self::new(self.x.mul(rhs), self.y.mul(rhs))
1430 }
1431}
1432
1433impl Mul<&f32> for Vec2 {
1434 type Output = Self;
1435 #[inline]
1436 fn mul(self, rhs: &f32) -> Self {
1437 self.mul(*rhs)
1438 }
1439}
1440
1441impl Mul<&f32> for &Vec2 {
1442 type Output = Vec2;
1443 #[inline]
1444 fn mul(self, rhs: &f32) -> Vec2 {
1445 (*self).mul(*rhs)
1446 }
1447}
1448
1449impl Mul<f32> for &Vec2 {
1450 type Output = Vec2;
1451 #[inline]
1452 fn mul(self, rhs: f32) -> Vec2 {
1453 (*self).mul(rhs)
1454 }
1455}
1456
1457impl MulAssign<f32> for Vec2 {
1458 #[inline]
1459 fn mul_assign(&mut self, rhs: f32) {
1460 self.x.mul_assign(rhs);
1461 self.y.mul_assign(rhs);
1462 }
1463}
1464
1465impl MulAssign<&f32> for Vec2 {
1466 #[inline]
1467 fn mul_assign(&mut self, rhs: &f32) {
1468 self.mul_assign(*rhs);
1469 }
1470}
1471
1472impl Mul<Vec2> for f32 {
1473 type Output = Vec2;
1474 #[inline]
1475 fn mul(self, rhs: Vec2) -> Vec2 {
1476 Vec2::new(self.mul(rhs.x), self.mul(rhs.y))
1477 }
1478}
1479
1480impl Mul<&Vec2> for f32 {
1481 type Output = Vec2;
1482 #[inline]
1483 fn mul(self, rhs: &Vec2) -> Vec2 {
1484 self.mul(*rhs)
1485 }
1486}
1487
1488impl Mul<&Vec2> for &f32 {
1489 type Output = Vec2;
1490 #[inline]
1491 fn mul(self, rhs: &Vec2) -> Vec2 {
1492 (*self).mul(*rhs)
1493 }
1494}
1495
1496impl Mul<Vec2> for &f32 {
1497 type Output = Vec2;
1498 #[inline]
1499 fn mul(self, rhs: Vec2) -> Vec2 {
1500 (*self).mul(rhs)
1501 }
1502}
1503
1504impl Add for Vec2 {
1505 type Output = Self;
1506 #[inline]
1507 fn add(self, rhs: Self) -> Self {
1508 Self::new(self.x.add(rhs.x), self.y.add(rhs.y))
1509 }
1510}
1511
1512impl Add<&Self> for Vec2 {
1513 type Output = Self;
1514 #[inline]
1515 fn add(self, rhs: &Self) -> Self {
1516 self.add(*rhs)
1517 }
1518}
1519
1520impl Add<&Vec2> for &Vec2 {
1521 type Output = Vec2;
1522 #[inline]
1523 fn add(self, rhs: &Vec2) -> Vec2 {
1524 (*self).add(*rhs)
1525 }
1526}
1527
1528impl Add<Vec2> for &Vec2 {
1529 type Output = Vec2;
1530 #[inline]
1531 fn add(self, rhs: Vec2) -> Vec2 {
1532 (*self).add(rhs)
1533 }
1534}
1535
1536impl AddAssign for Vec2 {
1537 #[inline]
1538 fn add_assign(&mut self, rhs: Self) {
1539 self.x.add_assign(rhs.x);
1540 self.y.add_assign(rhs.y);
1541 }
1542}
1543
1544impl AddAssign<&Self> for Vec2 {
1545 #[inline]
1546 fn add_assign(&mut self, rhs: &Self) {
1547 self.add_assign(*rhs);
1548 }
1549}
1550
1551impl Add<f32> for Vec2 {
1552 type Output = Self;
1553 #[inline]
1554 fn add(self, rhs: f32) -> Self {
1555 Self::new(self.x.add(rhs), self.y.add(rhs))
1556 }
1557}
1558
1559impl Add<&f32> for Vec2 {
1560 type Output = Self;
1561 #[inline]
1562 fn add(self, rhs: &f32) -> Self {
1563 self.add(*rhs)
1564 }
1565}
1566
1567impl Add<&f32> for &Vec2 {
1568 type Output = Vec2;
1569 #[inline]
1570 fn add(self, rhs: &f32) -> Vec2 {
1571 (*self).add(*rhs)
1572 }
1573}
1574
1575impl Add<f32> for &Vec2 {
1576 type Output = Vec2;
1577 #[inline]
1578 fn add(self, rhs: f32) -> Vec2 {
1579 (*self).add(rhs)
1580 }
1581}
1582
1583impl AddAssign<f32> for Vec2 {
1584 #[inline]
1585 fn add_assign(&mut self, rhs: f32) {
1586 self.x.add_assign(rhs);
1587 self.y.add_assign(rhs);
1588 }
1589}
1590
1591impl AddAssign<&f32> for Vec2 {
1592 #[inline]
1593 fn add_assign(&mut self, rhs: &f32) {
1594 self.add_assign(*rhs);
1595 }
1596}
1597
1598impl Add<Vec2> for f32 {
1599 type Output = Vec2;
1600 #[inline]
1601 fn add(self, rhs: Vec2) -> Vec2 {
1602 Vec2::new(self.add(rhs.x), self.add(rhs.y))
1603 }
1604}
1605
1606impl Add<&Vec2> for f32 {
1607 type Output = Vec2;
1608 #[inline]
1609 fn add(self, rhs: &Vec2) -> Vec2 {
1610 self.add(*rhs)
1611 }
1612}
1613
1614impl Add<&Vec2> for &f32 {
1615 type Output = Vec2;
1616 #[inline]
1617 fn add(self, rhs: &Vec2) -> Vec2 {
1618 (*self).add(*rhs)
1619 }
1620}
1621
1622impl Add<Vec2> for &f32 {
1623 type Output = Vec2;
1624 #[inline]
1625 fn add(self, rhs: Vec2) -> Vec2 {
1626 (*self).add(rhs)
1627 }
1628}
1629
1630impl Sub for Vec2 {
1631 type Output = Self;
1632 #[inline]
1633 fn sub(self, rhs: Self) -> Self {
1634 Self::new(self.x.sub(rhs.x), self.y.sub(rhs.y))
1635 }
1636}
1637
1638impl Sub<&Self> for Vec2 {
1639 type Output = Self;
1640 #[inline]
1641 fn sub(self, rhs: &Self) -> Self {
1642 self.sub(*rhs)
1643 }
1644}
1645
1646impl Sub<&Vec2> for &Vec2 {
1647 type Output = Vec2;
1648 #[inline]
1649 fn sub(self, rhs: &Vec2) -> Vec2 {
1650 (*self).sub(*rhs)
1651 }
1652}
1653
1654impl Sub<Vec2> for &Vec2 {
1655 type Output = Vec2;
1656 #[inline]
1657 fn sub(self, rhs: Vec2) -> Vec2 {
1658 (*self).sub(rhs)
1659 }
1660}
1661
1662impl SubAssign for Vec2 {
1663 #[inline]
1664 fn sub_assign(&mut self, rhs: Self) {
1665 self.x.sub_assign(rhs.x);
1666 self.y.sub_assign(rhs.y);
1667 }
1668}
1669
1670impl SubAssign<&Self> for Vec2 {
1671 #[inline]
1672 fn sub_assign(&mut self, rhs: &Self) {
1673 self.sub_assign(*rhs);
1674 }
1675}
1676
1677impl Sub<f32> for Vec2 {
1678 type Output = Self;
1679 #[inline]
1680 fn sub(self, rhs: f32) -> Self {
1681 Self::new(self.x.sub(rhs), self.y.sub(rhs))
1682 }
1683}
1684
1685impl Sub<&f32> for Vec2 {
1686 type Output = Self;
1687 #[inline]
1688 fn sub(self, rhs: &f32) -> Self {
1689 self.sub(*rhs)
1690 }
1691}
1692
1693impl Sub<&f32> for &Vec2 {
1694 type Output = Vec2;
1695 #[inline]
1696 fn sub(self, rhs: &f32) -> Vec2 {
1697 (*self).sub(*rhs)
1698 }
1699}
1700
1701impl Sub<f32> for &Vec2 {
1702 type Output = Vec2;
1703 #[inline]
1704 fn sub(self, rhs: f32) -> Vec2 {
1705 (*self).sub(rhs)
1706 }
1707}
1708
1709impl SubAssign<f32> for Vec2 {
1710 #[inline]
1711 fn sub_assign(&mut self, rhs: f32) {
1712 self.x.sub_assign(rhs);
1713 self.y.sub_assign(rhs);
1714 }
1715}
1716
1717impl SubAssign<&f32> for Vec2 {
1718 #[inline]
1719 fn sub_assign(&mut self, rhs: &f32) {
1720 self.sub_assign(*rhs);
1721 }
1722}
1723
1724impl Sub<Vec2> for f32 {
1725 type Output = Vec2;
1726 #[inline]
1727 fn sub(self, rhs: Vec2) -> Vec2 {
1728 Vec2::new(self.sub(rhs.x), self.sub(rhs.y))
1729 }
1730}
1731
1732impl Sub<&Vec2> for f32 {
1733 type Output = Vec2;
1734 #[inline]
1735 fn sub(self, rhs: &Vec2) -> Vec2 {
1736 self.sub(*rhs)
1737 }
1738}
1739
1740impl Sub<&Vec2> for &f32 {
1741 type Output = Vec2;
1742 #[inline]
1743 fn sub(self, rhs: &Vec2) -> Vec2 {
1744 (*self).sub(*rhs)
1745 }
1746}
1747
1748impl Sub<Vec2> for &f32 {
1749 type Output = Vec2;
1750 #[inline]
1751 fn sub(self, rhs: Vec2) -> Vec2 {
1752 (*self).sub(rhs)
1753 }
1754}
1755
1756impl Rem for Vec2 {
1757 type Output = Self;
1758 #[inline]
1759 fn rem(self, rhs: Self) -> Self {
1760 Self::new(self.x.rem(rhs.x), self.y.rem(rhs.y))
1761 }
1762}
1763
1764impl Rem<&Self> for Vec2 {
1765 type Output = Self;
1766 #[inline]
1767 fn rem(self, rhs: &Self) -> Self {
1768 self.rem(*rhs)
1769 }
1770}
1771
1772impl Rem<&Vec2> for &Vec2 {
1773 type Output = Vec2;
1774 #[inline]
1775 fn rem(self, rhs: &Vec2) -> Vec2 {
1776 (*self).rem(*rhs)
1777 }
1778}
1779
1780impl Rem<Vec2> for &Vec2 {
1781 type Output = Vec2;
1782 #[inline]
1783 fn rem(self, rhs: Vec2) -> Vec2 {
1784 (*self).rem(rhs)
1785 }
1786}
1787
1788impl RemAssign for Vec2 {
1789 #[inline]
1790 fn rem_assign(&mut self, rhs: Self) {
1791 self.x.rem_assign(rhs.x);
1792 self.y.rem_assign(rhs.y);
1793 }
1794}
1795
1796impl RemAssign<&Self> for Vec2 {
1797 #[inline]
1798 fn rem_assign(&mut self, rhs: &Self) {
1799 self.rem_assign(*rhs);
1800 }
1801}
1802
1803impl Rem<f32> for Vec2 {
1804 type Output = Self;
1805 #[inline]
1806 fn rem(self, rhs: f32) -> Self {
1807 Self::new(self.x.rem(rhs), self.y.rem(rhs))
1808 }
1809}
1810
1811impl Rem<&f32> for Vec2 {
1812 type Output = Self;
1813 #[inline]
1814 fn rem(self, rhs: &f32) -> Self {
1815 self.rem(*rhs)
1816 }
1817}
1818
1819impl Rem<&f32> for &Vec2 {
1820 type Output = Vec2;
1821 #[inline]
1822 fn rem(self, rhs: &f32) -> Vec2 {
1823 (*self).rem(*rhs)
1824 }
1825}
1826
1827impl Rem<f32> for &Vec2 {
1828 type Output = Vec2;
1829 #[inline]
1830 fn rem(self, rhs: f32) -> Vec2 {
1831 (*self).rem(rhs)
1832 }
1833}
1834
1835impl RemAssign<f32> for Vec2 {
1836 #[inline]
1837 fn rem_assign(&mut self, rhs: f32) {
1838 self.x.rem_assign(rhs);
1839 self.y.rem_assign(rhs);
1840 }
1841}
1842
1843impl RemAssign<&f32> for Vec2 {
1844 #[inline]
1845 fn rem_assign(&mut self, rhs: &f32) {
1846 self.rem_assign(*rhs);
1847 }
1848}
1849
1850impl Rem<Vec2> for f32 {
1851 type Output = Vec2;
1852 #[inline]
1853 fn rem(self, rhs: Vec2) -> Vec2 {
1854 Vec2::new(self.rem(rhs.x), self.rem(rhs.y))
1855 }
1856}
1857
1858impl Rem<&Vec2> for f32 {
1859 type Output = Vec2;
1860 #[inline]
1861 fn rem(self, rhs: &Vec2) -> Vec2 {
1862 self.rem(*rhs)
1863 }
1864}
1865
1866impl Rem<&Vec2> for &f32 {
1867 type Output = Vec2;
1868 #[inline]
1869 fn rem(self, rhs: &Vec2) -> Vec2 {
1870 (*self).rem(*rhs)
1871 }
1872}
1873
1874impl Rem<Vec2> for &f32 {
1875 type Output = Vec2;
1876 #[inline]
1877 fn rem(self, rhs: Vec2) -> Vec2 {
1878 (*self).rem(rhs)
1879 }
1880}
1881
1882impl AsRef<[f32; 2]> for Vec2 {
1883 #[inline]
1884 fn as_ref(&self) -> &[f32; 2] {
1885 unsafe { &*(self as *const Self as *const [f32; 2]) }
1886 }
1887}
1888
1889impl AsMut<[f32; 2]> for Vec2 {
1890 #[inline]
1891 fn as_mut(&mut self) -> &mut [f32; 2] {
1892 unsafe { &mut *(self as *mut Self as *mut [f32; 2]) }
1893 }
1894}
1895
1896impl Sum for Vec2 {
1897 #[inline]
1898 fn sum<I>(iter: I) -> Self
1899 where
1900 I: Iterator<Item = Self>,
1901 {
1902 iter.fold(Self::ZERO, Self::add)
1903 }
1904}
1905
1906impl<'a> Sum<&'a Self> for Vec2 {
1907 #[inline]
1908 fn sum<I>(iter: I) -> Self
1909 where
1910 I: Iterator<Item = &'a Self>,
1911 {
1912 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1913 }
1914}
1915
1916impl Product for Vec2 {
1917 #[inline]
1918 fn product<I>(iter: I) -> Self
1919 where
1920 I: Iterator<Item = Self>,
1921 {
1922 iter.fold(Self::ONE, Self::mul)
1923 }
1924}
1925
1926impl<'a> Product<&'a Self> for Vec2 {
1927 #[inline]
1928 fn product<I>(iter: I) -> Self
1929 where
1930 I: Iterator<Item = &'a Self>,
1931 {
1932 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1933 }
1934}
1935
1936impl Neg for Vec2 {
1937 type Output = Self;
1938 #[inline]
1939 fn neg(self) -> Self {
1940 Self::new(self.x.neg(), self.y.neg())
1941 }
1942}
1943
1944impl Neg for &Vec2 {
1945 type Output = Vec2;
1946 #[inline]
1947 fn neg(self) -> Vec2 {
1948 (*self).neg()
1949 }
1950}
1951
1952impl Index<usize> for Vec2 {
1953 type Output = f32;
1954 #[inline]
1955 #[track_caller]
1956 fn index(&self, index: usize) -> &Self::Output {
1957 match index {
1958 0 => &self.x,
1959 1 => &self.y,
1960 _ => panic!("index out of bounds"),
1961 }
1962 }
1963}
1964
1965impl IndexMut<usize> for Vec2 {
1966 #[inline]
1967 #[track_caller]
1968 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1969 match index {
1970 0 => &mut self.x,
1971 1 => &mut self.y,
1972 _ => panic!("index out of bounds"),
1973 }
1974 }
1975}
1976
1977impl fmt::Display for Vec2 {
1978 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1979 if let Some(p) = f.precision() {
1980 write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y)
1981 } else {
1982 write!(f, "[{}, {}]", self.x, self.y)
1983 }
1984 }
1985}
1986
1987impl fmt::Debug for Vec2 {
1988 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1989 fmt.debug_tuple(stringify!(Vec2))
1990 .field(&self.x)
1991 .field(&self.y)
1992 .finish()
1993 }
1994}
1995
1996impl From<[f32; 2]> for Vec2 {
1997 #[inline]
1998 fn from(a: [f32; 2]) -> Self {
1999 Self::new(a[0], a[1])
2000 }
2001}
2002
2003impl From<Vec2> for [f32; 2] {
2004 #[inline]
2005 fn from(v: Vec2) -> Self {
2006 [v.x, v.y]
2007 }
2008}
2009
2010impl From<(f32, f32)> for Vec2 {
2011 #[inline]
2012 fn from(t: (f32, f32)) -> Self {
2013 Self::new(t.0, t.1)
2014 }
2015}
2016
2017impl From<Vec2> for (f32, f32) {
2018 #[inline]
2019 fn from(v: Vec2) -> Self {
2020 (v.x, v.y)
2021 }
2022}
2023
2024impl From<BVec2> for Vec2 {
2025 #[inline]
2026 fn from(v: BVec2) -> Self {
2027 Self::new(f32::from(v.x), f32::from(v.y))
2028 }
2029}