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