vex/
vector2.rs

1use crate::common;
2use crate::vector3::Vector3;
3
4use std::cmp;
5use std::convert::From;
6use std::f32::EPSILON;
7use std::fmt;
8use std::fmt::{Display, Formatter};
9
10use std::ops::{
11    Index,
12    IndexMut,
13    Neg,
14    Add,
15    AddAssign,
16    Sub,
17    SubAssign,
18    Mul,
19    MulAssign,
20    Div,
21    DivAssign,
22};
23
24#[repr(C, packed)]
25#[derive(Copy, Clone, Debug)]
26pub struct Vector2 {
27    pub x: f32,
28    pub y: f32,
29}
30
31impl Vector2 {
32    /// Creates a vector <0.0, 0.0>
33    ///
34    /// # Examples
35    /// ```
36    /// use vex::Vector2;
37    /// 
38    /// let actual = Vector2::new();
39    /// let expected = Vector2 { x: 0.0, y: 0.0 };
40    /// assert_eq!(actual, expected);
41    /// ```
42    #[inline]
43    pub fn new() -> Vector2 {
44        Vector2 { x: 0.0, y: 0.0 }
45    }
46
47    /// Creates a vector <1.0, 1.0>
48    ///
49    /// # Examples
50    /// ```
51    /// use vex::Vector2;
52    /// 
53    /// let actual = Vector2::one();
54    /// let expected = Vector2 { x: 1.0, y: 1.0 };
55    /// assert_eq!(actual, expected);
56    /// ```
57    #[inline]
58    pub fn one() -> Vector2 {
59        Vector2 { x: 1.0, y: 1.0 }
60    }
61
62    /// Creates a vector from the provided values
63    ///
64    /// # Examples
65    /// ```
66    /// use vex::Vector2;
67    /// 
68    /// let actual = Vector2::make(1.0, 2.0);
69    /// let expected = Vector2 { x: 1.0, y: 2.0 };
70    /// assert_eq!(actual, expected);
71    /// ```
72    #[inline]
73    pub fn make(x: f32, y: f32) -> Vector2 {
74        Vector2 { x, y }
75    }
76
77    /// Find the dot product between two vectors
78    ///
79    /// # Examples
80    /// ```
81    /// use vex::Vector2;
82    /// 
83    /// let a = Vector2::make(1.0, 0.0);
84    /// let b = Vector2::make(0.0, 1.0);
85    /// let actual = Vector2::dot(&a, &b);
86    /// let expected = 0.0;
87    /// assert_eq!(actual, expected);
88    /// ```
89    #[inline]
90    pub fn dot(a: &Vector2, b: &Vector2) -> f32 {
91        a.x * b.x + a.y * b.y
92    }
93
94    /// Find the cross product between two vectors
95    ///
96    /// # Examples
97    /// ```
98    /// use vex::Vector2;
99    /// 
100    /// let a = Vector2::make(1.0, 0.0);
101    /// let b = Vector2::make(0.0, 1.0);
102    /// let actual = Vector2::cross(&a, &b);
103    /// let expected = 1.0;
104    /// assert_eq!(actual, expected);
105    /// ```
106    #[inline]
107    pub fn cross(a: &Vector2, b: &Vector2) -> f32 {
108        a.x * b.y - a.y * b.x
109    }
110
111    /// Find the cross product between a scalar (left) and vector (right)
112    ///
113    /// # Examples
114    /// ```
115    /// use vex::Vector2;
116    /// 
117    /// let s = 1.0;
118    /// let v = Vector2::make(1.0, 0.0);
119    /// let actual = Vector2::cross_scalar_vec(s, &v);
120    /// let expected = Vector2::make(0.0, 1.0);
121    /// assert_eq!(actual, expected);
122    /// ```
123    #[inline]
124    pub fn cross_scalar_vec(s: f32, v: &Vector2) -> Vector2 {
125        Vector2::make(-s * v.y, s * v.x)
126    }
127
128    /// Find the cross product between a vector (left) and scalar (right)
129    ///
130    /// # Examples
131    /// ```
132    /// use vex::Vector2;
133    /// 
134    /// let s = 1.0;
135    /// let v = Vector2::make(1.0, 0.0);
136    /// let actual = Vector2::cross_vec_scalar(&v, s);
137    /// let expected = Vector2::make(0.0, -1.0);
138    /// assert_eq!(actual, expected);
139    /// ```
140    #[inline]
141    pub fn cross_vec_scalar(v: &Vector2, s: f32) -> Vector2 {
142        Vector2::make(s * v.y, -s * v.x)
143    }
144
145    /// Find the minimum (component-wise) vector between two vectors
146    ///
147    /// # Examples
148    /// ```
149    /// use vex::Vector2;
150    /// 
151    /// let a = Vector2::make(1.0, 4.0);
152    /// let b = Vector2::make(2.0, 3.0);
153    /// let actual = Vector2::min(&a, &b);
154    /// let expected = Vector2::make(1.0, 3.0);
155    /// assert_eq!(actual, expected);
156    /// ```
157    #[inline]
158    pub fn min(a: &Vector2, b: &Vector2) -> Vector2 {
159        Vector2::make(a.x.min(b.x), a.y.min(b.y))
160    }
161
162    /// Find the maximum (component-wise) vector between two vectors
163    ///
164    /// # Examples
165    /// ```
166    /// use vex::Vector2;
167    /// 
168    /// let a = Vector2::make(1.0, 4.0);
169    /// let b = Vector2::make(2.0, 3.0);
170    /// let actual = Vector2::max(&a, &b);
171    /// let expected = Vector2::make(2.0, 4.0);
172    /// assert_eq!(actual, expected);
173    /// ```
174    #[inline]
175    pub fn max(a: &Vector2, b: &Vector2) -> Vector2 {
176        Vector2::make(a.x.max(b.x), a.y.max(b.y))
177    }
178
179    /// Find the clamped (component-wise) vector between two vectors
180    ///
181    /// # Examples
182    /// ```
183    /// use vex::Vector2;
184    /// 
185    /// let a = Vector2::make(1.0, 3.0);
186    /// let b = Vector2::make(2.0, 4.0);
187    /// let mut actual = Vector2::make(0.0, 5.0);
188    /// actual.clamp(&a, &b);
189    /// let expected = Vector2::make(1.0, 4.0);
190    /// assert_eq!(actual, expected);
191    /// ```
192    #[inline]
193    pub fn clamp(&mut self, a: &Vector2, b: &Vector2) {
194        let low = Self::min(a, b);
195        let high = Self::max(a, b);
196        let result = Self::max(&low, &Self::min(self, &high));
197        self.set(result.x, result.y);
198    }
199
200    /// Set the components of a vector
201    ///
202    /// # Examples
203    /// ```
204    /// use vex::Vector2;
205    /// 
206    /// let mut actual = Vector2::new();
207    /// actual.set(1.0, 2.0);
208    /// let expected = Vector2::make(1.0, 2.0);
209    /// assert_eq!(actual, expected);
210    /// ```
211    #[inline]
212    pub fn set(&mut self, x: f32, y: f32) {
213        self.x = x;
214        self.y = y;
215    }
216
217    /// Get the magnitude of the vector
218    ///
219    /// # Examples
220    /// ```
221    /// use vex::Vector2;
222    /// 
223    /// let actual = Vector2::make(1.0, 2.0).mag();
224    /// let expected = 2.2360679775;
225    /// assert_eq!(actual, expected);
226    /// ```
227    #[inline]
228    pub fn mag(&self) -> f32 {
229        self.mag_sq().sqrt()
230    }
231
232    /// Get the squared magnitude of the vector
233    ///
234    /// # Examples
235    /// ```
236    /// use vex::Vector2;
237    /// 
238    /// let actual = Vector2::make(1.0, 2.0).mag_sq();
239    /// let expected = 5.0;
240    /// assert_eq!(actual, expected);
241    /// ```
242    #[inline]
243    pub fn mag_sq(&self) -> f32 {
244        self.x * self.x + self.y * self.y
245    }
246
247    /// Normalize the vector
248    ///
249    /// # Examples
250    /// ```
251    /// use vex::Vector2;
252    /// 
253    /// let mut actual = Vector2::make(1.0, 2.0);
254    /// actual.norm();
255    /// let expected = Vector2::make(0.4472135955, 0.894427191);
256    /// assert_eq!(actual, expected);
257    /// ```
258    #[inline]
259    pub fn norm(&mut self) -> f32 {
260        let length = self.mag();
261        if length > EPSILON {
262            self.x /= length;
263            self.y /= length;
264            length
265        } else {
266            0.0
267        }
268    }
269
270    /// Set the components of a vector to their absolute values
271    ///
272    /// # Examples
273    /// ```
274    /// use vex::Vector2;
275    /// 
276    /// let mut actual = Vector2::make(-1.0, -2.0);
277    /// actual.abs();
278    /// let expected = Vector2::make(1.0, 2.0);
279    /// assert_eq!(actual, expected);
280    /// ```
281    #[inline]
282    pub fn abs(&mut self) {
283        self.x = self.x.abs();
284        self.y = self.y.abs();
285    }
286
287    /// Skew the vector
288    ///
289    /// # Examples
290    /// ```
291    /// use vex::Vector2;
292    /// 
293    /// let mut actual = Vector2::make(1.0, 2.0);
294    /// actual.skew();
295    /// let expected = Vector2::make(-2.0, 1.0);
296    /// assert_eq!(actual, expected);
297    /// ```
298    #[inline]
299    pub fn skew(&mut self) {
300        let x = self.x;
301        self.x = -self.y;
302        self.y = x;
303    }
304
305    /// Determine whether or not all components of the vector are valid
306    ///
307    /// # Examples
308    /// ```
309    /// use vex::Vector2;
310    /// 
311    /// let actual = Vector2::make(1.0, 2.0);
312    /// assert!(actual.is_valid());
313    /// ```
314    #[inline]
315    pub fn is_valid(&self) -> bool {
316        for i in 0..2 {
317            if !common::is_valid(self[i]) {
318                return false;
319            }
320        }
321
322        true
323    }
324}
325
326impl From<Vector3> for Vector2 {
327    /// Creates a Vector2 from the components of a Vector3
328    ///
329    /// # Examples
330    /// ```
331    /// use vex::Vector2;
332    /// use vex::Vector3;
333    /// 
334    /// let input = Vector3::make(1.0, 2.0, 3.0);
335    /// let actual = Vector2::from(input);
336    /// let expected = Vector2 { x: 1.0, y: 2.0 };
337    /// assert_eq!(actual, expected);
338    /// ```
339    #[inline]
340    fn from(item: Vector3) -> Self {
341        Vector2 {
342            x: item.x,
343            y: item.y,
344        }
345    }
346}
347
348impl Index<u32> for Vector2 {
349    type Output = f32;
350
351    /// Looks up a component by index
352    ///
353    /// # Examples
354    /// ```
355    /// use vex::Vector2;
356    /// 
357    /// let mut v = Vector2::make(1.0, 2.0);
358    /// assert_eq!(v[0], 1.0);
359    /// assert_eq!(v[1], 2.0);
360    /// ```
361    #[inline]
362    fn index(&self, index: u32) -> &f32 {
363        unsafe {
364            match index {
365                0 => &self.x,
366                1 => &self.y,
367                _ => panic!("Invalid index for Vector2: {}", index),
368            }
369        }
370    }
371}
372
373impl IndexMut<u32> for Vector2 {
374    /// Mutate a component by index
375    ///
376    /// # Examples
377    /// ```
378    /// use vex::Vector2;
379    /// 
380    /// let mut v = Vector2::new();
381    /// v[0] = 3.0;
382    /// v[1] = 4.0;
383    /// assert_eq!(v[0], 3.0);
384    /// assert_eq!(v[1], 4.0);
385    /// ```
386    #[inline]
387    fn index_mut<'a>(&'a mut self, index: u32) -> &'a mut f32 {
388        unsafe {
389            match index {
390                0 => &mut self.x,
391                1 => &mut self.y,
392                _ => panic!("Invalid index for Vector2: {}", index),
393            }
394        }
395    }
396}
397
398impl Neg for Vector2 {
399    type Output = Vector2;
400
401    /// Negates all components in a vector
402    ///
403    /// # Examples
404    /// ```
405    /// use vex::Vector2;
406    /// 
407    /// let actual = -Vector2::make(1.0, 2.0);
408    /// let expected = Vector2::make(-1.0, -2.0);
409    /// assert_eq!(actual, expected);
410    /// ```
411    #[inline]
412    fn neg(self) -> Vector2 {
413        Vector2::make(-self.x, -self.y)
414    }
415}
416
417impl Add<f32> for Vector2 {
418    type Output = Vector2;
419
420    /// Find the resulting vector by adding a scalar to a vector's components
421    ///
422    /// # Examples
423    /// ```
424    /// use vex::Vector2;
425    /// 
426    /// let actual = Vector2::make(1.0, 2.0) + 1.0;
427    /// let expected = Vector2::make(2.0, 3.0);
428    /// assert_eq!(actual, expected);
429    /// ```
430    #[inline]
431    fn add(self, _rhs: f32) -> Vector2 {
432        Vector2::make(self.x + _rhs, self.y + _rhs)
433    }
434}
435
436impl Add<Vector2> for Vector2 {
437    type Output = Vector2;
438
439    /// Add two vectors
440    ///
441    /// # Examples
442    /// ```
443    /// use vex::Vector2;
444    /// 
445    /// let a = Vector2::make(1.0, 2.0);
446    /// let b = Vector2::make(3.0, 4.0);
447    /// let actual = a + b;
448    /// let expected = Vector2::make(4.0, 6.0);
449    /// assert_eq!(actual, expected);
450    /// ```
451    #[inline]
452    fn add(self, _rhs: Vector2) -> Vector2 {
453        Vector2::make(self.x + _rhs.x, self.y + _rhs.y)
454    }
455}
456
457impl AddAssign<f32> for Vector2 {
458    /// Increment a vector by a scalar
459    ///
460    /// # Examples
461    /// ```
462    /// use vex::Vector2;
463    /// 
464    /// let mut actual = Vector2::make(1.0, 2.0);
465    /// actual += 10.0;
466    /// let expected = Vector2::make(11.0, 12.0);
467    /// assert_eq!(actual, expected);
468    /// ```
469    #[inline]
470    fn add_assign(&mut self, _rhs: f32) {
471        self.x += _rhs;
472        self.y += _rhs;
473    }
474}
475
476impl AddAssign<Vector2> for Vector2 {
477    /// Increment a vector by another vector
478    ///
479    /// # Examples
480    /// ```
481    /// use vex::Vector2;
482    /// 
483    /// let mut actual = Vector2::make(1.0, 2.0);
484    /// actual += Vector2::make(1.0, 2.0);
485    /// let expected = Vector2::make(2.0, 4.0);
486    /// assert_eq!(actual, expected);
487    /// ```
488    #[inline]
489    fn add_assign(&mut self, _rhs: Vector2) {
490        self.x += _rhs.x;
491        self.y += _rhs.y;
492    }
493}
494
495impl Sub<f32> for Vector2 {
496    type Output = Vector2;
497
498    /// Find the resulting vector by subtracting a scalar from a vector's components
499    ///
500    /// # Examples
501    /// ```
502    /// use vex::Vector2;
503    /// 
504    /// let actual = Vector2::make(1.0, 2.0) - 10.0;
505    /// let expected = Vector2::make(-9.0, -8.0);
506    /// assert_eq!(actual, expected);
507    /// ```
508    #[inline]
509    fn sub(self, _rhs: f32) -> Vector2 {
510        Vector2::make(self.x - _rhs, self.y - _rhs)
511    }
512}
513
514impl Sub<Vector2> for Vector2 {
515    type Output = Vector2;
516
517    /// Subtract two vectors
518    ///
519    /// # Examples
520    /// ```
521    /// use vex::Vector2;
522    /// 
523    /// let a = Vector2::make(1.0, 2.0);
524    /// let b = Vector2::make(4.0, 3.0);
525    /// let actual = a - b;
526    /// let expected = Vector2::make(-3.0, -1.0);
527    /// assert_eq!(actual, expected);
528    /// ```
529    #[inline]
530    fn sub(self, _rhs: Vector2) -> Vector2 {
531        Vector2::make(self.x - _rhs.x, self.y - _rhs.y)
532    }
533}
534
535impl SubAssign<f32> for Vector2 {
536    /// Decrement a vector by a scalar
537    ///
538    /// # Examples
539    /// ```
540    /// use vex::Vector2;
541    /// 
542    /// let mut actual = Vector2::make(1.0, 2.0);
543    /// actual -= 1.0;
544    /// let expected = Vector2::make(0.0, 1.0);
545    /// assert_eq!(actual, expected);
546    /// ```
547    #[inline]
548    fn sub_assign(&mut self, _rhs: f32) {
549        self.x -= _rhs;
550        self.y -= _rhs;
551    }
552}
553
554impl SubAssign<Vector2> for Vector2 {
555    /// Decrement a vector by another vector
556    ///
557    /// # Examples
558    /// ```
559    /// use vex::Vector2;
560    /// 
561    /// let mut actual = Vector2::make(1.0, 2.0);
562    /// actual -= Vector2::make(1.0, 2.0);
563    /// assert_eq!(actual, Vector2::new());
564    /// ```
565    #[inline]
566    fn sub_assign(&mut self, _rhs: Vector2) {
567        self.x -= _rhs.x;
568        self.y -= _rhs.y;
569    }
570}
571
572impl Mul<f32> for Vector2 {
573    type Output = Vector2;
574
575    /// Find the resulting vector by multiplying a scalar to a vector's components
576    ///
577    /// # Examples
578    /// ```
579    /// use vex::Vector2;
580    /// 
581    /// let actual = Vector2::make(1.0, 2.0) * 2.0;
582    /// let expected = Vector2::make(2.0, 4.0);
583    /// assert_eq!(actual, expected);
584    /// ```
585    #[inline]
586    fn mul(self, _rhs: f32) -> Vector2 {
587        Vector2::make(self.x * _rhs, self.y * _rhs)
588    }
589}
590
591impl Mul<Vector2> for Vector2 {
592    type Output = Vector2;
593
594    /// Multiply two vectors
595    ///
596    /// # Examples
597    /// ```
598    /// use vex::Vector2;
599    /// 
600    /// let a = Vector2::make(1.0, 2.0);
601    /// let b = Vector2::make(2.0, 3.0);
602    /// let actual = a * b;
603    /// let expected = Vector2::make(2.0, 6.0);
604    /// assert_eq!(actual, expected);
605    /// ```
606    #[inline]
607    fn mul(self, _rhs: Vector2) -> Vector2 {
608        Vector2::make(self.x * _rhs.x, self.y * _rhs.y)
609    }
610}
611
612impl MulAssign<f32> for Vector2 {
613    /// Multiply a vector by a scalar
614    ///
615    /// # Examples
616    /// ```
617    /// use vex::Vector2;
618    /// 
619    /// let mut actual = Vector2::make(1.0, 2.0);
620    /// actual *= 2.0;
621    /// let expected = Vector2::make(2.0, 4.0);
622    /// assert_eq!(actual, expected);
623    /// ```
624    #[inline]
625    fn mul_assign(&mut self, _rhs: f32) {
626        self.x *= _rhs;
627        self.y *= _rhs;
628    }
629}
630
631impl MulAssign<Vector2> for Vector2 {
632    /// Multiply a vector by another vector
633    ///
634    /// # Examples
635    /// ```
636    /// use vex::Vector2;
637    /// 
638    /// let mut actual = Vector2::make(1.0, 2.0);
639    /// actual *= Vector2::make(2.0, 3.0);
640    /// let expected = Vector2::make(2.0, 6.0);
641    /// assert_eq!(actual, expected);
642    /// ```
643    #[inline]
644    fn mul_assign(&mut self, _rhs: Vector2) {
645        self.x *= _rhs.x;
646        self.y *= _rhs.y;
647    }
648}
649
650impl Div<f32> for Vector2 {
651    type Output = Vector2;
652
653    /// Find the resulting vector by dividing a scalar to a vector's components
654    ///
655    /// # Examples
656    /// ```
657    /// use vex::Vector2;
658    /// 
659    /// let actual = Vector2::make(1.0, 2.0) / 2.0;
660    /// let expected = Vector2::make(0.5, 1.0);
661    /// assert_eq!(actual, expected);
662    /// ```
663    #[inline]
664    fn div(self, _rhs: f32) -> Vector2 {
665        Vector2::make(self.x / _rhs, self.y / _rhs)
666    }
667}
668
669impl Div<Vector2> for Vector2 {
670    type Output = Vector2;
671
672    /// Divide two vectors
673    ///
674    /// # Examples
675    /// ```
676    /// use vex::Vector2;
677    /// 
678    /// let a = Vector2::make(1.0, 2.0);
679    /// let b = Vector2::make(2.0, 8.0);
680    /// let actual = a / b;
681    /// let expected = Vector2::make(0.5, 0.25);
682    /// assert_eq!(actual, expected);
683    /// ```
684    #[inline]
685    fn div(self, _rhs: Vector2) -> Vector2 {
686        Vector2::make(self.x / _rhs.x, self.y / _rhs.y)
687    }
688}
689
690impl DivAssign<f32> for Vector2 {
691    /// Divide a vector by a scalar
692    ///
693    /// # Examples
694    /// ```
695    /// use vex::Vector2;
696    /// 
697    /// let mut actual = Vector2::make(1.0, 2.0);
698    /// actual /= 2.0;
699    /// let expected = Vector2::make(0.5, 1.0);
700    /// assert_eq!(actual, expected);
701    /// ```
702    #[inline]
703    fn div_assign(&mut self, _rhs: f32) {
704        self.x /= _rhs;
705        self.y /= _rhs;
706    }
707}
708
709impl DivAssign<Vector2> for Vector2 {
710    /// Divide a vector by another vector
711    ///
712    /// # Examples
713    /// ```
714    /// use vex::Vector2;
715    /// 
716    /// let mut actual = Vector2::make(1.0, 2.0);
717    /// actual /= Vector2::make(2.0, 8.0);
718    /// let expected = Vector2::make(0.5, 0.25);
719    /// assert_eq!(actual, expected);
720    /// ```
721    #[inline]
722    fn div_assign(&mut self, _rhs: Vector2) {
723        self.x /= _rhs.x;
724        self.y /= _rhs.y;
725    }
726}
727
728impl cmp::PartialEq for Vector2 {
729    /// Determines if two vectors' components are equivalent
730    ///
731    /// # Examples
732    /// ```
733    /// use vex::Vector2;
734    /// 
735    /// assert!(Vector2::new() == Vector2::new());
736    /// ```
737    #[inline]
738    fn eq(&self, _rhs: &Vector2) -> bool {
739        for i in 0..2 {
740            if self[i] != _rhs[i] {
741                return false;
742            }
743        }
744
745        true
746    }
747}
748
749impl Display for Vector2 {
750    #[inline]
751    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
752        unsafe { write!(f, "<{}  {}>", self.x, self.y) }
753    }
754}