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_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 { x: v, y: 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 {
120 x: if mask.test(0) { if_true.x } else { if_false.x },
121 y: 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 pub const fn from_slice(slice: &[f32]) -> Self {
147 assert!(slice.len() >= 2);
148 Self::new(slice[0], slice[1])
149 }
150
151 #[inline]
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]
205 #[must_use]
206 pub fn min(self, rhs: Self) -> Self {
207 Self {
208 x: if self.x < rhs.x { self.x } else { rhs.x },
209 y: if self.y < rhs.y { self.y } else { rhs.y },
210 }
211 }
212
213 #[inline]
220 #[must_use]
221 pub fn max(self, rhs: Self) -> Self {
222 Self {
223 x: if self.x > rhs.x { self.x } else { rhs.x },
224 y: if self.y > rhs.y { self.y } else { rhs.y },
225 }
226 }
227
228 #[inline]
239 #[must_use]
240 pub fn clamp(self, min: Self, max: Self) -> Self {
241 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
242 self.max(min).min(max)
243 }
244
245 #[inline]
252 #[must_use]
253 pub fn min_element(self) -> f32 {
254 let min = |a, b| if a < b { a } else { b };
255 min(self.x, self.y)
256 }
257
258 #[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 {
277 0
278 } else {
279 1
280 }
281 }
282
283 #[doc(alias = "argmax")]
285 #[inline]
286 #[must_use]
287 pub fn max_position(self) -> usize {
288 if self.x >= self.y {
289 0
290 } else {
291 1
292 }
293 }
294
295 #[inline]
299 #[must_use]
300 pub fn element_sum(self) -> f32 {
301 self.x + self.y
302 }
303
304 #[inline]
308 #[must_use]
309 pub fn element_product(self) -> f32 {
310 self.x * self.y
311 }
312
313 #[inline]
319 #[must_use]
320 pub fn cmpeq(self, rhs: Self) -> BVec2 {
321 BVec2::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y))
322 }
323
324 #[inline]
330 #[must_use]
331 pub fn cmpne(self, rhs: Self) -> BVec2 {
332 BVec2::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y))
333 }
334
335 #[inline]
341 #[must_use]
342 pub fn cmpge(self, rhs: Self) -> BVec2 {
343 BVec2::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y))
344 }
345
346 #[inline]
352 #[must_use]
353 pub fn cmpgt(self, rhs: Self) -> BVec2 {
354 BVec2::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y))
355 }
356
357 #[inline]
363 #[must_use]
364 pub fn cmple(self, rhs: Self) -> BVec2 {
365 BVec2::new(self.x.le(&rhs.x), self.y.le(&rhs.y))
366 }
367
368 #[inline]
374 #[must_use]
375 pub fn cmplt(self, rhs: Self) -> BVec2 {
376 BVec2::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y))
377 }
378
379 #[inline]
381 #[must_use]
382 pub fn abs(self) -> Self {
383 Self {
384 x: math::abs(self.x),
385 y: math::abs(self.y),
386 }
387 }
388
389 #[inline]
395 #[must_use]
396 pub fn signum(self) -> Self {
397 Self {
398 x: math::signum(self.x),
399 y: math::signum(self.y),
400 }
401 }
402
403 #[inline]
405 #[must_use]
406 pub fn copysign(self, rhs: Self) -> Self {
407 Self {
408 x: math::copysign(self.x, rhs.x),
409 y: math::copysign(self.y, rhs.y),
410 }
411 }
412
413 #[inline]
421 #[must_use]
422 pub fn is_negative_bitmask(self) -> u32 {
423 (self.x.is_sign_negative() as u32) | ((self.y.is_sign_negative() as u32) << 1)
424 }
425
426 #[inline]
431 #[must_use]
432 pub fn is_negative_mask(self) -> BVec2 {
433 BVec2::new(self.x.is_sign_negative(), self.y.is_sign_negative())
434 }
435
436 #[inline]
439 #[must_use]
440 pub fn is_finite(self) -> bool {
441 self.x.is_finite() && self.y.is_finite()
442 }
443
444 #[inline]
448 #[must_use]
449 pub fn is_finite_mask(self) -> BVec2 {
450 BVec2::new(self.x.is_finite(), self.y.is_finite())
451 }
452
453 #[inline]
455 #[must_use]
456 pub fn is_nan(self) -> bool {
457 self.x.is_nan() || self.y.is_nan()
458 }
459
460 #[inline]
464 #[must_use]
465 pub fn is_nan_mask(self) -> BVec2 {
466 BVec2::new(self.x.is_nan(), self.y.is_nan())
467 }
468
469 #[doc(alias = "magnitude")]
471 #[inline]
472 #[must_use]
473 pub fn length(self) -> f32 {
474 math::sqrt(self.dot(self))
475 }
476
477 #[allow(dead_code)]
479 fn is_non_zero(self) -> bool {
480 self.length_squared() > 0.0
481 }
482
483 #[doc(alias = "magnitude2")]
487 #[inline]
488 #[must_use]
489 pub fn length_squared(self) -> f32 {
490 self.dot(self)
491 }
492
493 #[inline]
497 #[must_use]
498 pub fn length_recip(self) -> f32 {
499 self.length().recip()
500 }
501
502 #[inline]
504 #[must_use]
505 pub fn distance(self, rhs: Self) -> f32 {
506 (self - rhs).length()
507 }
508
509 #[inline]
511 #[must_use]
512 pub fn distance_squared(self, rhs: Self) -> f32 {
513 (self - rhs).length_squared()
514 }
515
516 #[inline]
518 #[must_use]
519 pub fn div_euclid(self, rhs: Self) -> Self {
520 Self::new(
521 math::div_euclid(self.x, rhs.x),
522 math::div_euclid(self.y, rhs.y),
523 )
524 }
525
526 #[inline]
530 #[must_use]
531 pub fn rem_euclid(self, rhs: Self) -> Self {
532 Self::new(
533 math::rem_euclid(self.x, rhs.x),
534 math::rem_euclid(self.y, rhs.y),
535 )
536 }
537
538 #[inline]
548 #[must_use]
549 pub fn normalize(self) -> Self {
550 #[allow(clippy::let_and_return)]
551 let normalized = self.mul(self.length_recip());
552 glam_assert!(normalized.is_finite());
553 normalized
554 }
555
556 #[inline]
563 #[must_use]
564 pub fn try_normalize(self) -> Option<Self> {
565 let rcp = self.length_recip();
566 if rcp.is_finite() && rcp > 0.0 {
567 Some(self * rcp)
568 } else {
569 None
570 }
571 }
572
573 #[inline]
581 #[must_use]
582 pub fn normalize_or(self, fallback: Self) -> Self {
583 let rcp = self.length_recip();
584 if rcp.is_finite() && rcp > 0.0 {
585 self * rcp
586 } else {
587 fallback
588 }
589 }
590
591 #[inline]
598 #[must_use]
599 pub fn normalize_or_zero(self) -> Self {
600 self.normalize_or(Self::ZERO)
601 }
602
603 #[inline]
607 #[must_use]
608 pub fn normalize_and_length(self) -> (Self, f32) {
609 let length = self.length();
610 let rcp = 1.0 / length;
611 if rcp.is_finite() && rcp > 0.0 {
612 (self * rcp, length)
613 } else {
614 (Self::X, 0.0)
615 }
616 }
617
618 #[inline]
622 #[must_use]
623 pub fn is_normalized(self) -> bool {
624 math::abs(self.length_squared() - 1.0) <= 2e-4
625 }
626
627 #[inline]
635 #[must_use]
636 pub fn project_onto(self, rhs: Self) -> Self {
637 let other_len_sq_rcp = rhs.dot(rhs).recip();
638 glam_assert!(other_len_sq_rcp.is_finite());
639 rhs * self.dot(rhs) * other_len_sq_rcp
640 }
641
642 #[doc(alias("plane"))]
653 #[inline]
654 #[must_use]
655 pub fn reject_from(self, rhs: Self) -> Self {
656 self - self.project_onto(rhs)
657 }
658
659 #[inline]
667 #[must_use]
668 pub fn project_onto_normalized(self, rhs: Self) -> Self {
669 glam_assert!(rhs.is_normalized());
670 rhs * self.dot(rhs)
671 }
672
673 #[doc(alias("plane"))]
684 #[inline]
685 #[must_use]
686 pub fn reject_from_normalized(self, rhs: Self) -> Self {
687 self - self.project_onto_normalized(rhs)
688 }
689
690 #[inline]
693 #[must_use]
694 pub fn round(self) -> Self {
695 Self {
696 x: math::round(self.x),
697 y: math::round(self.y),
698 }
699 }
700
701 #[inline]
704 #[must_use]
705 pub fn floor(self) -> Self {
706 Self {
707 x: math::floor(self.x),
708 y: math::floor(self.y),
709 }
710 }
711
712 #[inline]
715 #[must_use]
716 pub fn ceil(self) -> Self {
717 Self {
718 x: math::ceil(self.x),
719 y: math::ceil(self.y),
720 }
721 }
722
723 #[inline]
726 #[must_use]
727 pub fn trunc(self) -> Self {
728 Self {
729 x: math::trunc(self.x),
730 y: math::trunc(self.y),
731 }
732 }
733
734 #[inline]
738 #[must_use]
739 pub fn step(self, rhs: Self) -> Self {
740 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
741 }
742
743 #[inline]
745 #[must_use]
746 pub fn saturate(self) -> Self {
747 self.clamp(Self::ZERO, Self::ONE)
748 }
749
750 #[inline]
757 #[must_use]
758 pub fn fract(self) -> Self {
759 self - self.trunc()
760 }
761
762 #[inline]
769 #[must_use]
770 pub fn fract_gl(self) -> Self {
771 self - self.floor()
772 }
773
774 #[inline]
777 #[must_use]
778 pub fn exp(self) -> Self {
779 Self::new(math::exp(self.x), math::exp(self.y))
780 }
781
782 #[inline]
784 #[must_use]
785 pub fn exp2(self) -> Self {
786 Self::new(math::exp2(self.x), math::exp2(self.y))
787 }
788
789 #[inline]
792 #[must_use]
793 pub fn ln(self) -> Self {
794 Self::new(math::ln(self.x), math::ln(self.y))
795 }
796
797 #[inline]
800 #[must_use]
801 pub fn log2(self) -> Self {
802 Self::new(math::log2(self.x), math::log2(self.y))
803 }
804
805 #[inline]
807 #[must_use]
808 pub fn powf(self, n: f32) -> Self {
809 Self::new(math::powf(self.x, n), math::powf(self.y, n))
810 }
811
812 #[inline]
815 #[must_use]
816 pub fn sqrt(self) -> Self {
817 Self::new(math::sqrt(self.x), math::sqrt(self.y))
818 }
819
820 #[inline]
822 #[must_use]
823 pub fn cos(self) -> Self {
824 Self::new(math::cos(self.x), math::cos(self.y))
825 }
826
827 #[inline]
829 #[must_use]
830 pub fn sin(self) -> Self {
831 Self::new(math::sin(self.x), math::sin(self.y))
832 }
833
834 #[inline]
836 #[must_use]
837 pub fn sin_cos(self) -> (Self, Self) {
838 let (sin_x, cos_x) = math::sin_cos(self.x);
839 let (sin_y, cos_y) = math::sin_cos(self.y);
840
841 (Self::new(sin_x, sin_y), Self::new(cos_x, cos_y))
842 }
843
844 #[inline]
846 #[must_use]
847 pub fn recip(self) -> Self {
848 Self {
849 x: 1.0 / self.x,
850 y: 1.0 / self.y,
851 }
852 }
853
854 #[doc(alias = "mix")]
860 #[inline]
861 #[must_use]
862 pub fn lerp(self, rhs: Self, s: f32) -> Self {
863 self * (1.0 - s) + rhs * s
864 }
865
866 #[inline]
871 #[must_use]
872 pub fn move_towards(self, rhs: Self, d: f32) -> Self {
873 let a = rhs - self;
874 let len = a.length();
875 if len <= d || len <= 1e-4 {
876 return rhs;
877 }
878 self + a / len * d
879 }
880
881 #[inline]
887 pub fn midpoint(self, rhs: Self) -> Self {
888 (self + rhs) * 0.5
889 }
890
891 #[inline]
901 #[must_use]
902 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
903 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
904 }
905
906 #[inline]
912 #[must_use]
913 pub fn clamp_length(self, min: f32, max: f32) -> Self {
914 glam_assert!(0.0 <= min);
915 glam_assert!(min <= max);
916 let length_sq = self.length_squared();
917 if length_sq < min * min {
918 min * (self / math::sqrt(length_sq))
919 } else if length_sq > max * max {
920 max * (self / math::sqrt(length_sq))
921 } else {
922 self
923 }
924 }
925
926 #[inline]
932 #[must_use]
933 pub fn clamp_length_max(self, max: f32) -> Self {
934 glam_assert!(0.0 <= max);
935 let length_sq = self.length_squared();
936 if length_sq > max * max {
937 max * (self / math::sqrt(length_sq))
938 } else {
939 self
940 }
941 }
942
943 #[inline]
949 #[must_use]
950 pub fn clamp_length_min(self, min: f32) -> Self {
951 glam_assert!(0.0 <= min);
952 let length_sq = self.length_squared();
953 if length_sq < min * min {
954 min * (self / math::sqrt(length_sq))
955 } else {
956 self
957 }
958 }
959
960 #[inline]
968 #[must_use]
969 pub fn mul_add(self, a: Self, b: Self) -> Self {
970 Self::new(
971 math::mul_add(self.x, a.x, b.x),
972 math::mul_add(self.y, a.y, b.y),
973 )
974 }
975
976 #[inline]
985 #[must_use]
986 pub fn reflect(self, normal: Self) -> Self {
987 glam_assert!(normal.is_normalized());
988 self - 2.0 * self.dot(normal) * normal
989 }
990
991 #[inline]
1001 #[must_use]
1002 pub fn refract(self, normal: Self, eta: f32) -> Self {
1003 glam_assert!(self.is_normalized());
1004 glam_assert!(normal.is_normalized());
1005 let n_dot_i = normal.dot(self);
1006 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1007 if k >= 0.0 {
1008 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1009 } else {
1010 Self::ZERO
1011 }
1012 }
1013
1014 #[inline]
1019 #[must_use]
1020 pub fn from_angle(angle: f32) -> Self {
1021 let (sin, cos) = math::sin_cos(angle);
1022 Self { x: cos, y: sin }
1023 }
1024
1025 #[inline]
1029 #[must_use]
1030 pub fn to_angle(self) -> f32 {
1031 math::atan2(self.y, self.x)
1032 }
1033
1034 #[inline]
1045 #[must_use]
1046 pub fn angle_to(self, rhs: Self) -> f32 {
1047 glam_assert!(self.is_non_zero());
1048 glam_assert!(rhs.is_non_zero());
1049 let angle = math::acos_approx(
1050 self.dot(rhs) / math::sqrt(self.length_squared() * rhs.length_squared()),
1051 );
1052
1053 angle * math::signum(self.perp_dot(rhs))
1054 }
1055
1056 #[inline]
1058 #[must_use]
1059 pub fn perp(self) -> Self {
1060 Self {
1061 x: -self.y,
1062 y: self.x,
1063 }
1064 }
1065
1066 #[doc(alias = "wedge")]
1069 #[doc(alias = "cross")]
1070 #[doc(alias = "determinant")]
1071 #[inline]
1072 #[must_use]
1073 pub fn perp_dot(self, rhs: Self) -> f32 {
1074 (self.x * rhs.y) - (self.y * rhs.x)
1075 }
1076
1077 #[inline]
1085 #[must_use]
1086 pub fn rotate(self, rhs: Self) -> Self {
1087 Self {
1088 x: self.x * rhs.x - self.y * rhs.y,
1089 y: self.y * rhs.x + self.x * rhs.y,
1090 }
1091 }
1092
1093 #[inline]
1096 #[must_use]
1097 pub fn rotate_angle(self, angle: f32) -> Self {
1098 self.rotate(Self::from_angle(angle))
1099 }
1100
1101 #[inline]
1107 #[must_use]
1108 pub fn rotate_towards(self, rhs: Self, max_angle: f32) -> Self {
1109 let a = self.angle_to(rhs);
1110 let abs_a = math::abs(a);
1111 let angle = max_angle.clamp(abs_a - core::f32::consts::PI, abs_a) * math::signum(a);
1113 Self::from_angle(angle).rotate(self)
1114 }
1115
1116 #[cfg(feature = "f64")]
1118 #[inline]
1119 #[must_use]
1120 pub fn as_dvec2(self) -> crate::DVec2 {
1121 crate::DVec2::new(self.x as f64, self.y as f64)
1122 }
1123
1124 #[cfg(feature = "i8")]
1126 #[inline]
1127 #[must_use]
1128 pub fn as_i8vec2(self) -> crate::I8Vec2 {
1129 crate::I8Vec2::new(self.x as i8, self.y as i8)
1130 }
1131
1132 #[cfg(feature = "u8")]
1134 #[inline]
1135 #[must_use]
1136 pub fn as_u8vec2(self) -> crate::U8Vec2 {
1137 crate::U8Vec2::new(self.x as u8, self.y as u8)
1138 }
1139
1140 #[cfg(feature = "i16")]
1142 #[inline]
1143 #[must_use]
1144 pub fn as_i16vec2(self) -> crate::I16Vec2 {
1145 crate::I16Vec2::new(self.x as i16, self.y as i16)
1146 }
1147
1148 #[cfg(feature = "u16")]
1150 #[inline]
1151 #[must_use]
1152 pub fn as_u16vec2(self) -> crate::U16Vec2 {
1153 crate::U16Vec2::new(self.x as u16, self.y as u16)
1154 }
1155
1156 #[cfg(feature = "i32")]
1158 #[inline]
1159 #[must_use]
1160 pub fn as_ivec2(self) -> crate::IVec2 {
1161 crate::IVec2::new(self.x as i32, self.y as i32)
1162 }
1163
1164 #[cfg(feature = "u32")]
1166 #[inline]
1167 #[must_use]
1168 pub fn as_uvec2(self) -> crate::UVec2 {
1169 crate::UVec2::new(self.x as u32, self.y as u32)
1170 }
1171
1172 #[cfg(feature = "i64")]
1174 #[inline]
1175 #[must_use]
1176 pub fn as_i64vec2(self) -> crate::I64Vec2 {
1177 crate::I64Vec2::new(self.x as i64, self.y as i64)
1178 }
1179
1180 #[cfg(feature = "u64")]
1182 #[inline]
1183 #[must_use]
1184 pub fn as_u64vec2(self) -> crate::U64Vec2 {
1185 crate::U64Vec2::new(self.x as u64, self.y as u64)
1186 }
1187
1188 #[cfg(feature = "isize")]
1190 #[inline]
1191 #[must_use]
1192 pub fn as_isizevec2(self) -> crate::ISizeVec2 {
1193 crate::ISizeVec2::new(self.x as isize, self.y as isize)
1194 }
1195
1196 #[cfg(feature = "usize")]
1198 #[inline]
1199 #[must_use]
1200 pub fn as_usizevec2(self) -> crate::USizeVec2 {
1201 crate::USizeVec2::new(self.x as usize, self.y as usize)
1202 }
1203}
1204
1205impl Default for Vec2 {
1206 #[inline(always)]
1207 fn default() -> Self {
1208 Self::ZERO
1209 }
1210}
1211
1212impl Div for Vec2 {
1213 type Output = Self;
1214 #[inline]
1215 fn div(self, rhs: Self) -> Self {
1216 Self {
1217 x: self.x.div(rhs.x),
1218 y: self.y.div(rhs.y),
1219 }
1220 }
1221}
1222
1223impl Div<&Self> for Vec2 {
1224 type Output = Self;
1225 #[inline]
1226 fn div(self, rhs: &Self) -> Self {
1227 self.div(*rhs)
1228 }
1229}
1230
1231impl Div<&Vec2> for &Vec2 {
1232 type Output = Vec2;
1233 #[inline]
1234 fn div(self, rhs: &Vec2) -> Vec2 {
1235 (*self).div(*rhs)
1236 }
1237}
1238
1239impl Div<Vec2> for &Vec2 {
1240 type Output = Vec2;
1241 #[inline]
1242 fn div(self, rhs: Vec2) -> Vec2 {
1243 (*self).div(rhs)
1244 }
1245}
1246
1247impl DivAssign for Vec2 {
1248 #[inline]
1249 fn div_assign(&mut self, rhs: Self) {
1250 self.x.div_assign(rhs.x);
1251 self.y.div_assign(rhs.y);
1252 }
1253}
1254
1255impl DivAssign<&Self> for Vec2 {
1256 #[inline]
1257 fn div_assign(&mut self, rhs: &Self) {
1258 self.div_assign(*rhs);
1259 }
1260}
1261
1262impl Div<f32> for Vec2 {
1263 type Output = Self;
1264 #[inline]
1265 fn div(self, rhs: f32) -> Self {
1266 Self {
1267 x: self.x.div(rhs),
1268 y: self.y.div(rhs),
1269 }
1270 }
1271}
1272
1273impl Div<&f32> for Vec2 {
1274 type Output = Self;
1275 #[inline]
1276 fn div(self, rhs: &f32) -> Self {
1277 self.div(*rhs)
1278 }
1279}
1280
1281impl Div<&f32> for &Vec2 {
1282 type Output = Vec2;
1283 #[inline]
1284 fn div(self, rhs: &f32) -> Vec2 {
1285 (*self).div(*rhs)
1286 }
1287}
1288
1289impl Div<f32> for &Vec2 {
1290 type Output = Vec2;
1291 #[inline]
1292 fn div(self, rhs: f32) -> Vec2 {
1293 (*self).div(rhs)
1294 }
1295}
1296
1297impl DivAssign<f32> for Vec2 {
1298 #[inline]
1299 fn div_assign(&mut self, rhs: f32) {
1300 self.x.div_assign(rhs);
1301 self.y.div_assign(rhs);
1302 }
1303}
1304
1305impl DivAssign<&f32> for Vec2 {
1306 #[inline]
1307 fn div_assign(&mut self, rhs: &f32) {
1308 self.div_assign(*rhs);
1309 }
1310}
1311
1312impl Div<Vec2> for f32 {
1313 type Output = Vec2;
1314 #[inline]
1315 fn div(self, rhs: Vec2) -> Vec2 {
1316 Vec2 {
1317 x: self.div(rhs.x),
1318 y: self.div(rhs.y),
1319 }
1320 }
1321}
1322
1323impl Div<&Vec2> for f32 {
1324 type Output = Vec2;
1325 #[inline]
1326 fn div(self, rhs: &Vec2) -> Vec2 {
1327 self.div(*rhs)
1328 }
1329}
1330
1331impl Div<&Vec2> for &f32 {
1332 type Output = Vec2;
1333 #[inline]
1334 fn div(self, rhs: &Vec2) -> Vec2 {
1335 (*self).div(*rhs)
1336 }
1337}
1338
1339impl Div<Vec2> for &f32 {
1340 type Output = Vec2;
1341 #[inline]
1342 fn div(self, rhs: Vec2) -> Vec2 {
1343 (*self).div(rhs)
1344 }
1345}
1346
1347impl Mul for Vec2 {
1348 type Output = Self;
1349 #[inline]
1350 fn mul(self, rhs: Self) -> Self {
1351 Self {
1352 x: self.x.mul(rhs.x),
1353 y: self.y.mul(rhs.y),
1354 }
1355 }
1356}
1357
1358impl Mul<&Self> for Vec2 {
1359 type Output = Self;
1360 #[inline]
1361 fn mul(self, rhs: &Self) -> Self {
1362 self.mul(*rhs)
1363 }
1364}
1365
1366impl Mul<&Vec2> for &Vec2 {
1367 type Output = Vec2;
1368 #[inline]
1369 fn mul(self, rhs: &Vec2) -> Vec2 {
1370 (*self).mul(*rhs)
1371 }
1372}
1373
1374impl Mul<Vec2> for &Vec2 {
1375 type Output = Vec2;
1376 #[inline]
1377 fn mul(self, rhs: Vec2) -> Vec2 {
1378 (*self).mul(rhs)
1379 }
1380}
1381
1382impl MulAssign for Vec2 {
1383 #[inline]
1384 fn mul_assign(&mut self, rhs: Self) {
1385 self.x.mul_assign(rhs.x);
1386 self.y.mul_assign(rhs.y);
1387 }
1388}
1389
1390impl MulAssign<&Self> for Vec2 {
1391 #[inline]
1392 fn mul_assign(&mut self, rhs: &Self) {
1393 self.mul_assign(*rhs);
1394 }
1395}
1396
1397impl Mul<f32> for Vec2 {
1398 type Output = Self;
1399 #[inline]
1400 fn mul(self, rhs: f32) -> Self {
1401 Self {
1402 x: self.x.mul(rhs),
1403 y: self.y.mul(rhs),
1404 }
1405 }
1406}
1407
1408impl Mul<&f32> for Vec2 {
1409 type Output = Self;
1410 #[inline]
1411 fn mul(self, rhs: &f32) -> Self {
1412 self.mul(*rhs)
1413 }
1414}
1415
1416impl Mul<&f32> for &Vec2 {
1417 type Output = Vec2;
1418 #[inline]
1419 fn mul(self, rhs: &f32) -> Vec2 {
1420 (*self).mul(*rhs)
1421 }
1422}
1423
1424impl Mul<f32> for &Vec2 {
1425 type Output = Vec2;
1426 #[inline]
1427 fn mul(self, rhs: f32) -> Vec2 {
1428 (*self).mul(rhs)
1429 }
1430}
1431
1432impl MulAssign<f32> for Vec2 {
1433 #[inline]
1434 fn mul_assign(&mut self, rhs: f32) {
1435 self.x.mul_assign(rhs);
1436 self.y.mul_assign(rhs);
1437 }
1438}
1439
1440impl MulAssign<&f32> for Vec2 {
1441 #[inline]
1442 fn mul_assign(&mut self, rhs: &f32) {
1443 self.mul_assign(*rhs);
1444 }
1445}
1446
1447impl Mul<Vec2> for f32 {
1448 type Output = Vec2;
1449 #[inline]
1450 fn mul(self, rhs: Vec2) -> Vec2 {
1451 Vec2 {
1452 x: self.mul(rhs.x),
1453 y: self.mul(rhs.y),
1454 }
1455 }
1456}
1457
1458impl Mul<&Vec2> for f32 {
1459 type Output = Vec2;
1460 #[inline]
1461 fn mul(self, rhs: &Vec2) -> Vec2 {
1462 self.mul(*rhs)
1463 }
1464}
1465
1466impl Mul<&Vec2> for &f32 {
1467 type Output = Vec2;
1468 #[inline]
1469 fn mul(self, rhs: &Vec2) -> Vec2 {
1470 (*self).mul(*rhs)
1471 }
1472}
1473
1474impl Mul<Vec2> for &f32 {
1475 type Output = Vec2;
1476 #[inline]
1477 fn mul(self, rhs: Vec2) -> Vec2 {
1478 (*self).mul(rhs)
1479 }
1480}
1481
1482impl Add for Vec2 {
1483 type Output = Self;
1484 #[inline]
1485 fn add(self, rhs: Self) -> Self {
1486 Self {
1487 x: self.x.add(rhs.x),
1488 y: self.y.add(rhs.y),
1489 }
1490 }
1491}
1492
1493impl Add<&Self> for Vec2 {
1494 type Output = Self;
1495 #[inline]
1496 fn add(self, rhs: &Self) -> Self {
1497 self.add(*rhs)
1498 }
1499}
1500
1501impl Add<&Vec2> for &Vec2 {
1502 type Output = Vec2;
1503 #[inline]
1504 fn add(self, rhs: &Vec2) -> Vec2 {
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 AddAssign for Vec2 {
1518 #[inline]
1519 fn add_assign(&mut self, rhs: Self) {
1520 self.x.add_assign(rhs.x);
1521 self.y.add_assign(rhs.y);
1522 }
1523}
1524
1525impl AddAssign<&Self> for Vec2 {
1526 #[inline]
1527 fn add_assign(&mut self, rhs: &Self) {
1528 self.add_assign(*rhs);
1529 }
1530}
1531
1532impl Add<f32> for Vec2 {
1533 type Output = Self;
1534 #[inline]
1535 fn add(self, rhs: f32) -> Self {
1536 Self {
1537 x: self.x.add(rhs),
1538 y: self.y.add(rhs),
1539 }
1540 }
1541}
1542
1543impl Add<&f32> for Vec2 {
1544 type Output = Self;
1545 #[inline]
1546 fn add(self, rhs: &f32) -> Self {
1547 self.add(*rhs)
1548 }
1549}
1550
1551impl Add<&f32> for &Vec2 {
1552 type Output = Vec2;
1553 #[inline]
1554 fn add(self, rhs: &f32) -> Vec2 {
1555 (*self).add(*rhs)
1556 }
1557}
1558
1559impl Add<f32> for &Vec2 {
1560 type Output = Vec2;
1561 #[inline]
1562 fn add(self, rhs: f32) -> Vec2 {
1563 (*self).add(rhs)
1564 }
1565}
1566
1567impl AddAssign<f32> for Vec2 {
1568 #[inline]
1569 fn add_assign(&mut self, rhs: f32) {
1570 self.x.add_assign(rhs);
1571 self.y.add_assign(rhs);
1572 }
1573}
1574
1575impl AddAssign<&f32> for Vec2 {
1576 #[inline]
1577 fn add_assign(&mut self, rhs: &f32) {
1578 self.add_assign(*rhs);
1579 }
1580}
1581
1582impl Add<Vec2> for f32 {
1583 type Output = Vec2;
1584 #[inline]
1585 fn add(self, rhs: Vec2) -> Vec2 {
1586 Vec2 {
1587 x: self.add(rhs.x),
1588 y: self.add(rhs.y),
1589 }
1590 }
1591}
1592
1593impl Add<&Vec2> for f32 {
1594 type Output = Vec2;
1595 #[inline]
1596 fn add(self, rhs: &Vec2) -> Vec2 {
1597 self.add(*rhs)
1598 }
1599}
1600
1601impl Add<&Vec2> for &f32 {
1602 type Output = Vec2;
1603 #[inline]
1604 fn add(self, rhs: &Vec2) -> Vec2 {
1605 (*self).add(*rhs)
1606 }
1607}
1608
1609impl Add<Vec2> for &f32 {
1610 type Output = Vec2;
1611 #[inline]
1612 fn add(self, rhs: Vec2) -> Vec2 {
1613 (*self).add(rhs)
1614 }
1615}
1616
1617impl Sub for Vec2 {
1618 type Output = Self;
1619 #[inline]
1620 fn sub(self, rhs: Self) -> Self {
1621 Self {
1622 x: self.x.sub(rhs.x),
1623 y: self.y.sub(rhs.y),
1624 }
1625 }
1626}
1627
1628impl Sub<&Self> for Vec2 {
1629 type Output = Self;
1630 #[inline]
1631 fn sub(self, rhs: &Self) -> Self {
1632 self.sub(*rhs)
1633 }
1634}
1635
1636impl Sub<&Vec2> for &Vec2 {
1637 type Output = Vec2;
1638 #[inline]
1639 fn sub(self, rhs: &Vec2) -> Vec2 {
1640 (*self).sub(*rhs)
1641 }
1642}
1643
1644impl Sub<Vec2> for &Vec2 {
1645 type Output = Vec2;
1646 #[inline]
1647 fn sub(self, rhs: Vec2) -> Vec2 {
1648 (*self).sub(rhs)
1649 }
1650}
1651
1652impl SubAssign for Vec2 {
1653 #[inline]
1654 fn sub_assign(&mut self, rhs: Self) {
1655 self.x.sub_assign(rhs.x);
1656 self.y.sub_assign(rhs.y);
1657 }
1658}
1659
1660impl SubAssign<&Self> for Vec2 {
1661 #[inline]
1662 fn sub_assign(&mut self, rhs: &Self) {
1663 self.sub_assign(*rhs);
1664 }
1665}
1666
1667impl Sub<f32> for Vec2 {
1668 type Output = Self;
1669 #[inline]
1670 fn sub(self, rhs: f32) -> Self {
1671 Self {
1672 x: self.x.sub(rhs),
1673 y: self.y.sub(rhs),
1674 }
1675 }
1676}
1677
1678impl Sub<&f32> for Vec2 {
1679 type Output = Self;
1680 #[inline]
1681 fn sub(self, rhs: &f32) -> Self {
1682 self.sub(*rhs)
1683 }
1684}
1685
1686impl Sub<&f32> for &Vec2 {
1687 type Output = Vec2;
1688 #[inline]
1689 fn sub(self, rhs: &f32) -> Vec2 {
1690 (*self).sub(*rhs)
1691 }
1692}
1693
1694impl Sub<f32> for &Vec2 {
1695 type Output = Vec2;
1696 #[inline]
1697 fn sub(self, rhs: f32) -> Vec2 {
1698 (*self).sub(rhs)
1699 }
1700}
1701
1702impl SubAssign<f32> for Vec2 {
1703 #[inline]
1704 fn sub_assign(&mut self, rhs: f32) {
1705 self.x.sub_assign(rhs);
1706 self.y.sub_assign(rhs);
1707 }
1708}
1709
1710impl SubAssign<&f32> for Vec2 {
1711 #[inline]
1712 fn sub_assign(&mut self, rhs: &f32) {
1713 self.sub_assign(*rhs);
1714 }
1715}
1716
1717impl Sub<Vec2> for f32 {
1718 type Output = Vec2;
1719 #[inline]
1720 fn sub(self, rhs: Vec2) -> Vec2 {
1721 Vec2 {
1722 x: self.sub(rhs.x),
1723 y: self.sub(rhs.y),
1724 }
1725 }
1726}
1727
1728impl Sub<&Vec2> for f32 {
1729 type Output = Vec2;
1730 #[inline]
1731 fn sub(self, rhs: &Vec2) -> Vec2 {
1732 self.sub(*rhs)
1733 }
1734}
1735
1736impl Sub<&Vec2> for &f32 {
1737 type Output = Vec2;
1738 #[inline]
1739 fn sub(self, rhs: &Vec2) -> Vec2 {
1740 (*self).sub(*rhs)
1741 }
1742}
1743
1744impl Sub<Vec2> for &f32 {
1745 type Output = Vec2;
1746 #[inline]
1747 fn sub(self, rhs: Vec2) -> Vec2 {
1748 (*self).sub(rhs)
1749 }
1750}
1751
1752impl Rem for Vec2 {
1753 type Output = Self;
1754 #[inline]
1755 fn rem(self, rhs: Self) -> Self {
1756 Self {
1757 x: self.x.rem(rhs.x),
1758 y: self.y.rem(rhs.y),
1759 }
1760 }
1761}
1762
1763impl Rem<&Self> for Vec2 {
1764 type Output = Self;
1765 #[inline]
1766 fn rem(self, rhs: &Self) -> Self {
1767 self.rem(*rhs)
1768 }
1769}
1770
1771impl Rem<&Vec2> for &Vec2 {
1772 type Output = Vec2;
1773 #[inline]
1774 fn rem(self, rhs: &Vec2) -> Vec2 {
1775 (*self).rem(*rhs)
1776 }
1777}
1778
1779impl Rem<Vec2> for &Vec2 {
1780 type Output = Vec2;
1781 #[inline]
1782 fn rem(self, rhs: Vec2) -> Vec2 {
1783 (*self).rem(rhs)
1784 }
1785}
1786
1787impl RemAssign for Vec2 {
1788 #[inline]
1789 fn rem_assign(&mut self, rhs: Self) {
1790 self.x.rem_assign(rhs.x);
1791 self.y.rem_assign(rhs.y);
1792 }
1793}
1794
1795impl RemAssign<&Self> for Vec2 {
1796 #[inline]
1797 fn rem_assign(&mut self, rhs: &Self) {
1798 self.rem_assign(*rhs);
1799 }
1800}
1801
1802impl Rem<f32> for Vec2 {
1803 type Output = Self;
1804 #[inline]
1805 fn rem(self, rhs: f32) -> Self {
1806 Self {
1807 x: self.x.rem(rhs),
1808 y: self.y.rem(rhs),
1809 }
1810 }
1811}
1812
1813impl Rem<&f32> for Vec2 {
1814 type Output = Self;
1815 #[inline]
1816 fn rem(self, rhs: &f32) -> Self {
1817 self.rem(*rhs)
1818 }
1819}
1820
1821impl Rem<&f32> for &Vec2 {
1822 type Output = Vec2;
1823 #[inline]
1824 fn rem(self, rhs: &f32) -> Vec2 {
1825 (*self).rem(*rhs)
1826 }
1827}
1828
1829impl Rem<f32> for &Vec2 {
1830 type Output = Vec2;
1831 #[inline]
1832 fn rem(self, rhs: f32) -> Vec2 {
1833 (*self).rem(rhs)
1834 }
1835}
1836
1837impl RemAssign<f32> for Vec2 {
1838 #[inline]
1839 fn rem_assign(&mut self, rhs: f32) {
1840 self.x.rem_assign(rhs);
1841 self.y.rem_assign(rhs);
1842 }
1843}
1844
1845impl RemAssign<&f32> for Vec2 {
1846 #[inline]
1847 fn rem_assign(&mut self, rhs: &f32) {
1848 self.rem_assign(*rhs);
1849 }
1850}
1851
1852impl Rem<Vec2> for f32 {
1853 type Output = Vec2;
1854 #[inline]
1855 fn rem(self, rhs: Vec2) -> Vec2 {
1856 Vec2 {
1857 x: self.rem(rhs.x),
1858 y: self.rem(rhs.y),
1859 }
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 Rem<&Vec2> for &f32 {
1872 type Output = Vec2;
1873 #[inline]
1874 fn rem(self, rhs: &Vec2) -> Vec2 {
1875 (*self).rem(*rhs)
1876 }
1877}
1878
1879impl Rem<Vec2> for &f32 {
1880 type Output = Vec2;
1881 #[inline]
1882 fn rem(self, rhs: Vec2) -> Vec2 {
1883 (*self).rem(rhs)
1884 }
1885}
1886
1887impl AsRef<[f32; 2]> for Vec2 {
1888 #[inline]
1889 fn as_ref(&self) -> &[f32; 2] {
1890 unsafe { &*(self as *const Self as *const [f32; 2]) }
1891 }
1892}
1893
1894impl AsMut<[f32; 2]> for Vec2 {
1895 #[inline]
1896 fn as_mut(&mut self) -> &mut [f32; 2] {
1897 unsafe { &mut *(self as *mut Self as *mut [f32; 2]) }
1898 }
1899}
1900
1901impl Sum for Vec2 {
1902 #[inline]
1903 fn sum<I>(iter: I) -> Self
1904 where
1905 I: Iterator<Item = Self>,
1906 {
1907 iter.fold(Self::ZERO, Self::add)
1908 }
1909}
1910
1911impl<'a> Sum<&'a Self> for Vec2 {
1912 #[inline]
1913 fn sum<I>(iter: I) -> Self
1914 where
1915 I: Iterator<Item = &'a Self>,
1916 {
1917 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1918 }
1919}
1920
1921impl Product for Vec2 {
1922 #[inline]
1923 fn product<I>(iter: I) -> Self
1924 where
1925 I: Iterator<Item = Self>,
1926 {
1927 iter.fold(Self::ONE, Self::mul)
1928 }
1929}
1930
1931impl<'a> Product<&'a Self> for Vec2 {
1932 #[inline]
1933 fn product<I>(iter: I) -> Self
1934 where
1935 I: Iterator<Item = &'a Self>,
1936 {
1937 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1938 }
1939}
1940
1941impl Neg for Vec2 {
1942 type Output = Self;
1943 #[inline]
1944 fn neg(self) -> Self {
1945 Self {
1946 x: self.x.neg(),
1947 y: self.y.neg(),
1948 }
1949 }
1950}
1951
1952impl Neg for &Vec2 {
1953 type Output = Vec2;
1954 #[inline]
1955 fn neg(self) -> Vec2 {
1956 (*self).neg()
1957 }
1958}
1959
1960impl Index<usize> for Vec2 {
1961 type Output = f32;
1962 #[inline]
1963 fn index(&self, index: usize) -> &Self::Output {
1964 match index {
1965 0 => &self.x,
1966 1 => &self.y,
1967 _ => panic!("index out of bounds"),
1968 }
1969 }
1970}
1971
1972impl IndexMut<usize> for Vec2 {
1973 #[inline]
1974 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1975 match index {
1976 0 => &mut self.x,
1977 1 => &mut self.y,
1978 _ => panic!("index out of bounds"),
1979 }
1980 }
1981}
1982
1983impl fmt::Display for Vec2 {
1984 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1985 if let Some(p) = f.precision() {
1986 write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y)
1987 } else {
1988 write!(f, "[{}, {}]", self.x, self.y)
1989 }
1990 }
1991}
1992
1993impl fmt::Debug for Vec2 {
1994 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1995 fmt.debug_tuple(stringify!(Vec2))
1996 .field(&self.x)
1997 .field(&self.y)
1998 .finish()
1999 }
2000}
2001
2002impl From<[f32; 2]> for Vec2 {
2003 #[inline]
2004 fn from(a: [f32; 2]) -> Self {
2005 Self::new(a[0], a[1])
2006 }
2007}
2008
2009impl From<Vec2> for [f32; 2] {
2010 #[inline]
2011 fn from(v: Vec2) -> Self {
2012 [v.x, v.y]
2013 }
2014}
2015
2016impl From<(f32, f32)> for Vec2 {
2017 #[inline]
2018 fn from(t: (f32, f32)) -> Self {
2019 Self::new(t.0, t.1)
2020 }
2021}
2022
2023impl From<Vec2> for (f32, f32) {
2024 #[inline]
2025 fn from(v: Vec2) -> Self {
2026 (v.x, v.y)
2027 }
2028}
2029
2030impl From<BVec2> for Vec2 {
2031 #[inline]
2032 fn from(v: BVec2) -> Self {
2033 Self::new(f32::from(v.x), f32::from(v.y))
2034 }
2035}