• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Generated from vec.rs.tera template. Edit the template, not the generated file.
2 
3 use crate::{f32::math, BVec2, Vec3};
4 
5 use core::fmt;
6 use core::iter::{Product, Sum};
7 use core::{f32, ops::*};
8 
9 /// Creates a 2-dimensional vector.
10 #[inline(always)]
11 #[must_use]
vec2(x: f32, y: f32) -> Vec212 pub const fn vec2(x: f32, y: f32) -> Vec2 {
13     Vec2::new(x, y)
14 }
15 
16 /// A 2-dimensional vector.
17 #[derive(Clone, Copy, PartialEq)]
18 #[cfg_attr(feature = "cuda", repr(align(8)))]
19 #[cfg_attr(not(target_arch = "spirv"), repr(C))]
20 #[cfg_attr(target_arch = "spirv", repr(simd))]
21 pub struct Vec2 {
22     pub x: f32,
23     pub y: f32,
24 }
25 
26 impl Vec2 {
27     /// All zeroes.
28     pub const ZERO: Self = Self::splat(0.0);
29 
30     /// All ones.
31     pub const ONE: Self = Self::splat(1.0);
32 
33     /// All negative ones.
34     pub const NEG_ONE: Self = Self::splat(-1.0);
35 
36     /// All `f32::MIN`.
37     pub const MIN: Self = Self::splat(f32::MIN);
38 
39     /// All `f32::MAX`.
40     pub const MAX: Self = Self::splat(f32::MAX);
41 
42     /// All `f32::NAN`.
43     pub const NAN: Self = Self::splat(f32::NAN);
44 
45     /// All `f32::INFINITY`.
46     pub const INFINITY: Self = Self::splat(f32::INFINITY);
47 
48     /// All `f32::NEG_INFINITY`.
49     pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY);
50 
51     /// A unit vector pointing along the positive X axis.
52     pub const X: Self = Self::new(1.0, 0.0);
53 
54     /// A unit vector pointing along the positive Y axis.
55     pub const Y: Self = Self::new(0.0, 1.0);
56 
57     /// A unit vector pointing along the negative X axis.
58     pub const NEG_X: Self = Self::new(-1.0, 0.0);
59 
60     /// A unit vector pointing along the negative Y axis.
61     pub const NEG_Y: Self = Self::new(0.0, -1.0);
62 
63     /// The unit axes.
64     pub const AXES: [Self; 2] = [Self::X, Self::Y];
65 
66     /// Creates a new vector.
67     #[inline(always)]
68     #[must_use]
new(x: f32, y: f32) -> Self69     pub const fn new(x: f32, y: f32) -> Self {
70         Self { x, y }
71     }
72 
73     /// Creates a vector with all elements set to `v`.
74     #[inline]
75     #[must_use]
splat(v: f32) -> Self76     pub const fn splat(v: f32) -> Self {
77         Self { x: v, y: v }
78     }
79 
80     /// Returns a vector containing each element of `self` modified by a mapping function `f`.
81     #[inline]
82     #[must_use]
map<F>(self, f: F) -> Self where F: Fn(f32) -> f32,83     pub fn map<F>(self, f: F) -> Self
84     where
85         F: Fn(f32) -> f32,
86     {
87         Self::new(f(self.x), f(self.y))
88     }
89 
90     /// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
91     /// for each element of `self`.
92     ///
93     /// A true element in the mask uses the corresponding element from `if_true`, and false
94     /// uses the element from `if_false`.
95     #[inline]
96     #[must_use]
select(mask: BVec2, if_true: Self, if_false: Self) -> Self97     pub fn select(mask: BVec2, if_true: Self, if_false: Self) -> Self {
98         Self {
99             x: if mask.test(0) { if_true.x } else { if_false.x },
100             y: if mask.test(1) { if_true.y } else { if_false.y },
101         }
102     }
103 
104     /// Creates a new vector from an array.
105     #[inline]
106     #[must_use]
from_array(a: [f32; 2]) -> Self107     pub const fn from_array(a: [f32; 2]) -> Self {
108         Self::new(a[0], a[1])
109     }
110 
111     /// `[x, y]`
112     #[inline]
113     #[must_use]
to_array(&self) -> [f32; 2]114     pub const fn to_array(&self) -> [f32; 2] {
115         [self.x, self.y]
116     }
117 
118     /// Creates a vector from the first 2 values in `slice`.
119     ///
120     /// # Panics
121     ///
122     /// Panics if `slice` is less than 2 elements long.
123     #[inline]
124     #[must_use]
from_slice(slice: &[f32]) -> Self125     pub const fn from_slice(slice: &[f32]) -> Self {
126         assert!(slice.len() >= 2);
127         Self::new(slice[0], slice[1])
128     }
129 
130     /// Writes the elements of `self` to the first 2 elements in `slice`.
131     ///
132     /// # Panics
133     ///
134     /// Panics if `slice` is less than 2 elements long.
135     #[inline]
write_to_slice(self, slice: &mut [f32])136     pub fn write_to_slice(self, slice: &mut [f32]) {
137         slice[..2].copy_from_slice(&self.to_array());
138     }
139 
140     /// Creates a 3D vector from `self` and the given `z` value.
141     #[inline]
142     #[must_use]
extend(self, z: f32) -> Vec3143     pub const fn extend(self, z: f32) -> Vec3 {
144         Vec3::new(self.x, self.y, z)
145     }
146 
147     /// Creates a 2D vector from `self` with the given value of `x`.
148     #[inline]
149     #[must_use]
with_x(mut self, x: f32) -> Self150     pub fn with_x(mut self, x: f32) -> Self {
151         self.x = x;
152         self
153     }
154 
155     /// Creates a 2D vector from `self` with the given value of `y`.
156     #[inline]
157     #[must_use]
with_y(mut self, y: f32) -> Self158     pub fn with_y(mut self, y: f32) -> Self {
159         self.y = y;
160         self
161     }
162 
163     /// Computes the dot product of `self` and `rhs`.
164     #[inline]
165     #[must_use]
dot(self, rhs: Self) -> f32166     pub fn dot(self, rhs: Self) -> f32 {
167         (self.x * rhs.x) + (self.y * rhs.y)
168     }
169 
170     /// Returns a vector where every component is the dot product of `self` and `rhs`.
171     #[inline]
172     #[must_use]
dot_into_vec(self, rhs: Self) -> Self173     pub fn dot_into_vec(self, rhs: Self) -> Self {
174         Self::splat(self.dot(rhs))
175     }
176 
177     /// Returns a vector containing the minimum values for each element of `self` and `rhs`.
178     ///
179     /// In other words this computes `[self.x.min(rhs.x), self.y.min(rhs.y), ..]`.
180     #[inline]
181     #[must_use]
min(self, rhs: Self) -> Self182     pub fn min(self, rhs: Self) -> Self {
183         Self {
184             x: self.x.min(rhs.x),
185             y: self.y.min(rhs.y),
186         }
187     }
188 
189     /// Returns a vector containing the maximum values for each element of `self` and `rhs`.
190     ///
191     /// In other words this computes `[self.x.max(rhs.x), self.y.max(rhs.y), ..]`.
192     #[inline]
193     #[must_use]
max(self, rhs: Self) -> Self194     pub fn max(self, rhs: Self) -> Self {
195         Self {
196             x: self.x.max(rhs.x),
197             y: self.y.max(rhs.y),
198         }
199     }
200 
201     /// Component-wise clamping of values, similar to [`f32::clamp`].
202     ///
203     /// Each element in `min` must be less-or-equal to the corresponding element in `max`.
204     ///
205     /// # Panics
206     ///
207     /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
208     #[inline]
209     #[must_use]
clamp(self, min: Self, max: Self) -> Self210     pub fn clamp(self, min: Self, max: Self) -> Self {
211         glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
212         self.max(min).min(max)
213     }
214 
215     /// Returns the horizontal minimum of `self`.
216     ///
217     /// In other words this computes `min(x, y, ..)`.
218     #[inline]
219     #[must_use]
min_element(self) -> f32220     pub fn min_element(self) -> f32 {
221         self.x.min(self.y)
222     }
223 
224     /// Returns the horizontal maximum of `self`.
225     ///
226     /// In other words this computes `max(x, y, ..)`.
227     #[inline]
228     #[must_use]
max_element(self) -> f32229     pub fn max_element(self) -> f32 {
230         self.x.max(self.y)
231     }
232 
233     /// Returns the sum of all elements of `self`.
234     ///
235     /// In other words, this computes `self.x + self.y + ..`.
236     #[inline]
237     #[must_use]
element_sum(self) -> f32238     pub fn element_sum(self) -> f32 {
239         self.x + self.y
240     }
241 
242     /// Returns the product of all elements of `self`.
243     ///
244     /// In other words, this computes `self.x * self.y * ..`.
245     #[inline]
246     #[must_use]
element_product(self) -> f32247     pub fn element_product(self) -> f32 {
248         self.x * self.y
249     }
250 
251     /// Returns a vector mask containing the result of a `==` comparison for each element of
252     /// `self` and `rhs`.
253     ///
254     /// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all
255     /// elements.
256     #[inline]
257     #[must_use]
cmpeq(self, rhs: Self) -> BVec2258     pub fn cmpeq(self, rhs: Self) -> BVec2 {
259         BVec2::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y))
260     }
261 
262     /// Returns a vector mask containing the result of a `!=` comparison for each element of
263     /// `self` and `rhs`.
264     ///
265     /// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all
266     /// elements.
267     #[inline]
268     #[must_use]
cmpne(self, rhs: Self) -> BVec2269     pub fn cmpne(self, rhs: Self) -> BVec2 {
270         BVec2::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y))
271     }
272 
273     /// Returns a vector mask containing the result of a `>=` comparison for each element of
274     /// `self` and `rhs`.
275     ///
276     /// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all
277     /// elements.
278     #[inline]
279     #[must_use]
cmpge(self, rhs: Self) -> BVec2280     pub fn cmpge(self, rhs: Self) -> BVec2 {
281         BVec2::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y))
282     }
283 
284     /// Returns a vector mask containing the result of a `>` comparison for each element of
285     /// `self` and `rhs`.
286     ///
287     /// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all
288     /// elements.
289     #[inline]
290     #[must_use]
cmpgt(self, rhs: Self) -> BVec2291     pub fn cmpgt(self, rhs: Self) -> BVec2 {
292         BVec2::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y))
293     }
294 
295     /// Returns a vector mask containing the result of a `<=` comparison for each element of
296     /// `self` and `rhs`.
297     ///
298     /// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all
299     /// elements.
300     #[inline]
301     #[must_use]
cmple(self, rhs: Self) -> BVec2302     pub fn cmple(self, rhs: Self) -> BVec2 {
303         BVec2::new(self.x.le(&rhs.x), self.y.le(&rhs.y))
304     }
305 
306     /// Returns a vector mask containing the result of a `<` comparison for each element of
307     /// `self` and `rhs`.
308     ///
309     /// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all
310     /// elements.
311     #[inline]
312     #[must_use]
cmplt(self, rhs: Self) -> BVec2313     pub fn cmplt(self, rhs: Self) -> BVec2 {
314         BVec2::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y))
315     }
316 
317     /// Returns a vector containing the absolute value of each element of `self`.
318     #[inline]
319     #[must_use]
abs(self) -> Self320     pub fn abs(self) -> Self {
321         Self {
322             x: math::abs(self.x),
323             y: math::abs(self.y),
324         }
325     }
326 
327     /// Returns a vector with elements representing the sign of `self`.
328     ///
329     /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
330     /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
331     /// - `NAN` if the number is `NAN`
332     #[inline]
333     #[must_use]
signum(self) -> Self334     pub fn signum(self) -> Self {
335         Self {
336             x: math::signum(self.x),
337             y: math::signum(self.y),
338         }
339     }
340 
341     /// Returns a vector with signs of `rhs` and the magnitudes of `self`.
342     #[inline]
343     #[must_use]
copysign(self, rhs: Self) -> Self344     pub fn copysign(self, rhs: Self) -> Self {
345         Self {
346             x: math::copysign(self.x, rhs.x),
347             y: math::copysign(self.y, rhs.y),
348         }
349     }
350 
351     /// Returns a bitmask with the lowest 2 bits set to the sign bits from the elements of `self`.
352     ///
353     /// A negative element results in a `1` bit and a positive element in a `0` bit.  Element `x` goes
354     /// into the first lowest bit, element `y` into the second, etc.
355     #[inline]
356     #[must_use]
is_negative_bitmask(self) -> u32357     pub fn is_negative_bitmask(self) -> u32 {
358         (self.x.is_sign_negative() as u32) | (self.y.is_sign_negative() as u32) << 1
359     }
360 
361     /// Returns `true` if, and only if, all elements are finite.  If any element is either
362     /// `NaN`, positive or negative infinity, this will return `false`.
363     #[inline]
364     #[must_use]
is_finite(self) -> bool365     pub fn is_finite(self) -> bool {
366         self.x.is_finite() && self.y.is_finite()
367     }
368 
369     /// Performs `is_finite` on each element of self, returning a vector mask of the results.
370     ///
371     /// In other words, this computes `[x.is_finite(), y.is_finite(), ...]`.
is_finite_mask(self) -> BVec2372     pub fn is_finite_mask(self) -> BVec2 {
373         BVec2::new(self.x.is_finite(), self.y.is_finite())
374     }
375 
376     /// Returns `true` if any elements are `NaN`.
377     #[inline]
378     #[must_use]
is_nan(self) -> bool379     pub fn is_nan(self) -> bool {
380         self.x.is_nan() || self.y.is_nan()
381     }
382 
383     /// Performs `is_nan` on each element of self, returning a vector mask of the results.
384     ///
385     /// In other words, this computes `[x.is_nan(), y.is_nan(), ...]`.
386     #[inline]
387     #[must_use]
is_nan_mask(self) -> BVec2388     pub fn is_nan_mask(self) -> BVec2 {
389         BVec2::new(self.x.is_nan(), self.y.is_nan())
390     }
391 
392     /// Computes the length of `self`.
393     #[doc(alias = "magnitude")]
394     #[inline]
395     #[must_use]
length(self) -> f32396     pub fn length(self) -> f32 {
397         math::sqrt(self.dot(self))
398     }
399 
400     /// Computes the squared length of `self`.
401     ///
402     /// This is faster than `length()` as it avoids a square root operation.
403     #[doc(alias = "magnitude2")]
404     #[inline]
405     #[must_use]
length_squared(self) -> f32406     pub fn length_squared(self) -> f32 {
407         self.dot(self)
408     }
409 
410     /// Computes `1.0 / length()`.
411     ///
412     /// For valid results, `self` must _not_ be of length zero.
413     #[inline]
414     #[must_use]
length_recip(self) -> f32415     pub fn length_recip(self) -> f32 {
416         self.length().recip()
417     }
418 
419     /// Computes the Euclidean distance between two points in space.
420     #[inline]
421     #[must_use]
distance(self, rhs: Self) -> f32422     pub fn distance(self, rhs: Self) -> f32 {
423         (self - rhs).length()
424     }
425 
426     /// Compute the squared euclidean distance between two points in space.
427     #[inline]
428     #[must_use]
distance_squared(self, rhs: Self) -> f32429     pub fn distance_squared(self, rhs: Self) -> f32 {
430         (self - rhs).length_squared()
431     }
432 
433     /// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
434     #[inline]
435     #[must_use]
div_euclid(self, rhs: Self) -> Self436     pub fn div_euclid(self, rhs: Self) -> Self {
437         Self::new(
438             math::div_euclid(self.x, rhs.x),
439             math::div_euclid(self.y, rhs.y),
440         )
441     }
442 
443     /// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
444     ///
445     /// [Euclidean division]: f32::rem_euclid
446     #[inline]
447     #[must_use]
rem_euclid(self, rhs: Self) -> Self448     pub fn rem_euclid(self, rhs: Self) -> Self {
449         Self::new(
450             math::rem_euclid(self.x, rhs.x),
451             math::rem_euclid(self.y, rhs.y),
452         )
453     }
454 
455     /// Returns `self` normalized to length 1.0.
456     ///
457     /// For valid results, `self` must be finite and _not_ of length zero, nor very close to zero.
458     ///
459     /// See also [`Self::try_normalize()`] and [`Self::normalize_or_zero()`].
460     ///
461     /// Panics
462     ///
463     /// Will panic if the resulting normalized vector is not finite when `glam_assert` is enabled.
464     #[inline]
465     #[must_use]
normalize(self) -> Self466     pub fn normalize(self) -> Self {
467         #[allow(clippy::let_and_return)]
468         let normalized = self.mul(self.length_recip());
469         glam_assert!(normalized.is_finite());
470         normalized
471     }
472 
473     /// Returns `self` normalized to length 1.0 if possible, else returns `None`.
474     ///
475     /// In particular, if the input is zero (or very close to zero), or non-finite,
476     /// the result of this operation will be `None`.
477     ///
478     /// See also [`Self::normalize_or_zero()`].
479     #[inline]
480     #[must_use]
try_normalize(self) -> Option<Self>481     pub fn try_normalize(self) -> Option<Self> {
482         let rcp = self.length_recip();
483         if rcp.is_finite() && rcp > 0.0 {
484             Some(self * rcp)
485         } else {
486             None
487         }
488     }
489 
490     /// Returns `self` normalized to length 1.0 if possible, else returns a
491     /// fallback value.
492     ///
493     /// In particular, if the input is zero (or very close to zero), or non-finite,
494     /// the result of this operation will be the fallback value.
495     ///
496     /// See also [`Self::try_normalize()`].
497     #[inline]
498     #[must_use]
normalize_or(self, fallback: Self) -> Self499     pub fn normalize_or(self, fallback: Self) -> Self {
500         let rcp = self.length_recip();
501         if rcp.is_finite() && rcp > 0.0 {
502             self * rcp
503         } else {
504             fallback
505         }
506     }
507 
508     /// Returns `self` normalized to length 1.0 if possible, else returns zero.
509     ///
510     /// In particular, if the input is zero (or very close to zero), or non-finite,
511     /// the result of this operation will be zero.
512     ///
513     /// See also [`Self::try_normalize()`].
514     #[inline]
515     #[must_use]
normalize_or_zero(self) -> Self516     pub fn normalize_or_zero(self) -> Self {
517         self.normalize_or(Self::ZERO)
518     }
519 
520     /// Returns whether `self` is length `1.0` or not.
521     ///
522     /// Uses a precision threshold of approximately `1e-4`.
523     #[inline]
524     #[must_use]
is_normalized(self) -> bool525     pub fn is_normalized(self) -> bool {
526         math::abs(self.length_squared() - 1.0) <= 2e-4
527     }
528 
529     /// Returns the vector projection of `self` onto `rhs`.
530     ///
531     /// `rhs` must be of non-zero length.
532     ///
533     /// # Panics
534     ///
535     /// Will panic if `rhs` is zero length when `glam_assert` is enabled.
536     #[inline]
537     #[must_use]
project_onto(self, rhs: Self) -> Self538     pub fn project_onto(self, rhs: Self) -> Self {
539         let other_len_sq_rcp = rhs.dot(rhs).recip();
540         glam_assert!(other_len_sq_rcp.is_finite());
541         rhs * self.dot(rhs) * other_len_sq_rcp
542     }
543 
544     /// Returns the vector rejection of `self` from `rhs`.
545     ///
546     /// The vector rejection is the vector perpendicular to the projection of `self` onto
547     /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
548     ///
549     /// `rhs` must be of non-zero length.
550     ///
551     /// # Panics
552     ///
553     /// Will panic if `rhs` has a length of zero when `glam_assert` is enabled.
554     #[doc(alias("plane"))]
555     #[inline]
556     #[must_use]
reject_from(self, rhs: Self) -> Self557     pub fn reject_from(self, rhs: Self) -> Self {
558         self - self.project_onto(rhs)
559     }
560 
561     /// Returns the vector projection of `self` onto `rhs`.
562     ///
563     /// `rhs` must be normalized.
564     ///
565     /// # Panics
566     ///
567     /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
568     #[inline]
569     #[must_use]
project_onto_normalized(self, rhs: Self) -> Self570     pub fn project_onto_normalized(self, rhs: Self) -> Self {
571         glam_assert!(rhs.is_normalized());
572         rhs * self.dot(rhs)
573     }
574 
575     /// Returns the vector rejection of `self` from `rhs`.
576     ///
577     /// The vector rejection is the vector perpendicular to the projection of `self` onto
578     /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
579     ///
580     /// `rhs` must be normalized.
581     ///
582     /// # Panics
583     ///
584     /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
585     #[doc(alias("plane"))]
586     #[inline]
587     #[must_use]
reject_from_normalized(self, rhs: Self) -> Self588     pub fn reject_from_normalized(self, rhs: Self) -> Self {
589         self - self.project_onto_normalized(rhs)
590     }
591 
592     /// Returns a vector containing the nearest integer to a number for each element of `self`.
593     /// Round half-way cases away from 0.0.
594     #[inline]
595     #[must_use]
round(self) -> Self596     pub fn round(self) -> Self {
597         Self {
598             x: math::round(self.x),
599             y: math::round(self.y),
600         }
601     }
602 
603     /// Returns a vector containing the largest integer less than or equal to a number for each
604     /// element of `self`.
605     #[inline]
606     #[must_use]
floor(self) -> Self607     pub fn floor(self) -> Self {
608         Self {
609             x: math::floor(self.x),
610             y: math::floor(self.y),
611         }
612     }
613 
614     /// Returns a vector containing the smallest integer greater than or equal to a number for
615     /// each element of `self`.
616     #[inline]
617     #[must_use]
ceil(self) -> Self618     pub fn ceil(self) -> Self {
619         Self {
620             x: math::ceil(self.x),
621             y: math::ceil(self.y),
622         }
623     }
624 
625     /// Returns a vector containing the integer part each element of `self`. This means numbers are
626     /// always truncated towards zero.
627     #[inline]
628     #[must_use]
trunc(self) -> Self629     pub fn trunc(self) -> Self {
630         Self {
631             x: math::trunc(self.x),
632             y: math::trunc(self.y),
633         }
634     }
635 
636     /// Returns a vector containing the fractional part of the vector as `self - self.trunc()`.
637     ///
638     /// Note that this differs from the GLSL implementation of `fract` which returns
639     /// `self - self.floor()`.
640     ///
641     /// Note that this is fast but not precise for large numbers.
642     #[inline]
643     #[must_use]
fract(self) -> Self644     pub fn fract(self) -> Self {
645         self - self.trunc()
646     }
647 
648     /// Returns a vector containing the fractional part of the vector as `self - self.floor()`.
649     ///
650     /// Note that this differs from the Rust implementation of `fract` which returns
651     /// `self - self.trunc()`.
652     ///
653     /// Note that this is fast but not precise for large numbers.
654     #[inline]
655     #[must_use]
fract_gl(self) -> Self656     pub fn fract_gl(self) -> Self {
657         self - self.floor()
658     }
659 
660     /// Returns a vector containing `e^self` (the exponential function) for each element of
661     /// `self`.
662     #[inline]
663     #[must_use]
exp(self) -> Self664     pub fn exp(self) -> Self {
665         Self::new(math::exp(self.x), math::exp(self.y))
666     }
667 
668     /// Returns a vector containing each element of `self` raised to the power of `n`.
669     #[inline]
670     #[must_use]
powf(self, n: f32) -> Self671     pub fn powf(self, n: f32) -> Self {
672         Self::new(math::powf(self.x, n), math::powf(self.y, n))
673     }
674 
675     /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
676     #[inline]
677     #[must_use]
recip(self) -> Self678     pub fn recip(self) -> Self {
679         Self {
680             x: 1.0 / self.x,
681             y: 1.0 / self.y,
682         }
683     }
684 
685     /// Performs a linear interpolation between `self` and `rhs` based on the value `s`.
686     ///
687     /// When `s` is `0.0`, the result will be equal to `self`.  When `s` is `1.0`, the result
688     /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
689     /// extrapolated.
690     #[doc(alias = "mix")]
691     #[inline]
692     #[must_use]
lerp(self, rhs: Self, s: f32) -> Self693     pub fn lerp(self, rhs: Self, s: f32) -> Self {
694         self * (1.0 - s) + rhs * s
695     }
696 
697     /// Moves towards `rhs` based on the value `d`.
698     ///
699     /// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
700     /// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
701     #[inline]
702     #[must_use]
move_towards(&self, rhs: Self, d: f32) -> Self703     pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
704         let a = rhs - *self;
705         let len = a.length();
706         if len <= d || len <= 1e-4 {
707             return rhs;
708         }
709         *self + a / len * d
710     }
711 
712     /// Calculates the midpoint between `self` and `rhs`.
713     ///
714     /// The midpoint is the average of, or halfway point between, two vectors.
715     /// `a.midpoint(b)` should yield the same result as `a.lerp(b, 0.5)`
716     /// while being slightly cheaper to compute.
717     #[inline]
midpoint(self, rhs: Self) -> Self718     pub fn midpoint(self, rhs: Self) -> Self {
719         (self + rhs) * 0.5
720     }
721 
722     /// Returns true if the absolute difference of all elements between `self` and `rhs` is
723     /// less than or equal to `max_abs_diff`.
724     ///
725     /// This can be used to compare if two vectors contain similar elements. It works best when
726     /// comparing with a known value. The `max_abs_diff` that should be used used depends on
727     /// the values being compared against.
728     ///
729     /// For more see
730     /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
731     #[inline]
732     #[must_use]
abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool733     pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
734         self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
735     }
736 
737     /// Returns a vector with a length no less than `min` and no more than `max`.
738     ///
739     /// # Panics
740     ///
741     /// Will panic if `min` is greater than `max`, or if either `min` or `max` is negative, when `glam_assert` is enabled.
742     #[inline]
743     #[must_use]
clamp_length(self, min: f32, max: f32) -> Self744     pub fn clamp_length(self, min: f32, max: f32) -> Self {
745         glam_assert!(0.0 <= min);
746         glam_assert!(min <= max);
747         let length_sq = self.length_squared();
748         if length_sq < min * min {
749             min * (self / math::sqrt(length_sq))
750         } else if length_sq > max * max {
751             max * (self / math::sqrt(length_sq))
752         } else {
753             self
754         }
755     }
756 
757     /// Returns a vector with a length no more than `max`.
758     ///
759     /// # Panics
760     ///
761     /// Will panic if `max` is negative when `glam_assert` is enabled.
762     #[inline]
763     #[must_use]
clamp_length_max(self, max: f32) -> Self764     pub fn clamp_length_max(self, max: f32) -> Self {
765         glam_assert!(0.0 <= max);
766         let length_sq = self.length_squared();
767         if length_sq > max * max {
768             max * (self / math::sqrt(length_sq))
769         } else {
770             self
771         }
772     }
773 
774     /// Returns a vector with a length no less than `min`.
775     ///
776     /// # Panics
777     ///
778     /// Will panic if `min` is negative when `glam_assert` is enabled.
779     #[inline]
780     #[must_use]
clamp_length_min(self, min: f32) -> Self781     pub fn clamp_length_min(self, min: f32) -> Self {
782         glam_assert!(0.0 <= min);
783         let length_sq = self.length_squared();
784         if length_sq < min * min {
785             min * (self / math::sqrt(length_sq))
786         } else {
787             self
788         }
789     }
790 
791     /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding
792     /// error, yielding a more accurate result than an unfused multiply-add.
793     ///
794     /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
795     /// architecture has a dedicated fma CPU instruction. However, this is not always true,
796     /// and will be heavily dependant on designing algorithms with specific target hardware in
797     /// mind.
798     #[inline]
799     #[must_use]
mul_add(self, a: Self, b: Self) -> Self800     pub fn mul_add(self, a: Self, b: Self) -> Self {
801         Self::new(
802             math::mul_add(self.x, a.x, b.x),
803             math::mul_add(self.y, a.y, b.y),
804         )
805     }
806 
807     /// Returns the reflection vector for a given incident vector `self` and surface normal
808     /// `normal`.
809     ///
810     /// `normal` must be normalized.
811     ///
812     /// # Panics
813     ///
814     /// Will panic if `normal` is not normalized when `glam_assert` is enabled.
815     #[inline]
816     #[must_use]
reflect(self, normal: Self) -> Self817     pub fn reflect(self, normal: Self) -> Self {
818         glam_assert!(normal.is_normalized());
819         self - 2.0 * self.dot(normal) * normal
820     }
821 
822     /// Returns the refraction direction for a given incident vector `self`, surface normal
823     /// `normal` and ratio of indices of refraction, `eta`. When total internal reflection occurs,
824     /// a zero vector will be returned.
825     ///
826     /// `self` and `normal` must be normalized.
827     ///
828     /// # Panics
829     ///
830     /// Will panic if `self` or `normal` is not normalized when `glam_assert` is enabled.
831     #[inline]
832     #[must_use]
refract(self, normal: Self, eta: f32) -> Self833     pub fn refract(self, normal: Self, eta: f32) -> Self {
834         glam_assert!(self.is_normalized());
835         glam_assert!(normal.is_normalized());
836         let n_dot_i = normal.dot(self);
837         let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
838         if k >= 0.0 {
839             eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
840         } else {
841             Self::ZERO
842         }
843     }
844 
845     /// Creates a 2D vector containing `[angle.cos(), angle.sin()]`. This can be used in
846     /// conjunction with the [`rotate()`][Self::rotate()] method, e.g.
847     /// `Vec2::from_angle(PI).rotate(Vec2::Y)` will create the vector `[-1, 0]`
848     /// and rotate [`Vec2::Y`] around it returning `-Vec2::Y`.
849     #[inline]
850     #[must_use]
from_angle(angle: f32) -> Self851     pub fn from_angle(angle: f32) -> Self {
852         let (sin, cos) = math::sin_cos(angle);
853         Self { x: cos, y: sin }
854     }
855 
856     /// Returns the angle (in radians) of this vector in the range `[-π, +π]`.
857     ///
858     /// The input does not need to be a unit vector however it must be non-zero.
859     #[inline]
860     #[must_use]
to_angle(self) -> f32861     pub fn to_angle(self) -> f32 {
862         math::atan2(self.y, self.x)
863     }
864 
865     #[inline]
866     #[must_use]
867     #[deprecated(
868         since = "0.27.0",
869         note = "Use angle_to() instead, the semantics of angle_between will change in the future."
870     )]
angle_between(self, rhs: Self) -> f32871     pub fn angle_between(self, rhs: Self) -> f32 {
872         self.angle_to(rhs)
873     }
874 
875     /// Returns the angle of rotation (in radians) from `self` to `rhs` in the range `[-π, +π]`.
876     ///
877     /// The inputs do not need to be unit vectors however they must be non-zero.
878     #[inline]
879     #[must_use]
angle_to(self, rhs: Self) -> f32880     pub fn angle_to(self, rhs: Self) -> f32 {
881         let angle = math::acos_approx(
882             self.dot(rhs) / math::sqrt(self.length_squared() * rhs.length_squared()),
883         );
884 
885         angle * math::signum(self.perp_dot(rhs))
886     }
887 
888     /// Returns a vector that is equal to `self` rotated by 90 degrees.
889     #[inline]
890     #[must_use]
perp(self) -> Self891     pub fn perp(self) -> Self {
892         Self {
893             x: -self.y,
894             y: self.x,
895         }
896     }
897 
898     /// The perpendicular dot product of `self` and `rhs`.
899     /// Also known as the wedge product, 2D cross product, and determinant.
900     #[doc(alias = "wedge")]
901     #[doc(alias = "cross")]
902     #[doc(alias = "determinant")]
903     #[inline]
904     #[must_use]
perp_dot(self, rhs: Self) -> f32905     pub fn perp_dot(self, rhs: Self) -> f32 {
906         (self.x * rhs.y) - (self.y * rhs.x)
907     }
908 
909     /// Returns `rhs` rotated by the angle of `self`. If `self` is normalized,
910     /// then this just rotation. This is what you usually want. Otherwise,
911     /// it will be like a rotation with a multiplication by `self`'s length.
912     #[inline]
913     #[must_use]
rotate(self, rhs: Self) -> Self914     pub fn rotate(self, rhs: Self) -> Self {
915         Self {
916             x: self.x * rhs.x - self.y * rhs.y,
917             y: self.y * rhs.x + self.x * rhs.y,
918         }
919     }
920 
921     /// Rotates towards `rhs` up to `max_angle` (in radians).
922     ///
923     /// When `max_angle` is `0.0`, the result will be equal to `self`. When `max_angle` is equal to
924     /// `self.angle_between(rhs)`, the result will be parallel to `rhs`. If `max_angle` is negative,
925     /// rotates towards the exact opposite of `rhs`. Will not go past the target.
926     #[inline]
927     #[must_use]
rotate_towards(&self, rhs: Self, max_angle: f32) -> Self928     pub fn rotate_towards(&self, rhs: Self, max_angle: f32) -> Self {
929         let a = self.angle_to(rhs);
930         let abs_a = math::abs(a);
931         if abs_a <= 1e-4 {
932             return rhs;
933         }
934         // When `max_angle < 0`, rotate no further than `PI` radians away
935         let angle = max_angle.clamp(abs_a - core::f32::consts::PI, abs_a) * math::signum(a);
936         Self::from_angle(angle).rotate(*self)
937     }
938 
939     /// Casts all elements of `self` to `f64`.
940     #[inline]
941     #[must_use]
as_dvec2(&self) -> crate::DVec2942     pub fn as_dvec2(&self) -> crate::DVec2 {
943         crate::DVec2::new(self.x as f64, self.y as f64)
944     }
945 
946     /// Casts all elements of `self` to `i8`.
947     #[inline]
948     #[must_use]
as_i8vec2(&self) -> crate::I8Vec2949     pub fn as_i8vec2(&self) -> crate::I8Vec2 {
950         crate::I8Vec2::new(self.x as i8, self.y as i8)
951     }
952 
953     /// Casts all elements of `self` to `u8`.
954     #[inline]
955     #[must_use]
as_u8vec2(&self) -> crate::U8Vec2956     pub fn as_u8vec2(&self) -> crate::U8Vec2 {
957         crate::U8Vec2::new(self.x as u8, self.y as u8)
958     }
959 
960     /// Casts all elements of `self` to `i16`.
961     #[inline]
962     #[must_use]
as_i16vec2(&self) -> crate::I16Vec2963     pub fn as_i16vec2(&self) -> crate::I16Vec2 {
964         crate::I16Vec2::new(self.x as i16, self.y as i16)
965     }
966 
967     /// Casts all elements of `self` to `u16`.
968     #[inline]
969     #[must_use]
as_u16vec2(&self) -> crate::U16Vec2970     pub fn as_u16vec2(&self) -> crate::U16Vec2 {
971         crate::U16Vec2::new(self.x as u16, self.y as u16)
972     }
973 
974     /// Casts all elements of `self` to `i32`.
975     #[inline]
976     #[must_use]
as_ivec2(&self) -> crate::IVec2977     pub fn as_ivec2(&self) -> crate::IVec2 {
978         crate::IVec2::new(self.x as i32, self.y as i32)
979     }
980 
981     /// Casts all elements of `self` to `u32`.
982     #[inline]
983     #[must_use]
as_uvec2(&self) -> crate::UVec2984     pub fn as_uvec2(&self) -> crate::UVec2 {
985         crate::UVec2::new(self.x as u32, self.y as u32)
986     }
987 
988     /// Casts all elements of `self` to `i64`.
989     #[inline]
990     #[must_use]
as_i64vec2(&self) -> crate::I64Vec2991     pub fn as_i64vec2(&self) -> crate::I64Vec2 {
992         crate::I64Vec2::new(self.x as i64, self.y as i64)
993     }
994 
995     /// Casts all elements of `self` to `u64`.
996     #[inline]
997     #[must_use]
as_u64vec2(&self) -> crate::U64Vec2998     pub fn as_u64vec2(&self) -> crate::U64Vec2 {
999         crate::U64Vec2::new(self.x as u64, self.y as u64)
1000     }
1001 }
1002 
1003 impl Default for Vec2 {
1004     #[inline(always)]
default() -> Self1005     fn default() -> Self {
1006         Self::ZERO
1007     }
1008 }
1009 
1010 impl Div<Vec2> for Vec2 {
1011     type Output = Self;
1012     #[inline]
div(self, rhs: Self) -> Self1013     fn div(self, rhs: Self) -> Self {
1014         Self {
1015             x: self.x.div(rhs.x),
1016             y: self.y.div(rhs.y),
1017         }
1018     }
1019 }
1020 
1021 impl Div<&Vec2> for Vec2 {
1022     type Output = Vec2;
1023     #[inline]
div(self, rhs: &Vec2) -> Vec21024     fn div(self, rhs: &Vec2) -> Vec2 {
1025         self.div(*rhs)
1026     }
1027 }
1028 
1029 impl Div<&Vec2> for &Vec2 {
1030     type Output = Vec2;
1031     #[inline]
div(self, rhs: &Vec2) -> Vec21032     fn div(self, rhs: &Vec2) -> Vec2 {
1033         (*self).div(*rhs)
1034     }
1035 }
1036 
1037 impl Div<Vec2> for &Vec2 {
1038     type Output = Vec2;
1039     #[inline]
div(self, rhs: Vec2) -> Vec21040     fn div(self, rhs: Vec2) -> Vec2 {
1041         (*self).div(rhs)
1042     }
1043 }
1044 
1045 impl DivAssign<Vec2> for Vec2 {
1046     #[inline]
div_assign(&mut self, rhs: Self)1047     fn div_assign(&mut self, rhs: Self) {
1048         self.x.div_assign(rhs.x);
1049         self.y.div_assign(rhs.y);
1050     }
1051 }
1052 
1053 impl DivAssign<&Vec2> for Vec2 {
1054     #[inline]
div_assign(&mut self, rhs: &Vec2)1055     fn div_assign(&mut self, rhs: &Vec2) {
1056         self.div_assign(*rhs)
1057     }
1058 }
1059 
1060 impl Div<f32> for Vec2 {
1061     type Output = Self;
1062     #[inline]
div(self, rhs: f32) -> Self1063     fn div(self, rhs: f32) -> Self {
1064         Self {
1065             x: self.x.div(rhs),
1066             y: self.y.div(rhs),
1067         }
1068     }
1069 }
1070 
1071 impl Div<&f32> for Vec2 {
1072     type Output = Vec2;
1073     #[inline]
div(self, rhs: &f32) -> Vec21074     fn div(self, rhs: &f32) -> Vec2 {
1075         self.div(*rhs)
1076     }
1077 }
1078 
1079 impl Div<&f32> for &Vec2 {
1080     type Output = Vec2;
1081     #[inline]
div(self, rhs: &f32) -> Vec21082     fn div(self, rhs: &f32) -> Vec2 {
1083         (*self).div(*rhs)
1084     }
1085 }
1086 
1087 impl Div<f32> for &Vec2 {
1088     type Output = Vec2;
1089     #[inline]
div(self, rhs: f32) -> Vec21090     fn div(self, rhs: f32) -> Vec2 {
1091         (*self).div(rhs)
1092     }
1093 }
1094 
1095 impl DivAssign<f32> for Vec2 {
1096     #[inline]
div_assign(&mut self, rhs: f32)1097     fn div_assign(&mut self, rhs: f32) {
1098         self.x.div_assign(rhs);
1099         self.y.div_assign(rhs);
1100     }
1101 }
1102 
1103 impl DivAssign<&f32> for Vec2 {
1104     #[inline]
div_assign(&mut self, rhs: &f32)1105     fn div_assign(&mut self, rhs: &f32) {
1106         self.div_assign(*rhs)
1107     }
1108 }
1109 
1110 impl Div<Vec2> for f32 {
1111     type Output = Vec2;
1112     #[inline]
div(self, rhs: Vec2) -> Vec21113     fn div(self, rhs: Vec2) -> Vec2 {
1114         Vec2 {
1115             x: self.div(rhs.x),
1116             y: self.div(rhs.y),
1117         }
1118     }
1119 }
1120 
1121 impl Div<&Vec2> for f32 {
1122     type Output = Vec2;
1123     #[inline]
div(self, rhs: &Vec2) -> Vec21124     fn div(self, rhs: &Vec2) -> Vec2 {
1125         self.div(*rhs)
1126     }
1127 }
1128 
1129 impl Div<&Vec2> for &f32 {
1130     type Output = Vec2;
1131     #[inline]
div(self, rhs: &Vec2) -> Vec21132     fn div(self, rhs: &Vec2) -> Vec2 {
1133         (*self).div(*rhs)
1134     }
1135 }
1136 
1137 impl Div<Vec2> for &f32 {
1138     type Output = Vec2;
1139     #[inline]
div(self, rhs: Vec2) -> Vec21140     fn div(self, rhs: Vec2) -> Vec2 {
1141         (*self).div(rhs)
1142     }
1143 }
1144 
1145 impl Mul<Vec2> for Vec2 {
1146     type Output = Self;
1147     #[inline]
mul(self, rhs: Self) -> Self1148     fn mul(self, rhs: Self) -> Self {
1149         Self {
1150             x: self.x.mul(rhs.x),
1151             y: self.y.mul(rhs.y),
1152         }
1153     }
1154 }
1155 
1156 impl Mul<&Vec2> for Vec2 {
1157     type Output = Vec2;
1158     #[inline]
mul(self, rhs: &Vec2) -> Vec21159     fn mul(self, rhs: &Vec2) -> Vec2 {
1160         self.mul(*rhs)
1161     }
1162 }
1163 
1164 impl Mul<&Vec2> for &Vec2 {
1165     type Output = Vec2;
1166     #[inline]
mul(self, rhs: &Vec2) -> Vec21167     fn mul(self, rhs: &Vec2) -> Vec2 {
1168         (*self).mul(*rhs)
1169     }
1170 }
1171 
1172 impl Mul<Vec2> for &Vec2 {
1173     type Output = Vec2;
1174     #[inline]
mul(self, rhs: Vec2) -> Vec21175     fn mul(self, rhs: Vec2) -> Vec2 {
1176         (*self).mul(rhs)
1177     }
1178 }
1179 
1180 impl MulAssign<Vec2> for Vec2 {
1181     #[inline]
mul_assign(&mut self, rhs: Self)1182     fn mul_assign(&mut self, rhs: Self) {
1183         self.x.mul_assign(rhs.x);
1184         self.y.mul_assign(rhs.y);
1185     }
1186 }
1187 
1188 impl MulAssign<&Vec2> for Vec2 {
1189     #[inline]
mul_assign(&mut self, rhs: &Vec2)1190     fn mul_assign(&mut self, rhs: &Vec2) {
1191         self.mul_assign(*rhs)
1192     }
1193 }
1194 
1195 impl Mul<f32> for Vec2 {
1196     type Output = Self;
1197     #[inline]
mul(self, rhs: f32) -> Self1198     fn mul(self, rhs: f32) -> Self {
1199         Self {
1200             x: self.x.mul(rhs),
1201             y: self.y.mul(rhs),
1202         }
1203     }
1204 }
1205 
1206 impl Mul<&f32> for Vec2 {
1207     type Output = Vec2;
1208     #[inline]
mul(self, rhs: &f32) -> Vec21209     fn mul(self, rhs: &f32) -> Vec2 {
1210         self.mul(*rhs)
1211     }
1212 }
1213 
1214 impl Mul<&f32> for &Vec2 {
1215     type Output = Vec2;
1216     #[inline]
mul(self, rhs: &f32) -> Vec21217     fn mul(self, rhs: &f32) -> Vec2 {
1218         (*self).mul(*rhs)
1219     }
1220 }
1221 
1222 impl Mul<f32> for &Vec2 {
1223     type Output = Vec2;
1224     #[inline]
mul(self, rhs: f32) -> Vec21225     fn mul(self, rhs: f32) -> Vec2 {
1226         (*self).mul(rhs)
1227     }
1228 }
1229 
1230 impl MulAssign<f32> for Vec2 {
1231     #[inline]
mul_assign(&mut self, rhs: f32)1232     fn mul_assign(&mut self, rhs: f32) {
1233         self.x.mul_assign(rhs);
1234         self.y.mul_assign(rhs);
1235     }
1236 }
1237 
1238 impl MulAssign<&f32> for Vec2 {
1239     #[inline]
mul_assign(&mut self, rhs: &f32)1240     fn mul_assign(&mut self, rhs: &f32) {
1241         self.mul_assign(*rhs)
1242     }
1243 }
1244 
1245 impl Mul<Vec2> for f32 {
1246     type Output = Vec2;
1247     #[inline]
mul(self, rhs: Vec2) -> Vec21248     fn mul(self, rhs: Vec2) -> Vec2 {
1249         Vec2 {
1250             x: self.mul(rhs.x),
1251             y: self.mul(rhs.y),
1252         }
1253     }
1254 }
1255 
1256 impl Mul<&Vec2> for f32 {
1257     type Output = Vec2;
1258     #[inline]
mul(self, rhs: &Vec2) -> Vec21259     fn mul(self, rhs: &Vec2) -> Vec2 {
1260         self.mul(*rhs)
1261     }
1262 }
1263 
1264 impl Mul<&Vec2> for &f32 {
1265     type Output = Vec2;
1266     #[inline]
mul(self, rhs: &Vec2) -> Vec21267     fn mul(self, rhs: &Vec2) -> Vec2 {
1268         (*self).mul(*rhs)
1269     }
1270 }
1271 
1272 impl Mul<Vec2> for &f32 {
1273     type Output = Vec2;
1274     #[inline]
mul(self, rhs: Vec2) -> Vec21275     fn mul(self, rhs: Vec2) -> Vec2 {
1276         (*self).mul(rhs)
1277     }
1278 }
1279 
1280 impl Add<Vec2> for Vec2 {
1281     type Output = Self;
1282     #[inline]
add(self, rhs: Self) -> Self1283     fn add(self, rhs: Self) -> Self {
1284         Self {
1285             x: self.x.add(rhs.x),
1286             y: self.y.add(rhs.y),
1287         }
1288     }
1289 }
1290 
1291 impl Add<&Vec2> for Vec2 {
1292     type Output = Vec2;
1293     #[inline]
add(self, rhs: &Vec2) -> Vec21294     fn add(self, rhs: &Vec2) -> Vec2 {
1295         self.add(*rhs)
1296     }
1297 }
1298 
1299 impl Add<&Vec2> for &Vec2 {
1300     type Output = Vec2;
1301     #[inline]
add(self, rhs: &Vec2) -> Vec21302     fn add(self, rhs: &Vec2) -> Vec2 {
1303         (*self).add(*rhs)
1304     }
1305 }
1306 
1307 impl Add<Vec2> for &Vec2 {
1308     type Output = Vec2;
1309     #[inline]
add(self, rhs: Vec2) -> Vec21310     fn add(self, rhs: Vec2) -> Vec2 {
1311         (*self).add(rhs)
1312     }
1313 }
1314 
1315 impl AddAssign<Vec2> for Vec2 {
1316     #[inline]
add_assign(&mut self, rhs: Self)1317     fn add_assign(&mut self, rhs: Self) {
1318         self.x.add_assign(rhs.x);
1319         self.y.add_assign(rhs.y);
1320     }
1321 }
1322 
1323 impl AddAssign<&Vec2> for Vec2 {
1324     #[inline]
add_assign(&mut self, rhs: &Vec2)1325     fn add_assign(&mut self, rhs: &Vec2) {
1326         self.add_assign(*rhs)
1327     }
1328 }
1329 
1330 impl Add<f32> for Vec2 {
1331     type Output = Self;
1332     #[inline]
add(self, rhs: f32) -> Self1333     fn add(self, rhs: f32) -> Self {
1334         Self {
1335             x: self.x.add(rhs),
1336             y: self.y.add(rhs),
1337         }
1338     }
1339 }
1340 
1341 impl Add<&f32> for Vec2 {
1342     type Output = Vec2;
1343     #[inline]
add(self, rhs: &f32) -> Vec21344     fn add(self, rhs: &f32) -> Vec2 {
1345         self.add(*rhs)
1346     }
1347 }
1348 
1349 impl Add<&f32> for &Vec2 {
1350     type Output = Vec2;
1351     #[inline]
add(self, rhs: &f32) -> Vec21352     fn add(self, rhs: &f32) -> Vec2 {
1353         (*self).add(*rhs)
1354     }
1355 }
1356 
1357 impl Add<f32> for &Vec2 {
1358     type Output = Vec2;
1359     #[inline]
add(self, rhs: f32) -> Vec21360     fn add(self, rhs: f32) -> Vec2 {
1361         (*self).add(rhs)
1362     }
1363 }
1364 
1365 impl AddAssign<f32> for Vec2 {
1366     #[inline]
add_assign(&mut self, rhs: f32)1367     fn add_assign(&mut self, rhs: f32) {
1368         self.x.add_assign(rhs);
1369         self.y.add_assign(rhs);
1370     }
1371 }
1372 
1373 impl AddAssign<&f32> for Vec2 {
1374     #[inline]
add_assign(&mut self, rhs: &f32)1375     fn add_assign(&mut self, rhs: &f32) {
1376         self.add_assign(*rhs)
1377     }
1378 }
1379 
1380 impl Add<Vec2> for f32 {
1381     type Output = Vec2;
1382     #[inline]
add(self, rhs: Vec2) -> Vec21383     fn add(self, rhs: Vec2) -> Vec2 {
1384         Vec2 {
1385             x: self.add(rhs.x),
1386             y: self.add(rhs.y),
1387         }
1388     }
1389 }
1390 
1391 impl Add<&Vec2> for f32 {
1392     type Output = Vec2;
1393     #[inline]
add(self, rhs: &Vec2) -> Vec21394     fn add(self, rhs: &Vec2) -> Vec2 {
1395         self.add(*rhs)
1396     }
1397 }
1398 
1399 impl Add<&Vec2> for &f32 {
1400     type Output = Vec2;
1401     #[inline]
add(self, rhs: &Vec2) -> Vec21402     fn add(self, rhs: &Vec2) -> Vec2 {
1403         (*self).add(*rhs)
1404     }
1405 }
1406 
1407 impl Add<Vec2> for &f32 {
1408     type Output = Vec2;
1409     #[inline]
add(self, rhs: Vec2) -> Vec21410     fn add(self, rhs: Vec2) -> Vec2 {
1411         (*self).add(rhs)
1412     }
1413 }
1414 
1415 impl Sub<Vec2> for Vec2 {
1416     type Output = Self;
1417     #[inline]
sub(self, rhs: Self) -> Self1418     fn sub(self, rhs: Self) -> Self {
1419         Self {
1420             x: self.x.sub(rhs.x),
1421             y: self.y.sub(rhs.y),
1422         }
1423     }
1424 }
1425 
1426 impl Sub<&Vec2> for Vec2 {
1427     type Output = Vec2;
1428     #[inline]
sub(self, rhs: &Vec2) -> Vec21429     fn sub(self, rhs: &Vec2) -> Vec2 {
1430         self.sub(*rhs)
1431     }
1432 }
1433 
1434 impl Sub<&Vec2> for &Vec2 {
1435     type Output = Vec2;
1436     #[inline]
sub(self, rhs: &Vec2) -> Vec21437     fn sub(self, rhs: &Vec2) -> Vec2 {
1438         (*self).sub(*rhs)
1439     }
1440 }
1441 
1442 impl Sub<Vec2> for &Vec2 {
1443     type Output = Vec2;
1444     #[inline]
sub(self, rhs: Vec2) -> Vec21445     fn sub(self, rhs: Vec2) -> Vec2 {
1446         (*self).sub(rhs)
1447     }
1448 }
1449 
1450 impl SubAssign<Vec2> for Vec2 {
1451     #[inline]
sub_assign(&mut self, rhs: Vec2)1452     fn sub_assign(&mut self, rhs: Vec2) {
1453         self.x.sub_assign(rhs.x);
1454         self.y.sub_assign(rhs.y);
1455     }
1456 }
1457 
1458 impl SubAssign<&Vec2> for Vec2 {
1459     #[inline]
sub_assign(&mut self, rhs: &Vec2)1460     fn sub_assign(&mut self, rhs: &Vec2) {
1461         self.sub_assign(*rhs)
1462     }
1463 }
1464 
1465 impl Sub<f32> for Vec2 {
1466     type Output = Self;
1467     #[inline]
sub(self, rhs: f32) -> Self1468     fn sub(self, rhs: f32) -> Self {
1469         Self {
1470             x: self.x.sub(rhs),
1471             y: self.y.sub(rhs),
1472         }
1473     }
1474 }
1475 
1476 impl Sub<&f32> for Vec2 {
1477     type Output = Vec2;
1478     #[inline]
sub(self, rhs: &f32) -> Vec21479     fn sub(self, rhs: &f32) -> Vec2 {
1480         self.sub(*rhs)
1481     }
1482 }
1483 
1484 impl Sub<&f32> for &Vec2 {
1485     type Output = Vec2;
1486     #[inline]
sub(self, rhs: &f32) -> Vec21487     fn sub(self, rhs: &f32) -> Vec2 {
1488         (*self).sub(*rhs)
1489     }
1490 }
1491 
1492 impl Sub<f32> for &Vec2 {
1493     type Output = Vec2;
1494     #[inline]
sub(self, rhs: f32) -> Vec21495     fn sub(self, rhs: f32) -> Vec2 {
1496         (*self).sub(rhs)
1497     }
1498 }
1499 
1500 impl SubAssign<f32> for Vec2 {
1501     #[inline]
sub_assign(&mut self, rhs: f32)1502     fn sub_assign(&mut self, rhs: f32) {
1503         self.x.sub_assign(rhs);
1504         self.y.sub_assign(rhs);
1505     }
1506 }
1507 
1508 impl SubAssign<&f32> for Vec2 {
1509     #[inline]
sub_assign(&mut self, rhs: &f32)1510     fn sub_assign(&mut self, rhs: &f32) {
1511         self.sub_assign(*rhs)
1512     }
1513 }
1514 
1515 impl Sub<Vec2> for f32 {
1516     type Output = Vec2;
1517     #[inline]
sub(self, rhs: Vec2) -> Vec21518     fn sub(self, rhs: Vec2) -> Vec2 {
1519         Vec2 {
1520             x: self.sub(rhs.x),
1521             y: self.sub(rhs.y),
1522         }
1523     }
1524 }
1525 
1526 impl Sub<&Vec2> for f32 {
1527     type Output = Vec2;
1528     #[inline]
sub(self, rhs: &Vec2) -> Vec21529     fn sub(self, rhs: &Vec2) -> Vec2 {
1530         self.sub(*rhs)
1531     }
1532 }
1533 
1534 impl Sub<&Vec2> for &f32 {
1535     type Output = Vec2;
1536     #[inline]
sub(self, rhs: &Vec2) -> Vec21537     fn sub(self, rhs: &Vec2) -> Vec2 {
1538         (*self).sub(*rhs)
1539     }
1540 }
1541 
1542 impl Sub<Vec2> for &f32 {
1543     type Output = Vec2;
1544     #[inline]
sub(self, rhs: Vec2) -> Vec21545     fn sub(self, rhs: Vec2) -> Vec2 {
1546         (*self).sub(rhs)
1547     }
1548 }
1549 
1550 impl Rem<Vec2> for Vec2 {
1551     type Output = Self;
1552     #[inline]
rem(self, rhs: Self) -> Self1553     fn rem(self, rhs: Self) -> Self {
1554         Self {
1555             x: self.x.rem(rhs.x),
1556             y: self.y.rem(rhs.y),
1557         }
1558     }
1559 }
1560 
1561 impl Rem<&Vec2> for Vec2 {
1562     type Output = Vec2;
1563     #[inline]
rem(self, rhs: &Vec2) -> Vec21564     fn rem(self, rhs: &Vec2) -> Vec2 {
1565         self.rem(*rhs)
1566     }
1567 }
1568 
1569 impl Rem<&Vec2> for &Vec2 {
1570     type Output = Vec2;
1571     #[inline]
rem(self, rhs: &Vec2) -> Vec21572     fn rem(self, rhs: &Vec2) -> Vec2 {
1573         (*self).rem(*rhs)
1574     }
1575 }
1576 
1577 impl Rem<Vec2> for &Vec2 {
1578     type Output = Vec2;
1579     #[inline]
rem(self, rhs: Vec2) -> Vec21580     fn rem(self, rhs: Vec2) -> Vec2 {
1581         (*self).rem(rhs)
1582     }
1583 }
1584 
1585 impl RemAssign<Vec2> for Vec2 {
1586     #[inline]
rem_assign(&mut self, rhs: Self)1587     fn rem_assign(&mut self, rhs: Self) {
1588         self.x.rem_assign(rhs.x);
1589         self.y.rem_assign(rhs.y);
1590     }
1591 }
1592 
1593 impl RemAssign<&Vec2> for Vec2 {
1594     #[inline]
rem_assign(&mut self, rhs: &Vec2)1595     fn rem_assign(&mut self, rhs: &Vec2) {
1596         self.rem_assign(*rhs)
1597     }
1598 }
1599 
1600 impl Rem<f32> for Vec2 {
1601     type Output = Self;
1602     #[inline]
rem(self, rhs: f32) -> Self1603     fn rem(self, rhs: f32) -> Self {
1604         Self {
1605             x: self.x.rem(rhs),
1606             y: self.y.rem(rhs),
1607         }
1608     }
1609 }
1610 
1611 impl Rem<&f32> for Vec2 {
1612     type Output = Vec2;
1613     #[inline]
rem(self, rhs: &f32) -> Vec21614     fn rem(self, rhs: &f32) -> Vec2 {
1615         self.rem(*rhs)
1616     }
1617 }
1618 
1619 impl Rem<&f32> for &Vec2 {
1620     type Output = Vec2;
1621     #[inline]
rem(self, rhs: &f32) -> Vec21622     fn rem(self, rhs: &f32) -> Vec2 {
1623         (*self).rem(*rhs)
1624     }
1625 }
1626 
1627 impl Rem<f32> for &Vec2 {
1628     type Output = Vec2;
1629     #[inline]
rem(self, rhs: f32) -> Vec21630     fn rem(self, rhs: f32) -> Vec2 {
1631         (*self).rem(rhs)
1632     }
1633 }
1634 
1635 impl RemAssign<f32> for Vec2 {
1636     #[inline]
rem_assign(&mut self, rhs: f32)1637     fn rem_assign(&mut self, rhs: f32) {
1638         self.x.rem_assign(rhs);
1639         self.y.rem_assign(rhs);
1640     }
1641 }
1642 
1643 impl RemAssign<&f32> for Vec2 {
1644     #[inline]
rem_assign(&mut self, rhs: &f32)1645     fn rem_assign(&mut self, rhs: &f32) {
1646         self.rem_assign(*rhs)
1647     }
1648 }
1649 
1650 impl Rem<Vec2> for f32 {
1651     type Output = Vec2;
1652     #[inline]
rem(self, rhs: Vec2) -> Vec21653     fn rem(self, rhs: Vec2) -> Vec2 {
1654         Vec2 {
1655             x: self.rem(rhs.x),
1656             y: self.rem(rhs.y),
1657         }
1658     }
1659 }
1660 
1661 impl Rem<&Vec2> for f32 {
1662     type Output = Vec2;
1663     #[inline]
rem(self, rhs: &Vec2) -> Vec21664     fn rem(self, rhs: &Vec2) -> Vec2 {
1665         self.rem(*rhs)
1666     }
1667 }
1668 
1669 impl Rem<&Vec2> for &f32 {
1670     type Output = Vec2;
1671     #[inline]
rem(self, rhs: &Vec2) -> Vec21672     fn rem(self, rhs: &Vec2) -> Vec2 {
1673         (*self).rem(*rhs)
1674     }
1675 }
1676 
1677 impl Rem<Vec2> for &f32 {
1678     type Output = Vec2;
1679     #[inline]
rem(self, rhs: Vec2) -> Vec21680     fn rem(self, rhs: Vec2) -> Vec2 {
1681         (*self).rem(rhs)
1682     }
1683 }
1684 
1685 #[cfg(not(target_arch = "spirv"))]
1686 impl AsRef<[f32; 2]> for Vec2 {
1687     #[inline]
as_ref(&self) -> &[f32; 2]1688     fn as_ref(&self) -> &[f32; 2] {
1689         unsafe { &*(self as *const Vec2 as *const [f32; 2]) }
1690     }
1691 }
1692 
1693 #[cfg(not(target_arch = "spirv"))]
1694 impl AsMut<[f32; 2]> for Vec2 {
1695     #[inline]
as_mut(&mut self) -> &mut [f32; 2]1696     fn as_mut(&mut self) -> &mut [f32; 2] {
1697         unsafe { &mut *(self as *mut Vec2 as *mut [f32; 2]) }
1698     }
1699 }
1700 
1701 impl Sum for Vec2 {
1702     #[inline]
sum<I>(iter: I) -> Self where I: Iterator<Item = Self>,1703     fn sum<I>(iter: I) -> Self
1704     where
1705         I: Iterator<Item = Self>,
1706     {
1707         iter.fold(Self::ZERO, Self::add)
1708     }
1709 }
1710 
1711 impl<'a> Sum<&'a Self> for Vec2 {
1712     #[inline]
sum<I>(iter: I) -> Self where I: Iterator<Item = &'a Self>,1713     fn sum<I>(iter: I) -> Self
1714     where
1715         I: Iterator<Item = &'a Self>,
1716     {
1717         iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1718     }
1719 }
1720 
1721 impl Product for Vec2 {
1722     #[inline]
product<I>(iter: I) -> Self where I: Iterator<Item = Self>,1723     fn product<I>(iter: I) -> Self
1724     where
1725         I: Iterator<Item = Self>,
1726     {
1727         iter.fold(Self::ONE, Self::mul)
1728     }
1729 }
1730 
1731 impl<'a> Product<&'a Self> for Vec2 {
1732     #[inline]
product<I>(iter: I) -> Self where I: Iterator<Item = &'a Self>,1733     fn product<I>(iter: I) -> Self
1734     where
1735         I: Iterator<Item = &'a Self>,
1736     {
1737         iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1738     }
1739 }
1740 
1741 impl Neg for Vec2 {
1742     type Output = Self;
1743     #[inline]
neg(self) -> Self1744     fn neg(self) -> Self {
1745         Self {
1746             x: self.x.neg(),
1747             y: self.y.neg(),
1748         }
1749     }
1750 }
1751 
1752 impl Neg for &Vec2 {
1753     type Output = Vec2;
1754     #[inline]
neg(self) -> Vec21755     fn neg(self) -> Vec2 {
1756         (*self).neg()
1757     }
1758 }
1759 
1760 impl Index<usize> for Vec2 {
1761     type Output = f32;
1762     #[inline]
index(&self, index: usize) -> &Self::Output1763     fn index(&self, index: usize) -> &Self::Output {
1764         match index {
1765             0 => &self.x,
1766             1 => &self.y,
1767             _ => panic!("index out of bounds"),
1768         }
1769     }
1770 }
1771 
1772 impl IndexMut<usize> for Vec2 {
1773     #[inline]
index_mut(&mut self, index: usize) -> &mut Self::Output1774     fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1775         match index {
1776             0 => &mut self.x,
1777             1 => &mut self.y,
1778             _ => panic!("index out of bounds"),
1779         }
1780     }
1781 }
1782 
1783 impl fmt::Display for Vec2 {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result1784     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1785         if let Some(p) = f.precision() {
1786             write!(f, "[{:.*}, {:.*}]", p, self.x, p, self.y)
1787         } else {
1788             write!(f, "[{}, {}]", self.x, self.y)
1789         }
1790     }
1791 }
1792 
1793 impl fmt::Debug for Vec2 {
fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result1794     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1795         fmt.debug_tuple(stringify!(Vec2))
1796             .field(&self.x)
1797             .field(&self.y)
1798             .finish()
1799     }
1800 }
1801 
1802 impl From<[f32; 2]> for Vec2 {
1803     #[inline]
from(a: [f32; 2]) -> Self1804     fn from(a: [f32; 2]) -> Self {
1805         Self::new(a[0], a[1])
1806     }
1807 }
1808 
1809 impl From<Vec2> for [f32; 2] {
1810     #[inline]
from(v: Vec2) -> Self1811     fn from(v: Vec2) -> Self {
1812         [v.x, v.y]
1813     }
1814 }
1815 
1816 impl From<(f32, f32)> for Vec2 {
1817     #[inline]
from(t: (f32, f32)) -> Self1818     fn from(t: (f32, f32)) -> Self {
1819         Self::new(t.0, t.1)
1820     }
1821 }
1822 
1823 impl From<Vec2> for (f32, f32) {
1824     #[inline]
from(v: Vec2) -> Self1825     fn from(v: Vec2) -> Self {
1826         (v.x, v.y)
1827     }
1828 }
1829 
1830 impl From<BVec2> for Vec2 {
1831     #[inline]
from(v: BVec2) -> Self1832     fn from(v: BVec2) -> Self {
1833         Self::new(f32::from(v.x), f32::from(v.y))
1834     }
1835 }
1836