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_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: f64, y: f64) -> Self {
91 Self { x, y }
92 }
93
94 #[inline]
96 #[must_use]
97 pub const fn splat(v: f64) -> Self {
98 Self { x: v, y: v }
99 }
100
101 #[inline]
103 #[must_use]
104 pub fn map<F>(self, f: F) -> Self
105 where
106 F: Fn(f64) -> f64,
107 {
108 Self::new(f(self.x), f(self.y))
109 }
110
111 #[inline]
117 #[must_use]
118 pub fn select(mask: BVec2, if_true: Self, if_false: Self) -> Self {
119 Self {
120 x: if mask.test(0) { if_true.x } else { if_false.x },
121 y: if mask.test(1) { if_true.y } else { if_false.y },
122 }
123 }
124
125 #[inline]
127 #[must_use]
128 pub const fn from_array(a: [f64; 2]) -> Self {
129 Self::new(a[0], a[1])
130 }
131
132 #[inline]
134 #[must_use]
135 pub const fn to_array(&self) -> [f64; 2] {
136 [self.x, self.y]
137 }
138
139 #[inline]
145 #[must_use]
146 pub const fn from_slice(slice: &[f64]) -> 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 [f64]) {
158 slice[..2].copy_from_slice(&self.to_array());
159 }
160
161 #[inline]
163 #[must_use]
164 pub const fn extend(self, z: f64) -> DVec3 {
165 DVec3::new(self.x, self.y, z)
166 }
167
168 #[inline]
170 #[must_use]
171 pub fn with_x(mut self, x: f64) -> Self {
172 self.x = x;
173 self
174 }
175
176 #[inline]
178 #[must_use]
179 pub fn with_y(mut self, y: f64) -> Self {
180 self.y = y;
181 self
182 }
183
184 #[inline]
186 #[must_use]
187 pub fn dot(self, rhs: Self) -> f64 {
188 (self.x * rhs.x) + (self.y * rhs.y)
189 }
190
191 #[inline]
193 #[must_use]
194 pub fn dot_into_vec(self, rhs: Self) -> Self {
195 Self::splat(self.dot(rhs))
196 }
197
198 #[inline]
205 #[must_use]
206 pub fn min(self, rhs: Self) -> Self {
207 Self {
208 x: if self.x < rhs.x { self.x } else { rhs.x },
209 y: if self.y < rhs.y { self.y } else { rhs.y },
210 }
211 }
212
213 #[inline]
220 #[must_use]
221 pub fn max(self, rhs: Self) -> Self {
222 Self {
223 x: if self.x > rhs.x { self.x } else { rhs.x },
224 y: if self.y > rhs.y { self.y } else { rhs.y },
225 }
226 }
227
228 #[inline]
239 #[must_use]
240 pub fn clamp(self, min: Self, max: Self) -> Self {
241 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
242 self.max(min).min(max)
243 }
244
245 #[inline]
252 #[must_use]
253 pub fn min_element(self) -> f64 {
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) -> f64 {
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) -> f64 {
301 self.x + self.y
302 }
303
304 #[inline]
308 #[must_use]
309 pub fn element_product(self) -> f64 {
310 self.x * self.y
311 }
312
313 #[inline]
319 #[must_use]
320 pub fn cmpeq(self, rhs: Self) -> BVec2 {
321 BVec2::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y))
322 }
323
324 #[inline]
330 #[must_use]
331 pub fn cmpne(self, rhs: Self) -> BVec2 {
332 BVec2::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y))
333 }
334
335 #[inline]
341 #[must_use]
342 pub fn cmpge(self, rhs: Self) -> BVec2 {
343 BVec2::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y))
344 }
345
346 #[inline]
352 #[must_use]
353 pub fn cmpgt(self, rhs: Self) -> BVec2 {
354 BVec2::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y))
355 }
356
357 #[inline]
363 #[must_use]
364 pub fn cmple(self, rhs: Self) -> BVec2 {
365 BVec2::new(self.x.le(&rhs.x), self.y.le(&rhs.y))
366 }
367
368 #[inline]
374 #[must_use]
375 pub fn cmplt(self, rhs: Self) -> BVec2 {
376 BVec2::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y))
377 }
378
379 #[inline]
381 #[must_use]
382 pub fn abs(self) -> Self {
383 Self {
384 x: math::abs(self.x),
385 y: math::abs(self.y),
386 }
387 }
388
389 #[inline]
395 #[must_use]
396 pub fn signum(self) -> Self {
397 Self {
398 x: math::signum(self.x),
399 y: math::signum(self.y),
400 }
401 }
402
403 #[inline]
405 #[must_use]
406 pub fn copysign(self, rhs: Self) -> Self {
407 Self {
408 x: math::copysign(self.x, rhs.x),
409 y: math::copysign(self.y, rhs.y),
410 }
411 }
412
413 #[inline]
421 #[must_use]
422 pub fn is_negative_bitmask(self) -> u32 {
423 (self.x.is_sign_negative() as u32) | ((self.y.is_sign_negative() as u32) << 1)
424 }
425
426 #[inline]
429 #[must_use]
430 pub fn is_finite(self) -> bool {
431 self.x.is_finite() && self.y.is_finite()
432 }
433
434 #[inline]
438 #[must_use]
439 pub fn is_finite_mask(self) -> BVec2 {
440 BVec2::new(self.x.is_finite(), self.y.is_finite())
441 }
442
443 #[inline]
445 #[must_use]
446 pub fn is_nan(self) -> bool {
447 self.x.is_nan() || self.y.is_nan()
448 }
449
450 #[inline]
454 #[must_use]
455 pub fn is_nan_mask(self) -> BVec2 {
456 BVec2::new(self.x.is_nan(), self.y.is_nan())
457 }
458
459 #[doc(alias = "magnitude")]
461 #[inline]
462 #[must_use]
463 pub fn length(self) -> f64 {
464 math::sqrt(self.dot(self))
465 }
466
467 #[doc(alias = "magnitude2")]
471 #[inline]
472 #[must_use]
473 pub fn length_squared(self) -> f64 {
474 self.dot(self)
475 }
476
477 #[inline]
481 #[must_use]
482 pub fn length_recip(self) -> f64 {
483 self.length().recip()
484 }
485
486 #[inline]
488 #[must_use]
489 pub fn distance(self, rhs: Self) -> f64 {
490 (self - rhs).length()
491 }
492
493 #[inline]
495 #[must_use]
496 pub fn distance_squared(self, rhs: Self) -> f64 {
497 (self - rhs).length_squared()
498 }
499
500 #[inline]
502 #[must_use]
503 pub fn div_euclid(self, rhs: Self) -> Self {
504 Self::new(
505 math::div_euclid(self.x, rhs.x),
506 math::div_euclid(self.y, rhs.y),
507 )
508 }
509
510 #[inline]
514 #[must_use]
515 pub fn rem_euclid(self, rhs: Self) -> Self {
516 Self::new(
517 math::rem_euclid(self.x, rhs.x),
518 math::rem_euclid(self.y, rhs.y),
519 )
520 }
521
522 #[inline]
532 #[must_use]
533 pub fn normalize(self) -> Self {
534 #[allow(clippy::let_and_return)]
535 let normalized = self.mul(self.length_recip());
536 glam_assert!(normalized.is_finite());
537 normalized
538 }
539
540 #[inline]
547 #[must_use]
548 pub fn try_normalize(self) -> Option<Self> {
549 let rcp = self.length_recip();
550 if rcp.is_finite() && rcp > 0.0 {
551 Some(self * rcp)
552 } else {
553 None
554 }
555 }
556
557 #[inline]
565 #[must_use]
566 pub fn normalize_or(self, fallback: Self) -> Self {
567 let rcp = self.length_recip();
568 if rcp.is_finite() && rcp > 0.0 {
569 self * rcp
570 } else {
571 fallback
572 }
573 }
574
575 #[inline]
582 #[must_use]
583 pub fn normalize_or_zero(self) -> Self {
584 self.normalize_or(Self::ZERO)
585 }
586
587 #[inline]
591 #[must_use]
592 pub fn normalize_and_length(self) -> (Self, f64) {
593 let length = self.length();
594 let rcp = 1.0 / length;
595 if rcp.is_finite() && rcp > 0.0 {
596 (self * rcp, length)
597 } else {
598 (Self::X, 0.0)
599 }
600 }
601
602 #[inline]
606 #[must_use]
607 pub fn is_normalized(self) -> bool {
608 math::abs(self.length_squared() - 1.0) <= 2e-4
609 }
610
611 #[inline]
619 #[must_use]
620 pub fn project_onto(self, rhs: Self) -> Self {
621 let other_len_sq_rcp = rhs.dot(rhs).recip();
622 glam_assert!(other_len_sq_rcp.is_finite());
623 rhs * self.dot(rhs) * other_len_sq_rcp
624 }
625
626 #[doc(alias("plane"))]
637 #[inline]
638 #[must_use]
639 pub fn reject_from(self, rhs: Self) -> Self {
640 self - self.project_onto(rhs)
641 }
642
643 #[inline]
651 #[must_use]
652 pub fn project_onto_normalized(self, rhs: Self) -> Self {
653 glam_assert!(rhs.is_normalized());
654 rhs * self.dot(rhs)
655 }
656
657 #[doc(alias("plane"))]
668 #[inline]
669 #[must_use]
670 pub fn reject_from_normalized(self, rhs: Self) -> Self {
671 self - self.project_onto_normalized(rhs)
672 }
673
674 #[inline]
677 #[must_use]
678 pub fn round(self) -> Self {
679 Self {
680 x: math::round(self.x),
681 y: math::round(self.y),
682 }
683 }
684
685 #[inline]
688 #[must_use]
689 pub fn floor(self) -> Self {
690 Self {
691 x: math::floor(self.x),
692 y: math::floor(self.y),
693 }
694 }
695
696 #[inline]
699 #[must_use]
700 pub fn ceil(self) -> Self {
701 Self {
702 x: math::ceil(self.x),
703 y: math::ceil(self.y),
704 }
705 }
706
707 #[inline]
710 #[must_use]
711 pub fn trunc(self) -> Self {
712 Self {
713 x: math::trunc(self.x),
714 y: math::trunc(self.y),
715 }
716 }
717
718 #[inline]
722 #[must_use]
723 pub fn step(self, rhs: Self) -> Self {
724 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
725 }
726
727 #[inline]
729 #[must_use]
730 pub fn saturate(self) -> Self {
731 self.clamp(Self::ZERO, Self::ONE)
732 }
733
734 #[inline]
741 #[must_use]
742 pub fn fract(self) -> Self {
743 self - self.trunc()
744 }
745
746 #[inline]
753 #[must_use]
754 pub fn fract_gl(self) -> Self {
755 self - self.floor()
756 }
757
758 #[inline]
761 #[must_use]
762 pub fn exp(self) -> Self {
763 Self::new(math::exp(self.x), math::exp(self.y))
764 }
765
766 #[inline]
768 #[must_use]
769 pub fn exp2(self) -> Self {
770 Self::new(math::exp2(self.x), math::exp2(self.y))
771 }
772
773 #[inline]
776 #[must_use]
777 pub fn ln(self) -> Self {
778 Self::new(math::ln(self.x), math::ln(self.y))
779 }
780
781 #[inline]
784 #[must_use]
785 pub fn log2(self) -> Self {
786 Self::new(math::log2(self.x), math::log2(self.y))
787 }
788
789 #[inline]
791 #[must_use]
792 pub fn powf(self, n: f64) -> Self {
793 Self::new(math::powf(self.x, n), math::powf(self.y, n))
794 }
795
796 #[inline]
799 #[must_use]
800 pub fn sqrt(self) -> Self {
801 Self::new(math::sqrt(self.x), math::sqrt(self.y))
802 }
803
804 #[inline]
806 #[must_use]
807 pub fn cos(self) -> Self {
808 Self::new(math::cos(self.x), math::cos(self.y))
809 }
810
811 #[inline]
813 #[must_use]
814 pub fn sin(self) -> Self {
815 Self::new(math::sin(self.x), math::sin(self.y))
816 }
817
818 #[inline]
820 #[must_use]
821 pub fn sin_cos(self) -> (Self, Self) {
822 let (sin_x, cos_x) = math::sin_cos(self.x);
823 let (sin_y, cos_y) = math::sin_cos(self.y);
824
825 (Self::new(sin_x, sin_y), Self::new(cos_x, cos_y))
826 }
827
828 #[inline]
830 #[must_use]
831 pub fn recip(self) -> Self {
832 Self {
833 x: 1.0 / self.x,
834 y: 1.0 / self.y,
835 }
836 }
837
838 #[doc(alias = "mix")]
844 #[inline]
845 #[must_use]
846 pub fn lerp(self, rhs: Self, s: f64) -> Self {
847 self * (1.0 - s) + rhs * s
848 }
849
850 #[inline]
855 #[must_use]
856 pub fn move_towards(self, rhs: Self, d: f64) -> Self {
857 let a = rhs - self;
858 let len = a.length();
859 if len <= d || len <= 1e-4 {
860 return rhs;
861 }
862 self + a / len * d
863 }
864
865 #[inline]
871 pub fn midpoint(self, rhs: Self) -> Self {
872 (self + rhs) * 0.5
873 }
874
875 #[inline]
885 #[must_use]
886 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
887 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
888 }
889
890 #[inline]
896 #[must_use]
897 pub fn clamp_length(self, min: f64, max: f64) -> Self {
898 glam_assert!(0.0 <= min);
899 glam_assert!(min <= max);
900 let length_sq = self.length_squared();
901 if length_sq < min * min {
902 min * (self / math::sqrt(length_sq))
903 } else if length_sq > max * max {
904 max * (self / math::sqrt(length_sq))
905 } else {
906 self
907 }
908 }
909
910 #[inline]
916 #[must_use]
917 pub fn clamp_length_max(self, max: f64) -> Self {
918 glam_assert!(0.0 <= max);
919 let length_sq = self.length_squared();
920 if length_sq > max * max {
921 max * (self / math::sqrt(length_sq))
922 } else {
923 self
924 }
925 }
926
927 #[inline]
933 #[must_use]
934 pub fn clamp_length_min(self, min: f64) -> Self {
935 glam_assert!(0.0 <= min);
936 let length_sq = self.length_squared();
937 if length_sq < min * min {
938 min * (self / math::sqrt(length_sq))
939 } else {
940 self
941 }
942 }
943
944 #[inline]
952 #[must_use]
953 pub fn mul_add(self, a: Self, b: Self) -> Self {
954 Self::new(
955 math::mul_add(self.x, a.x, b.x),
956 math::mul_add(self.y, a.y, b.y),
957 )
958 }
959
960 #[inline]
969 #[must_use]
970 pub fn reflect(self, normal: Self) -> Self {
971 glam_assert!(normal.is_normalized());
972 self - 2.0 * self.dot(normal) * normal
973 }
974
975 #[inline]
985 #[must_use]
986 pub fn refract(self, normal: Self, eta: f64) -> Self {
987 glam_assert!(self.is_normalized());
988 glam_assert!(normal.is_normalized());
989 let n_dot_i = normal.dot(self);
990 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
991 if k >= 0.0 {
992 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
993 } else {
994 Self::ZERO
995 }
996 }
997
998 #[inline]
1003 #[must_use]
1004 pub fn from_angle(angle: f64) -> Self {
1005 let (sin, cos) = math::sin_cos(angle);
1006 Self { x: cos, y: sin }
1007 }
1008
1009 #[inline]
1013 #[must_use]
1014 pub fn to_angle(self) -> f64 {
1015 math::atan2(self.y, self.x)
1016 }
1017
1018 #[inline]
1022 #[must_use]
1023 pub fn angle_to(self, rhs: Self) -> f64 {
1024 let angle = math::acos_approx(
1025 self.dot(rhs) / math::sqrt(self.length_squared() * rhs.length_squared()),
1026 );
1027
1028 angle * math::signum(self.perp_dot(rhs))
1029 }
1030
1031 #[inline]
1033 #[must_use]
1034 pub fn perp(self) -> Self {
1035 Self {
1036 x: -self.y,
1037 y: self.x,
1038 }
1039 }
1040
1041 #[doc(alias = "wedge")]
1044 #[doc(alias = "cross")]
1045 #[doc(alias = "determinant")]
1046 #[inline]
1047 #[must_use]
1048 pub fn perp_dot(self, rhs: Self) -> f64 {
1049 (self.x * rhs.y) - (self.y * rhs.x)
1050 }
1051
1052 #[inline]
1060 #[must_use]
1061 pub fn rotate(self, rhs: Self) -> Self {
1062 Self {
1063 x: self.x * rhs.x - self.y * rhs.y,
1064 y: self.y * rhs.x + self.x * rhs.y,
1065 }
1066 }
1067
1068 #[inline]
1074 #[must_use]
1075 pub fn rotate_towards(self, rhs: Self, max_angle: f64) -> Self {
1076 let a = self.angle_to(rhs);
1077 let abs_a = math::abs(a);
1078 let angle = max_angle.clamp(abs_a - core::f64::consts::PI, abs_a) * math::signum(a);
1080 Self::from_angle(angle).rotate(self)
1081 }
1082
1083 #[inline]
1085 #[must_use]
1086 pub fn as_vec2(self) -> crate::Vec2 {
1087 crate::Vec2::new(self.x as f32, self.y as f32)
1088 }
1089
1090 #[inline]
1092 #[must_use]
1093 pub fn as_i8vec2(self) -> crate::I8Vec2 {
1094 crate::I8Vec2::new(self.x as i8, self.y as i8)
1095 }
1096
1097 #[inline]
1099 #[must_use]
1100 pub fn as_u8vec2(self) -> crate::U8Vec2 {
1101 crate::U8Vec2::new(self.x as u8, self.y as u8)
1102 }
1103
1104 #[inline]
1106 #[must_use]
1107 pub fn as_i16vec2(self) -> crate::I16Vec2 {
1108 crate::I16Vec2::new(self.x as i16, self.y as i16)
1109 }
1110
1111 #[inline]
1113 #[must_use]
1114 pub fn as_u16vec2(self) -> crate::U16Vec2 {
1115 crate::U16Vec2::new(self.x as u16, self.y as u16)
1116 }
1117
1118 #[inline]
1120 #[must_use]
1121 pub fn as_ivec2(self) -> crate::IVec2 {
1122 crate::IVec2::new(self.x as i32, self.y as i32)
1123 }
1124
1125 #[inline]
1127 #[must_use]
1128 pub fn as_uvec2(self) -> crate::UVec2 {
1129 crate::UVec2::new(self.x as u32, self.y as u32)
1130 }
1131
1132 #[inline]
1134 #[must_use]
1135 pub fn as_i64vec2(self) -> crate::I64Vec2 {
1136 crate::I64Vec2::new(self.x as i64, self.y as i64)
1137 }
1138
1139 #[inline]
1141 #[must_use]
1142 pub fn as_u64vec2(self) -> crate::U64Vec2 {
1143 crate::U64Vec2::new(self.x as u64, self.y as u64)
1144 }
1145
1146 #[inline]
1148 #[must_use]
1149 pub fn as_isizevec2(self) -> crate::ISizeVec2 {
1150 crate::ISizeVec2::new(self.x as isize, self.y as isize)
1151 }
1152
1153 #[inline]
1155 #[must_use]
1156 pub fn as_usizevec2(self) -> crate::USizeVec2 {
1157 crate::USizeVec2::new(self.x as usize, self.y as usize)
1158 }
1159}
1160
1161impl Default for DVec2 {
1162 #[inline(always)]
1163 fn default() -> Self {
1164 Self::ZERO
1165 }
1166}
1167
1168impl Div for DVec2 {
1169 type Output = Self;
1170 #[inline]
1171 fn div(self, rhs: Self) -> Self {
1172 Self {
1173 x: self.x.div(rhs.x),
1174 y: self.y.div(rhs.y),
1175 }
1176 }
1177}
1178
1179impl Div<&Self> for DVec2 {
1180 type Output = Self;
1181 #[inline]
1182 fn div(self, rhs: &Self) -> Self {
1183 self.div(*rhs)
1184 }
1185}
1186
1187impl Div<&DVec2> for &DVec2 {
1188 type Output = DVec2;
1189 #[inline]
1190 fn div(self, rhs: &DVec2) -> DVec2 {
1191 (*self).div(*rhs)
1192 }
1193}
1194
1195impl Div<DVec2> for &DVec2 {
1196 type Output = DVec2;
1197 #[inline]
1198 fn div(self, rhs: DVec2) -> DVec2 {
1199 (*self).div(rhs)
1200 }
1201}
1202
1203impl DivAssign for DVec2 {
1204 #[inline]
1205 fn div_assign(&mut self, rhs: Self) {
1206 self.x.div_assign(rhs.x);
1207 self.y.div_assign(rhs.y);
1208 }
1209}
1210
1211impl DivAssign<&Self> for DVec2 {
1212 #[inline]
1213 fn div_assign(&mut self, rhs: &Self) {
1214 self.div_assign(*rhs);
1215 }
1216}
1217
1218impl Div<f64> for DVec2 {
1219 type Output = Self;
1220 #[inline]
1221 fn div(self, rhs: f64) -> Self {
1222 Self {
1223 x: self.x.div(rhs),
1224 y: self.y.div(rhs),
1225 }
1226 }
1227}
1228
1229impl Div<&f64> for DVec2 {
1230 type Output = Self;
1231 #[inline]
1232 fn div(self, rhs: &f64) -> Self {
1233 self.div(*rhs)
1234 }
1235}
1236
1237impl Div<&f64> for &DVec2 {
1238 type Output = DVec2;
1239 #[inline]
1240 fn div(self, rhs: &f64) -> DVec2 {
1241 (*self).div(*rhs)
1242 }
1243}
1244
1245impl Div<f64> for &DVec2 {
1246 type Output = DVec2;
1247 #[inline]
1248 fn div(self, rhs: f64) -> DVec2 {
1249 (*self).div(rhs)
1250 }
1251}
1252
1253impl DivAssign<f64> for DVec2 {
1254 #[inline]
1255 fn div_assign(&mut self, rhs: f64) {
1256 self.x.div_assign(rhs);
1257 self.y.div_assign(rhs);
1258 }
1259}
1260
1261impl DivAssign<&f64> for DVec2 {
1262 #[inline]
1263 fn div_assign(&mut self, rhs: &f64) {
1264 self.div_assign(*rhs);
1265 }
1266}
1267
1268impl Div<DVec2> for f64 {
1269 type Output = DVec2;
1270 #[inline]
1271 fn div(self, rhs: DVec2) -> DVec2 {
1272 DVec2 {
1273 x: self.div(rhs.x),
1274 y: self.div(rhs.y),
1275 }
1276 }
1277}
1278
1279impl Div<&DVec2> for f64 {
1280 type Output = DVec2;
1281 #[inline]
1282 fn div(self, rhs: &DVec2) -> DVec2 {
1283 self.div(*rhs)
1284 }
1285}
1286
1287impl Div<&DVec2> for &f64 {
1288 type Output = DVec2;
1289 #[inline]
1290 fn div(self, rhs: &DVec2) -> DVec2 {
1291 (*self).div(*rhs)
1292 }
1293}
1294
1295impl Div<DVec2> for &f64 {
1296 type Output = DVec2;
1297 #[inline]
1298 fn div(self, rhs: DVec2) -> DVec2 {
1299 (*self).div(rhs)
1300 }
1301}
1302
1303impl Mul for DVec2 {
1304 type Output = Self;
1305 #[inline]
1306 fn mul(self, rhs: Self) -> Self {
1307 Self {
1308 x: self.x.mul(rhs.x),
1309 y: self.y.mul(rhs.y),
1310 }
1311 }
1312}
1313
1314impl Mul<&Self> for DVec2 {
1315 type Output = Self;
1316 #[inline]
1317 fn mul(self, rhs: &Self) -> Self {
1318 self.mul(*rhs)
1319 }
1320}
1321
1322impl Mul<&DVec2> for &DVec2 {
1323 type Output = DVec2;
1324 #[inline]
1325 fn mul(self, rhs: &DVec2) -> DVec2 {
1326 (*self).mul(*rhs)
1327 }
1328}
1329
1330impl Mul<DVec2> for &DVec2 {
1331 type Output = DVec2;
1332 #[inline]
1333 fn mul(self, rhs: DVec2) -> DVec2 {
1334 (*self).mul(rhs)
1335 }
1336}
1337
1338impl MulAssign for DVec2 {
1339 #[inline]
1340 fn mul_assign(&mut self, rhs: Self) {
1341 self.x.mul_assign(rhs.x);
1342 self.y.mul_assign(rhs.y);
1343 }
1344}
1345
1346impl MulAssign<&Self> for DVec2 {
1347 #[inline]
1348 fn mul_assign(&mut self, rhs: &Self) {
1349 self.mul_assign(*rhs);
1350 }
1351}
1352
1353impl Mul<f64> for DVec2 {
1354 type Output = Self;
1355 #[inline]
1356 fn mul(self, rhs: f64) -> Self {
1357 Self {
1358 x: self.x.mul(rhs),
1359 y: self.y.mul(rhs),
1360 }
1361 }
1362}
1363
1364impl Mul<&f64> for DVec2 {
1365 type Output = Self;
1366 #[inline]
1367 fn mul(self, rhs: &f64) -> Self {
1368 self.mul(*rhs)
1369 }
1370}
1371
1372impl Mul<&f64> for &DVec2 {
1373 type Output = DVec2;
1374 #[inline]
1375 fn mul(self, rhs: &f64) -> DVec2 {
1376 (*self).mul(*rhs)
1377 }
1378}
1379
1380impl Mul<f64> for &DVec2 {
1381 type Output = DVec2;
1382 #[inline]
1383 fn mul(self, rhs: f64) -> DVec2 {
1384 (*self).mul(rhs)
1385 }
1386}
1387
1388impl MulAssign<f64> for DVec2 {
1389 #[inline]
1390 fn mul_assign(&mut self, rhs: f64) {
1391 self.x.mul_assign(rhs);
1392 self.y.mul_assign(rhs);
1393 }
1394}
1395
1396impl MulAssign<&f64> for DVec2 {
1397 #[inline]
1398 fn mul_assign(&mut self, rhs: &f64) {
1399 self.mul_assign(*rhs);
1400 }
1401}
1402
1403impl Mul<DVec2> for f64 {
1404 type Output = DVec2;
1405 #[inline]
1406 fn mul(self, rhs: DVec2) -> DVec2 {
1407 DVec2 {
1408 x: self.mul(rhs.x),
1409 y: self.mul(rhs.y),
1410 }
1411 }
1412}
1413
1414impl Mul<&DVec2> for f64 {
1415 type Output = DVec2;
1416 #[inline]
1417 fn mul(self, rhs: &DVec2) -> DVec2 {
1418 self.mul(*rhs)
1419 }
1420}
1421
1422impl Mul<&DVec2> for &f64 {
1423 type Output = DVec2;
1424 #[inline]
1425 fn mul(self, rhs: &DVec2) -> DVec2 {
1426 (*self).mul(*rhs)
1427 }
1428}
1429
1430impl Mul<DVec2> for &f64 {
1431 type Output = DVec2;
1432 #[inline]
1433 fn mul(self, rhs: DVec2) -> DVec2 {
1434 (*self).mul(rhs)
1435 }
1436}
1437
1438impl Add for DVec2 {
1439 type Output = Self;
1440 #[inline]
1441 fn add(self, rhs: Self) -> Self {
1442 Self {
1443 x: self.x.add(rhs.x),
1444 y: self.y.add(rhs.y),
1445 }
1446 }
1447}
1448
1449impl Add<&Self> for DVec2 {
1450 type Output = Self;
1451 #[inline]
1452 fn add(self, rhs: &Self) -> Self {
1453 self.add(*rhs)
1454 }
1455}
1456
1457impl Add<&DVec2> for &DVec2 {
1458 type Output = DVec2;
1459 #[inline]
1460 fn add(self, rhs: &DVec2) -> DVec2 {
1461 (*self).add(*rhs)
1462 }
1463}
1464
1465impl Add<DVec2> for &DVec2 {
1466 type Output = DVec2;
1467 #[inline]
1468 fn add(self, rhs: DVec2) -> DVec2 {
1469 (*self).add(rhs)
1470 }
1471}
1472
1473impl AddAssign for DVec2 {
1474 #[inline]
1475 fn add_assign(&mut self, rhs: Self) {
1476 self.x.add_assign(rhs.x);
1477 self.y.add_assign(rhs.y);
1478 }
1479}
1480
1481impl AddAssign<&Self> for DVec2 {
1482 #[inline]
1483 fn add_assign(&mut self, rhs: &Self) {
1484 self.add_assign(*rhs);
1485 }
1486}
1487
1488impl Add<f64> for DVec2 {
1489 type Output = Self;
1490 #[inline]
1491 fn add(self, rhs: f64) -> Self {
1492 Self {
1493 x: self.x.add(rhs),
1494 y: self.y.add(rhs),
1495 }
1496 }
1497}
1498
1499impl Add<&f64> for DVec2 {
1500 type Output = Self;
1501 #[inline]
1502 fn add(self, rhs: &f64) -> Self {
1503 self.add(*rhs)
1504 }
1505}
1506
1507impl Add<&f64> for &DVec2 {
1508 type Output = DVec2;
1509 #[inline]
1510 fn add(self, rhs: &f64) -> DVec2 {
1511 (*self).add(*rhs)
1512 }
1513}
1514
1515impl Add<f64> for &DVec2 {
1516 type Output = DVec2;
1517 #[inline]
1518 fn add(self, rhs: f64) -> DVec2 {
1519 (*self).add(rhs)
1520 }
1521}
1522
1523impl AddAssign<f64> for DVec2 {
1524 #[inline]
1525 fn add_assign(&mut self, rhs: f64) {
1526 self.x.add_assign(rhs);
1527 self.y.add_assign(rhs);
1528 }
1529}
1530
1531impl AddAssign<&f64> for DVec2 {
1532 #[inline]
1533 fn add_assign(&mut self, rhs: &f64) {
1534 self.add_assign(*rhs);
1535 }
1536}
1537
1538impl Add<DVec2> for f64 {
1539 type Output = DVec2;
1540 #[inline]
1541 fn add(self, rhs: DVec2) -> DVec2 {
1542 DVec2 {
1543 x: self.add(rhs.x),
1544 y: self.add(rhs.y),
1545 }
1546 }
1547}
1548
1549impl Add<&DVec2> for f64 {
1550 type Output = DVec2;
1551 #[inline]
1552 fn add(self, rhs: &DVec2) -> DVec2 {
1553 self.add(*rhs)
1554 }
1555}
1556
1557impl Add<&DVec2> for &f64 {
1558 type Output = DVec2;
1559 #[inline]
1560 fn add(self, rhs: &DVec2) -> DVec2 {
1561 (*self).add(*rhs)
1562 }
1563}
1564
1565impl Add<DVec2> for &f64 {
1566 type Output = DVec2;
1567 #[inline]
1568 fn add(self, rhs: DVec2) -> DVec2 {
1569 (*self).add(rhs)
1570 }
1571}
1572
1573impl Sub for DVec2 {
1574 type Output = Self;
1575 #[inline]
1576 fn sub(self, rhs: Self) -> Self {
1577 Self {
1578 x: self.x.sub(rhs.x),
1579 y: self.y.sub(rhs.y),
1580 }
1581 }
1582}
1583
1584impl Sub<&Self> for DVec2 {
1585 type Output = Self;
1586 #[inline]
1587 fn sub(self, rhs: &Self) -> Self {
1588 self.sub(*rhs)
1589 }
1590}
1591
1592impl Sub<&DVec2> for &DVec2 {
1593 type Output = DVec2;
1594 #[inline]
1595 fn sub(self, rhs: &DVec2) -> DVec2 {
1596 (*self).sub(*rhs)
1597 }
1598}
1599
1600impl Sub<DVec2> for &DVec2 {
1601 type Output = DVec2;
1602 #[inline]
1603 fn sub(self, rhs: DVec2) -> DVec2 {
1604 (*self).sub(rhs)
1605 }
1606}
1607
1608impl SubAssign for DVec2 {
1609 #[inline]
1610 fn sub_assign(&mut self, rhs: Self) {
1611 self.x.sub_assign(rhs.x);
1612 self.y.sub_assign(rhs.y);
1613 }
1614}
1615
1616impl SubAssign<&Self> for DVec2 {
1617 #[inline]
1618 fn sub_assign(&mut self, rhs: &Self) {
1619 self.sub_assign(*rhs);
1620 }
1621}
1622
1623impl Sub<f64> for DVec2 {
1624 type Output = Self;
1625 #[inline]
1626 fn sub(self, rhs: f64) -> Self {
1627 Self {
1628 x: self.x.sub(rhs),
1629 y: self.y.sub(rhs),
1630 }
1631 }
1632}
1633
1634impl Sub<&f64> for DVec2 {
1635 type Output = Self;
1636 #[inline]
1637 fn sub(self, rhs: &f64) -> Self {
1638 self.sub(*rhs)
1639 }
1640}
1641
1642impl Sub<&f64> for &DVec2 {
1643 type Output = DVec2;
1644 #[inline]
1645 fn sub(self, rhs: &f64) -> DVec2 {
1646 (*self).sub(*rhs)
1647 }
1648}
1649
1650impl Sub<f64> for &DVec2 {
1651 type Output = DVec2;
1652 #[inline]
1653 fn sub(self, rhs: f64) -> DVec2 {
1654 (*self).sub(rhs)
1655 }
1656}
1657
1658impl SubAssign<f64> for DVec2 {
1659 #[inline]
1660 fn sub_assign(&mut self, rhs: f64) {
1661 self.x.sub_assign(rhs);
1662 self.y.sub_assign(rhs);
1663 }
1664}
1665
1666impl SubAssign<&f64> for DVec2 {
1667 #[inline]
1668 fn sub_assign(&mut self, rhs: &f64) {
1669 self.sub_assign(*rhs);
1670 }
1671}
1672
1673impl Sub<DVec2> for f64 {
1674 type Output = DVec2;
1675 #[inline]
1676 fn sub(self, rhs: DVec2) -> DVec2 {
1677 DVec2 {
1678 x: self.sub(rhs.x),
1679 y: self.sub(rhs.y),
1680 }
1681 }
1682}
1683
1684impl Sub<&DVec2> for f64 {
1685 type Output = DVec2;
1686 #[inline]
1687 fn sub(self, rhs: &DVec2) -> DVec2 {
1688 self.sub(*rhs)
1689 }
1690}
1691
1692impl Sub<&DVec2> for &f64 {
1693 type Output = DVec2;
1694 #[inline]
1695 fn sub(self, rhs: &DVec2) -> DVec2 {
1696 (*self).sub(*rhs)
1697 }
1698}
1699
1700impl Sub<DVec2> for &f64 {
1701 type Output = DVec2;
1702 #[inline]
1703 fn sub(self, rhs: DVec2) -> DVec2 {
1704 (*self).sub(rhs)
1705 }
1706}
1707
1708impl Rem for DVec2 {
1709 type Output = Self;
1710 #[inline]
1711 fn rem(self, rhs: Self) -> Self {
1712 Self {
1713 x: self.x.rem(rhs.x),
1714 y: self.y.rem(rhs.y),
1715 }
1716 }
1717}
1718
1719impl Rem<&Self> for DVec2 {
1720 type Output = Self;
1721 #[inline]
1722 fn rem(self, rhs: &Self) -> Self {
1723 self.rem(*rhs)
1724 }
1725}
1726
1727impl Rem<&DVec2> for &DVec2 {
1728 type Output = DVec2;
1729 #[inline]
1730 fn rem(self, rhs: &DVec2) -> DVec2 {
1731 (*self).rem(*rhs)
1732 }
1733}
1734
1735impl Rem<DVec2> for &DVec2 {
1736 type Output = DVec2;
1737 #[inline]
1738 fn rem(self, rhs: DVec2) -> DVec2 {
1739 (*self).rem(rhs)
1740 }
1741}
1742
1743impl RemAssign for DVec2 {
1744 #[inline]
1745 fn rem_assign(&mut self, rhs: Self) {
1746 self.x.rem_assign(rhs.x);
1747 self.y.rem_assign(rhs.y);
1748 }
1749}
1750
1751impl RemAssign<&Self> for DVec2 {
1752 #[inline]
1753 fn rem_assign(&mut self, rhs: &Self) {
1754 self.rem_assign(*rhs);
1755 }
1756}
1757
1758impl Rem<f64> for DVec2 {
1759 type Output = Self;
1760 #[inline]
1761 fn rem(self, rhs: f64) -> Self {
1762 Self {
1763 x: self.x.rem(rhs),
1764 y: self.y.rem(rhs),
1765 }
1766 }
1767}
1768
1769impl Rem<&f64> for DVec2 {
1770 type Output = Self;
1771 #[inline]
1772 fn rem(self, rhs: &f64) -> Self {
1773 self.rem(*rhs)
1774 }
1775}
1776
1777impl Rem<&f64> for &DVec2 {
1778 type Output = DVec2;
1779 #[inline]
1780 fn rem(self, rhs: &f64) -> DVec2 {
1781 (*self).rem(*rhs)
1782 }
1783}
1784
1785impl Rem<f64> for &DVec2 {
1786 type Output = DVec2;
1787 #[inline]
1788 fn rem(self, rhs: f64) -> DVec2 {
1789 (*self).rem(rhs)
1790 }
1791}
1792
1793impl RemAssign<f64> for DVec2 {
1794 #[inline]
1795 fn rem_assign(&mut self, rhs: f64) {
1796 self.x.rem_assign(rhs);
1797 self.y.rem_assign(rhs);
1798 }
1799}
1800
1801impl RemAssign<&f64> for DVec2 {
1802 #[inline]
1803 fn rem_assign(&mut self, rhs: &f64) {
1804 self.rem_assign(*rhs);
1805 }
1806}
1807
1808impl Rem<DVec2> for f64 {
1809 type Output = DVec2;
1810 #[inline]
1811 fn rem(self, rhs: DVec2) -> DVec2 {
1812 DVec2 {
1813 x: self.rem(rhs.x),
1814 y: self.rem(rhs.y),
1815 }
1816 }
1817}
1818
1819impl Rem<&DVec2> for f64 {
1820 type Output = DVec2;
1821 #[inline]
1822 fn rem(self, rhs: &DVec2) -> DVec2 {
1823 self.rem(*rhs)
1824 }
1825}
1826
1827impl Rem<&DVec2> for &f64 {
1828 type Output = DVec2;
1829 #[inline]
1830 fn rem(self, rhs: &DVec2) -> DVec2 {
1831 (*self).rem(*rhs)
1832 }
1833}
1834
1835impl Rem<DVec2> for &f64 {
1836 type Output = DVec2;
1837 #[inline]
1838 fn rem(self, rhs: DVec2) -> DVec2 {
1839 (*self).rem(rhs)
1840 }
1841}
1842
1843impl AsRef<[f64; 2]> for DVec2 {
1844 #[inline]
1845 fn as_ref(&self) -> &[f64; 2] {
1846 unsafe { &*(self as *const Self as *const [f64; 2]) }
1847 }
1848}
1849
1850impl AsMut<[f64; 2]> for DVec2 {
1851 #[inline]
1852 fn as_mut(&mut self) -> &mut [f64; 2] {
1853 unsafe { &mut *(self as *mut Self as *mut [f64; 2]) }
1854 }
1855}
1856
1857impl Sum for DVec2 {
1858 #[inline]
1859 fn sum<I>(iter: I) -> Self
1860 where
1861 I: Iterator<Item = Self>,
1862 {
1863 iter.fold(Self::ZERO, Self::add)
1864 }
1865}
1866
1867impl<'a> Sum<&'a Self> for DVec2 {
1868 #[inline]
1869 fn sum<I>(iter: I) -> Self
1870 where
1871 I: Iterator<Item = &'a Self>,
1872 {
1873 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1874 }
1875}
1876
1877impl Product for DVec2 {
1878 #[inline]
1879 fn product<I>(iter: I) -> Self
1880 where
1881 I: Iterator<Item = Self>,
1882 {
1883 iter.fold(Self::ONE, Self::mul)
1884 }
1885}
1886
1887impl<'a> Product<&'a Self> for DVec2 {
1888 #[inline]
1889 fn product<I>(iter: I) -> Self
1890 where
1891 I: Iterator<Item = &'a Self>,
1892 {
1893 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1894 }
1895}
1896
1897impl Neg for DVec2 {
1898 type Output = Self;
1899 #[inline]
1900 fn neg(self) -> Self {
1901 Self {
1902 x: self.x.neg(),
1903 y: self.y.neg(),
1904 }
1905 }
1906}
1907
1908impl Neg for &DVec2 {
1909 type Output = DVec2;
1910 #[inline]
1911 fn neg(self) -> DVec2 {
1912 (*self).neg()
1913 }
1914}
1915
1916impl Index<usize> for DVec2 {
1917 type Output = f64;
1918 #[inline]
1919 fn index(&self, index: usize) -> &Self::Output {
1920 match index {
1921 0 => &self.x,
1922 1 => &self.y,
1923 _ => panic!("index out of bounds"),
1924 }
1925 }
1926}
1927
1928impl IndexMut<usize> for DVec2 {
1929 #[inline]
1930 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1931 match index {
1932 0 => &mut self.x,
1933 1 => &mut self.y,
1934 _ => panic!("index out of bounds"),
1935 }
1936 }
1937}
1938
1939impl fmt::Display for DVec2 {
1940 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1941 if let Some(p) = f.precision() {
1942 write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y)
1943 } else {
1944 write!(f, "[{}, {}]", self.x, self.y)
1945 }
1946 }
1947}
1948
1949impl fmt::Debug for DVec2 {
1950 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1951 fmt.debug_tuple(stringify!(DVec2))
1952 .field(&self.x)
1953 .field(&self.y)
1954 .finish()
1955 }
1956}
1957
1958impl From<[f64; 2]> for DVec2 {
1959 #[inline]
1960 fn from(a: [f64; 2]) -> Self {
1961 Self::new(a[0], a[1])
1962 }
1963}
1964
1965impl From<DVec2> for [f64; 2] {
1966 #[inline]
1967 fn from(v: DVec2) -> Self {
1968 [v.x, v.y]
1969 }
1970}
1971
1972impl From<(f64, f64)> for DVec2 {
1973 #[inline]
1974 fn from(t: (f64, f64)) -> Self {
1975 Self::new(t.0, t.1)
1976 }
1977}
1978
1979impl From<DVec2> for (f64, f64) {
1980 #[inline]
1981 fn from(v: DVec2) -> Self {
1982 (v.x, v.y)
1983 }
1984}
1985
1986impl From<Vec2> for DVec2 {
1987 #[inline]
1988 fn from(v: Vec2) -> Self {
1989 Self::new(f64::from(v.x), f64::from(v.y))
1990 }
1991}
1992
1993impl From<IVec2> for DVec2 {
1994 #[inline]
1995 fn from(v: IVec2) -> Self {
1996 Self::new(f64::from(v.x), f64::from(v.y))
1997 }
1998}
1999
2000impl From<UVec2> for DVec2 {
2001 #[inline]
2002 fn from(v: UVec2) -> Self {
2003 Self::new(f64::from(v.x), f64::from(v.y))
2004 }
2005}
2006
2007impl From<BVec2> for DVec2 {
2008 #[inline]
2009 fn from(v: BVec2) -> Self {
2010 Self::new(f64::from(v.x), f64::from(v.y))
2011 }
2012}