physics2d/math/
vec2.rs

1use std::ops::{Add, Sub, Neg, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign};
2
3/// A 2-dimensional vector.
4///
5/// The `Vec2` type can be used to represent anything that has two dimensions: a size, a point, a velocity, etc.
6///
7#[derive(PartialEq, Copy, Clone, Debug)]
8pub struct Vec2 {
9    /// X coordinate of the vector.
10    pub x: f32,
11    /// Y coordinate of the vector.
12    pub y: f32,
13}
14
15impl Vec2 {
16    /// Creates a new vector from its coordinates.
17    pub fn new(x: f32, y: f32) -> Vec2 {
18        Vec2 { x, y }
19    }
20    
21    /// Returns the length (magnitude) of the vector.
22    ///
23    /// # Examples
24    ///
25    /// ```
26    /// # use physics2d::Vec2;
27    /// let v = Vec2::new(3.0, 4.0);
28    /// assert_eq!(v.len(), 5.0);
29    /// ```
30    #[inline]
31    pub fn len(&self) -> f32 {
32        self.sqr_len().sqrt()
33    }
34    
35    /// Returns the _square_ of the length (magnitude) of the vector.
36    ///
37    /// # Examples
38    ///
39    /// ```
40    /// # use physics2d::Vec2;
41    /// let v = Vec2::new(3.0, 4.0);
42    /// assert_eq!(v.sqr_len(), 25.0);
43    /// assert_eq!(v.sqr_len(), v.len() * v.len());
44    /// ```
45    #[inline]
46    pub fn sqr_len(&self) -> f32 {
47        self.x * self.x + self.y * self.y
48    }
49    
50    /// Returns the dot product of this vector with another vector.
51    ///
52    /// # Examples
53    ///
54    /// ```
55    /// # use physics2d::Vec2;
56    /// let a = Vec2::new(3.0, 4.0);
57    /// let b = Vec2::new(4.0, 5.0);
58    ///
59    /// assert_eq!(a.dot(&b), 32.0);
60    /// ```
61    #[inline]
62    pub fn dot(&self, b: &Vec2) -> f32 {
63        self.x * b.x + self.y * b.y
64    }
65    
66    /// Returns the normalized (unit) vector for the given vector.
67    ///
68    /// # Examples
69    ///
70    /// ```
71    /// # use physics2d::Vec2;
72    /// let v = Vec2::new(3.0, 4.0);
73    /// let l = v.len();
74    /// let n = v.normalized();
75    ///
76    /// assert_eq!(n, v / l);
77    /// assert_eq!(n.len(), 1.0);
78    /// ```
79    #[inline]
80    pub fn normalized(self) -> Vec2 {
81        let len = self.len();
82        if len == 0.0 {
83            Vec2::ZERO
84        } else {
85            self / len
86        }
87    }
88    
89    /// Returns a vector whose components are the minimum of the corresponding components
90    /// of the two vectors.
91    ///
92    /// # Examples
93    ///
94    /// ```
95    /// # use physics2d::Vec2;
96    /// let a = Vec2::new(3.0, 40.0);
97    /// let b = Vec2::new(40.0, 3.0);
98    ///
99    /// assert_eq!(a.min(&b), Vec2::new(3.0, 3.0));
100    /// ```
101    #[inline]
102    pub fn min(&self, other: &Vec2) -> Vec2 {
103        Vec2::new(self.x.min(other.x), self.y.min(other.y))
104    }
105    
106    /// Returns a vector whose components are the maximum of the corresponding components
107    /// of the two vectors.
108    ///
109    /// # Examples
110    ///
111    /// ```
112    /// # use physics2d::Vec2;
113    /// let a = Vec2::new(3.0, 40.0);
114    /// let b = Vec2::new(40.0, 3.0);
115    ///
116    /// assert_eq!(a.max(&b), Vec2::new(40.0, 40.0));
117    /// ```
118    #[inline]
119    pub fn max(&self, other: &Vec2) -> Vec2 {
120        Vec2::new(self.x.max(other.x), self.y.max(other.y))
121    }
122    
123    pub const ZERO: Vec2 = Vec2 { x: 0.0, y: 0.0 };
124    
125    pub const UP: Vec2 = Vec2 { x: 0.0, y: 1.0 };
126    
127    pub const RIGHT: Vec2 = Vec2 { x: 1.0, y: 0.0 };
128    
129    pub const DOWN: Vec2 = Vec2 { x: 0.0, y: -1.0 };
130    
131    pub const LEFT: Vec2 = Vec2 { x: -1.0, y: 0.0 };
132    
133    pub const ONE: Vec2 = Vec2 { x: 1.0, y: 1.0 };
134}
135
136/// The vector cross product.
137pub trait Cross<RHS = Self> {
138    /// The type of the result of the cross product.
139    type Output;
140    
141    /// Performs the cross product.
142    fn cross(self, other: RHS) -> Self::Output;
143}
144
145impl Cross for Vec2 {
146    type Output = f32;
147    
148    fn cross(self, other: Vec2) -> f32 {
149        self.x * other.y - self.y * other.x
150    }
151}
152
153impl<'a> Cross<Vec2> for &'a Vec2 {
154    type Output = f32;
155    
156    fn cross(self, other: Vec2) -> f32 {
157        self.x * other.y - self.y * other.x
158    }
159}
160
161impl<'b> Cross<&'b Vec2> for Vec2 {
162    type Output = f32;
163    
164    fn cross(self, other: &'b Vec2) -> f32 {
165        self.x * other.y - self.y * other.x
166    }
167}
168
169impl<'a, 'b> Cross<&'b Vec2> for &'a Vec2 {
170    type Output = f32;
171    
172    fn cross(self, other: &'b Vec2) -> f32 {
173        self.x * other.y - self.y * other.x
174    }
175}
176
177impl Cross<f32> for Vec2 {
178    type Output = Vec2;
179    
180    fn cross(self, s: f32) -> Vec2 {
181        Vec2::new(s * self.y, -s * self.x)
182    }
183}
184
185impl<'a> Cross<f32> for &'a Vec2 {
186    type Output = Vec2;
187    
188    fn cross(self, s: f32) -> Vec2 {
189        Vec2::new(s * self.y, -s * self.x)
190    }
191}
192
193impl<'b> Cross<&'b f32> for Vec2 {
194    type Output = Vec2;
195    
196    fn cross(self, s: &'b f32) -> Vec2 {
197        Vec2::new(s * self.y, -s * self.x)
198    }
199}
200
201impl<'a, 'b> Cross<&'b f32> for &'a Vec2 {
202    type Output = Vec2;
203    
204    fn cross(self, s: &'b f32) -> Vec2 {
205        Vec2::new(s * self.y, -s * self.x)
206    }
207}
208
209impl Cross<Vec2> for f32 {
210    type Output = Vec2;
211    
212    fn cross(self, other: Vec2) -> Vec2 {
213        -other.cross(self)
214    }
215}
216
217impl<'a> Cross<Vec2> for &'a f32 {
218    type Output = Vec2;
219    
220    fn cross(self, other: Vec2) -> Vec2 {
221        -other.cross(self)
222    }
223}
224
225impl<'b> Cross<&'b Vec2> for f32 {
226    type Output = Vec2;
227    
228    fn cross(self, other: &'b Vec2) -> Vec2 {
229        -other.cross(self)
230    }
231}
232
233impl<'a, 'b> Cross<&'b Vec2> for &'a f32 {
234    type Output = Vec2;
235    
236    fn cross(self, other: &'b Vec2) -> Vec2 {
237        -other.cross(self)
238    }
239}
240
241
242impl Neg for Vec2 {
243    type Output = Self;
244    
245    fn neg(self) -> Self {
246        Vec2::new(-self.x, -self.y)
247    }
248}
249
250impl<'a> Neg for &'a Vec2 {
251    type Output = Vec2;
252    
253    fn neg(self) -> Vec2 {
254        Vec2::new(-self.x, -self.y)
255    }
256}
257
258
259impl Add for Vec2 {
260    type Output = Vec2;
261    
262    fn add(self, other: Vec2) -> Vec2 {
263        Vec2::new(self.x + other.x, self.y + other.y)
264    }
265}
266
267impl<'a> Add<Vec2> for &'a Vec2 {
268    type Output = Vec2;
269    
270    fn add(self, other: Vec2) -> Vec2 {
271        Vec2::new(self.x + other.x, self.y + other.y)
272    }
273}
274
275impl<'b> Add<&'b Vec2> for Vec2 {
276    type Output = Vec2;
277    
278    fn add(self, other: &'b Vec2) -> Vec2 {
279        Vec2::new(self.x + other.x, self.y + other.y)
280    }
281}
282
283impl<'a, 'b> Add<&'b Vec2> for &'a Vec2 {
284    type Output = Vec2;
285    
286    fn add(self, other: &'b Vec2) -> Vec2 {
287        Vec2::new(self.x + other.x, self.y + other.y)
288    }
289}
290
291impl AddAssign for Vec2 {
292    fn add_assign(&mut self, other: Vec2) {
293        *self = Vec2 {
294            x: self.x + other.x,
295            y: self.y + other.y,
296        };
297    }
298}
299
300impl<'b> AddAssign<&'b Vec2> for Vec2 {
301    fn add_assign(&mut self, other: &'b Vec2) {
302        *self = Vec2 {
303            x: self.x + other.x,
304            y: self.y + other.y,
305        };
306    }
307}
308
309
310impl Sub for Vec2 {
311    type Output = Vec2;
312    
313    fn sub(self, other: Vec2) -> Vec2 {
314        Vec2::new(self.x - other.x, self.y - other.y)
315    }
316}
317
318impl<'a> Sub<Vec2> for &'a Vec2 {
319    type Output = Vec2;
320    
321    fn sub(self, other: Vec2) -> Vec2 {
322        Vec2::new(self.x - other.x, self.y - other.y)
323    }
324}
325
326impl<'b> Sub<&'b Vec2> for Vec2 {
327    type Output = Vec2;
328    
329    fn sub(self, other: &'b Vec2) -> Vec2 {
330        Vec2::new(self.x - other.x, self.y - other.y)
331    }
332}
333
334impl<'a, 'b> Sub<&'b Vec2> for &'a Vec2 {
335    type Output = Vec2;
336    
337    fn sub(self, other: &'b Vec2) -> Vec2 {
338        Vec2::new(self.x - other.x, self.y - other.y)
339    }
340}
341
342impl SubAssign for Vec2 {
343    fn sub_assign(&mut self, other: Vec2) {
344        *self = Vec2 {
345            x: self.x - other.x,
346            y: self.y - other.y,
347        };
348    }
349}
350
351impl<'b> SubAssign<&'b Vec2> for Vec2 {
352    fn sub_assign(&mut self, other: &'b Vec2) {
353        *self = Vec2 {
354            x: self.x - other.x,
355            y: self.y - other.y,
356        };
357    }
358}
359
360
361impl Mul<f32> for Vec2 {
362    type Output = Vec2;
363    
364    fn mul(self, s: f32) -> Vec2 {
365        Vec2::new(self.x * s, self.y * s)
366    }
367}
368
369impl<'a> Mul<f32> for &'a Vec2 {
370    type Output = Vec2;
371    
372    fn mul(self, s: f32) -> Vec2 {
373        Vec2::new(self.x * s, self.y * s)
374    }
375}
376
377impl<'b> Mul<&'b f32> for Vec2 {
378    type Output = Vec2;
379    
380    fn mul(self, s: &'b f32) -> Vec2 {
381        Vec2::new(self.x * s, self.y * s)
382    }
383}
384
385impl<'a, 'b> Mul<&'b f32> for &'a Vec2 {
386    type Output = Vec2;
387    
388    fn mul(self, s: &'b f32) -> Vec2 {
389        Vec2::new(self.x * s, self.y * s)
390    }
391}
392
393impl Mul<Vec2> for f32 {
394    type Output = Vec2;
395    
396    fn mul(self, v: Vec2) -> Vec2 {
397        Vec2::new(self * v.x, self * v.y)
398    }
399}
400
401impl<'a> Mul<Vec2> for &'a f32 {
402    type Output = Vec2;
403    
404    fn mul(self, v: Vec2) -> Vec2 {
405        Vec2::new(self * v.x, self * v.y)
406    }
407}
408
409impl<'b> Mul<&'b Vec2> for f32 {
410    type Output = Vec2;
411    
412    fn mul(self, v: &'b Vec2) -> Vec2 {
413        Vec2::new(self * v.x, self * v.y)
414    }
415}
416
417impl<'a, 'b> Mul<&'b Vec2> for &'a f32 {
418    type Output = Vec2;
419    
420    fn mul(self, v: &'b Vec2) -> Vec2 {
421        Vec2::new(self * v.x, self * v.y)
422    }
423}
424
425impl MulAssign<f32> for Vec2 {
426    fn mul_assign(&mut self, s: f32) {
427        *self = Vec2 {
428            x: self.x * s,
429            y: self.y * s,
430        };
431    }
432}
433
434impl<'b> MulAssign<&'b f32> for Vec2 {
435    fn mul_assign(&mut self, s: &'b f32) {
436        *self = Vec2 {
437            x: self.x * s,
438            y: self.y * s,
439        };
440    }
441}
442
443
444impl Div<f32> for Vec2 {
445    type Output = Vec2;
446    
447    fn div(self, s: f32) -> Vec2 {
448        self * (1.0 / s)
449    }
450}
451
452impl<'a> Div<f32> for &'a Vec2 {
453    type Output = Vec2;
454    
455    fn div(self, s: f32) -> Vec2 {
456        self * (1.0 / s)
457    }
458}
459
460impl<'b> Div<&'b f32> for Vec2 {
461    type Output = Vec2;
462    
463    fn div(self, s: &'b f32) -> Vec2 {
464        self * (1.0 / s)
465    }
466}
467
468impl<'a, 'b> Div<&'b f32> for &'a Vec2 {
469    type Output = Vec2;
470    
471    fn div(self, s: &'b f32) -> Vec2 {
472        self * (1.0 / s)
473    }
474}
475
476impl DivAssign<f32> for Vec2 {
477    fn div_assign(&mut self, s: f32) {
478        *self *= 1.0 / s;
479    }
480}
481
482impl<'b> DivAssign<&'b f32> for Vec2 {
483    fn div_assign(&mut self, s: &'b f32) {
484        *self *= 1.0 / s;
485    }
486}