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