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::ops::*;
16
17#[cfg(feature = "zerocopy-08")]
18use zerocopy_derive_08::*;
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-08",
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::new(v, 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::new(
128 if mask.test(0) { if_true.x } else { if_false.x },
129 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::new(
216 if self.x < rhs.x { self.x } else { rhs.x },
217 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::new(
231 if self.x > rhs.x { self.x } else { rhs.x },
232 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::new(math::abs(self.x), math::abs(self.y))
392 }
393
394 #[inline]
400 #[must_use]
401 pub fn signum(self) -> Self {
402 Self::new(math::signum(self.x), math::signum(self.y))
403 }
404
405 #[inline]
407 #[must_use]
408 pub fn copysign(self, rhs: Self) -> Self {
409 Self::new(math::copysign(self.x, rhs.x), math::copysign(self.y, rhs.y))
410 }
411
412 #[inline]
420 #[must_use]
421 pub fn is_negative_bitmask(self) -> u32 {
422 (self.x.is_sign_negative() as u32) | ((self.y.is_sign_negative() as u32) << 1)
423 }
424
425 #[inline]
430 #[must_use]
431 pub fn is_negative_mask(self) -> BVec2 {
432 BVec2::new(self.x.is_sign_negative(), self.y.is_sign_negative())
433 }
434
435 #[inline]
438 #[must_use]
439 pub fn is_finite(self) -> bool {
440 self.x.is_finite() && self.y.is_finite()
441 }
442
443 #[inline]
447 #[must_use]
448 pub fn is_finite_mask(self) -> BVec2 {
449 BVec2::new(self.x.is_finite(), self.y.is_finite())
450 }
451
452 #[inline]
454 #[must_use]
455 pub fn is_nan(self) -> bool {
456 self.x.is_nan() || self.y.is_nan()
457 }
458
459 #[inline]
463 #[must_use]
464 pub fn is_nan_mask(self) -> BVec2 {
465 BVec2::new(self.x.is_nan(), self.y.is_nan())
466 }
467
468 #[doc(alias = "magnitude")]
470 #[inline]
471 #[must_use]
472 pub fn length(self) -> f64 {
473 math::sqrt(self.dot(self))
474 }
475
476 #[allow(dead_code)]
478 fn is_non_zero(self) -> bool {
479 self.length_squared() > 0.0
480 }
481
482 #[doc(alias = "magnitude2")]
486 #[inline]
487 #[must_use]
488 pub fn length_squared(self) -> f64 {
489 self.dot(self)
490 }
491
492 #[inline]
496 #[must_use]
497 pub fn length_recip(self) -> f64 {
498 1.0 / self.length()
499 }
500
501 #[inline]
503 #[must_use]
504 pub fn distance(self, rhs: Self) -> f64 {
505 (self - rhs).length()
506 }
507
508 #[inline]
510 #[must_use]
511 pub fn distance_squared(self, rhs: Self) -> f64 {
512 (self - rhs).length_squared()
513 }
514
515 #[inline]
517 #[must_use]
518 pub fn div_euclid(self, rhs: Self) -> Self {
519 Self::new(
520 math::div_euclid(self.x, rhs.x),
521 math::div_euclid(self.y, rhs.y),
522 )
523 }
524
525 #[inline]
529 #[must_use]
530 pub fn rem_euclid(self, rhs: Self) -> Self {
531 Self::new(
532 math::rem_euclid(self.x, rhs.x),
533 math::rem_euclid(self.y, rhs.y),
534 )
535 }
536
537 #[inline]
547 #[must_use]
548 pub fn normalize(self) -> Self {
549 #[allow(clippy::let_and_return)]
550 let normalized = self.mul(self.length_recip());
551 glam_assert!(normalized.is_finite());
552 normalized
553 }
554
555 #[inline]
562 #[must_use]
563 pub fn try_normalize(self) -> Option<Self> {
564 let rcp = self.length_recip();
565 if rcp.is_finite() && rcp > 0.0 {
566 Some(self * rcp)
567 } else {
568 None
569 }
570 }
571
572 #[inline]
580 #[must_use]
581 pub fn normalize_or(self, fallback: Self) -> Self {
582 let rcp = self.length_recip();
583 if rcp.is_finite() && rcp > 0.0 {
584 self * rcp
585 } else {
586 fallback
587 }
588 }
589
590 #[inline]
597 #[must_use]
598 pub fn normalize_or_zero(self) -> Self {
599 self.normalize_or(Self::ZERO)
600 }
601
602 #[inline]
606 #[must_use]
607 pub fn normalize_and_length(self) -> (Self, f64) {
608 let length = self.length();
609 let rcp = 1.0 / length;
610 if rcp.is_finite() && rcp > 0.0 {
611 (self * rcp, length)
612 } else {
613 (Self::X, 0.0)
614 }
615 }
616
617 #[inline]
621 #[must_use]
622 pub fn is_normalized(self) -> bool {
623 math::abs(self.length_squared() - 1.0) <= 2e-4
624 }
625
626 #[inline]
634 #[must_use]
635 pub fn project_onto(self, rhs: Self) -> Self {
636 let other_len_sq_rcp = 1.0 / rhs.dot(rhs);
637 glam_assert!(other_len_sq_rcp.is_finite());
638 rhs * self.dot(rhs) * other_len_sq_rcp
639 }
640
641 #[doc(alias("plane"))]
652 #[inline]
653 #[must_use]
654 pub fn reject_from(self, rhs: Self) -> Self {
655 self - self.project_onto(rhs)
656 }
657
658 #[inline]
666 #[must_use]
667 pub fn project_onto_normalized(self, rhs: Self) -> Self {
668 glam_assert!(rhs.is_normalized());
669 rhs * self.dot(rhs)
670 }
671
672 #[doc(alias("plane"))]
683 #[inline]
684 #[must_use]
685 pub fn reject_from_normalized(self, rhs: Self) -> Self {
686 self - self.project_onto_normalized(rhs)
687 }
688
689 #[inline]
692 #[must_use]
693 pub fn round(self) -> Self {
694 Self::new(math::round(self.x), math::round(self.y))
695 }
696
697 #[inline]
700 #[must_use]
701 pub fn floor(self) -> Self {
702 Self::new(math::floor(self.x), math::floor(self.y))
703 }
704
705 #[inline]
708 #[must_use]
709 pub fn ceil(self) -> Self {
710 Self::new(math::ceil(self.x), math::ceil(self.y))
711 }
712
713 #[inline]
716 #[must_use]
717 pub fn trunc(self) -> Self {
718 Self::new(math::trunc(self.x), math::trunc(self.y))
719 }
720
721 #[inline]
725 #[must_use]
726 pub fn step(self, rhs: Self) -> Self {
727 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
728 }
729
730 #[inline]
741 #[must_use]
742 pub fn smoothstep(self, edge0: Self, edge1: Self) -> Self {
743 glam_assert!(edge0.cmplt(edge1).all());
744 let t = ((self - edge0) / (edge1 - edge0)).saturate();
745 t * t * (Self::splat(3.0) - Self::splat(2.0) * t)
746 }
747
748 #[inline]
750 #[must_use]
751 pub fn saturate(self) -> Self {
752 self.clamp(Self::ZERO, Self::ONE)
753 }
754
755 #[inline]
762 #[must_use]
763 pub fn fract(self) -> Self {
764 self - self.trunc()
765 }
766
767 #[inline]
774 #[must_use]
775 pub fn fract_gl(self) -> Self {
776 self - self.floor()
777 }
778
779 #[inline]
782 #[must_use]
783 pub fn exp(self) -> Self {
784 Self::new(math::exp(self.x), math::exp(self.y))
785 }
786
787 #[inline]
789 #[must_use]
790 pub fn exp2(self) -> Self {
791 Self::new(math::exp2(self.x), math::exp2(self.y))
792 }
793
794 #[inline]
797 #[must_use]
798 pub fn ln(self) -> Self {
799 Self::new(math::ln(self.x), math::ln(self.y))
800 }
801
802 #[inline]
805 #[must_use]
806 pub fn log2(self) -> Self {
807 Self::new(math::log2(self.x), math::log2(self.y))
808 }
809
810 #[inline]
812 #[must_use]
813 pub fn powf(self, n: f64) -> Self {
814 Self::new(math::powf(self.x, n), math::powf(self.y, n))
815 }
816
817 #[inline]
820 #[must_use]
821 pub fn sqrt(self) -> Self {
822 Self::new(math::sqrt(self.x), math::sqrt(self.y))
823 }
824
825 #[inline]
827 #[must_use]
828 pub fn cos(self) -> Self {
829 Self::new(math::cos(self.x), math::cos(self.y))
830 }
831
832 #[inline]
834 #[must_use]
835 pub fn sin(self) -> Self {
836 Self::new(math::sin(self.x), math::sin(self.y))
837 }
838
839 #[inline]
841 #[must_use]
842 pub fn sin_cos(self) -> (Self, Self) {
843 let (sin_x, cos_x) = math::sin_cos(self.x);
844 let (sin_y, cos_y) = math::sin_cos(self.y);
845
846 (Self::new(sin_x, sin_y), Self::new(cos_x, cos_y))
847 }
848
849 #[inline]
851 #[must_use]
852 pub fn recip(self) -> Self {
853 Self::new(1.0 / self.x, 1.0 / self.y)
854 }
855
856 #[doc(alias = "mix")]
868 #[inline]
869 #[must_use]
870 pub fn lerp(self, rhs: Self, s: f64) -> Self {
871 self * (1.0 - s) + rhs * s
872 }
873
874 #[doc(alias = "mix")]
891 #[inline]
892 #[must_use]
893 pub fn lerp_monotonic(self, rhs: Self, s: f64) -> Self {
894 self + (rhs - self) * s
895 }
896
897 #[inline]
902 #[must_use]
903 pub fn move_towards(self, rhs: Self, d: f64) -> Self {
904 let a = rhs - self;
905 let len = a.length();
906 if len <= d || len <= 1e-4 {
907 return rhs;
908 }
909 self + a / len * d
910 }
911
912 #[inline]
918 pub fn midpoint(self, rhs: Self) -> Self {
919 (self + rhs) * 0.5
920 }
921
922 #[inline]
932 #[must_use]
933 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
934 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
935 }
936
937 #[inline]
943 #[must_use]
944 pub fn clamp_length(self, min: f64, max: f64) -> Self {
945 glam_assert!(0.0 <= min);
946 glam_assert!(min <= max);
947 let length_sq = self.length_squared();
948 if length_sq < min * min {
949 min * (self / math::sqrt(length_sq))
950 } else if length_sq > max * max {
951 max * (self / math::sqrt(length_sq))
952 } else {
953 self
954 }
955 }
956
957 #[inline]
963 #[must_use]
964 pub fn clamp_length_max(self, max: f64) -> Self {
965 glam_assert!(0.0 <= max);
966 let length_sq = self.length_squared();
967 if length_sq > max * max {
968 max * (self / math::sqrt(length_sq))
969 } else {
970 self
971 }
972 }
973
974 #[inline]
980 #[must_use]
981 pub fn clamp_length_min(self, min: f64) -> Self {
982 glam_assert!(0.0 <= min);
983 let length_sq = self.length_squared();
984 if length_sq < min * min {
985 min * (self / math::sqrt(length_sq))
986 } else {
987 self
988 }
989 }
990
991 #[inline]
999 #[must_use]
1000 pub fn mul_add(self, a: Self, b: Self) -> Self {
1001 Self::new(
1002 math::mul_add(self.x, a.x, b.x),
1003 math::mul_add(self.y, a.y, b.y),
1004 )
1005 }
1006
1007 #[inline]
1016 #[must_use]
1017 pub fn reflect(self, normal: Self) -> Self {
1018 glam_assert!(normal.is_normalized());
1019 self - 2.0 * self.dot(normal) * normal
1020 }
1021
1022 #[inline]
1032 #[must_use]
1033 pub fn refract(self, normal: Self, eta: f64) -> Self {
1034 glam_assert!(self.is_normalized());
1035 glam_assert!(normal.is_normalized());
1036 let n_dot_i = normal.dot(self);
1037 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1038 if k >= 0.0 {
1039 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1040 } else {
1041 Self::ZERO
1042 }
1043 }
1044
1045 #[inline]
1050 #[must_use]
1051 pub fn from_angle(angle: f64) -> Self {
1052 let (sin, cos) = math::sin_cos(angle);
1053 Self::new(cos, sin)
1054 }
1055
1056 #[inline]
1060 #[must_use]
1061 pub fn to_angle(self) -> f64 {
1062 math::atan2(self.y, self.x)
1063 }
1064
1065 #[inline]
1076 #[must_use]
1077 pub fn angle_to(self, rhs: Self) -> f64 {
1078 glam_assert!(self.is_non_zero());
1079 glam_assert!(rhs.is_non_zero());
1080 let angle = math::acos_approx(
1081 self.dot(rhs) / math::sqrt(self.length_squared() * rhs.length_squared()),
1082 );
1083
1084 angle * math::signum(self.perp_dot(rhs))
1085 }
1086
1087 #[inline]
1089 #[must_use]
1090 pub fn perp(self) -> Self {
1091 Self::new(-self.y, self.x)
1092 }
1093
1094 #[doc(alias = "wedge")]
1097 #[doc(alias = "cross")]
1098 #[doc(alias = "determinant")]
1099 #[inline]
1100 #[must_use]
1101 pub fn perp_dot(self, rhs: Self) -> f64 {
1102 (self.x * rhs.y) - (self.y * rhs.x)
1103 }
1104
1105 #[inline]
1113 #[must_use]
1114 pub fn rotate(self, rhs: Self) -> Self {
1115 Self::new(
1116 self.x * rhs.x - self.y * rhs.y,
1117 self.y * rhs.x + self.x * rhs.y,
1118 )
1119 }
1120
1121 #[inline]
1124 #[must_use]
1125 pub fn rotate_angle(self, angle: f64) -> Self {
1126 self.rotate(Self::from_angle(angle))
1127 }
1128
1129 #[inline]
1135 #[must_use]
1136 pub fn rotate_towards(self, rhs: Self, max_angle: f64) -> Self {
1137 let a = self.angle_to(rhs);
1138 let abs_a = math::abs(a);
1139 let angle = max_angle.clamp(abs_a - core::f64::consts::PI, abs_a) * math::signum(a);
1141 Self::from_angle(angle).rotate(self)
1142 }
1143
1144 #[inline]
1146 #[must_use]
1147 pub fn as_vec2(self) -> crate::Vec2 {
1148 crate::Vec2::new(self.x as f32, self.y as f32)
1149 }
1150
1151 #[cfg(feature = "i8")]
1153 #[inline]
1154 #[must_use]
1155 pub fn as_i8vec2(self) -> crate::I8Vec2 {
1156 crate::I8Vec2::new(self.x as i8, self.y as i8)
1157 }
1158
1159 #[cfg(feature = "u8")]
1161 #[inline]
1162 #[must_use]
1163 pub fn as_u8vec2(self) -> crate::U8Vec2 {
1164 crate::U8Vec2::new(self.x as u8, self.y as u8)
1165 }
1166
1167 #[cfg(feature = "i16")]
1169 #[inline]
1170 #[must_use]
1171 pub fn as_i16vec2(self) -> crate::I16Vec2 {
1172 crate::I16Vec2::new(self.x as i16, self.y as i16)
1173 }
1174
1175 #[cfg(feature = "u16")]
1177 #[inline]
1178 #[must_use]
1179 pub fn as_u16vec2(self) -> crate::U16Vec2 {
1180 crate::U16Vec2::new(self.x as u16, self.y as u16)
1181 }
1182
1183 #[cfg(feature = "i32")]
1185 #[inline]
1186 #[must_use]
1187 pub fn as_ivec2(self) -> crate::IVec2 {
1188 crate::IVec2::new(self.x as i32, self.y as i32)
1189 }
1190
1191 #[cfg(feature = "u32")]
1193 #[inline]
1194 #[must_use]
1195 pub fn as_uvec2(self) -> crate::UVec2 {
1196 crate::UVec2::new(self.x as u32, self.y as u32)
1197 }
1198
1199 #[cfg(feature = "i64")]
1201 #[inline]
1202 #[must_use]
1203 pub fn as_i64vec2(self) -> crate::I64Vec2 {
1204 crate::I64Vec2::new(self.x as i64, self.y as i64)
1205 }
1206
1207 #[cfg(feature = "u64")]
1209 #[inline]
1210 #[must_use]
1211 pub fn as_u64vec2(self) -> crate::U64Vec2 {
1212 crate::U64Vec2::new(self.x as u64, self.y as u64)
1213 }
1214
1215 #[cfg(feature = "isize")]
1217 #[inline]
1218 #[must_use]
1219 pub fn as_isizevec2(self) -> crate::ISizeVec2 {
1220 crate::ISizeVec2::new(self.x as isize, self.y as isize)
1221 }
1222
1223 #[cfg(feature = "usize")]
1225 #[inline]
1226 #[must_use]
1227 pub fn as_usizevec2(self) -> crate::USizeVec2 {
1228 crate::USizeVec2::new(self.x as usize, self.y as usize)
1229 }
1230}
1231
1232impl Default for DVec2 {
1233 #[inline(always)]
1234 fn default() -> Self {
1235 Self::ZERO
1236 }
1237}
1238
1239impl Div for DVec2 {
1240 type Output = Self;
1241 #[inline]
1242 fn div(self, rhs: Self) -> Self {
1243 Self::new(self.x.div(rhs.x), self.y.div(rhs.y))
1244 }
1245}
1246
1247impl Div<&Self> for DVec2 {
1248 type Output = Self;
1249 #[inline]
1250 fn div(self, rhs: &Self) -> Self {
1251 self.div(*rhs)
1252 }
1253}
1254
1255impl Div<&DVec2> for &DVec2 {
1256 type Output = DVec2;
1257 #[inline]
1258 fn div(self, rhs: &DVec2) -> DVec2 {
1259 (*self).div(*rhs)
1260 }
1261}
1262
1263impl Div<DVec2> for &DVec2 {
1264 type Output = DVec2;
1265 #[inline]
1266 fn div(self, rhs: DVec2) -> DVec2 {
1267 (*self).div(rhs)
1268 }
1269}
1270
1271impl DivAssign for DVec2 {
1272 #[inline]
1273 fn div_assign(&mut self, rhs: Self) {
1274 self.x.div_assign(rhs.x);
1275 self.y.div_assign(rhs.y);
1276 }
1277}
1278
1279impl DivAssign<&Self> for DVec2 {
1280 #[inline]
1281 fn div_assign(&mut self, rhs: &Self) {
1282 self.div_assign(*rhs);
1283 }
1284}
1285
1286impl Div<f64> for DVec2 {
1287 type Output = Self;
1288 #[inline]
1289 fn div(self, rhs: f64) -> Self {
1290 Self::new(self.x.div(rhs), self.y.div(rhs))
1291 }
1292}
1293
1294impl Div<&f64> for DVec2 {
1295 type Output = Self;
1296 #[inline]
1297 fn div(self, rhs: &f64) -> Self {
1298 self.div(*rhs)
1299 }
1300}
1301
1302impl Div<&f64> for &DVec2 {
1303 type Output = DVec2;
1304 #[inline]
1305 fn div(self, rhs: &f64) -> DVec2 {
1306 (*self).div(*rhs)
1307 }
1308}
1309
1310impl Div<f64> for &DVec2 {
1311 type Output = DVec2;
1312 #[inline]
1313 fn div(self, rhs: f64) -> DVec2 {
1314 (*self).div(rhs)
1315 }
1316}
1317
1318impl DivAssign<f64> for DVec2 {
1319 #[inline]
1320 fn div_assign(&mut self, rhs: f64) {
1321 self.x.div_assign(rhs);
1322 self.y.div_assign(rhs);
1323 }
1324}
1325
1326impl DivAssign<&f64> for DVec2 {
1327 #[inline]
1328 fn div_assign(&mut self, rhs: &f64) {
1329 self.div_assign(*rhs);
1330 }
1331}
1332
1333impl Div<DVec2> for f64 {
1334 type Output = DVec2;
1335 #[inline]
1336 fn div(self, rhs: DVec2) -> DVec2 {
1337 DVec2::new(self.div(rhs.x), self.div(rhs.y))
1338 }
1339}
1340
1341impl Div<&DVec2> for f64 {
1342 type Output = DVec2;
1343 #[inline]
1344 fn div(self, rhs: &DVec2) -> DVec2 {
1345 self.div(*rhs)
1346 }
1347}
1348
1349impl Div<&DVec2> for &f64 {
1350 type Output = DVec2;
1351 #[inline]
1352 fn div(self, rhs: &DVec2) -> DVec2 {
1353 (*self).div(*rhs)
1354 }
1355}
1356
1357impl Div<DVec2> for &f64 {
1358 type Output = DVec2;
1359 #[inline]
1360 fn div(self, rhs: DVec2) -> DVec2 {
1361 (*self).div(rhs)
1362 }
1363}
1364
1365impl Mul for DVec2 {
1366 type Output = Self;
1367 #[inline]
1368 fn mul(self, rhs: Self) -> Self {
1369 Self::new(self.x.mul(rhs.x), self.y.mul(rhs.y))
1370 }
1371}
1372
1373impl Mul<&Self> for DVec2 {
1374 type Output = Self;
1375 #[inline]
1376 fn mul(self, rhs: &Self) -> Self {
1377 self.mul(*rhs)
1378 }
1379}
1380
1381impl Mul<&DVec2> for &DVec2 {
1382 type Output = DVec2;
1383 #[inline]
1384 fn mul(self, rhs: &DVec2) -> DVec2 {
1385 (*self).mul(*rhs)
1386 }
1387}
1388
1389impl Mul<DVec2> for &DVec2 {
1390 type Output = DVec2;
1391 #[inline]
1392 fn mul(self, rhs: DVec2) -> DVec2 {
1393 (*self).mul(rhs)
1394 }
1395}
1396
1397impl MulAssign for DVec2 {
1398 #[inline]
1399 fn mul_assign(&mut self, rhs: Self) {
1400 self.x.mul_assign(rhs.x);
1401 self.y.mul_assign(rhs.y);
1402 }
1403}
1404
1405impl MulAssign<&Self> for DVec2 {
1406 #[inline]
1407 fn mul_assign(&mut self, rhs: &Self) {
1408 self.mul_assign(*rhs);
1409 }
1410}
1411
1412impl Mul<f64> for DVec2 {
1413 type Output = Self;
1414 #[inline]
1415 fn mul(self, rhs: f64) -> Self {
1416 Self::new(self.x.mul(rhs), self.y.mul(rhs))
1417 }
1418}
1419
1420impl Mul<&f64> for DVec2 {
1421 type Output = Self;
1422 #[inline]
1423 fn mul(self, rhs: &f64) -> Self {
1424 self.mul(*rhs)
1425 }
1426}
1427
1428impl Mul<&f64> for &DVec2 {
1429 type Output = DVec2;
1430 #[inline]
1431 fn mul(self, rhs: &f64) -> DVec2 {
1432 (*self).mul(*rhs)
1433 }
1434}
1435
1436impl Mul<f64> for &DVec2 {
1437 type Output = DVec2;
1438 #[inline]
1439 fn mul(self, rhs: f64) -> DVec2 {
1440 (*self).mul(rhs)
1441 }
1442}
1443
1444impl MulAssign<f64> for DVec2 {
1445 #[inline]
1446 fn mul_assign(&mut self, rhs: f64) {
1447 self.x.mul_assign(rhs);
1448 self.y.mul_assign(rhs);
1449 }
1450}
1451
1452impl MulAssign<&f64> for DVec2 {
1453 #[inline]
1454 fn mul_assign(&mut self, rhs: &f64) {
1455 self.mul_assign(*rhs);
1456 }
1457}
1458
1459impl Mul<DVec2> for f64 {
1460 type Output = DVec2;
1461 #[inline]
1462 fn mul(self, rhs: DVec2) -> DVec2 {
1463 DVec2::new(self.mul(rhs.x), self.mul(rhs.y))
1464 }
1465}
1466
1467impl Mul<&DVec2> for f64 {
1468 type Output = DVec2;
1469 #[inline]
1470 fn mul(self, rhs: &DVec2) -> DVec2 {
1471 self.mul(*rhs)
1472 }
1473}
1474
1475impl Mul<&DVec2> for &f64 {
1476 type Output = DVec2;
1477 #[inline]
1478 fn mul(self, rhs: &DVec2) -> DVec2 {
1479 (*self).mul(*rhs)
1480 }
1481}
1482
1483impl Mul<DVec2> for &f64 {
1484 type Output = DVec2;
1485 #[inline]
1486 fn mul(self, rhs: DVec2) -> DVec2 {
1487 (*self).mul(rhs)
1488 }
1489}
1490
1491impl Add for DVec2 {
1492 type Output = Self;
1493 #[inline]
1494 fn add(self, rhs: Self) -> Self {
1495 Self::new(self.x.add(rhs.x), self.y.add(rhs.y))
1496 }
1497}
1498
1499impl Add<&Self> for DVec2 {
1500 type Output = Self;
1501 #[inline]
1502 fn add(self, rhs: &Self) -> Self {
1503 self.add(*rhs)
1504 }
1505}
1506
1507impl Add<&DVec2> for &DVec2 {
1508 type Output = DVec2;
1509 #[inline]
1510 fn add(self, rhs: &DVec2) -> DVec2 {
1511 (*self).add(*rhs)
1512 }
1513}
1514
1515impl Add<DVec2> for &DVec2 {
1516 type Output = DVec2;
1517 #[inline]
1518 fn add(self, rhs: DVec2) -> DVec2 {
1519 (*self).add(rhs)
1520 }
1521}
1522
1523impl AddAssign for DVec2 {
1524 #[inline]
1525 fn add_assign(&mut self, rhs: Self) {
1526 self.x.add_assign(rhs.x);
1527 self.y.add_assign(rhs.y);
1528 }
1529}
1530
1531impl AddAssign<&Self> for DVec2 {
1532 #[inline]
1533 fn add_assign(&mut self, rhs: &Self) {
1534 self.add_assign(*rhs);
1535 }
1536}
1537
1538impl Add<f64> for DVec2 {
1539 type Output = Self;
1540 #[inline]
1541 fn add(self, rhs: f64) -> Self {
1542 Self::new(self.x.add(rhs), self.y.add(rhs))
1543 }
1544}
1545
1546impl Add<&f64> for DVec2 {
1547 type Output = Self;
1548 #[inline]
1549 fn add(self, rhs: &f64) -> Self {
1550 self.add(*rhs)
1551 }
1552}
1553
1554impl Add<&f64> for &DVec2 {
1555 type Output = DVec2;
1556 #[inline]
1557 fn add(self, rhs: &f64) -> DVec2 {
1558 (*self).add(*rhs)
1559 }
1560}
1561
1562impl Add<f64> for &DVec2 {
1563 type Output = DVec2;
1564 #[inline]
1565 fn add(self, rhs: f64) -> DVec2 {
1566 (*self).add(rhs)
1567 }
1568}
1569
1570impl AddAssign<f64> for DVec2 {
1571 #[inline]
1572 fn add_assign(&mut self, rhs: f64) {
1573 self.x.add_assign(rhs);
1574 self.y.add_assign(rhs);
1575 }
1576}
1577
1578impl AddAssign<&f64> for DVec2 {
1579 #[inline]
1580 fn add_assign(&mut self, rhs: &f64) {
1581 self.add_assign(*rhs);
1582 }
1583}
1584
1585impl Add<DVec2> for f64 {
1586 type Output = DVec2;
1587 #[inline]
1588 fn add(self, rhs: DVec2) -> DVec2 {
1589 DVec2::new(self.add(rhs.x), self.add(rhs.y))
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 Add<&DVec2> for &f64 {
1602 type Output = DVec2;
1603 #[inline]
1604 fn add(self, rhs: &DVec2) -> DVec2 {
1605 (*self).add(*rhs)
1606 }
1607}
1608
1609impl Add<DVec2> for &f64 {
1610 type Output = DVec2;
1611 #[inline]
1612 fn add(self, rhs: DVec2) -> DVec2 {
1613 (*self).add(rhs)
1614 }
1615}
1616
1617impl Sub for DVec2 {
1618 type Output = Self;
1619 #[inline]
1620 fn sub(self, rhs: Self) -> Self {
1621 Self::new(self.x.sub(rhs.x), self.y.sub(rhs.y))
1622 }
1623}
1624
1625impl Sub<&Self> for DVec2 {
1626 type Output = Self;
1627 #[inline]
1628 fn sub(self, rhs: &Self) -> Self {
1629 self.sub(*rhs)
1630 }
1631}
1632
1633impl Sub<&DVec2> for &DVec2 {
1634 type Output = DVec2;
1635 #[inline]
1636 fn sub(self, rhs: &DVec2) -> DVec2 {
1637 (*self).sub(*rhs)
1638 }
1639}
1640
1641impl Sub<DVec2> for &DVec2 {
1642 type Output = DVec2;
1643 #[inline]
1644 fn sub(self, rhs: DVec2) -> DVec2 {
1645 (*self).sub(rhs)
1646 }
1647}
1648
1649impl SubAssign for DVec2 {
1650 #[inline]
1651 fn sub_assign(&mut self, rhs: Self) {
1652 self.x.sub_assign(rhs.x);
1653 self.y.sub_assign(rhs.y);
1654 }
1655}
1656
1657impl SubAssign<&Self> for DVec2 {
1658 #[inline]
1659 fn sub_assign(&mut self, rhs: &Self) {
1660 self.sub_assign(*rhs);
1661 }
1662}
1663
1664impl Sub<f64> for DVec2 {
1665 type Output = Self;
1666 #[inline]
1667 fn sub(self, rhs: f64) -> Self {
1668 Self::new(self.x.sub(rhs), self.y.sub(rhs))
1669 }
1670}
1671
1672impl Sub<&f64> for DVec2 {
1673 type Output = Self;
1674 #[inline]
1675 fn sub(self, rhs: &f64) -> Self {
1676 self.sub(*rhs)
1677 }
1678}
1679
1680impl Sub<&f64> for &DVec2 {
1681 type Output = DVec2;
1682 #[inline]
1683 fn sub(self, rhs: &f64) -> DVec2 {
1684 (*self).sub(*rhs)
1685 }
1686}
1687
1688impl Sub<f64> for &DVec2 {
1689 type Output = DVec2;
1690 #[inline]
1691 fn sub(self, rhs: f64) -> DVec2 {
1692 (*self).sub(rhs)
1693 }
1694}
1695
1696impl SubAssign<f64> for DVec2 {
1697 #[inline]
1698 fn sub_assign(&mut self, rhs: f64) {
1699 self.x.sub_assign(rhs);
1700 self.y.sub_assign(rhs);
1701 }
1702}
1703
1704impl SubAssign<&f64> for DVec2 {
1705 #[inline]
1706 fn sub_assign(&mut self, rhs: &f64) {
1707 self.sub_assign(*rhs);
1708 }
1709}
1710
1711impl Sub<DVec2> for f64 {
1712 type Output = DVec2;
1713 #[inline]
1714 fn sub(self, rhs: DVec2) -> DVec2 {
1715 DVec2::new(self.sub(rhs.x), self.sub(rhs.y))
1716 }
1717}
1718
1719impl Sub<&DVec2> for f64 {
1720 type Output = DVec2;
1721 #[inline]
1722 fn sub(self, rhs: &DVec2) -> DVec2 {
1723 self.sub(*rhs)
1724 }
1725}
1726
1727impl Sub<&DVec2> for &f64 {
1728 type Output = DVec2;
1729 #[inline]
1730 fn sub(self, rhs: &DVec2) -> DVec2 {
1731 (*self).sub(*rhs)
1732 }
1733}
1734
1735impl Sub<DVec2> for &f64 {
1736 type Output = DVec2;
1737 #[inline]
1738 fn sub(self, rhs: DVec2) -> DVec2 {
1739 (*self).sub(rhs)
1740 }
1741}
1742
1743impl Rem for DVec2 {
1744 type Output = Self;
1745 #[inline]
1746 fn rem(self, rhs: Self) -> Self {
1747 Self::new(self.x.rem(rhs.x), self.y.rem(rhs.y))
1748 }
1749}
1750
1751impl Rem<&Self> for DVec2 {
1752 type Output = Self;
1753 #[inline]
1754 fn rem(self, rhs: &Self) -> Self {
1755 self.rem(*rhs)
1756 }
1757}
1758
1759impl Rem<&DVec2> for &DVec2 {
1760 type Output = DVec2;
1761 #[inline]
1762 fn rem(self, rhs: &DVec2) -> DVec2 {
1763 (*self).rem(*rhs)
1764 }
1765}
1766
1767impl Rem<DVec2> for &DVec2 {
1768 type Output = DVec2;
1769 #[inline]
1770 fn rem(self, rhs: DVec2) -> DVec2 {
1771 (*self).rem(rhs)
1772 }
1773}
1774
1775impl RemAssign for DVec2 {
1776 #[inline]
1777 fn rem_assign(&mut self, rhs: Self) {
1778 self.x.rem_assign(rhs.x);
1779 self.y.rem_assign(rhs.y);
1780 }
1781}
1782
1783impl RemAssign<&Self> for DVec2 {
1784 #[inline]
1785 fn rem_assign(&mut self, rhs: &Self) {
1786 self.rem_assign(*rhs);
1787 }
1788}
1789
1790impl Rem<f64> for DVec2 {
1791 type Output = Self;
1792 #[inline]
1793 fn rem(self, rhs: f64) -> Self {
1794 Self::new(self.x.rem(rhs), self.y.rem(rhs))
1795 }
1796}
1797
1798impl Rem<&f64> for DVec2 {
1799 type Output = Self;
1800 #[inline]
1801 fn rem(self, rhs: &f64) -> Self {
1802 self.rem(*rhs)
1803 }
1804}
1805
1806impl Rem<&f64> for &DVec2 {
1807 type Output = DVec2;
1808 #[inline]
1809 fn rem(self, rhs: &f64) -> DVec2 {
1810 (*self).rem(*rhs)
1811 }
1812}
1813
1814impl Rem<f64> for &DVec2 {
1815 type Output = DVec2;
1816 #[inline]
1817 fn rem(self, rhs: f64) -> DVec2 {
1818 (*self).rem(rhs)
1819 }
1820}
1821
1822impl RemAssign<f64> for DVec2 {
1823 #[inline]
1824 fn rem_assign(&mut self, rhs: f64) {
1825 self.x.rem_assign(rhs);
1826 self.y.rem_assign(rhs);
1827 }
1828}
1829
1830impl RemAssign<&f64> for DVec2 {
1831 #[inline]
1832 fn rem_assign(&mut self, rhs: &f64) {
1833 self.rem_assign(*rhs);
1834 }
1835}
1836
1837impl Rem<DVec2> for f64 {
1838 type Output = DVec2;
1839 #[inline]
1840 fn rem(self, rhs: DVec2) -> DVec2 {
1841 DVec2::new(self.rem(rhs.x), self.rem(rhs.y))
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 Rem<DVec2> for &f64 {
1862 type Output = DVec2;
1863 #[inline]
1864 fn rem(self, rhs: DVec2) -> DVec2 {
1865 (*self).rem(rhs)
1866 }
1867}
1868
1869impl AsRef<[f64; 2]> for DVec2 {
1870 #[inline]
1871 fn as_ref(&self) -> &[f64; 2] {
1872 unsafe { &*(self as *const Self as *const [f64; 2]) }
1873 }
1874}
1875
1876impl AsMut<[f64; 2]> for DVec2 {
1877 #[inline]
1878 fn as_mut(&mut self) -> &mut [f64; 2] {
1879 unsafe { &mut *(self as *mut Self as *mut [f64; 2]) }
1880 }
1881}
1882
1883impl Sum for DVec2 {
1884 #[inline]
1885 fn sum<I>(iter: I) -> Self
1886 where
1887 I: Iterator<Item = Self>,
1888 {
1889 iter.fold(Self::ZERO, Self::add)
1890 }
1891}
1892
1893impl<'a> Sum<&'a Self> for DVec2 {
1894 #[inline]
1895 fn sum<I>(iter: I) -> Self
1896 where
1897 I: Iterator<Item = &'a Self>,
1898 {
1899 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1900 }
1901}
1902
1903impl Product for DVec2 {
1904 #[inline]
1905 fn product<I>(iter: I) -> Self
1906 where
1907 I: Iterator<Item = Self>,
1908 {
1909 iter.fold(Self::ONE, Self::mul)
1910 }
1911}
1912
1913impl<'a> Product<&'a Self> for DVec2 {
1914 #[inline]
1915 fn product<I>(iter: I) -> Self
1916 where
1917 I: Iterator<Item = &'a Self>,
1918 {
1919 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1920 }
1921}
1922
1923impl Neg for DVec2 {
1924 type Output = Self;
1925 #[inline]
1926 fn neg(self) -> Self {
1927 Self::new(self.x.neg(), self.y.neg())
1928 }
1929}
1930
1931impl Neg for &DVec2 {
1932 type Output = DVec2;
1933 #[inline]
1934 fn neg(self) -> DVec2 {
1935 (*self).neg()
1936 }
1937}
1938
1939impl Index<usize> for DVec2 {
1940 type Output = f64;
1941 #[inline]
1942 fn index(&self, index: usize) -> &Self::Output {
1943 match index {
1944 0 => &self.x,
1945 1 => &self.y,
1946 _ => panic!("index out of bounds"),
1947 }
1948 }
1949}
1950
1951impl IndexMut<usize> for DVec2 {
1952 #[inline]
1953 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1954 match index {
1955 0 => &mut self.x,
1956 1 => &mut self.y,
1957 _ => panic!("index out of bounds"),
1958 }
1959 }
1960}
1961
1962impl fmt::Display for DVec2 {
1963 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1964 if let Some(p) = f.precision() {
1965 write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y)
1966 } else {
1967 write!(f, "[{}, {}]", self.x, self.y)
1968 }
1969 }
1970}
1971
1972impl fmt::Debug for DVec2 {
1973 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1974 fmt.debug_tuple(stringify!(DVec2))
1975 .field(&self.x)
1976 .field(&self.y)
1977 .finish()
1978 }
1979}
1980
1981impl From<[f64; 2]> for DVec2 {
1982 #[inline]
1983 fn from(a: [f64; 2]) -> Self {
1984 Self::new(a[0], a[1])
1985 }
1986}
1987
1988impl From<DVec2> for [f64; 2] {
1989 #[inline]
1990 fn from(v: DVec2) -> Self {
1991 [v.x, v.y]
1992 }
1993}
1994
1995impl From<(f64, f64)> for DVec2 {
1996 #[inline]
1997 fn from(t: (f64, f64)) -> Self {
1998 Self::new(t.0, t.1)
1999 }
2000}
2001
2002impl From<DVec2> for (f64, f64) {
2003 #[inline]
2004 fn from(v: DVec2) -> Self {
2005 (v.x, v.y)
2006 }
2007}
2008
2009impl From<Vec2> for DVec2 {
2010 #[inline]
2011 fn from(v: Vec2) -> Self {
2012 Self::new(f64::from(v.x), f64::from(v.y))
2013 }
2014}
2015
2016#[cfg(feature = "i32")]
2017impl From<IVec2> for DVec2 {
2018 #[inline]
2019 fn from(v: IVec2) -> Self {
2020 Self::new(f64::from(v.x), f64::from(v.y))
2021 }
2022}
2023
2024#[cfg(feature = "u32")]
2025impl From<UVec2> for DVec2 {
2026 #[inline]
2027 fn from(v: UVec2) -> Self {
2028 Self::new(f64::from(v.x), f64::from(v.y))
2029 }
2030}
2031
2032impl From<BVec2> for DVec2 {
2033 #[inline]
2034 fn from(v: BVec2) -> Self {
2035 Self::new(f64::from(v.x), f64::from(v.y))
2036 }
2037}