1use crate::{f64::math, BVec2, DVec3};
4
5use crate::Vec2;
6
7#[cfg(feature = "i32")]
8use crate::IVec2;
9
10#[cfg(feature = "u32")]
11use crate::UVec2;
12
13use core::fmt;
14use core::iter::{Product, Sum};
15use core::{f32, ops::*};
16
17#[cfg(feature = "zerocopy")]
18use zerocopy_derive::*;
19
20#[inline(always)]
22#[must_use]
23pub const fn dvec2(x: f64, y: f64) -> DVec2 {
24 DVec2::new(x, y)
25}
26
27#[derive(Clone, Copy, PartialEq)]
29#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
30#[cfg_attr(
31 feature = "zerocopy",
32 derive(FromBytes, Immutable, IntoBytes, KnownLayout)
33)]
34#[cfg_attr(feature = "cuda", repr(align(16)))]
35#[repr(C)]
36#[cfg_attr(target_arch = "spirv", rust_gpu::vector::v1)]
37pub struct DVec2 {
38 pub x: f64,
39 pub y: f64,
40}
41
42impl DVec2 {
43 pub const ZERO: Self = Self::splat(0.0);
45
46 pub const ONE: Self = Self::splat(1.0);
48
49 pub const NEG_ONE: Self = Self::splat(-1.0);
51
52 pub const MIN: Self = Self::splat(f64::MIN);
54
55 pub const MAX: Self = Self::splat(f64::MAX);
57
58 pub const NAN: Self = Self::splat(f64::NAN);
60
61 pub const INFINITY: Self = Self::splat(f64::INFINITY);
63
64 pub const NEG_INFINITY: Self = Self::splat(f64::NEG_INFINITY);
66
67 pub const X: Self = Self::new(1.0, 0.0);
69
70 pub const Y: Self = Self::new(0.0, 1.0);
72
73 pub const NEG_X: Self = Self::new(-1.0, 0.0);
75
76 pub const NEG_Y: Self = Self::new(0.0, -1.0);
78
79 pub const AXES: [Self; 2] = [Self::X, Self::Y];
81
82 pub const USES_CORE_SIMD: bool = false;
84 pub const USES_NEON: bool = false;
86 pub const USES_SCALAR_MATH: bool = true;
88 pub const USES_SSE2: bool = false;
90 pub const USES_WASM_SIMD: bool = false;
92 #[deprecated(since = "0.31.0", note = "Renamed to USES_WASM_SIMD")]
93 pub const USES_WASM32_SIMD: bool = false;
94
95 #[inline(always)]
97 #[must_use]
98 pub const fn new(x: f64, y: f64) -> Self {
99 Self { x, y }
100 }
101
102 #[inline]
104 #[must_use]
105 pub const fn splat(v: f64) -> Self {
106 Self { x: v, y: v }
107 }
108
109 #[inline]
111 #[must_use]
112 pub fn map<F>(self, mut f: F) -> Self
113 where
114 F: FnMut(f64) -> f64,
115 {
116 Self::new(f(self.x), f(self.y))
117 }
118
119 #[inline]
125 #[must_use]
126 pub fn select(mask: BVec2, if_true: Self, if_false: Self) -> Self {
127 Self {
128 x: if mask.test(0) { if_true.x } else { if_false.x },
129 y: if mask.test(1) { if_true.y } else { if_false.y },
130 }
131 }
132
133 #[inline]
135 #[must_use]
136 pub const fn from_array(a: [f64; 2]) -> Self {
137 Self::new(a[0], a[1])
138 }
139
140 #[inline]
142 #[must_use]
143 pub const fn to_array(&self) -> [f64; 2] {
144 [self.x, self.y]
145 }
146
147 #[inline]
153 #[must_use]
154 pub const fn from_slice(slice: &[f64]) -> Self {
155 assert!(slice.len() >= 2);
156 Self::new(slice[0], slice[1])
157 }
158
159 #[inline]
165 pub fn write_to_slice(self, slice: &mut [f64]) {
166 slice[..2].copy_from_slice(&self.to_array());
167 }
168
169 #[inline]
171 #[must_use]
172 pub const fn extend(self, z: f64) -> DVec3 {
173 DVec3::new(self.x, self.y, z)
174 }
175
176 #[inline]
178 #[must_use]
179 pub fn with_x(mut self, x: f64) -> Self {
180 self.x = x;
181 self
182 }
183
184 #[inline]
186 #[must_use]
187 pub fn with_y(mut self, y: f64) -> Self {
188 self.y = y;
189 self
190 }
191
192 #[inline]
194 #[must_use]
195 pub fn dot(self, rhs: Self) -> f64 {
196 (self.x * rhs.x) + (self.y * rhs.y)
197 }
198
199 #[inline]
201 #[must_use]
202 pub fn dot_into_vec(self, rhs: Self) -> Self {
203 Self::splat(self.dot(rhs))
204 }
205
206 #[inline]
213 #[must_use]
214 pub fn min(self, rhs: Self) -> Self {
215 Self {
216 x: if self.x < rhs.x { self.x } else { rhs.x },
217 y: if self.y < rhs.y { self.y } else { rhs.y },
218 }
219 }
220
221 #[inline]
228 #[must_use]
229 pub fn max(self, rhs: Self) -> Self {
230 Self {
231 x: if self.x > rhs.x { self.x } else { rhs.x },
232 y: if self.y > rhs.y { self.y } else { rhs.y },
233 }
234 }
235
236 #[inline]
247 #[must_use]
248 pub fn clamp(self, min: Self, max: Self) -> Self {
249 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
250 self.max(min).min(max)
251 }
252
253 #[inline]
260 #[must_use]
261 pub fn min_element(self) -> f64 {
262 let min = |a, b| if a < b { a } else { b };
263 min(self.x, self.y)
264 }
265
266 #[inline]
273 #[must_use]
274 pub fn max_element(self) -> f64 {
275 let max = |a, b| if a > b { a } else { b };
276 max(self.x, self.y)
277 }
278
279 #[doc(alias = "argmin")]
281 #[inline]
282 #[must_use]
283 pub fn min_position(self) -> usize {
284 if self.x <= self.y {
285 0
286 } else {
287 1
288 }
289 }
290
291 #[doc(alias = "argmax")]
293 #[inline]
294 #[must_use]
295 pub fn max_position(self) -> usize {
296 if self.x >= self.y {
297 0
298 } else {
299 1
300 }
301 }
302
303 #[inline]
307 #[must_use]
308 pub fn element_sum(self) -> f64 {
309 self.x + self.y
310 }
311
312 #[inline]
316 #[must_use]
317 pub fn element_product(self) -> f64 {
318 self.x * self.y
319 }
320
321 #[inline]
327 #[must_use]
328 pub fn cmpeq(self, rhs: Self) -> BVec2 {
329 BVec2::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y))
330 }
331
332 #[inline]
338 #[must_use]
339 pub fn cmpne(self, rhs: Self) -> BVec2 {
340 BVec2::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y))
341 }
342
343 #[inline]
349 #[must_use]
350 pub fn cmpge(self, rhs: Self) -> BVec2 {
351 BVec2::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y))
352 }
353
354 #[inline]
360 #[must_use]
361 pub fn cmpgt(self, rhs: Self) -> BVec2 {
362 BVec2::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y))
363 }
364
365 #[inline]
371 #[must_use]
372 pub fn cmple(self, rhs: Self) -> BVec2 {
373 BVec2::new(self.x.le(&rhs.x), self.y.le(&rhs.y))
374 }
375
376 #[inline]
382 #[must_use]
383 pub fn cmplt(self, rhs: Self) -> BVec2 {
384 BVec2::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y))
385 }
386
387 #[inline]
389 #[must_use]
390 pub fn abs(self) -> Self {
391 Self {
392 x: math::abs(self.x),
393 y: math::abs(self.y),
394 }
395 }
396
397 #[inline]
403 #[must_use]
404 pub fn signum(self) -> Self {
405 Self {
406 x: math::signum(self.x),
407 y: math::signum(self.y),
408 }
409 }
410
411 #[inline]
413 #[must_use]
414 pub fn copysign(self, rhs: Self) -> Self {
415 Self {
416 x: math::copysign(self.x, rhs.x),
417 y: math::copysign(self.y, rhs.y),
418 }
419 }
420
421 #[inline]
429 #[must_use]
430 pub fn is_negative_bitmask(self) -> u32 {
431 (self.x.is_sign_negative() as u32) | ((self.y.is_sign_negative() as u32) << 1)
432 }
433
434 #[inline]
439 #[must_use]
440 pub fn is_negative_mask(self) -> BVec2 {
441 BVec2::new(self.x.is_sign_negative(), self.y.is_sign_negative())
442 }
443
444 #[inline]
447 #[must_use]
448 pub fn is_finite(self) -> bool {
449 self.x.is_finite() && self.y.is_finite()
450 }
451
452 #[inline]
456 #[must_use]
457 pub fn is_finite_mask(self) -> BVec2 {
458 BVec2::new(self.x.is_finite(), self.y.is_finite())
459 }
460
461 #[inline]
463 #[must_use]
464 pub fn is_nan(self) -> bool {
465 self.x.is_nan() || self.y.is_nan()
466 }
467
468 #[inline]
472 #[must_use]
473 pub fn is_nan_mask(self) -> BVec2 {
474 BVec2::new(self.x.is_nan(), self.y.is_nan())
475 }
476
477 #[doc(alias = "magnitude")]
479 #[inline]
480 #[must_use]
481 pub fn length(self) -> f64 {
482 math::sqrt(self.dot(self))
483 }
484
485 #[doc(alias = "magnitude2")]
489 #[inline]
490 #[must_use]
491 pub fn length_squared(self) -> f64 {
492 self.dot(self)
493 }
494
495 #[inline]
499 #[must_use]
500 pub fn length_recip(self) -> f64 {
501 self.length().recip()
502 }
503
504 #[inline]
506 #[must_use]
507 pub fn distance(self, rhs: Self) -> f64 {
508 (self - rhs).length()
509 }
510
511 #[inline]
513 #[must_use]
514 pub fn distance_squared(self, rhs: Self) -> f64 {
515 (self - rhs).length_squared()
516 }
517
518 #[inline]
520 #[must_use]
521 pub fn div_euclid(self, rhs: Self) -> Self {
522 Self::new(
523 math::div_euclid(self.x, rhs.x),
524 math::div_euclid(self.y, rhs.y),
525 )
526 }
527
528 #[inline]
532 #[must_use]
533 pub fn rem_euclid(self, rhs: Self) -> Self {
534 Self::new(
535 math::rem_euclid(self.x, rhs.x),
536 math::rem_euclid(self.y, rhs.y),
537 )
538 }
539
540 #[inline]
550 #[must_use]
551 pub fn normalize(self) -> Self {
552 #[allow(clippy::let_and_return)]
553 let normalized = self.mul(self.length_recip());
554 glam_assert!(normalized.is_finite());
555 normalized
556 }
557
558 #[inline]
565 #[must_use]
566 pub fn try_normalize(self) -> Option<Self> {
567 let rcp = self.length_recip();
568 if rcp.is_finite() && rcp > 0.0 {
569 Some(self * rcp)
570 } else {
571 None
572 }
573 }
574
575 #[inline]
583 #[must_use]
584 pub fn normalize_or(self, fallback: Self) -> Self {
585 let rcp = self.length_recip();
586 if rcp.is_finite() && rcp > 0.0 {
587 self * rcp
588 } else {
589 fallback
590 }
591 }
592
593 #[inline]
600 #[must_use]
601 pub fn normalize_or_zero(self) -> Self {
602 self.normalize_or(Self::ZERO)
603 }
604
605 #[inline]
609 #[must_use]
610 pub fn normalize_and_length(self) -> (Self, f64) {
611 let length = self.length();
612 let rcp = 1.0 / length;
613 if rcp.is_finite() && rcp > 0.0 {
614 (self * rcp, length)
615 } else {
616 (Self::X, 0.0)
617 }
618 }
619
620 #[inline]
624 #[must_use]
625 pub fn is_normalized(self) -> bool {
626 math::abs(self.length_squared() - 1.0) <= 2e-4
627 }
628
629 #[inline]
637 #[must_use]
638 pub fn project_onto(self, rhs: Self) -> Self {
639 let other_len_sq_rcp = rhs.dot(rhs).recip();
640 glam_assert!(other_len_sq_rcp.is_finite());
641 rhs * self.dot(rhs) * other_len_sq_rcp
642 }
643
644 #[doc(alias("plane"))]
655 #[inline]
656 #[must_use]
657 pub fn reject_from(self, rhs: Self) -> Self {
658 self - self.project_onto(rhs)
659 }
660
661 #[inline]
669 #[must_use]
670 pub fn project_onto_normalized(self, rhs: Self) -> Self {
671 glam_assert!(rhs.is_normalized());
672 rhs * self.dot(rhs)
673 }
674
675 #[doc(alias("plane"))]
686 #[inline]
687 #[must_use]
688 pub fn reject_from_normalized(self, rhs: Self) -> Self {
689 self - self.project_onto_normalized(rhs)
690 }
691
692 #[inline]
695 #[must_use]
696 pub fn round(self) -> Self {
697 Self {
698 x: math::round(self.x),
699 y: math::round(self.y),
700 }
701 }
702
703 #[inline]
706 #[must_use]
707 pub fn floor(self) -> Self {
708 Self {
709 x: math::floor(self.x),
710 y: math::floor(self.y),
711 }
712 }
713
714 #[inline]
717 #[must_use]
718 pub fn ceil(self) -> Self {
719 Self {
720 x: math::ceil(self.x),
721 y: math::ceil(self.y),
722 }
723 }
724
725 #[inline]
728 #[must_use]
729 pub fn trunc(self) -> Self {
730 Self {
731 x: math::trunc(self.x),
732 y: math::trunc(self.y),
733 }
734 }
735
736 #[inline]
740 #[must_use]
741 pub fn step(self, rhs: Self) -> Self {
742 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
743 }
744
745 #[inline]
747 #[must_use]
748 pub fn saturate(self) -> Self {
749 self.clamp(Self::ZERO, Self::ONE)
750 }
751
752 #[inline]
759 #[must_use]
760 pub fn fract(self) -> Self {
761 self - self.trunc()
762 }
763
764 #[inline]
771 #[must_use]
772 pub fn fract_gl(self) -> Self {
773 self - self.floor()
774 }
775
776 #[inline]
779 #[must_use]
780 pub fn exp(self) -> Self {
781 Self::new(math::exp(self.x), math::exp(self.y))
782 }
783
784 #[inline]
786 #[must_use]
787 pub fn exp2(self) -> Self {
788 Self::new(math::exp2(self.x), math::exp2(self.y))
789 }
790
791 #[inline]
794 #[must_use]
795 pub fn ln(self) -> Self {
796 Self::new(math::ln(self.x), math::ln(self.y))
797 }
798
799 #[inline]
802 #[must_use]
803 pub fn log2(self) -> Self {
804 Self::new(math::log2(self.x), math::log2(self.y))
805 }
806
807 #[inline]
809 #[must_use]
810 pub fn powf(self, n: f64) -> Self {
811 Self::new(math::powf(self.x, n), math::powf(self.y, n))
812 }
813
814 #[inline]
817 #[must_use]
818 pub fn sqrt(self) -> Self {
819 Self::new(math::sqrt(self.x), math::sqrt(self.y))
820 }
821
822 #[inline]
824 #[must_use]
825 pub fn cos(self) -> Self {
826 Self::new(math::cos(self.x), math::cos(self.y))
827 }
828
829 #[inline]
831 #[must_use]
832 pub fn sin(self) -> Self {
833 Self::new(math::sin(self.x), math::sin(self.y))
834 }
835
836 #[inline]
838 #[must_use]
839 pub fn sin_cos(self) -> (Self, Self) {
840 let (sin_x, cos_x) = math::sin_cos(self.x);
841 let (sin_y, cos_y) = math::sin_cos(self.y);
842
843 (Self::new(sin_x, sin_y), Self::new(cos_x, cos_y))
844 }
845
846 #[inline]
848 #[must_use]
849 pub fn recip(self) -> Self {
850 Self {
851 x: 1.0 / self.x,
852 y: 1.0 / self.y,
853 }
854 }
855
856 #[doc(alias = "mix")]
862 #[inline]
863 #[must_use]
864 pub fn lerp(self, rhs: Self, s: f64) -> Self {
865 self * (1.0 - s) + rhs * s
866 }
867
868 #[inline]
873 #[must_use]
874 pub fn move_towards(self, rhs: Self, d: f64) -> Self {
875 let a = rhs - self;
876 let len = a.length();
877 if len <= d || len <= 1e-4 {
878 return rhs;
879 }
880 self + a / len * d
881 }
882
883 #[inline]
889 pub fn midpoint(self, rhs: Self) -> Self {
890 (self + rhs) * 0.5
891 }
892
893 #[inline]
903 #[must_use]
904 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
905 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
906 }
907
908 #[inline]
914 #[must_use]
915 pub fn clamp_length(self, min: f64, max: f64) -> Self {
916 glam_assert!(0.0 <= min);
917 glam_assert!(min <= max);
918 let length_sq = self.length_squared();
919 if length_sq < min * min {
920 min * (self / math::sqrt(length_sq))
921 } else if length_sq > max * max {
922 max * (self / math::sqrt(length_sq))
923 } else {
924 self
925 }
926 }
927
928 #[inline]
934 #[must_use]
935 pub fn clamp_length_max(self, max: f64) -> Self {
936 glam_assert!(0.0 <= max);
937 let length_sq = self.length_squared();
938 if length_sq > max * max {
939 max * (self / math::sqrt(length_sq))
940 } else {
941 self
942 }
943 }
944
945 #[inline]
951 #[must_use]
952 pub fn clamp_length_min(self, min: f64) -> Self {
953 glam_assert!(0.0 <= min);
954 let length_sq = self.length_squared();
955 if length_sq < min * min {
956 min * (self / math::sqrt(length_sq))
957 } else {
958 self
959 }
960 }
961
962 #[inline]
970 #[must_use]
971 pub fn mul_add(self, a: Self, b: Self) -> Self {
972 Self::new(
973 math::mul_add(self.x, a.x, b.x),
974 math::mul_add(self.y, a.y, b.y),
975 )
976 }
977
978 #[inline]
987 #[must_use]
988 pub fn reflect(self, normal: Self) -> Self {
989 glam_assert!(normal.is_normalized());
990 self - 2.0 * self.dot(normal) * normal
991 }
992
993 #[inline]
1003 #[must_use]
1004 pub fn refract(self, normal: Self, eta: f64) -> Self {
1005 glam_assert!(self.is_normalized());
1006 glam_assert!(normal.is_normalized());
1007 let n_dot_i = normal.dot(self);
1008 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1009 if k >= 0.0 {
1010 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1011 } else {
1012 Self::ZERO
1013 }
1014 }
1015
1016 #[inline]
1021 #[must_use]
1022 pub fn from_angle(angle: f64) -> Self {
1023 let (sin, cos) = math::sin_cos(angle);
1024 Self { x: cos, y: sin }
1025 }
1026
1027 #[inline]
1031 #[must_use]
1032 pub fn to_angle(self) -> f64 {
1033 math::atan2(self.y, self.x)
1034 }
1035
1036 #[inline]
1040 #[must_use]
1041 pub fn angle_to(self, rhs: Self) -> f64 {
1042 let angle = math::acos_approx(
1043 self.dot(rhs) / math::sqrt(self.length_squared() * rhs.length_squared()),
1044 );
1045
1046 angle * math::signum(self.perp_dot(rhs))
1047 }
1048
1049 #[inline]
1051 #[must_use]
1052 pub fn perp(self) -> Self {
1053 Self {
1054 x: -self.y,
1055 y: self.x,
1056 }
1057 }
1058
1059 #[doc(alias = "wedge")]
1062 #[doc(alias = "cross")]
1063 #[doc(alias = "determinant")]
1064 #[inline]
1065 #[must_use]
1066 pub fn perp_dot(self, rhs: Self) -> f64 {
1067 (self.x * rhs.y) - (self.y * rhs.x)
1068 }
1069
1070 #[inline]
1078 #[must_use]
1079 pub fn rotate(self, rhs: Self) -> Self {
1080 Self {
1081 x: self.x * rhs.x - self.y * rhs.y,
1082 y: self.y * rhs.x + self.x * rhs.y,
1083 }
1084 }
1085
1086 #[inline]
1092 #[must_use]
1093 pub fn rotate_towards(self, rhs: Self, max_angle: f64) -> Self {
1094 let a = self.angle_to(rhs);
1095 let abs_a = math::abs(a);
1096 let angle = max_angle.clamp(abs_a - core::f64::consts::PI, abs_a) * math::signum(a);
1098 Self::from_angle(angle).rotate(self)
1099 }
1100
1101 #[inline]
1103 #[must_use]
1104 pub fn as_vec2(self) -> crate::Vec2 {
1105 crate::Vec2::new(self.x as f32, self.y as f32)
1106 }
1107
1108 #[cfg(feature = "i8")]
1110 #[inline]
1111 #[must_use]
1112 pub fn as_i8vec2(self) -> crate::I8Vec2 {
1113 crate::I8Vec2::new(self.x as i8, self.y as i8)
1114 }
1115
1116 #[cfg(feature = "u8")]
1118 #[inline]
1119 #[must_use]
1120 pub fn as_u8vec2(self) -> crate::U8Vec2 {
1121 crate::U8Vec2::new(self.x as u8, self.y as u8)
1122 }
1123
1124 #[cfg(feature = "i16")]
1126 #[inline]
1127 #[must_use]
1128 pub fn as_i16vec2(self) -> crate::I16Vec2 {
1129 crate::I16Vec2::new(self.x as i16, self.y as i16)
1130 }
1131
1132 #[cfg(feature = "u16")]
1134 #[inline]
1135 #[must_use]
1136 pub fn as_u16vec2(self) -> crate::U16Vec2 {
1137 crate::U16Vec2::new(self.x as u16, self.y as u16)
1138 }
1139
1140 #[cfg(feature = "i32")]
1142 #[inline]
1143 #[must_use]
1144 pub fn as_ivec2(self) -> crate::IVec2 {
1145 crate::IVec2::new(self.x as i32, self.y as i32)
1146 }
1147
1148 #[cfg(feature = "u32")]
1150 #[inline]
1151 #[must_use]
1152 pub fn as_uvec2(self) -> crate::UVec2 {
1153 crate::UVec2::new(self.x as u32, self.y as u32)
1154 }
1155
1156 #[cfg(feature = "i64")]
1158 #[inline]
1159 #[must_use]
1160 pub fn as_i64vec2(self) -> crate::I64Vec2 {
1161 crate::I64Vec2::new(self.x as i64, self.y as i64)
1162 }
1163
1164 #[cfg(feature = "u64")]
1166 #[inline]
1167 #[must_use]
1168 pub fn as_u64vec2(self) -> crate::U64Vec2 {
1169 crate::U64Vec2::new(self.x as u64, self.y as u64)
1170 }
1171
1172 #[cfg(feature = "isize")]
1174 #[inline]
1175 #[must_use]
1176 pub fn as_isizevec2(self) -> crate::ISizeVec2 {
1177 crate::ISizeVec2::new(self.x as isize, self.y as isize)
1178 }
1179
1180 #[cfg(feature = "usize")]
1182 #[inline]
1183 #[must_use]
1184 pub fn as_usizevec2(self) -> crate::USizeVec2 {
1185 crate::USizeVec2::new(self.x as usize, self.y as usize)
1186 }
1187}
1188
1189impl Default for DVec2 {
1190 #[inline(always)]
1191 fn default() -> Self {
1192 Self::ZERO
1193 }
1194}
1195
1196impl Div for DVec2 {
1197 type Output = Self;
1198 #[inline]
1199 fn div(self, rhs: Self) -> Self {
1200 Self {
1201 x: self.x.div(rhs.x),
1202 y: self.y.div(rhs.y),
1203 }
1204 }
1205}
1206
1207impl Div<&Self> for DVec2 {
1208 type Output = Self;
1209 #[inline]
1210 fn div(self, rhs: &Self) -> Self {
1211 self.div(*rhs)
1212 }
1213}
1214
1215impl Div<&DVec2> for &DVec2 {
1216 type Output = DVec2;
1217 #[inline]
1218 fn div(self, rhs: &DVec2) -> DVec2 {
1219 (*self).div(*rhs)
1220 }
1221}
1222
1223impl Div<DVec2> for &DVec2 {
1224 type Output = DVec2;
1225 #[inline]
1226 fn div(self, rhs: DVec2) -> DVec2 {
1227 (*self).div(rhs)
1228 }
1229}
1230
1231impl DivAssign for DVec2 {
1232 #[inline]
1233 fn div_assign(&mut self, rhs: Self) {
1234 self.x.div_assign(rhs.x);
1235 self.y.div_assign(rhs.y);
1236 }
1237}
1238
1239impl DivAssign<&Self> for DVec2 {
1240 #[inline]
1241 fn div_assign(&mut self, rhs: &Self) {
1242 self.div_assign(*rhs);
1243 }
1244}
1245
1246impl Div<f64> for DVec2 {
1247 type Output = Self;
1248 #[inline]
1249 fn div(self, rhs: f64) -> Self {
1250 Self {
1251 x: self.x.div(rhs),
1252 y: self.y.div(rhs),
1253 }
1254 }
1255}
1256
1257impl Div<&f64> for DVec2 {
1258 type Output = Self;
1259 #[inline]
1260 fn div(self, rhs: &f64) -> Self {
1261 self.div(*rhs)
1262 }
1263}
1264
1265impl Div<&f64> for &DVec2 {
1266 type Output = DVec2;
1267 #[inline]
1268 fn div(self, rhs: &f64) -> DVec2 {
1269 (*self).div(*rhs)
1270 }
1271}
1272
1273impl Div<f64> for &DVec2 {
1274 type Output = DVec2;
1275 #[inline]
1276 fn div(self, rhs: f64) -> DVec2 {
1277 (*self).div(rhs)
1278 }
1279}
1280
1281impl DivAssign<f64> for DVec2 {
1282 #[inline]
1283 fn div_assign(&mut self, rhs: f64) {
1284 self.x.div_assign(rhs);
1285 self.y.div_assign(rhs);
1286 }
1287}
1288
1289impl DivAssign<&f64> for DVec2 {
1290 #[inline]
1291 fn div_assign(&mut self, rhs: &f64) {
1292 self.div_assign(*rhs);
1293 }
1294}
1295
1296impl Div<DVec2> for f64 {
1297 type Output = DVec2;
1298 #[inline]
1299 fn div(self, rhs: DVec2) -> DVec2 {
1300 DVec2 {
1301 x: self.div(rhs.x),
1302 y: self.div(rhs.y),
1303 }
1304 }
1305}
1306
1307impl Div<&DVec2> for f64 {
1308 type Output = DVec2;
1309 #[inline]
1310 fn div(self, rhs: &DVec2) -> DVec2 {
1311 self.div(*rhs)
1312 }
1313}
1314
1315impl Div<&DVec2> for &f64 {
1316 type Output = DVec2;
1317 #[inline]
1318 fn div(self, rhs: &DVec2) -> DVec2 {
1319 (*self).div(*rhs)
1320 }
1321}
1322
1323impl Div<DVec2> for &f64 {
1324 type Output = DVec2;
1325 #[inline]
1326 fn div(self, rhs: DVec2) -> DVec2 {
1327 (*self).div(rhs)
1328 }
1329}
1330
1331impl Mul for DVec2 {
1332 type Output = Self;
1333 #[inline]
1334 fn mul(self, rhs: Self) -> Self {
1335 Self {
1336 x: self.x.mul(rhs.x),
1337 y: self.y.mul(rhs.y),
1338 }
1339 }
1340}
1341
1342impl Mul<&Self> for DVec2 {
1343 type Output = Self;
1344 #[inline]
1345 fn mul(self, rhs: &Self) -> Self {
1346 self.mul(*rhs)
1347 }
1348}
1349
1350impl Mul<&DVec2> for &DVec2 {
1351 type Output = DVec2;
1352 #[inline]
1353 fn mul(self, rhs: &DVec2) -> DVec2 {
1354 (*self).mul(*rhs)
1355 }
1356}
1357
1358impl Mul<DVec2> for &DVec2 {
1359 type Output = DVec2;
1360 #[inline]
1361 fn mul(self, rhs: DVec2) -> DVec2 {
1362 (*self).mul(rhs)
1363 }
1364}
1365
1366impl MulAssign for DVec2 {
1367 #[inline]
1368 fn mul_assign(&mut self, rhs: Self) {
1369 self.x.mul_assign(rhs.x);
1370 self.y.mul_assign(rhs.y);
1371 }
1372}
1373
1374impl MulAssign<&Self> for DVec2 {
1375 #[inline]
1376 fn mul_assign(&mut self, rhs: &Self) {
1377 self.mul_assign(*rhs);
1378 }
1379}
1380
1381impl Mul<f64> for DVec2 {
1382 type Output = Self;
1383 #[inline]
1384 fn mul(self, rhs: f64) -> Self {
1385 Self {
1386 x: self.x.mul(rhs),
1387 y: self.y.mul(rhs),
1388 }
1389 }
1390}
1391
1392impl Mul<&f64> for DVec2 {
1393 type Output = Self;
1394 #[inline]
1395 fn mul(self, rhs: &f64) -> Self {
1396 self.mul(*rhs)
1397 }
1398}
1399
1400impl Mul<&f64> for &DVec2 {
1401 type Output = DVec2;
1402 #[inline]
1403 fn mul(self, rhs: &f64) -> DVec2 {
1404 (*self).mul(*rhs)
1405 }
1406}
1407
1408impl Mul<f64> for &DVec2 {
1409 type Output = DVec2;
1410 #[inline]
1411 fn mul(self, rhs: f64) -> DVec2 {
1412 (*self).mul(rhs)
1413 }
1414}
1415
1416impl MulAssign<f64> for DVec2 {
1417 #[inline]
1418 fn mul_assign(&mut self, rhs: f64) {
1419 self.x.mul_assign(rhs);
1420 self.y.mul_assign(rhs);
1421 }
1422}
1423
1424impl MulAssign<&f64> for DVec2 {
1425 #[inline]
1426 fn mul_assign(&mut self, rhs: &f64) {
1427 self.mul_assign(*rhs);
1428 }
1429}
1430
1431impl Mul<DVec2> for f64 {
1432 type Output = DVec2;
1433 #[inline]
1434 fn mul(self, rhs: DVec2) -> DVec2 {
1435 DVec2 {
1436 x: self.mul(rhs.x),
1437 y: self.mul(rhs.y),
1438 }
1439 }
1440}
1441
1442impl Mul<&DVec2> for f64 {
1443 type Output = DVec2;
1444 #[inline]
1445 fn mul(self, rhs: &DVec2) -> DVec2 {
1446 self.mul(*rhs)
1447 }
1448}
1449
1450impl Mul<&DVec2> for &f64 {
1451 type Output = DVec2;
1452 #[inline]
1453 fn mul(self, rhs: &DVec2) -> DVec2 {
1454 (*self).mul(*rhs)
1455 }
1456}
1457
1458impl Mul<DVec2> for &f64 {
1459 type Output = DVec2;
1460 #[inline]
1461 fn mul(self, rhs: DVec2) -> DVec2 {
1462 (*self).mul(rhs)
1463 }
1464}
1465
1466impl Add for DVec2 {
1467 type Output = Self;
1468 #[inline]
1469 fn add(self, rhs: Self) -> Self {
1470 Self {
1471 x: self.x.add(rhs.x),
1472 y: self.y.add(rhs.y),
1473 }
1474 }
1475}
1476
1477impl Add<&Self> for DVec2 {
1478 type Output = Self;
1479 #[inline]
1480 fn add(self, rhs: &Self) -> Self {
1481 self.add(*rhs)
1482 }
1483}
1484
1485impl Add<&DVec2> for &DVec2 {
1486 type Output = DVec2;
1487 #[inline]
1488 fn add(self, rhs: &DVec2) -> DVec2 {
1489 (*self).add(*rhs)
1490 }
1491}
1492
1493impl Add<DVec2> for &DVec2 {
1494 type Output = DVec2;
1495 #[inline]
1496 fn add(self, rhs: DVec2) -> DVec2 {
1497 (*self).add(rhs)
1498 }
1499}
1500
1501impl AddAssign for DVec2 {
1502 #[inline]
1503 fn add_assign(&mut self, rhs: Self) {
1504 self.x.add_assign(rhs.x);
1505 self.y.add_assign(rhs.y);
1506 }
1507}
1508
1509impl AddAssign<&Self> for DVec2 {
1510 #[inline]
1511 fn add_assign(&mut self, rhs: &Self) {
1512 self.add_assign(*rhs);
1513 }
1514}
1515
1516impl Add<f64> for DVec2 {
1517 type Output = Self;
1518 #[inline]
1519 fn add(self, rhs: f64) -> Self {
1520 Self {
1521 x: self.x.add(rhs),
1522 y: self.y.add(rhs),
1523 }
1524 }
1525}
1526
1527impl Add<&f64> for DVec2 {
1528 type Output = Self;
1529 #[inline]
1530 fn add(self, rhs: &f64) -> Self {
1531 self.add(*rhs)
1532 }
1533}
1534
1535impl Add<&f64> for &DVec2 {
1536 type Output = DVec2;
1537 #[inline]
1538 fn add(self, rhs: &f64) -> DVec2 {
1539 (*self).add(*rhs)
1540 }
1541}
1542
1543impl Add<f64> for &DVec2 {
1544 type Output = DVec2;
1545 #[inline]
1546 fn add(self, rhs: f64) -> DVec2 {
1547 (*self).add(rhs)
1548 }
1549}
1550
1551impl AddAssign<f64> for DVec2 {
1552 #[inline]
1553 fn add_assign(&mut self, rhs: f64) {
1554 self.x.add_assign(rhs);
1555 self.y.add_assign(rhs);
1556 }
1557}
1558
1559impl AddAssign<&f64> for DVec2 {
1560 #[inline]
1561 fn add_assign(&mut self, rhs: &f64) {
1562 self.add_assign(*rhs);
1563 }
1564}
1565
1566impl Add<DVec2> for f64 {
1567 type Output = DVec2;
1568 #[inline]
1569 fn add(self, rhs: DVec2) -> DVec2 {
1570 DVec2 {
1571 x: self.add(rhs.x),
1572 y: self.add(rhs.y),
1573 }
1574 }
1575}
1576
1577impl Add<&DVec2> for f64 {
1578 type Output = DVec2;
1579 #[inline]
1580 fn add(self, rhs: &DVec2) -> DVec2 {
1581 self.add(*rhs)
1582 }
1583}
1584
1585impl Add<&DVec2> for &f64 {
1586 type Output = DVec2;
1587 #[inline]
1588 fn add(self, rhs: &DVec2) -> DVec2 {
1589 (*self).add(*rhs)
1590 }
1591}
1592
1593impl Add<DVec2> for &f64 {
1594 type Output = DVec2;
1595 #[inline]
1596 fn add(self, rhs: DVec2) -> DVec2 {
1597 (*self).add(rhs)
1598 }
1599}
1600
1601impl Sub for DVec2 {
1602 type Output = Self;
1603 #[inline]
1604 fn sub(self, rhs: Self) -> Self {
1605 Self {
1606 x: self.x.sub(rhs.x),
1607 y: self.y.sub(rhs.y),
1608 }
1609 }
1610}
1611
1612impl Sub<&Self> for DVec2 {
1613 type Output = Self;
1614 #[inline]
1615 fn sub(self, rhs: &Self) -> Self {
1616 self.sub(*rhs)
1617 }
1618}
1619
1620impl Sub<&DVec2> for &DVec2 {
1621 type Output = DVec2;
1622 #[inline]
1623 fn sub(self, rhs: &DVec2) -> DVec2 {
1624 (*self).sub(*rhs)
1625 }
1626}
1627
1628impl Sub<DVec2> for &DVec2 {
1629 type Output = DVec2;
1630 #[inline]
1631 fn sub(self, rhs: DVec2) -> DVec2 {
1632 (*self).sub(rhs)
1633 }
1634}
1635
1636impl SubAssign for DVec2 {
1637 #[inline]
1638 fn sub_assign(&mut self, rhs: Self) {
1639 self.x.sub_assign(rhs.x);
1640 self.y.sub_assign(rhs.y);
1641 }
1642}
1643
1644impl SubAssign<&Self> for DVec2 {
1645 #[inline]
1646 fn sub_assign(&mut self, rhs: &Self) {
1647 self.sub_assign(*rhs);
1648 }
1649}
1650
1651impl Sub<f64> for DVec2 {
1652 type Output = Self;
1653 #[inline]
1654 fn sub(self, rhs: f64) -> Self {
1655 Self {
1656 x: self.x.sub(rhs),
1657 y: self.y.sub(rhs),
1658 }
1659 }
1660}
1661
1662impl Sub<&f64> for DVec2 {
1663 type Output = Self;
1664 #[inline]
1665 fn sub(self, rhs: &f64) -> Self {
1666 self.sub(*rhs)
1667 }
1668}
1669
1670impl Sub<&f64> for &DVec2 {
1671 type Output = DVec2;
1672 #[inline]
1673 fn sub(self, rhs: &f64) -> DVec2 {
1674 (*self).sub(*rhs)
1675 }
1676}
1677
1678impl Sub<f64> for &DVec2 {
1679 type Output = DVec2;
1680 #[inline]
1681 fn sub(self, rhs: f64) -> DVec2 {
1682 (*self).sub(rhs)
1683 }
1684}
1685
1686impl SubAssign<f64> for DVec2 {
1687 #[inline]
1688 fn sub_assign(&mut self, rhs: f64) {
1689 self.x.sub_assign(rhs);
1690 self.y.sub_assign(rhs);
1691 }
1692}
1693
1694impl SubAssign<&f64> for DVec2 {
1695 #[inline]
1696 fn sub_assign(&mut self, rhs: &f64) {
1697 self.sub_assign(*rhs);
1698 }
1699}
1700
1701impl Sub<DVec2> for f64 {
1702 type Output = DVec2;
1703 #[inline]
1704 fn sub(self, rhs: DVec2) -> DVec2 {
1705 DVec2 {
1706 x: self.sub(rhs.x),
1707 y: self.sub(rhs.y),
1708 }
1709 }
1710}
1711
1712impl Sub<&DVec2> for f64 {
1713 type Output = DVec2;
1714 #[inline]
1715 fn sub(self, rhs: &DVec2) -> DVec2 {
1716 self.sub(*rhs)
1717 }
1718}
1719
1720impl Sub<&DVec2> for &f64 {
1721 type Output = DVec2;
1722 #[inline]
1723 fn sub(self, rhs: &DVec2) -> DVec2 {
1724 (*self).sub(*rhs)
1725 }
1726}
1727
1728impl Sub<DVec2> for &f64 {
1729 type Output = DVec2;
1730 #[inline]
1731 fn sub(self, rhs: DVec2) -> DVec2 {
1732 (*self).sub(rhs)
1733 }
1734}
1735
1736impl Rem for DVec2 {
1737 type Output = Self;
1738 #[inline]
1739 fn rem(self, rhs: Self) -> Self {
1740 Self {
1741 x: self.x.rem(rhs.x),
1742 y: self.y.rem(rhs.y),
1743 }
1744 }
1745}
1746
1747impl Rem<&Self> for DVec2 {
1748 type Output = Self;
1749 #[inline]
1750 fn rem(self, rhs: &Self) -> Self {
1751 self.rem(*rhs)
1752 }
1753}
1754
1755impl Rem<&DVec2> for &DVec2 {
1756 type Output = DVec2;
1757 #[inline]
1758 fn rem(self, rhs: &DVec2) -> DVec2 {
1759 (*self).rem(*rhs)
1760 }
1761}
1762
1763impl Rem<DVec2> for &DVec2 {
1764 type Output = DVec2;
1765 #[inline]
1766 fn rem(self, rhs: DVec2) -> DVec2 {
1767 (*self).rem(rhs)
1768 }
1769}
1770
1771impl RemAssign for DVec2 {
1772 #[inline]
1773 fn rem_assign(&mut self, rhs: Self) {
1774 self.x.rem_assign(rhs.x);
1775 self.y.rem_assign(rhs.y);
1776 }
1777}
1778
1779impl RemAssign<&Self> for DVec2 {
1780 #[inline]
1781 fn rem_assign(&mut self, rhs: &Self) {
1782 self.rem_assign(*rhs);
1783 }
1784}
1785
1786impl Rem<f64> for DVec2 {
1787 type Output = Self;
1788 #[inline]
1789 fn rem(self, rhs: f64) -> Self {
1790 Self {
1791 x: self.x.rem(rhs),
1792 y: self.y.rem(rhs),
1793 }
1794 }
1795}
1796
1797impl Rem<&f64> for DVec2 {
1798 type Output = Self;
1799 #[inline]
1800 fn rem(self, rhs: &f64) -> Self {
1801 self.rem(*rhs)
1802 }
1803}
1804
1805impl Rem<&f64> for &DVec2 {
1806 type Output = DVec2;
1807 #[inline]
1808 fn rem(self, rhs: &f64) -> DVec2 {
1809 (*self).rem(*rhs)
1810 }
1811}
1812
1813impl Rem<f64> for &DVec2 {
1814 type Output = DVec2;
1815 #[inline]
1816 fn rem(self, rhs: f64) -> DVec2 {
1817 (*self).rem(rhs)
1818 }
1819}
1820
1821impl RemAssign<f64> for DVec2 {
1822 #[inline]
1823 fn rem_assign(&mut self, rhs: f64) {
1824 self.x.rem_assign(rhs);
1825 self.y.rem_assign(rhs);
1826 }
1827}
1828
1829impl RemAssign<&f64> for DVec2 {
1830 #[inline]
1831 fn rem_assign(&mut self, rhs: &f64) {
1832 self.rem_assign(*rhs);
1833 }
1834}
1835
1836impl Rem<DVec2> for f64 {
1837 type Output = DVec2;
1838 #[inline]
1839 fn rem(self, rhs: DVec2) -> DVec2 {
1840 DVec2 {
1841 x: self.rem(rhs.x),
1842 y: self.rem(rhs.y),
1843 }
1844 }
1845}
1846
1847impl Rem<&DVec2> for f64 {
1848 type Output = DVec2;
1849 #[inline]
1850 fn rem(self, rhs: &DVec2) -> DVec2 {
1851 self.rem(*rhs)
1852 }
1853}
1854
1855impl Rem<&DVec2> for &f64 {
1856 type Output = DVec2;
1857 #[inline]
1858 fn rem(self, rhs: &DVec2) -> DVec2 {
1859 (*self).rem(*rhs)
1860 }
1861}
1862
1863impl Rem<DVec2> for &f64 {
1864 type Output = DVec2;
1865 #[inline]
1866 fn rem(self, rhs: DVec2) -> DVec2 {
1867 (*self).rem(rhs)
1868 }
1869}
1870
1871impl AsRef<[f64; 2]> for DVec2 {
1872 #[inline]
1873 fn as_ref(&self) -> &[f64; 2] {
1874 unsafe { &*(self as *const Self as *const [f64; 2]) }
1875 }
1876}
1877
1878impl AsMut<[f64; 2]> for DVec2 {
1879 #[inline]
1880 fn as_mut(&mut self) -> &mut [f64; 2] {
1881 unsafe { &mut *(self as *mut Self as *mut [f64; 2]) }
1882 }
1883}
1884
1885impl Sum for DVec2 {
1886 #[inline]
1887 fn sum<I>(iter: I) -> Self
1888 where
1889 I: Iterator<Item = Self>,
1890 {
1891 iter.fold(Self::ZERO, Self::add)
1892 }
1893}
1894
1895impl<'a> Sum<&'a Self> for DVec2 {
1896 #[inline]
1897 fn sum<I>(iter: I) -> Self
1898 where
1899 I: Iterator<Item = &'a Self>,
1900 {
1901 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1902 }
1903}
1904
1905impl Product for DVec2 {
1906 #[inline]
1907 fn product<I>(iter: I) -> Self
1908 where
1909 I: Iterator<Item = Self>,
1910 {
1911 iter.fold(Self::ONE, Self::mul)
1912 }
1913}
1914
1915impl<'a> Product<&'a Self> for DVec2 {
1916 #[inline]
1917 fn product<I>(iter: I) -> Self
1918 where
1919 I: Iterator<Item = &'a Self>,
1920 {
1921 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1922 }
1923}
1924
1925impl Neg for DVec2 {
1926 type Output = Self;
1927 #[inline]
1928 fn neg(self) -> Self {
1929 Self {
1930 x: self.x.neg(),
1931 y: self.y.neg(),
1932 }
1933 }
1934}
1935
1936impl Neg for &DVec2 {
1937 type Output = DVec2;
1938 #[inline]
1939 fn neg(self) -> DVec2 {
1940 (*self).neg()
1941 }
1942}
1943
1944impl Index<usize> for DVec2 {
1945 type Output = f64;
1946 #[inline]
1947 fn index(&self, index: usize) -> &Self::Output {
1948 match index {
1949 0 => &self.x,
1950 1 => &self.y,
1951 _ => panic!("index out of bounds"),
1952 }
1953 }
1954}
1955
1956impl IndexMut<usize> for DVec2 {
1957 #[inline]
1958 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1959 match index {
1960 0 => &mut self.x,
1961 1 => &mut self.y,
1962 _ => panic!("index out of bounds"),
1963 }
1964 }
1965}
1966
1967impl fmt::Display for DVec2 {
1968 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1969 if let Some(p) = f.precision() {
1970 write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y)
1971 } else {
1972 write!(f, "[{}, {}]", self.x, self.y)
1973 }
1974 }
1975}
1976
1977impl fmt::Debug for DVec2 {
1978 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1979 fmt.debug_tuple(stringify!(DVec2))
1980 .field(&self.x)
1981 .field(&self.y)
1982 .finish()
1983 }
1984}
1985
1986impl From<[f64; 2]> for DVec2 {
1987 #[inline]
1988 fn from(a: [f64; 2]) -> Self {
1989 Self::new(a[0], a[1])
1990 }
1991}
1992
1993impl From<DVec2> for [f64; 2] {
1994 #[inline]
1995 fn from(v: DVec2) -> Self {
1996 [v.x, v.y]
1997 }
1998}
1999
2000impl From<(f64, f64)> for DVec2 {
2001 #[inline]
2002 fn from(t: (f64, f64)) -> Self {
2003 Self::new(t.0, t.1)
2004 }
2005}
2006
2007impl From<DVec2> for (f64, f64) {
2008 #[inline]
2009 fn from(v: DVec2) -> Self {
2010 (v.x, v.y)
2011 }
2012}
2013
2014impl From<Vec2> for DVec2 {
2015 #[inline]
2016 fn from(v: Vec2) -> Self {
2017 Self::new(f64::from(v.x), f64::from(v.y))
2018 }
2019}
2020
2021#[cfg(feature = "i32")]
2022impl From<IVec2> for DVec2 {
2023 #[inline]
2024 fn from(v: IVec2) -> Self {
2025 Self::new(f64::from(v.x), f64::from(v.y))
2026 }
2027}
2028
2029#[cfg(feature = "u32")]
2030impl From<UVec2> for DVec2 {
2031 #[inline]
2032 fn from(v: UVec2) -> Self {
2033 Self::new(f64::from(v.x), f64::from(v.y))
2034 }
2035}
2036
2037impl From<BVec2> for DVec2 {
2038 #[inline]
2039 fn from(v: BVec2) -> Self {
2040 Self::new(f64::from(v.x), f64::from(v.y))
2041 }
2042}