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]
720 #[must_use]
721 pub fn step(self, rhs: Self) -> Self {
722 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
723 }
724
725 #[inline]
727 #[must_use]
728 pub fn saturate(self) -> Self {
729 self.clamp(Self::ZERO, Self::ONE)
730 }
731
732 #[inline]
739 #[must_use]
740 pub fn fract(self) -> Self {
741 self - self.trunc()
742 }
743
744 #[inline]
751 #[must_use]
752 pub fn fract_gl(self) -> Self {
753 self - self.floor()
754 }
755
756 #[inline]
759 #[must_use]
760 pub fn exp(self) -> Self {
761 Self::new(math::exp(self.x), math::exp(self.y))
762 }
763
764 #[inline]
766 #[must_use]
767 pub fn exp2(self) -> Self {
768 Self::new(math::exp2(self.x), math::exp2(self.y))
769 }
770
771 #[inline]
774 #[must_use]
775 pub fn ln(self) -> Self {
776 Self::new(math::ln(self.x), math::ln(self.y))
777 }
778
779 #[inline]
782 #[must_use]
783 pub fn log2(self) -> Self {
784 Self::new(math::log2(self.x), math::log2(self.y))
785 }
786
787 #[inline]
789 #[must_use]
790 pub fn powf(self, n: f64) -> Self {
791 Self::new(math::powf(self.x, n), math::powf(self.y, n))
792 }
793
794 #[inline]
797 #[must_use]
798 pub fn sqrt(self) -> Self {
799 Self::new(math::sqrt(self.x), math::sqrt(self.y))
800 }
801
802 #[inline]
804 #[must_use]
805 pub fn cos(self) -> Self {
806 Self::new(math::cos(self.x), math::cos(self.y))
807 }
808
809 #[inline]
811 #[must_use]
812 pub fn sin(self) -> Self {
813 Self::new(math::sin(self.x), math::sin(self.y))
814 }
815
816 #[inline]
818 #[must_use]
819 pub fn sin_cos(self) -> (Self, Self) {
820 let (sin_x, cos_x) = math::sin_cos(self.x);
821 let (sin_y, cos_y) = math::sin_cos(self.y);
822
823 (Self::new(sin_x, sin_y), Self::new(cos_x, cos_y))
824 }
825
826 #[inline]
828 #[must_use]
829 pub fn recip(self) -> Self {
830 Self {
831 x: 1.0 / self.x,
832 y: 1.0 / self.y,
833 }
834 }
835
836 #[doc(alias = "mix")]
842 #[inline]
843 #[must_use]
844 pub fn lerp(self, rhs: Self, s: f64) -> Self {
845 self * (1.0 - s) + rhs * s
846 }
847
848 #[inline]
853 #[must_use]
854 pub fn move_towards(self, rhs: Self, d: f64) -> Self {
855 let a = rhs - self;
856 let len = a.length();
857 if len <= d || len <= 1e-4 {
858 return rhs;
859 }
860 self + a / len * d
861 }
862
863 #[inline]
869 pub fn midpoint(self, rhs: Self) -> Self {
870 (self + rhs) * 0.5
871 }
872
873 #[inline]
883 #[must_use]
884 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
885 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
886 }
887
888 #[inline]
894 #[must_use]
895 pub fn clamp_length(self, min: f64, max: f64) -> Self {
896 glam_assert!(0.0 <= min);
897 glam_assert!(min <= max);
898 let length_sq = self.length_squared();
899 if length_sq < min * min {
900 min * (self / math::sqrt(length_sq))
901 } else if length_sq > max * max {
902 max * (self / math::sqrt(length_sq))
903 } else {
904 self
905 }
906 }
907
908 #[inline]
914 #[must_use]
915 pub fn clamp_length_max(self, max: f64) -> Self {
916 glam_assert!(0.0 <= max);
917 let length_sq = self.length_squared();
918 if length_sq > max * max {
919 max * (self / math::sqrt(length_sq))
920 } else {
921 self
922 }
923 }
924
925 #[inline]
931 #[must_use]
932 pub fn clamp_length_min(self, min: f64) -> Self {
933 glam_assert!(0.0 <= min);
934 let length_sq = self.length_squared();
935 if length_sq < min * min {
936 min * (self / math::sqrt(length_sq))
937 } else {
938 self
939 }
940 }
941
942 #[inline]
950 #[must_use]
951 pub fn mul_add(self, a: Self, b: Self) -> Self {
952 Self::new(
953 math::mul_add(self.x, a.x, b.x),
954 math::mul_add(self.y, a.y, b.y),
955 )
956 }
957
958 #[inline]
967 #[must_use]
968 pub fn reflect(self, normal: Self) -> Self {
969 glam_assert!(normal.is_normalized());
970 self - 2.0 * self.dot(normal) * normal
971 }
972
973 #[inline]
983 #[must_use]
984 pub fn refract(self, normal: Self, eta: f64) -> Self {
985 glam_assert!(self.is_normalized());
986 glam_assert!(normal.is_normalized());
987 let n_dot_i = normal.dot(self);
988 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
989 if k >= 0.0 {
990 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
991 } else {
992 Self::ZERO
993 }
994 }
995
996 #[inline]
1001 #[must_use]
1002 pub fn from_angle(angle: f64) -> Self {
1003 let (sin, cos) = math::sin_cos(angle);
1004 Self { x: cos, y: sin }
1005 }
1006
1007 #[inline]
1011 #[must_use]
1012 pub fn to_angle(self) -> f64 {
1013 math::atan2(self.y, self.x)
1014 }
1015
1016 #[inline]
1020 #[must_use]
1021 pub fn angle_to(self, rhs: Self) -> f64 {
1022 let angle = math::acos_approx(
1023 self.dot(rhs) / math::sqrt(self.length_squared() * rhs.length_squared()),
1024 );
1025
1026 angle * math::signum(self.perp_dot(rhs))
1027 }
1028
1029 #[inline]
1031 #[must_use]
1032 pub fn perp(self) -> Self {
1033 Self {
1034 x: -self.y,
1035 y: self.x,
1036 }
1037 }
1038
1039 #[doc(alias = "wedge")]
1042 #[doc(alias = "cross")]
1043 #[doc(alias = "determinant")]
1044 #[inline]
1045 #[must_use]
1046 pub fn perp_dot(self, rhs: Self) -> f64 {
1047 (self.x * rhs.y) - (self.y * rhs.x)
1048 }
1049
1050 #[inline]
1058 #[must_use]
1059 pub fn rotate(self, rhs: Self) -> Self {
1060 Self {
1061 x: self.x * rhs.x - self.y * rhs.y,
1062 y: self.y * rhs.x + self.x * rhs.y,
1063 }
1064 }
1065
1066 #[inline]
1072 #[must_use]
1073 pub fn rotate_towards(self, rhs: Self, max_angle: f64) -> Self {
1074 let a = self.angle_to(rhs);
1075 let abs_a = math::abs(a);
1076 let angle = max_angle.clamp(abs_a - core::f64::consts::PI, abs_a) * math::signum(a);
1078 Self::from_angle(angle).rotate(self)
1079 }
1080
1081 #[inline]
1083 #[must_use]
1084 pub fn as_vec2(self) -> crate::Vec2 {
1085 crate::Vec2::new(self.x as f32, self.y as f32)
1086 }
1087
1088 #[inline]
1090 #[must_use]
1091 pub fn as_i8vec2(self) -> crate::I8Vec2 {
1092 crate::I8Vec2::new(self.x as i8, self.y as i8)
1093 }
1094
1095 #[inline]
1097 #[must_use]
1098 pub fn as_u8vec2(self) -> crate::U8Vec2 {
1099 crate::U8Vec2::new(self.x as u8, self.y as u8)
1100 }
1101
1102 #[inline]
1104 #[must_use]
1105 pub fn as_i16vec2(self) -> crate::I16Vec2 {
1106 crate::I16Vec2::new(self.x as i16, self.y as i16)
1107 }
1108
1109 #[inline]
1111 #[must_use]
1112 pub fn as_u16vec2(self) -> crate::U16Vec2 {
1113 crate::U16Vec2::new(self.x as u16, self.y as u16)
1114 }
1115
1116 #[inline]
1118 #[must_use]
1119 pub fn as_ivec2(self) -> crate::IVec2 {
1120 crate::IVec2::new(self.x as i32, self.y as i32)
1121 }
1122
1123 #[inline]
1125 #[must_use]
1126 pub fn as_uvec2(self) -> crate::UVec2 {
1127 crate::UVec2::new(self.x as u32, self.y as u32)
1128 }
1129
1130 #[inline]
1132 #[must_use]
1133 pub fn as_i64vec2(self) -> crate::I64Vec2 {
1134 crate::I64Vec2::new(self.x as i64, self.y as i64)
1135 }
1136
1137 #[inline]
1139 #[must_use]
1140 pub fn as_u64vec2(self) -> crate::U64Vec2 {
1141 crate::U64Vec2::new(self.x as u64, self.y as u64)
1142 }
1143
1144 #[inline]
1146 #[must_use]
1147 pub fn as_usizevec2(self) -> crate::USizeVec2 {
1148 crate::USizeVec2::new(self.x as usize, self.y as usize)
1149 }
1150}
1151
1152impl Default for DVec2 {
1153 #[inline(always)]
1154 fn default() -> Self {
1155 Self::ZERO
1156 }
1157}
1158
1159impl Div for DVec2 {
1160 type Output = Self;
1161 #[inline]
1162 fn div(self, rhs: Self) -> Self {
1163 Self {
1164 x: self.x.div(rhs.x),
1165 y: self.y.div(rhs.y),
1166 }
1167 }
1168}
1169
1170impl Div<&Self> for DVec2 {
1171 type Output = Self;
1172 #[inline]
1173 fn div(self, rhs: &Self) -> Self {
1174 self.div(*rhs)
1175 }
1176}
1177
1178impl Div<&DVec2> for &DVec2 {
1179 type Output = DVec2;
1180 #[inline]
1181 fn div(self, rhs: &DVec2) -> DVec2 {
1182 (*self).div(*rhs)
1183 }
1184}
1185
1186impl Div<DVec2> for &DVec2 {
1187 type Output = DVec2;
1188 #[inline]
1189 fn div(self, rhs: DVec2) -> DVec2 {
1190 (*self).div(rhs)
1191 }
1192}
1193
1194impl DivAssign for DVec2 {
1195 #[inline]
1196 fn div_assign(&mut self, rhs: Self) {
1197 self.x.div_assign(rhs.x);
1198 self.y.div_assign(rhs.y);
1199 }
1200}
1201
1202impl DivAssign<&Self> for DVec2 {
1203 #[inline]
1204 fn div_assign(&mut self, rhs: &Self) {
1205 self.div_assign(*rhs);
1206 }
1207}
1208
1209impl Div<f64> for DVec2 {
1210 type Output = Self;
1211 #[inline]
1212 fn div(self, rhs: f64) -> Self {
1213 Self {
1214 x: self.x.div(rhs),
1215 y: self.y.div(rhs),
1216 }
1217 }
1218}
1219
1220impl Div<&f64> for DVec2 {
1221 type Output = Self;
1222 #[inline]
1223 fn div(self, rhs: &f64) -> Self {
1224 self.div(*rhs)
1225 }
1226}
1227
1228impl Div<&f64> for &DVec2 {
1229 type Output = DVec2;
1230 #[inline]
1231 fn div(self, rhs: &f64) -> DVec2 {
1232 (*self).div(*rhs)
1233 }
1234}
1235
1236impl Div<f64> for &DVec2 {
1237 type Output = DVec2;
1238 #[inline]
1239 fn div(self, rhs: f64) -> DVec2 {
1240 (*self).div(rhs)
1241 }
1242}
1243
1244impl DivAssign<f64> for DVec2 {
1245 #[inline]
1246 fn div_assign(&mut self, rhs: f64) {
1247 self.x.div_assign(rhs);
1248 self.y.div_assign(rhs);
1249 }
1250}
1251
1252impl DivAssign<&f64> for DVec2 {
1253 #[inline]
1254 fn div_assign(&mut self, rhs: &f64) {
1255 self.div_assign(*rhs);
1256 }
1257}
1258
1259impl Div<DVec2> for f64 {
1260 type Output = DVec2;
1261 #[inline]
1262 fn div(self, rhs: DVec2) -> DVec2 {
1263 DVec2 {
1264 x: self.div(rhs.x),
1265 y: self.div(rhs.y),
1266 }
1267 }
1268}
1269
1270impl Div<&DVec2> for f64 {
1271 type Output = DVec2;
1272 #[inline]
1273 fn div(self, rhs: &DVec2) -> DVec2 {
1274 self.div(*rhs)
1275 }
1276}
1277
1278impl Div<&DVec2> for &f64 {
1279 type Output = DVec2;
1280 #[inline]
1281 fn div(self, rhs: &DVec2) -> DVec2 {
1282 (*self).div(*rhs)
1283 }
1284}
1285
1286impl Div<DVec2> for &f64 {
1287 type Output = DVec2;
1288 #[inline]
1289 fn div(self, rhs: DVec2) -> DVec2 {
1290 (*self).div(rhs)
1291 }
1292}
1293
1294impl Mul for DVec2 {
1295 type Output = Self;
1296 #[inline]
1297 fn mul(self, rhs: Self) -> Self {
1298 Self {
1299 x: self.x.mul(rhs.x),
1300 y: self.y.mul(rhs.y),
1301 }
1302 }
1303}
1304
1305impl Mul<&Self> for DVec2 {
1306 type Output = Self;
1307 #[inline]
1308 fn mul(self, rhs: &Self) -> Self {
1309 self.mul(*rhs)
1310 }
1311}
1312
1313impl Mul<&DVec2> for &DVec2 {
1314 type Output = DVec2;
1315 #[inline]
1316 fn mul(self, rhs: &DVec2) -> DVec2 {
1317 (*self).mul(*rhs)
1318 }
1319}
1320
1321impl Mul<DVec2> for &DVec2 {
1322 type Output = DVec2;
1323 #[inline]
1324 fn mul(self, rhs: DVec2) -> DVec2 {
1325 (*self).mul(rhs)
1326 }
1327}
1328
1329impl MulAssign for DVec2 {
1330 #[inline]
1331 fn mul_assign(&mut self, rhs: Self) {
1332 self.x.mul_assign(rhs.x);
1333 self.y.mul_assign(rhs.y);
1334 }
1335}
1336
1337impl MulAssign<&Self> for DVec2 {
1338 #[inline]
1339 fn mul_assign(&mut self, rhs: &Self) {
1340 self.mul_assign(*rhs);
1341 }
1342}
1343
1344impl Mul<f64> for DVec2 {
1345 type Output = Self;
1346 #[inline]
1347 fn mul(self, rhs: f64) -> Self {
1348 Self {
1349 x: self.x.mul(rhs),
1350 y: self.y.mul(rhs),
1351 }
1352 }
1353}
1354
1355impl Mul<&f64> for DVec2 {
1356 type Output = Self;
1357 #[inline]
1358 fn mul(self, rhs: &f64) -> Self {
1359 self.mul(*rhs)
1360 }
1361}
1362
1363impl Mul<&f64> for &DVec2 {
1364 type Output = DVec2;
1365 #[inline]
1366 fn mul(self, rhs: &f64) -> DVec2 {
1367 (*self).mul(*rhs)
1368 }
1369}
1370
1371impl Mul<f64> for &DVec2 {
1372 type Output = DVec2;
1373 #[inline]
1374 fn mul(self, rhs: f64) -> DVec2 {
1375 (*self).mul(rhs)
1376 }
1377}
1378
1379impl MulAssign<f64> for DVec2 {
1380 #[inline]
1381 fn mul_assign(&mut self, rhs: f64) {
1382 self.x.mul_assign(rhs);
1383 self.y.mul_assign(rhs);
1384 }
1385}
1386
1387impl MulAssign<&f64> for DVec2 {
1388 #[inline]
1389 fn mul_assign(&mut self, rhs: &f64) {
1390 self.mul_assign(*rhs);
1391 }
1392}
1393
1394impl Mul<DVec2> for f64 {
1395 type Output = DVec2;
1396 #[inline]
1397 fn mul(self, rhs: DVec2) -> DVec2 {
1398 DVec2 {
1399 x: self.mul(rhs.x),
1400 y: self.mul(rhs.y),
1401 }
1402 }
1403}
1404
1405impl Mul<&DVec2> for f64 {
1406 type Output = DVec2;
1407 #[inline]
1408 fn mul(self, rhs: &DVec2) -> DVec2 {
1409 self.mul(*rhs)
1410 }
1411}
1412
1413impl Mul<&DVec2> for &f64 {
1414 type Output = DVec2;
1415 #[inline]
1416 fn mul(self, rhs: &DVec2) -> DVec2 {
1417 (*self).mul(*rhs)
1418 }
1419}
1420
1421impl Mul<DVec2> for &f64 {
1422 type Output = DVec2;
1423 #[inline]
1424 fn mul(self, rhs: DVec2) -> DVec2 {
1425 (*self).mul(rhs)
1426 }
1427}
1428
1429impl Add for DVec2 {
1430 type Output = Self;
1431 #[inline]
1432 fn add(self, rhs: Self) -> Self {
1433 Self {
1434 x: self.x.add(rhs.x),
1435 y: self.y.add(rhs.y),
1436 }
1437 }
1438}
1439
1440impl Add<&Self> for DVec2 {
1441 type Output = Self;
1442 #[inline]
1443 fn add(self, rhs: &Self) -> Self {
1444 self.add(*rhs)
1445 }
1446}
1447
1448impl Add<&DVec2> for &DVec2 {
1449 type Output = DVec2;
1450 #[inline]
1451 fn add(self, rhs: &DVec2) -> DVec2 {
1452 (*self).add(*rhs)
1453 }
1454}
1455
1456impl Add<DVec2> for &DVec2 {
1457 type Output = DVec2;
1458 #[inline]
1459 fn add(self, rhs: DVec2) -> DVec2 {
1460 (*self).add(rhs)
1461 }
1462}
1463
1464impl AddAssign for DVec2 {
1465 #[inline]
1466 fn add_assign(&mut self, rhs: Self) {
1467 self.x.add_assign(rhs.x);
1468 self.y.add_assign(rhs.y);
1469 }
1470}
1471
1472impl AddAssign<&Self> for DVec2 {
1473 #[inline]
1474 fn add_assign(&mut self, rhs: &Self) {
1475 self.add_assign(*rhs);
1476 }
1477}
1478
1479impl Add<f64> for DVec2 {
1480 type Output = Self;
1481 #[inline]
1482 fn add(self, rhs: f64) -> Self {
1483 Self {
1484 x: self.x.add(rhs),
1485 y: self.y.add(rhs),
1486 }
1487 }
1488}
1489
1490impl Add<&f64> for DVec2 {
1491 type Output = Self;
1492 #[inline]
1493 fn add(self, rhs: &f64) -> Self {
1494 self.add(*rhs)
1495 }
1496}
1497
1498impl Add<&f64> for &DVec2 {
1499 type Output = DVec2;
1500 #[inline]
1501 fn add(self, rhs: &f64) -> DVec2 {
1502 (*self).add(*rhs)
1503 }
1504}
1505
1506impl Add<f64> for &DVec2 {
1507 type Output = DVec2;
1508 #[inline]
1509 fn add(self, rhs: f64) -> DVec2 {
1510 (*self).add(rhs)
1511 }
1512}
1513
1514impl AddAssign<f64> for DVec2 {
1515 #[inline]
1516 fn add_assign(&mut self, rhs: f64) {
1517 self.x.add_assign(rhs);
1518 self.y.add_assign(rhs);
1519 }
1520}
1521
1522impl AddAssign<&f64> for DVec2 {
1523 #[inline]
1524 fn add_assign(&mut self, rhs: &f64) {
1525 self.add_assign(*rhs);
1526 }
1527}
1528
1529impl Add<DVec2> for f64 {
1530 type Output = DVec2;
1531 #[inline]
1532 fn add(self, rhs: DVec2) -> DVec2 {
1533 DVec2 {
1534 x: self.add(rhs.x),
1535 y: self.add(rhs.y),
1536 }
1537 }
1538}
1539
1540impl Add<&DVec2> for f64 {
1541 type Output = DVec2;
1542 #[inline]
1543 fn add(self, rhs: &DVec2) -> DVec2 {
1544 self.add(*rhs)
1545 }
1546}
1547
1548impl Add<&DVec2> for &f64 {
1549 type Output = DVec2;
1550 #[inline]
1551 fn add(self, rhs: &DVec2) -> DVec2 {
1552 (*self).add(*rhs)
1553 }
1554}
1555
1556impl Add<DVec2> for &f64 {
1557 type Output = DVec2;
1558 #[inline]
1559 fn add(self, rhs: DVec2) -> DVec2 {
1560 (*self).add(rhs)
1561 }
1562}
1563
1564impl Sub for DVec2 {
1565 type Output = Self;
1566 #[inline]
1567 fn sub(self, rhs: Self) -> Self {
1568 Self {
1569 x: self.x.sub(rhs.x),
1570 y: self.y.sub(rhs.y),
1571 }
1572 }
1573}
1574
1575impl Sub<&Self> for DVec2 {
1576 type Output = Self;
1577 #[inline]
1578 fn sub(self, rhs: &Self) -> Self {
1579 self.sub(*rhs)
1580 }
1581}
1582
1583impl Sub<&DVec2> for &DVec2 {
1584 type Output = DVec2;
1585 #[inline]
1586 fn sub(self, rhs: &DVec2) -> DVec2 {
1587 (*self).sub(*rhs)
1588 }
1589}
1590
1591impl Sub<DVec2> for &DVec2 {
1592 type Output = DVec2;
1593 #[inline]
1594 fn sub(self, rhs: DVec2) -> DVec2 {
1595 (*self).sub(rhs)
1596 }
1597}
1598
1599impl SubAssign for DVec2 {
1600 #[inline]
1601 fn sub_assign(&mut self, rhs: Self) {
1602 self.x.sub_assign(rhs.x);
1603 self.y.sub_assign(rhs.y);
1604 }
1605}
1606
1607impl SubAssign<&Self> for DVec2 {
1608 #[inline]
1609 fn sub_assign(&mut self, rhs: &Self) {
1610 self.sub_assign(*rhs);
1611 }
1612}
1613
1614impl Sub<f64> for DVec2 {
1615 type Output = Self;
1616 #[inline]
1617 fn sub(self, rhs: f64) -> Self {
1618 Self {
1619 x: self.x.sub(rhs),
1620 y: self.y.sub(rhs),
1621 }
1622 }
1623}
1624
1625impl Sub<&f64> for DVec2 {
1626 type Output = Self;
1627 #[inline]
1628 fn sub(self, rhs: &f64) -> Self {
1629 self.sub(*rhs)
1630 }
1631}
1632
1633impl Sub<&f64> for &DVec2 {
1634 type Output = DVec2;
1635 #[inline]
1636 fn sub(self, rhs: &f64) -> DVec2 {
1637 (*self).sub(*rhs)
1638 }
1639}
1640
1641impl Sub<f64> for &DVec2 {
1642 type Output = DVec2;
1643 #[inline]
1644 fn sub(self, rhs: f64) -> DVec2 {
1645 (*self).sub(rhs)
1646 }
1647}
1648
1649impl SubAssign<f64> for DVec2 {
1650 #[inline]
1651 fn sub_assign(&mut self, rhs: f64) {
1652 self.x.sub_assign(rhs);
1653 self.y.sub_assign(rhs);
1654 }
1655}
1656
1657impl SubAssign<&f64> for DVec2 {
1658 #[inline]
1659 fn sub_assign(&mut self, rhs: &f64) {
1660 self.sub_assign(*rhs);
1661 }
1662}
1663
1664impl Sub<DVec2> for f64 {
1665 type Output = DVec2;
1666 #[inline]
1667 fn sub(self, rhs: DVec2) -> DVec2 {
1668 DVec2 {
1669 x: self.sub(rhs.x),
1670 y: self.sub(rhs.y),
1671 }
1672 }
1673}
1674
1675impl Sub<&DVec2> for f64 {
1676 type Output = DVec2;
1677 #[inline]
1678 fn sub(self, rhs: &DVec2) -> DVec2 {
1679 self.sub(*rhs)
1680 }
1681}
1682
1683impl Sub<&DVec2> for &f64 {
1684 type Output = DVec2;
1685 #[inline]
1686 fn sub(self, rhs: &DVec2) -> DVec2 {
1687 (*self).sub(*rhs)
1688 }
1689}
1690
1691impl Sub<DVec2> for &f64 {
1692 type Output = DVec2;
1693 #[inline]
1694 fn sub(self, rhs: DVec2) -> DVec2 {
1695 (*self).sub(rhs)
1696 }
1697}
1698
1699impl Rem for DVec2 {
1700 type Output = Self;
1701 #[inline]
1702 fn rem(self, rhs: Self) -> Self {
1703 Self {
1704 x: self.x.rem(rhs.x),
1705 y: self.y.rem(rhs.y),
1706 }
1707 }
1708}
1709
1710impl Rem<&Self> for DVec2 {
1711 type Output = Self;
1712 #[inline]
1713 fn rem(self, rhs: &Self) -> Self {
1714 self.rem(*rhs)
1715 }
1716}
1717
1718impl Rem<&DVec2> for &DVec2 {
1719 type Output = DVec2;
1720 #[inline]
1721 fn rem(self, rhs: &DVec2) -> DVec2 {
1722 (*self).rem(*rhs)
1723 }
1724}
1725
1726impl Rem<DVec2> for &DVec2 {
1727 type Output = DVec2;
1728 #[inline]
1729 fn rem(self, rhs: DVec2) -> DVec2 {
1730 (*self).rem(rhs)
1731 }
1732}
1733
1734impl RemAssign for DVec2 {
1735 #[inline]
1736 fn rem_assign(&mut self, rhs: Self) {
1737 self.x.rem_assign(rhs.x);
1738 self.y.rem_assign(rhs.y);
1739 }
1740}
1741
1742impl RemAssign<&Self> for DVec2 {
1743 #[inline]
1744 fn rem_assign(&mut self, rhs: &Self) {
1745 self.rem_assign(*rhs);
1746 }
1747}
1748
1749impl Rem<f64> for DVec2 {
1750 type Output = Self;
1751 #[inline]
1752 fn rem(self, rhs: f64) -> Self {
1753 Self {
1754 x: self.x.rem(rhs),
1755 y: self.y.rem(rhs),
1756 }
1757 }
1758}
1759
1760impl Rem<&f64> for DVec2 {
1761 type Output = Self;
1762 #[inline]
1763 fn rem(self, rhs: &f64) -> Self {
1764 self.rem(*rhs)
1765 }
1766}
1767
1768impl Rem<&f64> for &DVec2 {
1769 type Output = DVec2;
1770 #[inline]
1771 fn rem(self, rhs: &f64) -> DVec2 {
1772 (*self).rem(*rhs)
1773 }
1774}
1775
1776impl Rem<f64> for &DVec2 {
1777 type Output = DVec2;
1778 #[inline]
1779 fn rem(self, rhs: f64) -> DVec2 {
1780 (*self).rem(rhs)
1781 }
1782}
1783
1784impl RemAssign<f64> for DVec2 {
1785 #[inline]
1786 fn rem_assign(&mut self, rhs: f64) {
1787 self.x.rem_assign(rhs);
1788 self.y.rem_assign(rhs);
1789 }
1790}
1791
1792impl RemAssign<&f64> for DVec2 {
1793 #[inline]
1794 fn rem_assign(&mut self, rhs: &f64) {
1795 self.rem_assign(*rhs);
1796 }
1797}
1798
1799impl Rem<DVec2> for f64 {
1800 type Output = DVec2;
1801 #[inline]
1802 fn rem(self, rhs: DVec2) -> DVec2 {
1803 DVec2 {
1804 x: self.rem(rhs.x),
1805 y: self.rem(rhs.y),
1806 }
1807 }
1808}
1809
1810impl Rem<&DVec2> for f64 {
1811 type Output = DVec2;
1812 #[inline]
1813 fn rem(self, rhs: &DVec2) -> DVec2 {
1814 self.rem(*rhs)
1815 }
1816}
1817
1818impl Rem<&DVec2> for &f64 {
1819 type Output = DVec2;
1820 #[inline]
1821 fn rem(self, rhs: &DVec2) -> DVec2 {
1822 (*self).rem(*rhs)
1823 }
1824}
1825
1826impl Rem<DVec2> for &f64 {
1827 type Output = DVec2;
1828 #[inline]
1829 fn rem(self, rhs: DVec2) -> DVec2 {
1830 (*self).rem(rhs)
1831 }
1832}
1833
1834impl AsRef<[f64; 2]> for DVec2 {
1835 #[inline]
1836 fn as_ref(&self) -> &[f64; 2] {
1837 unsafe { &*(self as *const Self as *const [f64; 2]) }
1838 }
1839}
1840
1841impl AsMut<[f64; 2]> for DVec2 {
1842 #[inline]
1843 fn as_mut(&mut self) -> &mut [f64; 2] {
1844 unsafe { &mut *(self as *mut Self as *mut [f64; 2]) }
1845 }
1846}
1847
1848impl Sum for DVec2 {
1849 #[inline]
1850 fn sum<I>(iter: I) -> Self
1851 where
1852 I: Iterator<Item = Self>,
1853 {
1854 iter.fold(Self::ZERO, Self::add)
1855 }
1856}
1857
1858impl<'a> Sum<&'a Self> for DVec2 {
1859 #[inline]
1860 fn sum<I>(iter: I) -> Self
1861 where
1862 I: Iterator<Item = &'a Self>,
1863 {
1864 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1865 }
1866}
1867
1868impl Product for DVec2 {
1869 #[inline]
1870 fn product<I>(iter: I) -> Self
1871 where
1872 I: Iterator<Item = Self>,
1873 {
1874 iter.fold(Self::ONE, Self::mul)
1875 }
1876}
1877
1878impl<'a> Product<&'a Self> for DVec2 {
1879 #[inline]
1880 fn product<I>(iter: I) -> Self
1881 where
1882 I: Iterator<Item = &'a Self>,
1883 {
1884 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1885 }
1886}
1887
1888impl Neg for DVec2 {
1889 type Output = Self;
1890 #[inline]
1891 fn neg(self) -> Self {
1892 Self {
1893 x: self.x.neg(),
1894 y: self.y.neg(),
1895 }
1896 }
1897}
1898
1899impl Neg for &DVec2 {
1900 type Output = DVec2;
1901 #[inline]
1902 fn neg(self) -> DVec2 {
1903 (*self).neg()
1904 }
1905}
1906
1907impl Index<usize> for DVec2 {
1908 type Output = f64;
1909 #[inline]
1910 fn index(&self, index: usize) -> &Self::Output {
1911 match index {
1912 0 => &self.x,
1913 1 => &self.y,
1914 _ => panic!("index out of bounds"),
1915 }
1916 }
1917}
1918
1919impl IndexMut<usize> for DVec2 {
1920 #[inline]
1921 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1922 match index {
1923 0 => &mut self.x,
1924 1 => &mut self.y,
1925 _ => panic!("index out of bounds"),
1926 }
1927 }
1928}
1929
1930impl fmt::Display for DVec2 {
1931 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1932 if let Some(p) = f.precision() {
1933 write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y)
1934 } else {
1935 write!(f, "[{}, {}]", self.x, self.y)
1936 }
1937 }
1938}
1939
1940impl fmt::Debug for DVec2 {
1941 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1942 fmt.debug_tuple(stringify!(DVec2))
1943 .field(&self.x)
1944 .field(&self.y)
1945 .finish()
1946 }
1947}
1948
1949impl From<[f64; 2]> for DVec2 {
1950 #[inline]
1951 fn from(a: [f64; 2]) -> Self {
1952 Self::new(a[0], a[1])
1953 }
1954}
1955
1956impl From<DVec2> for [f64; 2] {
1957 #[inline]
1958 fn from(v: DVec2) -> Self {
1959 [v.x, v.y]
1960 }
1961}
1962
1963impl From<(f64, f64)> for DVec2 {
1964 #[inline]
1965 fn from(t: (f64, f64)) -> Self {
1966 Self::new(t.0, t.1)
1967 }
1968}
1969
1970impl From<DVec2> for (f64, f64) {
1971 #[inline]
1972 fn from(v: DVec2) -> Self {
1973 (v.x, v.y)
1974 }
1975}
1976
1977impl From<Vec2> for DVec2 {
1978 #[inline]
1979 fn from(v: Vec2) -> Self {
1980 Self::new(f64::from(v.x), f64::from(v.y))
1981 }
1982}
1983
1984impl From<IVec2> for DVec2 {
1985 #[inline]
1986 fn from(v: IVec2) -> Self {
1987 Self::new(f64::from(v.x), f64::from(v.y))
1988 }
1989}
1990
1991impl From<UVec2> for DVec2 {
1992 #[inline]
1993 fn from(v: UVec2) -> Self {
1994 Self::new(f64::from(v.x), f64::from(v.y))
1995 }
1996}
1997
1998impl From<BVec2> for DVec2 {
1999 #[inline]
2000 fn from(v: BVec2) -> Self {
2001 Self::new(f64::from(v.x), f64::from(v.y))
2002 }
2003}