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