swift_vec/vector/
v2d.rs

1//===================================================================================================================================================================================//
2//
3//  /$$    /$$                      /$$                                /$$$$$$  /$$$$$$$
4// | $$   | $$                     | $$                               /$$__  $$| $$__  $$
5// | $$   | $$ /$$$$$$   /$$$$$$$ /$$$$$$    /$$$$$$   /$$$$$$       |__/  \ $$| $$  \ $$
6// |  $$ / $$//$$__  $$ /$$_____/|_  $$_/   /$$__  $$ /$$__  $$        /$$$$$$/| $$  | $$
7//  \  $$ $$/| $$$$$$$$| $$        | $$    | $$  \ $$| $$  \__/       /$$____/ | $$  | $$
8//   \  $$$/ | $$_____/| $$        | $$ /$$| $$  | $$| $$            | $$      | $$  | $$
9//    \  $/  |  $$$$$$$|  $$$$$$$  |  $$$$/|  $$$$$$/| $$            | $$$$$$$$| $$$$$$$/
10//     \_/    \_______/ \_______/   \___/   \______/ |__/            |________/|_______/
11//
12//===================================================================================================================================================================================//
13
14//?
15//? Created by LunaticWyrm467 and others.
16//? 
17//? All code is licensed under the MIT license.
18//? Feel free to reproduce, modify, and do whatever.
19//?
20
21//!
22//! A private submodule for the vector module that contains all of the implementations
23//! for any of the non-shared behaviours of the 2D vector.
24//!
25
26#[cfg(feature = "glam")]
27use glam::{ Vec2 as GVec2, vec2, DVec2, dvec2, UVec2, uvec2, IVec2, ivec2 };
28
29use super::*;
30
31
32/*
33 * 2D Vector
34 *      Indexing
35 */
36
37
38/// Simply represents an axis of a 2-dimensional graph or plane,
39/// which can be used to index a scalar from a `Vec2` type.
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub enum Axis2 {
42    NoAxis,
43    X,
44    Y
45}
46use Axis2::{ X, Y, NoAxis };
47
48impl Axis2 {
49
50    /// Gets a normalized `Vec2` representing this axis.
51    pub fn to_vec2<T: Scalar>(&self) -> Vec2<T> {
52        match self {
53            X      => Vec2(T::one(),  T::zero()),
54            Y      => Vec2(T::zero(), T::one() ),
55            NoAxis => Vec2(T::zero(), T::zero())
56        }
57    }
58}
59
60impl <T: Scalar> Index<Axis2> for Vec2<T> {
61    type Output = T;
62    fn index(&self, index: Axis2) -> &Self::Output {
63        match index {
64            X      => &self.0,
65            Y      => &self.1,
66            NoAxis => panic!("`NoAxis` is not a valid index!")
67        }
68    }
69}
70
71impl <T: Scalar> IndexMut<Axis2> for Vec2<T> {
72    fn index_mut(&mut self, index: Axis2) -> &mut Self::Output {
73        match index {
74            X      => &mut self.0,
75            Y      => &mut self.1,
76            NoAxis => panic!("`NoAxis` is not a valid index!")
77        }
78    }
79}
80
81
82/*
83    2D Vector
84        Implementation
85*/
86
87
88/// A 2D Vector with an X and Y component.
89/// Contains behaviours and methods for allowing for algebraic, geometric, and trigonometric operations,
90/// as well as interpolation and other common vector operations.
91#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Hash)]
92#[repr(C)]
93pub struct Vec2<T: Scalar>(pub T, pub T);
94
95impl <T: Scalar> VectorAbstract<T, Vec2<T>> for Vec2<T> {}
96
97impl <T: Scalar> Vector<T, Vec2<T>, Axis2> for Vec2<T>  {
98    const ZERO: Vec2<T> = Vec2(T::ZERO, T::ZERO);
99    const ONE:  Vec2<T> = Vec2(T::ONE,  T::ONE );
100
101    fn identity(&self) -> Vec2<T> {
102        *self
103    }
104
105    fn rank() -> usize {
106        2
107    }
108
109    #[cfg(feature = "alloc")]
110    fn to_vec(&self) -> Vec<T> {
111        vec![self[X], self[Y]]
112    }
113
114    fn sum(&self) -> T {
115        self[X] + self[Y]
116    }
117
118    fn product(&self) -> T {
119        self[X] * self[Y]
120    }
121    
122    fn dot(&self, other: Vec2<T>) -> T {
123        self[X] * other[X] + self[Y] * other[Y]
124    }
125
126    fn argmax(&self) -> Axis2 {
127        if self[X] == self[Y] {
128            NoAxis
129        } else if self[X] > self[Y] {
130            X
131        } else {
132            Y
133        }
134    }
135
136    fn argmin(&self) -> Axis2 {
137        if self[X] == self[Y] {
138            NoAxis
139        } else if self[X] < self[Y] {
140            X
141        } else {
142            Y
143        }
144    }
145
146    fn min<I: Vectorized<T, Vec2<T>>>(&self, other: I) -> Vec2<T> {
147        let other: Vec2<T> = other.dvec();
148        Vec2(self[X].min(other[X]), self[Y].min(other[Y]))
149    }
150
151    fn max<I: Vectorized<T, Vec2<T>>>(&self, other: I) -> Vec2<T> {
152        let other: Vec2<T> = other.dvec();
153        Vec2(self[X].max(other[X]), self[Y].max(other[Y]))
154    }
155
156    fn clamp<I: Vectorized<T, Vec2<T>>>(&self, min: I, max: I) -> Vec2<T> {
157        let min: Vec2<T> = min.dvec();
158        let max: Vec2<T> = max.dvec();
159        Vec2(self[X].clamp(min[X], max[X]), self[Y].clamp(min[Y], max[Y]))
160    }
161}
162
163impl <T: SignedScalar> SignedVector<T, Vec2<T>, Axis2, T> for Vec2<T> {
164    fn signum(&self) -> Vec2<T> {
165        Vec2(self[X].signum(), self[Y].signum())
166    }
167
168    fn abs(&self) -> Vec2<T> {
169        Vec2(self[X].abs(), self[Y].abs())
170    }
171
172    fn cross(&self, other: Vec2<T>) -> T {
173        self[X] * other[Y] - self[Y] * other[X]
174    }
175}
176
177impl <T: IntScalar<T>> IntVector<T, Vec2<T>, Axis2> for Vec2<T> {
178    fn pow<I: Vectorized<T, Vec2<T>>>(&self, pow: I) -> Vec2<T> {
179        let pow: Vec2<T> = pow.dvec();
180        Vec2(self[X].pow(pow[X].to_u32().unwrap()), self[Y].pow(pow[Y].to_u32().unwrap()))
181    }
182
183    fn log<I: Vectorized<T, Vec2<T>>>(&self, base: I) -> Vec2<T> {
184        let base: Vec2<T> = base.dvec();
185        Vec2(self[X].ilog(base[X]), self[Y].ilog(base[Y]))
186    }
187}
188
189impl <T: FloatScalar> FloatVector<T, Vec2<T>, Axis2, T> for Vec2<T> {    
190    fn to_radians(&self) -> Vec2<T> {
191        Vec2(self[X].to_radians(), self[Y].to_radians())
192    }
193
194    fn to_degrees(&self) -> Vec2<T> {
195        Vec2(self[X].to_degrees(), self[Y].to_degrees())
196    }
197
198    fn sin(&self) -> Vec2<T> {
199        Vec2(self[X].sin(), self[Y].sin())
200    }
201
202    fn cos(&self) -> Vec2<T> {
203        Vec2(self[X].cos(), self[Y].cos())
204    }
205
206    fn tan(&self) -> Vec2<T> {
207        Vec2(self[X].tan(), self[Y].tan())
208    }
209
210    fn asin(&self) -> Vec2<T> {
211        Vec2(self[X].asin(), self[Y].asin())
212    }
213
214    fn acos(&self) -> Vec2<T> {
215        Vec2(self[X].acos(), self[Y].acos())
216    }
217
218    fn atan(&self) -> Vec2<T> {
219        Vec2(self[X].atan(), self[Y].atan())
220    }
221
222    fn distance_squared_to(&self, other: Vec2<T>) -> T {
223        (self[X] - other[X]).powi(2) + (self[Y] - other[Y]).powi(2)
224    }
225    
226    fn smoothstep<I: Vectorized<T, Vec2<T>>>(&self, a: I, b: I) -> Vec2<T> {
227        let a: Vec2<T> = a.dvec();
228        let b: Vec2<T> = b.dvec();
229
230        Vec2(
231            self[X].smoothstep(a[X], b[X]),
232            self[Y].smoothstep(a[Y], b[Y])
233        )
234    }
235
236    fn bezier_derivative(&self, control_1: Vec2<T>, control_2: Vec2<T>, terminal: Vec2<T>, t: T) -> Vec2<T> {
237        Vec2(
238            self[X].bezier_derivative(control_1[X], control_2[X], terminal[X], t),
239            self[Y].bezier_derivative(control_1[Y], control_2[Y], terminal[Y], t)
240        )
241    }
242
243    fn bezier_sample(&self, control_1: Vec2<T>, control_2: Vec2<T>, terminal: Vec2<T>, t: T) -> Vec2<T> {
244        Vec2(
245            self[X].bezier_sample(control_1[X], control_2[X], terminal[X], t),
246            self[Y].bezier_sample(control_1[Y], control_2[Y], terminal[Y], t)
247        )
248    }
249    
250    fn cubic_interpolate(&self, b: Vec2<T>, pre_a: Vec2<T>, post_b: Vec2<T>, weight: T) -> Vec2<T> {
251        Vec2(
252            self[X].cubic_interpolate(b[X], pre_a[X], post_b[X], weight),
253            self[Y].cubic_interpolate(b[Y], pre_a[Y], post_b[Y], weight)
254        )
255    }
256    
257    fn cubic_interpolate_in_time(&self, b: Vec2<T>, pre_a: Vec2<T>, post_b: Vec2<T>, weight: T, b_t: T, pre_a_t: T, post_b_t: T) -> Vec2<T> {
258        Vec2(
259            self[X].cubic_interpolate_in_time(b[X], pre_a[X], post_b[X], weight, b_t, pre_a_t, post_b_t),
260            self[Y].cubic_interpolate_in_time(b[Y], pre_a[Y], post_b[Y], weight, b_t, pre_a_t, post_b_t)
261        )
262    }
263
264    fn sqrt(&self) -> Vec2<T> {
265        Vec2(self[X].sqrt(), self[Y].sqrt())
266    }
267
268    fn pow2(&self) -> Vec2<T> {
269        Vec2(self[X] * self[X], self[Y] * self[Y])
270    }
271
272    fn pow<I: Vectorized<T, Vec2<T>>>(&self, pow: I) -> Vec2<T> {
273        let pow: Vec2<T> = pow.dvec();
274        Vec2(self[X].powf(pow[X]), self[Y].powf(pow[Y]))
275    }
276
277    fn log<I: Vectorized<T, Vec2<T>>>(&self, base: I) -> Vec2<T> {
278        let base: Vec2<T> = base.dvec();
279        Vec2(self[X].log(base[X]), self[Y].log(base[Y]))
280    }
281
282    fn floor(&self) -> Vec2<T> {
283        Vec2(self[X].floor(), self[Y].floor())
284    }
285
286    fn ceil(&self) -> Vec2<T> {
287        Vec2(self[Y].ceil(), self[Y].ceil())
288    }
289
290    fn round(&self) -> Vec2<T> {
291        Vec2(self[X].round(), self[Y].round())
292    }
293
294    fn approx_eq(&self, other: Vec2<T>) -> bool {
295        self[X].approx_eq(other[X]) && self[Y].approx_eq(other[Y])
296    }
297}
298
299impl <T: Scalar> Vec2<T> {
300    
301    /// A `Vec2` that describes an upwards direction:
302    /// `Vec2(0, 1)`.
303    pub const UP: Vec2<T> = Vec2(T::ZERO, T::ONE);
304    
305    /// A `Vec2` that describes a rightwards direction:
306    /// `Vec2(1, 0)`.
307    pub const RIGHT: Vec2<T> = Vec2(T::ONE, T::ZERO);
308
309    /// Initializes a `Vec2` that describes a plotted point along the x axis.
310    pub fn on_x(x: T) -> Vec2<T> {
311        Vec2(x, T::zero())
312    }
313
314    /// Initializes a `Vec2` that describes a plotted point along the y axis.
315    pub fn on_y(y: T) -> Vec2<T> {
316        Vec2(T::zero(), y)
317    }
318
319    /// Converts a `Vec2` to a `Vec2` of a different type.
320    /// Returns `None` if the cast was unsuccessful.
321    pub fn cast<U: Scalar>(&self) -> Option<Vec2<U>> {
322        match (U::from(self[X]), U::from(self[Y])) {
323            (Some(x), Some(y)) => Some(Vec2(x, y)),
324            _                  => None
325        }
326    }
327
328    /// Returns a `Vec2` that represents this `Vec2`'s X component.
329    pub fn of_x(&self) -> Vec2<T> {
330        Vec2(self[X], T::zero())
331    }
332
333    /// Returns a `Vec2` that represents this `Vec2`'s Y component.
334    pub fn of_y(&self) -> Vec2<T> {
335        Vec2(T::zero(), self[Y])
336    }
337
338    /// Gets the x component.
339    pub fn x(&self) -> T {
340        self.0
341    }
342    
343    /// Gets the y component.
344    pub fn y(&self) -> T {
345        self.1
346    }
347
348    /// Gets the x and y components of this `Vec2` as another identity function.
349    pub fn xy(&self) -> Vec2<T> {
350        self.identity()
351    }
352
353    /// Gets the x and y components of this `Vec2` in inverse order as a `Vec2`.
354    pub fn yx(&self) -> Vec2<T> {
355        Vec2(self[Y], self[X])
356    }
357
358    /// Calculates the aspect ratio of this `Vec2`.
359    pub fn aspect_ratio(&self) -> T {
360        self[X] / self[Y]
361    }    
362}
363
364impl <T: SignedScalar> Vec2<T> {
365
366    /// A `Vec2` that describes a downwards direction:
367    /// `Vec2(0, -1)`.
368    pub const DOWN: Vec2<T> = Vec2(T::ZERO, T::NEG_ONE);
369
370    /// A `Vec2` that describes a leftwards direction.
371    /// `Vec2(-1, 0)`.
372    pub const LEFT: Vec2<T> = Vec2(T::NEG_ONE, T::ZERO);
373}
374
375impl <T: FloatScalar> Vec2<T> {
376
377    /// Initializes a `Vec2` from an angle in radians.
378    pub fn from_angle(angle: T) -> Vec2<T> {
379        Vec2(angle.cos(), angle.sin())
380    }
381
382    /// Calculates the angle of a `Vec2` in respect to the positive x-axis.
383    /// # Returns
384    /// The angle of the `Vec2` in radians.
385    pub fn angle(&self) -> T {
386        self[Y].atan2(self[X])
387    }
388
389    /// Calculates the angle between the line connecting the two positions `self` and `other` and the x-axis.
390    /// # Returns
391    /// The angle in radians.
392    pub fn angle_between(&self, other: &Vec2<T>) -> T {
393        (other - self).angle()
394    }
395
396    /// Rotates this vector by a given angle in radians.
397    pub fn rotated(&self, angle: T) -> Vec2<T> {
398        
399        // Compute the sine and cosine of the angle.
400        let (sine, cosine): (T, T) = angle.sin_cos();
401
402        // Rotate the vector using the rotation matrix formula.
403        Vec2(
404            self[X] * cosine - self[Y] * sine,
405            self[X] * sine   + self[Y] * cosine
406        )
407    }
408
409    /// Rotates this vector by 90 degrees counter clockwise.
410    pub fn orthogonal(&self) -> Vec2<T> {
411        self.rotated(T::from(FRAC_PI_2).unwrap())
412    }
413
414    /// Spherically interpolates between two vectors.
415    /// This interpolation is focused on the length or magnitude of the vectors. If the magnitudes are equal,
416    /// the interpolation is linear and behaves the same way as `lerp()`.
417    pub fn slerp(&self, other: Vec2<T>, t: T) -> Vec2<T> {
418        let theta: T = self.angle_to(other);
419        self.rotated(theta * t)
420    }
421}
422
423
424/*
425    Glam
426        Support
427*/
428
429
430impl Vec2<f32> {
431
432    /// Converts this vector into a glam vector.
433    #[cfg(feature = "glam")]
434    pub fn to_glam(&self) -> GVec2 {
435        vec2(self.x(), self.y())
436    }
437}
438
439impl Vec2<f64> {
440
441    /// Converts this vector into a glam vector.
442    #[cfg(feature = "glam")]
443    pub fn to_glam(&self) -> DVec2 {
444        dvec2(self.x(), self.y())
445    }
446}
447
448impl Vec2<u32> {
449
450    /// Converts this vector into a glam vector.
451    #[cfg(feature = "glam")]
452    pub fn to_glam(&self) -> UVec2 {
453        uvec2(self.x(), self.y())
454    }
455}
456
457impl Vec2<i32> {
458
459    /// Converts this vector into a glam vector.
460    #[cfg(feature = "glam")]
461    pub fn to_glam(&self) -> IVec2 {
462        ivec2(self.x(), self.y())
463    }
464}
465
466
467/*
468    Global Operations
469        Base Arithmetic
470*/
471
472
473impl <T: Scalar> Add for Vec2<T> {
474    type Output = Vec2<T>;
475    fn add(self, other: Self) -> Self::Output {
476        Vec2(self.0 + other.0, self.1 + other.1)
477    }
478}
479
480impl <T: Scalar> Sub for Vec2<T> {
481    type Output = Vec2<T>;
482    fn sub(self, other: Self) -> Self::Output {
483        Vec2(self.0 - other.0, self.1 - other.1)
484    }
485}
486
487impl <T: Scalar> Mul for Vec2<T> {
488    type Output = Vec2<T>;
489    fn mul(self, other: Self) -> Self::Output {
490        Vec2(self.0 * other.0, self.1 * other.1)
491    }
492}
493
494impl <T: Scalar> Div for Vec2<T> {
495    type Output = Vec2<T>;
496    fn div(self, other: Self) -> Self::Output {
497        Vec2(self.0 / other.0, self.1 / other.1)
498    }
499}
500
501impl <T: Scalar> Rem for Vec2<T> {
502    type Output = Vec2<T>;
503    fn rem(self, other: Self) -> Self::Output {
504        Vec2(self.0 % other.0, self.1 % other.1)
505    }
506}
507
508impl <T: SignedScalar> Neg for Vec2<T> {
509    type Output = Vec2<T>;
510    fn neg(self) -> Self::Output {
511        Vec2(-self.0, -self.1)
512    }
513}
514
515
516/*
517    Global Operations
518        Reference Arithmetic
519*/
520
521
522impl <'a, T: Scalar> Add<&'a Vec2<T>> for &'a Vec2<T> {
523    type Output = Vec2<T>;
524    fn add(self, other: Self) -> Self::Output {
525        Vec2(self.0 + other.0, self.1 + other.1)
526    }
527}
528
529impl <'a, T: Scalar> Sub<&'a Vec2<T>> for &'a Vec2<T> {
530    type Output = Vec2<T>;
531    fn sub(self, other: Self) -> Self::Output {
532        Vec2(self.0 - other.0, self.1 - other.1)
533    }
534}
535
536impl <'a, T: Scalar> Mul<&'a Vec2<T>> for &'a Vec2<T> {
537    type Output = Vec2<T>;
538    fn mul(self, other: Self) -> Self::Output {
539        Vec2(self.0 * other.0, self.1 * other.1)
540    }
541}
542
543impl <'a, T: Scalar> Div<&'a Vec2<T>> for &'a Vec2<T> {
544    type Output = Vec2<T>;
545    fn div(self, other: Self) -> Self::Output {
546        Vec2(self.0 / other.0, self.1 / other.1)
547    }
548}
549
550impl <'a, T: Scalar> Rem<&'a Vec2<T>> for &'a Vec2<T> {
551    type Output = Vec2<T>;
552    fn rem(self, other: Self) -> Self::Output {
553        Vec2(self.0 % other.0, self.1 % other.1)
554    }
555}
556
557impl <T: SignedScalar> Neg for &Vec2<T> {
558    type Output = Vec2<T>;
559    fn neg(self) -> Self::Output {
560        Vec2(-self.0, -self.1)
561    }
562}
563
564
565/*
566    Global Operations
567        Reference vs Base Arithmetic
568*/
569
570
571impl <T: Scalar> Add<&Vec2<T>> for Vec2<T> {
572    type Output = Vec2<T>;
573    fn add(self, other: &Self) -> Self::Output {
574        Vec2(self.0 + other.0, self.1 + other.1)
575    }
576}
577
578impl <T: Scalar> Sub<&Vec2<T>> for Vec2<T> {
579    type Output = Vec2<T>;
580    fn sub(self, other: &Self) -> Self::Output {
581        Vec2(self.0 - other.0, self.1 - other.1)
582    }
583}
584
585impl <T: Scalar> Mul<&Vec2<T>> for Vec2<T> {
586    type Output = Vec2<T>;
587    fn mul(self, other: &Self) -> Self::Output {
588        Vec2(self.0 * other.0, self.1 * other.1)
589    }
590}
591
592impl <T: Scalar> Div<&Vec2<T>> for Vec2<T> {
593    type Output = Vec2<T>;
594    fn div(self, other: &Self) -> Self::Output {
595        Vec2(self.0 / other.0, self.1 / other.1)
596    }
597}
598
599impl <T: Scalar> Rem<&Vec2<T>> for Vec2<T> {
600    type Output = Vec2<T>;
601    fn rem(self, other: &Self) -> Self::Output {
602        Vec2(self.0 % other.0, self.1 % other.1)
603    }
604}
605
606
607/*
608    Global Operations
609        Base vs Scalar Arithmetic
610*/
611
612
613impl <T: Scalar> Add<T> for Vec2<T> {
614    type Output = Vec2<T>;
615    fn add(self, other: T) -> Self::Output {
616        self + Vec2(other, other)
617    }
618}
619
620impl <T: Scalar> Sub<T> for Vec2<T> {
621    type Output = Vec2<T>;
622    fn sub(self, other: T) -> Self::Output {
623        self - Vec2(other, other)
624    }
625}
626
627impl <T: Scalar> Mul<T> for Vec2<T> {
628    type Output = Vec2<T>;
629    fn mul(self, other: T) -> Self::Output {
630        self * Vec2(other, other)
631    }
632}
633
634impl <T: Scalar> Div<T> for Vec2<T> {
635    type Output = Vec2<T>;
636    fn div(self, other: T) -> Self::Output {
637        self / Vec2(other, other)
638    }
639}
640
641impl <T: Scalar> Rem<T> for Vec2<T> {
642    type Output = Vec2<T>;
643    fn rem(self, other: T) -> Self::Output {
644        self % Vec2(other, other)
645    }
646}
647
648
649/*
650    Global Operations
651        Reference vs Scalar Arithmetic
652*/
653
654
655impl <T: Scalar> Add<T> for &Vec2<T> {
656    type Output = Vec2<T>;
657    fn add(self, other: T) -> Self::Output {
658        self + &Vec2(other, other)
659    }
660}
661
662impl <T: Scalar> Sub<T> for &Vec2<T> {
663    type Output = Vec2<T>;
664    fn sub(self, other: T) -> Self::Output {
665        self - &Vec2(other, other)
666    }
667}
668
669impl <T: Scalar> Mul<T> for &Vec2<T> {
670    type Output = Vec2<T>;
671    fn mul(self, other: T) -> Self::Output {
672        self * &Vec2(other, other)
673    }
674}
675
676impl <T: Scalar> Div<T> for &Vec2<T> {
677    type Output = Vec2<T>;
678    fn div(self, other: T) -> Self::Output {
679        self / &Vec2(other, other)
680    }
681}
682
683impl <T: Scalar> Rem<T> for &Vec2<T> {
684    type Output = Vec2<T>;
685    fn rem(self, other: T) -> Self::Output {
686        self % &Vec2(other, other)
687    }
688}
689
690
691/*
692    Global Operations
693        Assignment & Arithmetic
694*/
695
696
697impl <T: Scalar> AddAssign for Vec2<T> {
698    fn add_assign(&mut self, other: Self) -> () {
699        *self = *self + other;
700    }
701}
702
703impl <T: Scalar> SubAssign for Vec2<T> {
704    fn sub_assign(&mut self, other: Self) -> () {
705        *self = *self - other;
706    }
707}
708
709impl <T: Scalar> MulAssign for Vec2<T> {
710    fn mul_assign(&mut self, other: Self) -> () {
711        *self = *self * other;
712    }
713}
714
715impl <T: Scalar> DivAssign for Vec2<T> {
716    fn div_assign(&mut self, other: Self) -> () {
717        *self = *self / other;
718    }
719}
720
721impl <T: Scalar> RemAssign for Vec2<T> {
722    fn rem_assign(&mut self, other: Self) -> () {
723        *self = *self % other;
724    }
725}
726
727
728/*
729    Global
730        Behaviours
731*/
732
733
734impl <T: Scalar> Default for Vec2<T> {
735    fn default() -> Self {
736        Vec2(T::default(), T::default())
737    }
738}
739
740impl <T: Scalar> Display for Vec2<T> {
741    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> fmt::Result {
742        write!(f, "Vec2({}, {})", self.0, self.1)
743    }
744}
745
746
747/*
748    Vectorized
749        Trait
750*/
751
752
753impl <T: Scalar> Vectorized<T, Vec2<T>> for T {
754    fn attempt_get_scalar(self) -> Option<T> {
755        Some(self)
756    }
757
758    fn dvec(self) -> Vec2<T> {
759        Vec2(self, self)
760    }
761}
762
763impl <T: Scalar> Vectorized<T, Vec2<T>> for (T, T) {
764    fn attempt_get_scalar(self) -> Option<T> {
765        None
766    }
767
768    fn dvec(self) -> Vec2<T> {
769        Vec2(self.0, self.1)
770    }
771}
772
773impl <T: Scalar> Vectorized<T, Vec2<T>> for Vec2<T> {
774    fn attempt_get_scalar(self) -> Option<T> {
775        None
776    }
777
778    fn dvec(self) -> Vec2<T> {
779        self
780    }
781}
782
783pub trait Vectorized2D<T: Scalar> {
784    
785    /// Converts a type that can be represented as a Vector of 2 as a `Vec2`.
786    /// This is the public interface for the `Vectorized` trait.
787    fn vec2(self) -> Vec2<T>;
788}
789
790impl <T: Scalar + Vectorized<T, Vec2<T>>> Vectorized2D<T> for T {
791    fn vec2(self) -> Vec2<T> {
792        self.dvec()
793    }
794}
795
796impl <T: Scalar + Vectorized<T, Vec2<T>>> Vectorized2D<T> for (T, T) {
797    fn vec2(self) -> Vec2<T> {
798        self.dvec()
799    }
800}