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 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::new(
208 if self.x < rhs.x { self.x } else { rhs.x },
209 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::new(
223 if self.x > rhs.x { self.x } else { rhs.x },
224 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::new(math::abs(self.x), math::abs(self.y))
384 }
385
386 #[inline]
392 #[must_use]
393 pub fn signum(self) -> Self {
394 Self::new(math::signum(self.x), math::signum(self.y))
395 }
396
397 #[inline]
399 #[must_use]
400 pub fn copysign(self, rhs: Self) -> Self {
401 Self::new(math::copysign(self.x, rhs.x), math::copysign(self.y, rhs.y))
402 }
403
404 #[inline]
412 #[must_use]
413 pub fn is_negative_bitmask(self) -> u32 {
414 (self.x.is_sign_negative() as u32) | ((self.y.is_sign_negative() as u32) << 1)
415 }
416
417 #[inline]
422 #[must_use]
423 pub fn is_negative_mask(self) -> BVec2 {
424 BVec2::new(self.x.is_sign_negative(), self.y.is_sign_negative())
425 }
426
427 #[inline]
430 #[must_use]
431 pub fn is_finite(self) -> bool {
432 self.x.is_finite() && self.y.is_finite()
433 }
434
435 #[inline]
439 #[must_use]
440 pub fn is_finite_mask(self) -> BVec2 {
441 BVec2::new(self.x.is_finite(), self.y.is_finite())
442 }
443
444 #[inline]
446 #[must_use]
447 pub fn is_nan(self) -> bool {
448 self.x.is_nan() || self.y.is_nan()
449 }
450
451 #[inline]
455 #[must_use]
456 pub fn is_nan_mask(self) -> BVec2 {
457 BVec2::new(self.x.is_nan(), self.y.is_nan())
458 }
459
460 #[doc(alias = "magnitude")]
462 #[inline]
463 #[must_use]
464 pub fn length(self) -> f32 {
465 math::sqrt(self.dot(self))
466 }
467
468 #[allow(dead_code)]
470 fn is_non_zero(self) -> bool {
471 self.length_squared() > 0.0
472 }
473
474 #[doc(alias = "magnitude2")]
478 #[inline]
479 #[must_use]
480 pub fn length_squared(self) -> f32 {
481 self.dot(self)
482 }
483
484 #[inline]
488 #[must_use]
489 pub fn length_recip(self) -> f32 {
490 1.0 / self.length()
491 }
492
493 #[inline]
495 #[must_use]
496 pub fn distance(self, rhs: Self) -> f32 {
497 (self - rhs).length()
498 }
499
500 #[inline]
502 #[must_use]
503 pub fn distance_squared(self, rhs: Self) -> f32 {
504 (self - rhs).length_squared()
505 }
506
507 #[inline]
509 #[must_use]
510 pub fn div_euclid(self, rhs: Self) -> Self {
511 Self::new(
512 math::div_euclid(self.x, rhs.x),
513 math::div_euclid(self.y, rhs.y),
514 )
515 }
516
517 #[inline]
521 #[must_use]
522 pub fn rem_euclid(self, rhs: Self) -> Self {
523 Self::new(
524 math::rem_euclid(self.x, rhs.x),
525 math::rem_euclid(self.y, rhs.y),
526 )
527 }
528
529 #[inline]
539 #[must_use]
540 pub fn normalize(self) -> Self {
541 #[allow(clippy::let_and_return)]
542 let normalized = self.mul(self.length_recip());
543 glam_assert!(normalized.is_finite());
544 normalized
545 }
546
547 #[inline]
554 #[must_use]
555 pub fn try_normalize(self) -> Option<Self> {
556 let rcp = self.length_recip();
557 if rcp.is_finite() && rcp > 0.0 {
558 Some(self * rcp)
559 } else {
560 None
561 }
562 }
563
564 #[inline]
572 #[must_use]
573 pub fn normalize_or(self, fallback: Self) -> Self {
574 let rcp = self.length_recip();
575 if rcp.is_finite() && rcp > 0.0 {
576 self * rcp
577 } else {
578 fallback
579 }
580 }
581
582 #[inline]
589 #[must_use]
590 pub fn normalize_or_zero(self) -> Self {
591 self.normalize_or(Self::ZERO)
592 }
593
594 #[inline]
598 #[must_use]
599 pub fn normalize_and_length(self) -> (Self, f32) {
600 let length = self.length();
601 let rcp = 1.0 / length;
602 if rcp.is_finite() && rcp > 0.0 {
603 (self * rcp, length)
604 } else {
605 (Self::X, 0.0)
606 }
607 }
608
609 #[inline]
613 #[must_use]
614 pub fn is_normalized(self) -> bool {
615 math::abs(self.length_squared() - 1.0) <= 2e-4
616 }
617
618 #[inline]
626 #[must_use]
627 pub fn project_onto(self, rhs: Self) -> Self {
628 let other_len_sq_rcp = 1.0 / rhs.dot(rhs);
629 glam_assert!(other_len_sq_rcp.is_finite());
630 rhs * self.dot(rhs) * other_len_sq_rcp
631 }
632
633 #[doc(alias("plane"))]
644 #[inline]
645 #[must_use]
646 pub fn reject_from(self, rhs: Self) -> Self {
647 self - self.project_onto(rhs)
648 }
649
650 #[inline]
658 #[must_use]
659 pub fn project_onto_normalized(self, rhs: Self) -> Self {
660 glam_assert!(rhs.is_normalized());
661 rhs * self.dot(rhs)
662 }
663
664 #[doc(alias("plane"))]
675 #[inline]
676 #[must_use]
677 pub fn reject_from_normalized(self, rhs: Self) -> Self {
678 self - self.project_onto_normalized(rhs)
679 }
680
681 #[inline]
684 #[must_use]
685 pub fn round(self) -> Self {
686 Self::new(math::round(self.x), math::round(self.y))
687 }
688
689 #[inline]
692 #[must_use]
693 pub fn floor(self) -> Self {
694 Self::new(math::floor(self.x), math::floor(self.y))
695 }
696
697 #[inline]
700 #[must_use]
701 pub fn ceil(self) -> Self {
702 Self::new(math::ceil(self.x), math::ceil(self.y))
703 }
704
705 #[inline]
708 #[must_use]
709 pub fn trunc(self) -> Self {
710 Self::new(math::trunc(self.x), math::trunc(self.y))
711 }
712
713 #[inline]
717 #[must_use]
718 pub fn step(self, rhs: Self) -> Self {
719 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
720 }
721
722 #[inline]
733 #[must_use]
734 pub fn smoothstep(self, edge0: Self, edge1: Self) -> Self {
735 glam_assert!(edge0.cmplt(edge1).all());
736 let t = ((self - edge0) / (edge1 - edge0)).saturate();
737 t * t * (Self::splat(3.0) - Self::splat(2.0) * t)
738 }
739
740 #[inline]
742 #[must_use]
743 pub fn saturate(self) -> Self {
744 self.clamp(Self::ZERO, Self::ONE)
745 }
746
747 #[inline]
754 #[must_use]
755 pub fn fract(self) -> Self {
756 self - self.trunc()
757 }
758
759 #[inline]
766 #[must_use]
767 pub fn fract_gl(self) -> Self {
768 self - self.floor()
769 }
770
771 #[inline]
774 #[must_use]
775 pub fn exp(self) -> Self {
776 Self::new(math::exp(self.x), math::exp(self.y))
777 }
778
779 #[inline]
781 #[must_use]
782 pub fn exp2(self) -> Self {
783 Self::new(math::exp2(self.x), math::exp2(self.y))
784 }
785
786 #[inline]
789 #[must_use]
790 pub fn ln(self) -> Self {
791 Self::new(math::ln(self.x), math::ln(self.y))
792 }
793
794 #[inline]
797 #[must_use]
798 pub fn log2(self) -> Self {
799 Self::new(math::log2(self.x), math::log2(self.y))
800 }
801
802 #[inline]
804 #[must_use]
805 pub fn powf(self, n: f32) -> Self {
806 Self::new(math::powf(self.x, n), math::powf(self.y, n))
807 }
808
809 #[inline]
812 #[must_use]
813 pub fn sqrt(self) -> Self {
814 Self::new(math::sqrt(self.x), math::sqrt(self.y))
815 }
816
817 #[inline]
819 #[must_use]
820 pub fn cos(self) -> Self {
821 Self::new(math::cos(self.x), math::cos(self.y))
822 }
823
824 #[inline]
826 #[must_use]
827 pub fn sin(self) -> Self {
828 Self::new(math::sin(self.x), math::sin(self.y))
829 }
830
831 #[inline]
833 #[must_use]
834 pub fn sin_cos(self) -> (Self, Self) {
835 let (sin_x, cos_x) = math::sin_cos(self.x);
836 let (sin_y, cos_y) = math::sin_cos(self.y);
837
838 (Self::new(sin_x, sin_y), Self::new(cos_x, cos_y))
839 }
840
841 #[inline]
843 #[must_use]
844 pub fn recip(self) -> Self {
845 Self::new(1.0 / self.x, 1.0 / self.y)
846 }
847
848 #[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 #[doc(alias = "mix")]
883 #[inline]
884 #[must_use]
885 pub fn lerp_monotonic(self, rhs: Self, s: f32) -> Self {
886 self + (rhs - self) * s
887 }
888
889 #[inline]
894 #[must_use]
895 pub fn move_towards(self, rhs: Self, d: f32) -> Self {
896 let a = rhs - self;
897 let len = a.length();
898 if len <= d || len <= 1e-4 {
899 return rhs;
900 }
901 self + a / len * d
902 }
903
904 #[inline]
910 pub fn midpoint(self, rhs: Self) -> Self {
911 (self + rhs) * 0.5
912 }
913
914 #[inline]
924 #[must_use]
925 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
926 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
927 }
928
929 #[inline]
935 #[must_use]
936 pub fn clamp_length(self, min: f32, max: f32) -> Self {
937 glam_assert!(0.0 <= min);
938 glam_assert!(min <= max);
939 let length_sq = self.length_squared();
940 if length_sq < min * min {
941 min * (self / math::sqrt(length_sq))
942 } else if length_sq > max * max {
943 max * (self / math::sqrt(length_sq))
944 } else {
945 self
946 }
947 }
948
949 #[inline]
955 #[must_use]
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 pub fn clamp_length_min(self, min: f32) -> Self {
974 glam_assert!(0.0 <= min);
975 let length_sq = self.length_squared();
976 if length_sq < min * min {
977 min * (self / math::sqrt(length_sq))
978 } else {
979 self
980 }
981 }
982
983 #[inline]
991 #[must_use]
992 pub fn mul_add(self, a: Self, b: Self) -> Self {
993 Self::new(
994 math::mul_add(self.x, a.x, b.x),
995 math::mul_add(self.y, a.y, b.y),
996 )
997 }
998
999 #[inline]
1008 #[must_use]
1009 pub fn reflect(self, normal: Self) -> Self {
1010 glam_assert!(normal.is_normalized());
1011 self - 2.0 * self.dot(normal) * normal
1012 }
1013
1014 #[inline]
1024 #[must_use]
1025 pub fn refract(self, normal: Self, eta: f32) -> Self {
1026 glam_assert!(self.is_normalized());
1027 glam_assert!(normal.is_normalized());
1028 let n_dot_i = normal.dot(self);
1029 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1030 if k >= 0.0 {
1031 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1032 } else {
1033 Self::ZERO
1034 }
1035 }
1036
1037 #[inline]
1042 #[must_use]
1043 pub fn from_angle(angle: f32) -> Self {
1044 let (sin, cos) = math::sin_cos(angle);
1045 Self::new(cos, sin)
1046 }
1047
1048 #[inline]
1052 #[must_use]
1053 pub fn to_angle(self) -> f32 {
1054 math::atan2(self.y, self.x)
1055 }
1056
1057 #[inline]
1068 #[must_use]
1069 pub fn angle_to(self, rhs: Self) -> f32 {
1070 glam_assert!(self.is_non_zero());
1071 glam_assert!(rhs.is_non_zero());
1072 let angle = math::acos_approx(
1073 self.dot(rhs) / math::sqrt(self.length_squared() * rhs.length_squared()),
1074 );
1075
1076 angle * math::signum(self.perp_dot(rhs))
1077 }
1078
1079 #[inline]
1081 #[must_use]
1082 pub fn perp(self) -> Self {
1083 Self::new(-self.y, self.x)
1084 }
1085
1086 #[doc(alias = "wedge")]
1089 #[doc(alias = "cross")]
1090 #[doc(alias = "determinant")]
1091 #[inline]
1092 #[must_use]
1093 pub fn perp_dot(self, rhs: Self) -> f32 {
1094 (self.x * rhs.y) - (self.y * rhs.x)
1095 }
1096
1097 #[inline]
1105 #[must_use]
1106 pub fn rotate(self, rhs: Self) -> Self {
1107 Self::new(
1108 self.x * rhs.x - self.y * rhs.y,
1109 self.y * rhs.x + self.x * rhs.y,
1110 )
1111 }
1112
1113 #[inline]
1116 #[must_use]
1117 pub fn rotate_angle(self, angle: f32) -> Self {
1118 self.rotate(Self::from_angle(angle))
1119 }
1120
1121 #[inline]
1127 #[must_use]
1128 pub fn rotate_towards(self, rhs: Self, max_angle: f32) -> Self {
1129 let a = self.angle_to(rhs);
1130 let abs_a = math::abs(a);
1131 let angle = max_angle.clamp(abs_a - core::f32::consts::PI, abs_a) * math::signum(a);
1133 Self::from_angle(angle).rotate(self)
1134 }
1135
1136 #[cfg(feature = "f64")]
1138 #[inline]
1139 #[must_use]
1140 pub fn as_dvec2(self) -> crate::DVec2 {
1141 crate::DVec2::new(self.x as f64, self.y as f64)
1142 }
1143
1144 #[cfg(feature = "i8")]
1146 #[inline]
1147 #[must_use]
1148 pub fn as_i8vec2(self) -> crate::I8Vec2 {
1149 crate::I8Vec2::new(self.x as i8, self.y as i8)
1150 }
1151
1152 #[cfg(feature = "u8")]
1154 #[inline]
1155 #[must_use]
1156 pub fn as_u8vec2(self) -> crate::U8Vec2 {
1157 crate::U8Vec2::new(self.x as u8, self.y as u8)
1158 }
1159
1160 #[cfg(feature = "i16")]
1162 #[inline]
1163 #[must_use]
1164 pub fn as_i16vec2(self) -> crate::I16Vec2 {
1165 crate::I16Vec2::new(self.x as i16, self.y as i16)
1166 }
1167
1168 #[cfg(feature = "u16")]
1170 #[inline]
1171 #[must_use]
1172 pub fn as_u16vec2(self) -> crate::U16Vec2 {
1173 crate::U16Vec2::new(self.x as u16, self.y as u16)
1174 }
1175
1176 #[cfg(feature = "i32")]
1178 #[inline]
1179 #[must_use]
1180 pub fn as_ivec2(self) -> crate::IVec2 {
1181 crate::IVec2::new(self.x as i32, self.y as i32)
1182 }
1183
1184 #[cfg(feature = "u32")]
1186 #[inline]
1187 #[must_use]
1188 pub fn as_uvec2(self) -> crate::UVec2 {
1189 crate::UVec2::new(self.x as u32, self.y as u32)
1190 }
1191
1192 #[cfg(feature = "i64")]
1194 #[inline]
1195 #[must_use]
1196 pub fn as_i64vec2(self) -> crate::I64Vec2 {
1197 crate::I64Vec2::new(self.x as i64, self.y as i64)
1198 }
1199
1200 #[cfg(feature = "u64")]
1202 #[inline]
1203 #[must_use]
1204 pub fn as_u64vec2(self) -> crate::U64Vec2 {
1205 crate::U64Vec2::new(self.x as u64, self.y as u64)
1206 }
1207
1208 #[cfg(feature = "isize")]
1210 #[inline]
1211 #[must_use]
1212 pub fn as_isizevec2(self) -> crate::ISizeVec2 {
1213 crate::ISizeVec2::new(self.x as isize, self.y as isize)
1214 }
1215
1216 #[cfg(feature = "usize")]
1218 #[inline]
1219 #[must_use]
1220 pub fn as_usizevec2(self) -> crate::USizeVec2 {
1221 crate::USizeVec2::new(self.x as usize, self.y as usize)
1222 }
1223}
1224
1225impl Default for Vec2 {
1226 #[inline(always)]
1227 fn default() -> Self {
1228 Self::ZERO
1229 }
1230}
1231
1232impl Div for Vec2 {
1233 type Output = Self;
1234 #[inline]
1235 fn div(self, rhs: Self) -> Self {
1236 Self::new(self.x.div(rhs.x), self.y.div(rhs.y))
1237 }
1238}
1239
1240impl Div<&Self> for Vec2 {
1241 type Output = Self;
1242 #[inline]
1243 fn div(self, rhs: &Self) -> Self {
1244 self.div(*rhs)
1245 }
1246}
1247
1248impl Div<&Vec2> for &Vec2 {
1249 type Output = Vec2;
1250 #[inline]
1251 fn div(self, rhs: &Vec2) -> Vec2 {
1252 (*self).div(*rhs)
1253 }
1254}
1255
1256impl Div<Vec2> for &Vec2 {
1257 type Output = Vec2;
1258 #[inline]
1259 fn div(self, rhs: Vec2) -> Vec2 {
1260 (*self).div(rhs)
1261 }
1262}
1263
1264impl DivAssign for Vec2 {
1265 #[inline]
1266 fn div_assign(&mut self, rhs: Self) {
1267 self.x.div_assign(rhs.x);
1268 self.y.div_assign(rhs.y);
1269 }
1270}
1271
1272impl DivAssign<&Self> for Vec2 {
1273 #[inline]
1274 fn div_assign(&mut self, rhs: &Self) {
1275 self.div_assign(*rhs);
1276 }
1277}
1278
1279impl Div<f32> for Vec2 {
1280 type Output = Self;
1281 #[inline]
1282 fn div(self, rhs: f32) -> Self {
1283 Self::new(self.x.div(rhs), self.y.div(rhs))
1284 }
1285}
1286
1287impl Div<&f32> for Vec2 {
1288 type Output = Self;
1289 #[inline]
1290 fn div(self, rhs: &f32) -> Self {
1291 self.div(*rhs)
1292 }
1293}
1294
1295impl Div<&f32> for &Vec2 {
1296 type Output = Vec2;
1297 #[inline]
1298 fn div(self, rhs: &f32) -> Vec2 {
1299 (*self).div(*rhs)
1300 }
1301}
1302
1303impl Div<f32> for &Vec2 {
1304 type Output = Vec2;
1305 #[inline]
1306 fn div(self, rhs: f32) -> Vec2 {
1307 (*self).div(rhs)
1308 }
1309}
1310
1311impl DivAssign<f32> for Vec2 {
1312 #[inline]
1313 fn div_assign(&mut self, rhs: f32) {
1314 self.x.div_assign(rhs);
1315 self.y.div_assign(rhs);
1316 }
1317}
1318
1319impl DivAssign<&f32> for Vec2 {
1320 #[inline]
1321 fn div_assign(&mut self, rhs: &f32) {
1322 self.div_assign(*rhs);
1323 }
1324}
1325
1326impl Div<Vec2> for f32 {
1327 type Output = Vec2;
1328 #[inline]
1329 fn div(self, rhs: Vec2) -> Vec2 {
1330 Vec2::new(self.div(rhs.x), self.div(rhs.y))
1331 }
1332}
1333
1334impl Div<&Vec2> for f32 {
1335 type Output = Vec2;
1336 #[inline]
1337 fn div(self, rhs: &Vec2) -> Vec2 {
1338 self.div(*rhs)
1339 }
1340}
1341
1342impl Div<&Vec2> for &f32 {
1343 type Output = Vec2;
1344 #[inline]
1345 fn div(self, rhs: &Vec2) -> Vec2 {
1346 (*self).div(*rhs)
1347 }
1348}
1349
1350impl Div<Vec2> for &f32 {
1351 type Output = Vec2;
1352 #[inline]
1353 fn div(self, rhs: Vec2) -> Vec2 {
1354 (*self).div(rhs)
1355 }
1356}
1357
1358impl Mul for Vec2 {
1359 type Output = Self;
1360 #[inline]
1361 fn mul(self, rhs: Self) -> Self {
1362 Self::new(self.x.mul(rhs.x), self.y.mul(rhs.y))
1363 }
1364}
1365
1366impl Mul<&Self> for Vec2 {
1367 type Output = Self;
1368 #[inline]
1369 fn mul(self, rhs: &Self) -> Self {
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 Mul<Vec2> for &Vec2 {
1383 type Output = Vec2;
1384 #[inline]
1385 fn mul(self, rhs: Vec2) -> Vec2 {
1386 (*self).mul(rhs)
1387 }
1388}
1389
1390impl MulAssign for Vec2 {
1391 #[inline]
1392 fn mul_assign(&mut self, rhs: Self) {
1393 self.x.mul_assign(rhs.x);
1394 self.y.mul_assign(rhs.y);
1395 }
1396}
1397
1398impl MulAssign<&Self> for Vec2 {
1399 #[inline]
1400 fn mul_assign(&mut self, rhs: &Self) {
1401 self.mul_assign(*rhs);
1402 }
1403}
1404
1405impl Mul<f32> for Vec2 {
1406 type Output = Self;
1407 #[inline]
1408 fn mul(self, rhs: f32) -> Self {
1409 Self::new(self.x.mul(rhs), self.y.mul(rhs))
1410 }
1411}
1412
1413impl Mul<&f32> for Vec2 {
1414 type Output = Self;
1415 #[inline]
1416 fn mul(self, rhs: &f32) -> Self {
1417 self.mul(*rhs)
1418 }
1419}
1420
1421impl Mul<&f32> for &Vec2 {
1422 type Output = Vec2;
1423 #[inline]
1424 fn mul(self, rhs: &f32) -> Vec2 {
1425 (*self).mul(*rhs)
1426 }
1427}
1428
1429impl Mul<f32> for &Vec2 {
1430 type Output = Vec2;
1431 #[inline]
1432 fn mul(self, rhs: f32) -> Vec2 {
1433 (*self).mul(rhs)
1434 }
1435}
1436
1437impl MulAssign<f32> for Vec2 {
1438 #[inline]
1439 fn mul_assign(&mut self, rhs: f32) {
1440 self.x.mul_assign(rhs);
1441 self.y.mul_assign(rhs);
1442 }
1443}
1444
1445impl MulAssign<&f32> for Vec2 {
1446 #[inline]
1447 fn mul_assign(&mut self, rhs: &f32) {
1448 self.mul_assign(*rhs);
1449 }
1450}
1451
1452impl Mul<Vec2> for f32 {
1453 type Output = Vec2;
1454 #[inline]
1455 fn mul(self, rhs: Vec2) -> Vec2 {
1456 Vec2::new(self.mul(rhs.x), self.mul(rhs.y))
1457 }
1458}
1459
1460impl Mul<&Vec2> for f32 {
1461 type Output = Vec2;
1462 #[inline]
1463 fn mul(self, rhs: &Vec2) -> Vec2 {
1464 self.mul(*rhs)
1465 }
1466}
1467
1468impl Mul<&Vec2> for &f32 {
1469 type Output = Vec2;
1470 #[inline]
1471 fn mul(self, rhs: &Vec2) -> Vec2 {
1472 (*self).mul(*rhs)
1473 }
1474}
1475
1476impl Mul<Vec2> for &f32 {
1477 type Output = Vec2;
1478 #[inline]
1479 fn mul(self, rhs: Vec2) -> Vec2 {
1480 (*self).mul(rhs)
1481 }
1482}
1483
1484impl Add for Vec2 {
1485 type Output = Self;
1486 #[inline]
1487 fn add(self, rhs: Self) -> Self {
1488 Self::new(self.x.add(rhs.x), self.y.add(rhs.y))
1489 }
1490}
1491
1492impl Add<&Self> for Vec2 {
1493 type Output = Self;
1494 #[inline]
1495 fn add(self, rhs: &Self) -> Self {
1496 self.add(*rhs)
1497 }
1498}
1499
1500impl Add<&Vec2> for &Vec2 {
1501 type Output = Vec2;
1502 #[inline]
1503 fn add(self, rhs: &Vec2) -> Vec2 {
1504 (*self).add(*rhs)
1505 }
1506}
1507
1508impl Add<Vec2> for &Vec2 {
1509 type Output = Vec2;
1510 #[inline]
1511 fn add(self, rhs: Vec2) -> Vec2 {
1512 (*self).add(rhs)
1513 }
1514}
1515
1516impl AddAssign for Vec2 {
1517 #[inline]
1518 fn add_assign(&mut self, rhs: Self) {
1519 self.x.add_assign(rhs.x);
1520 self.y.add_assign(rhs.y);
1521 }
1522}
1523
1524impl AddAssign<&Self> for Vec2 {
1525 #[inline]
1526 fn add_assign(&mut self, rhs: &Self) {
1527 self.add_assign(*rhs);
1528 }
1529}
1530
1531impl Add<f32> for Vec2 {
1532 type Output = Self;
1533 #[inline]
1534 fn add(self, rhs: f32) -> Self {
1535 Self::new(self.x.add(rhs), self.y.add(rhs))
1536 }
1537}
1538
1539impl Add<&f32> for Vec2 {
1540 type Output = Self;
1541 #[inline]
1542 fn add(self, rhs: &f32) -> Self {
1543 self.add(*rhs)
1544 }
1545}
1546
1547impl Add<&f32> for &Vec2 {
1548 type Output = Vec2;
1549 #[inline]
1550 fn add(self, rhs: &f32) -> Vec2 {
1551 (*self).add(*rhs)
1552 }
1553}
1554
1555impl Add<f32> for &Vec2 {
1556 type Output = Vec2;
1557 #[inline]
1558 fn add(self, rhs: f32) -> Vec2 {
1559 (*self).add(rhs)
1560 }
1561}
1562
1563impl AddAssign<f32> for Vec2 {
1564 #[inline]
1565 fn add_assign(&mut self, rhs: f32) {
1566 self.x.add_assign(rhs);
1567 self.y.add_assign(rhs);
1568 }
1569}
1570
1571impl AddAssign<&f32> for Vec2 {
1572 #[inline]
1573 fn add_assign(&mut self, rhs: &f32) {
1574 self.add_assign(*rhs);
1575 }
1576}
1577
1578impl Add<Vec2> for f32 {
1579 type Output = Vec2;
1580 #[inline]
1581 fn add(self, rhs: Vec2) -> Vec2 {
1582 Vec2::new(self.add(rhs.x), self.add(rhs.y))
1583 }
1584}
1585
1586impl Add<&Vec2> for f32 {
1587 type Output = Vec2;
1588 #[inline]
1589 fn add(self, rhs: &Vec2) -> Vec2 {
1590 self.add(*rhs)
1591 }
1592}
1593
1594impl Add<&Vec2> for &f32 {
1595 type Output = Vec2;
1596 #[inline]
1597 fn add(self, rhs: &Vec2) -> Vec2 {
1598 (*self).add(*rhs)
1599 }
1600}
1601
1602impl Add<Vec2> for &f32 {
1603 type Output = Vec2;
1604 #[inline]
1605 fn add(self, rhs: Vec2) -> Vec2 {
1606 (*self).add(rhs)
1607 }
1608}
1609
1610impl Sub for Vec2 {
1611 type Output = Self;
1612 #[inline]
1613 fn sub(self, rhs: Self) -> Self {
1614 Self::new(self.x.sub(rhs.x), self.y.sub(rhs.y))
1615 }
1616}
1617
1618impl Sub<&Self> for Vec2 {
1619 type Output = Self;
1620 #[inline]
1621 fn sub(self, rhs: &Self) -> Self {
1622 self.sub(*rhs)
1623 }
1624}
1625
1626impl Sub<&Vec2> for &Vec2 {
1627 type Output = Vec2;
1628 #[inline]
1629 fn sub(self, rhs: &Vec2) -> Vec2 {
1630 (*self).sub(*rhs)
1631 }
1632}
1633
1634impl Sub<Vec2> for &Vec2 {
1635 type Output = Vec2;
1636 #[inline]
1637 fn sub(self, rhs: Vec2) -> Vec2 {
1638 (*self).sub(rhs)
1639 }
1640}
1641
1642impl SubAssign for Vec2 {
1643 #[inline]
1644 fn sub_assign(&mut self, rhs: Self) {
1645 self.x.sub_assign(rhs.x);
1646 self.y.sub_assign(rhs.y);
1647 }
1648}
1649
1650impl SubAssign<&Self> for Vec2 {
1651 #[inline]
1652 fn sub_assign(&mut self, rhs: &Self) {
1653 self.sub_assign(*rhs);
1654 }
1655}
1656
1657impl Sub<f32> for Vec2 {
1658 type Output = Self;
1659 #[inline]
1660 fn sub(self, rhs: f32) -> Self {
1661 Self::new(self.x.sub(rhs), self.y.sub(rhs))
1662 }
1663}
1664
1665impl Sub<&f32> for Vec2 {
1666 type Output = Self;
1667 #[inline]
1668 fn sub(self, rhs: &f32) -> Self {
1669 self.sub(*rhs)
1670 }
1671}
1672
1673impl Sub<&f32> for &Vec2 {
1674 type Output = Vec2;
1675 #[inline]
1676 fn sub(self, rhs: &f32) -> Vec2 {
1677 (*self).sub(*rhs)
1678 }
1679}
1680
1681impl Sub<f32> for &Vec2 {
1682 type Output = Vec2;
1683 #[inline]
1684 fn sub(self, rhs: f32) -> Vec2 {
1685 (*self).sub(rhs)
1686 }
1687}
1688
1689impl SubAssign<f32> for Vec2 {
1690 #[inline]
1691 fn sub_assign(&mut self, rhs: f32) {
1692 self.x.sub_assign(rhs);
1693 self.y.sub_assign(rhs);
1694 }
1695}
1696
1697impl SubAssign<&f32> for Vec2 {
1698 #[inline]
1699 fn sub_assign(&mut self, rhs: &f32) {
1700 self.sub_assign(*rhs);
1701 }
1702}
1703
1704impl Sub<Vec2> for f32 {
1705 type Output = Vec2;
1706 #[inline]
1707 fn sub(self, rhs: Vec2) -> Vec2 {
1708 Vec2::new(self.sub(rhs.x), self.sub(rhs.y))
1709 }
1710}
1711
1712impl Sub<&Vec2> for f32 {
1713 type Output = Vec2;
1714 #[inline]
1715 fn sub(self, rhs: &Vec2) -> Vec2 {
1716 self.sub(*rhs)
1717 }
1718}
1719
1720impl Sub<&Vec2> for &f32 {
1721 type Output = Vec2;
1722 #[inline]
1723 fn sub(self, rhs: &Vec2) -> Vec2 {
1724 (*self).sub(*rhs)
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 Rem for Vec2 {
1737 type Output = Self;
1738 #[inline]
1739 fn rem(self, rhs: Self) -> Self {
1740 Self::new(self.x.rem(rhs.x), self.y.rem(rhs.y))
1741 }
1742}
1743
1744impl Rem<&Self> for Vec2 {
1745 type Output = Self;
1746 #[inline]
1747 fn rem(self, rhs: &Self) -> Self {
1748 self.rem(*rhs)
1749 }
1750}
1751
1752impl Rem<&Vec2> for &Vec2 {
1753 type Output = Vec2;
1754 #[inline]
1755 fn rem(self, rhs: &Vec2) -> Vec2 {
1756 (*self).rem(*rhs)
1757 }
1758}
1759
1760impl Rem<Vec2> for &Vec2 {
1761 type Output = Vec2;
1762 #[inline]
1763 fn rem(self, rhs: Vec2) -> Vec2 {
1764 (*self).rem(rhs)
1765 }
1766}
1767
1768impl RemAssign for Vec2 {
1769 #[inline]
1770 fn rem_assign(&mut self, rhs: Self) {
1771 self.x.rem_assign(rhs.x);
1772 self.y.rem_assign(rhs.y);
1773 }
1774}
1775
1776impl RemAssign<&Self> for Vec2 {
1777 #[inline]
1778 fn rem_assign(&mut self, rhs: &Self) {
1779 self.rem_assign(*rhs);
1780 }
1781}
1782
1783impl Rem<f32> for Vec2 {
1784 type Output = Self;
1785 #[inline]
1786 fn rem(self, rhs: f32) -> Self {
1787 Self::new(self.x.rem(rhs), self.y.rem(rhs))
1788 }
1789}
1790
1791impl Rem<&f32> for Vec2 {
1792 type Output = Self;
1793 #[inline]
1794 fn rem(self, rhs: &f32) -> Self {
1795 self.rem(*rhs)
1796 }
1797}
1798
1799impl Rem<&f32> for &Vec2 {
1800 type Output = Vec2;
1801 #[inline]
1802 fn rem(self, rhs: &f32) -> Vec2 {
1803 (*self).rem(*rhs)
1804 }
1805}
1806
1807impl Rem<f32> for &Vec2 {
1808 type Output = Vec2;
1809 #[inline]
1810 fn rem(self, rhs: f32) -> Vec2 {
1811 (*self).rem(rhs)
1812 }
1813}
1814
1815impl RemAssign<f32> for Vec2 {
1816 #[inline]
1817 fn rem_assign(&mut self, rhs: f32) {
1818 self.x.rem_assign(rhs);
1819 self.y.rem_assign(rhs);
1820 }
1821}
1822
1823impl RemAssign<&f32> for Vec2 {
1824 #[inline]
1825 fn rem_assign(&mut self, rhs: &f32) {
1826 self.rem_assign(*rhs);
1827 }
1828}
1829
1830impl Rem<Vec2> for f32 {
1831 type Output = Vec2;
1832 #[inline]
1833 fn rem(self, rhs: Vec2) -> Vec2 {
1834 Vec2::new(self.rem(rhs.x), self.rem(rhs.y))
1835 }
1836}
1837
1838impl Rem<&Vec2> for f32 {
1839 type Output = Vec2;
1840 #[inline]
1841 fn rem(self, rhs: &Vec2) -> Vec2 {
1842 self.rem(*rhs)
1843 }
1844}
1845
1846impl Rem<&Vec2> for &f32 {
1847 type Output = Vec2;
1848 #[inline]
1849 fn rem(self, rhs: &Vec2) -> Vec2 {
1850 (*self).rem(*rhs)
1851 }
1852}
1853
1854impl Rem<Vec2> for &f32 {
1855 type Output = Vec2;
1856 #[inline]
1857 fn rem(self, rhs: Vec2) -> Vec2 {
1858 (*self).rem(rhs)
1859 }
1860}
1861
1862impl AsRef<[f32; 2]> for Vec2 {
1863 #[inline]
1864 fn as_ref(&self) -> &[f32; 2] {
1865 unsafe { &*(self as *const Self as *const [f32; 2]) }
1866 }
1867}
1868
1869impl AsMut<[f32; 2]> for Vec2 {
1870 #[inline]
1871 fn as_mut(&mut self) -> &mut [f32; 2] {
1872 unsafe { &mut *(self as *mut Self as *mut [f32; 2]) }
1873 }
1874}
1875
1876impl Sum for Vec2 {
1877 #[inline]
1878 fn sum<I>(iter: I) -> Self
1879 where
1880 I: Iterator<Item = Self>,
1881 {
1882 iter.fold(Self::ZERO, Self::add)
1883 }
1884}
1885
1886impl<'a> Sum<&'a Self> for Vec2 {
1887 #[inline]
1888 fn sum<I>(iter: I) -> Self
1889 where
1890 I: Iterator<Item = &'a Self>,
1891 {
1892 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1893 }
1894}
1895
1896impl Product for Vec2 {
1897 #[inline]
1898 fn product<I>(iter: I) -> Self
1899 where
1900 I: Iterator<Item = Self>,
1901 {
1902 iter.fold(Self::ONE, Self::mul)
1903 }
1904}
1905
1906impl<'a> Product<&'a Self> for Vec2 {
1907 #[inline]
1908 fn product<I>(iter: I) -> Self
1909 where
1910 I: Iterator<Item = &'a Self>,
1911 {
1912 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1913 }
1914}
1915
1916impl Neg for Vec2 {
1917 type Output = Self;
1918 #[inline]
1919 fn neg(self) -> Self {
1920 Self::new(self.x.neg(), self.y.neg())
1921 }
1922}
1923
1924impl Neg for &Vec2 {
1925 type Output = Vec2;
1926 #[inline]
1927 fn neg(self) -> Vec2 {
1928 (*self).neg()
1929 }
1930}
1931
1932impl Index<usize> for Vec2 {
1933 type Output = f32;
1934 #[inline]
1935 fn index(&self, index: usize) -> &Self::Output {
1936 match index {
1937 0 => &self.x,
1938 1 => &self.y,
1939 _ => panic!("index out of bounds"),
1940 }
1941 }
1942}
1943
1944impl IndexMut<usize> for Vec2 {
1945 #[inline]
1946 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1947 match index {
1948 0 => &mut self.x,
1949 1 => &mut self.y,
1950 _ => panic!("index out of bounds"),
1951 }
1952 }
1953}
1954
1955impl fmt::Display for Vec2 {
1956 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1957 if let Some(p) = f.precision() {
1958 write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y)
1959 } else {
1960 write!(f, "[{}, {}]", self.x, self.y)
1961 }
1962 }
1963}
1964
1965impl fmt::Debug for Vec2 {
1966 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1967 fmt.debug_tuple(stringify!(Vec2))
1968 .field(&self.x)
1969 .field(&self.y)
1970 .finish()
1971 }
1972}
1973
1974impl From<[f32; 2]> for Vec2 {
1975 #[inline]
1976 fn from(a: [f32; 2]) -> Self {
1977 Self::new(a[0], a[1])
1978 }
1979}
1980
1981impl From<Vec2> for [f32; 2] {
1982 #[inline]
1983 fn from(v: Vec2) -> Self {
1984 [v.x, v.y]
1985 }
1986}
1987
1988impl From<(f32, f32)> for Vec2 {
1989 #[inline]
1990 fn from(t: (f32, f32)) -> Self {
1991 Self::new(t.0, t.1)
1992 }
1993}
1994
1995impl From<Vec2> for (f32, f32) {
1996 #[inline]
1997 fn from(v: Vec2) -> Self {
1998 (v.x, v.y)
1999 }
2000}
2001
2002impl From<BVec2> for Vec2 {
2003 #[inline]
2004 fn from(v: BVec2) -> Self {
2005 Self::new(f32::from(v.x), f32::from(v.y))
2006 }
2007}