Skip to main content

windows_numerics/
vector2.rs

1use super::*;
2
3impl Vector2 {
4    /// Creates a vector from its `x` and `y` components.
5    pub fn new(x: f32, y: f32) -> Self {
6        Self { x, y }
7    }
8    /// Returns the vector `(0, 0)`.
9    pub fn zero() -> Self {
10        Self { x: 0f32, y: 0f32 }
11    }
12    /// Returns the vector `(1, 1)`.
13    pub fn one() -> Self {
14        Self { x: 1f32, y: 1f32 }
15    }
16    /// Returns the unit vector `(1, 0)`.
17    pub fn unit_x() -> Self {
18        Self { x: 1.0, y: 0.0 }
19    }
20    /// Returns the unit vector `(0, 1)`.
21    pub fn unit_y() -> Self {
22        Self { x: 0.0, y: 1.0 }
23    }
24    /// Returns the dot product of `self` and `rhs`.
25    pub fn dot(&self, rhs: &Self) -> f32 {
26        self.x * rhs.x + self.y * rhs.y
27    }
28    /// Returns the squared length of the vector, avoiding the square root
29    /// computed by [`length`](Self::length).
30    pub fn length_squared(&self) -> f32 {
31        self.dot(self)
32    }
33    /// Returns the length (magnitude) of the vector.
34    #[cfg(feature = "std")]
35    pub fn length(&self) -> f32 {
36        self.length_squared().sqrt()
37    }
38    /// Returns the distance between the points `self` and `value`.
39    #[cfg(feature = "std")]
40    pub fn distance(&self, value: &Self) -> f32 {
41        (self - value).length()
42    }
43    /// Returns the squared distance between the points `self` and `value`.
44    pub fn distance_squared(&self, value: &Self) -> f32 {
45        (self - value).length_squared()
46    }
47    /// Returns a unit vector pointing in the same direction as `self`.
48    #[cfg(feature = "std")]
49    pub fn normalize(&self) -> Self {
50        self / self.length()
51    }
52
53    fn impl_neg(&self) -> Self {
54        Self {
55            x: -self.x,
56            y: -self.y,
57        }
58    }
59    fn impl_add(&self, rhs: &Self) -> Self {
60        Self {
61            x: self.x + rhs.x,
62            y: self.y + rhs.y,
63        }
64    }
65    fn impl_sub(&self, rhs: &Self) -> Self {
66        Self {
67            x: self.x - rhs.x,
68            y: self.y - rhs.y,
69        }
70    }
71    fn impl_div(&self, rhs: &Self) -> Self {
72        Self {
73            x: self.x / rhs.x,
74            y: self.y / rhs.y,
75        }
76    }
77    fn impl_div_f32(&self, rhs: f32) -> Self {
78        Self {
79            x: self.x / rhs,
80            y: self.y / rhs,
81        }
82    }
83    fn impl_mul(&self, rhs: &Self) -> Self {
84        Self {
85            x: self.x * rhs.x,
86            y: self.y * rhs.y,
87        }
88    }
89    fn impl_mul_f32(&self, rhs: f32) -> Self {
90        Self {
91            x: self.x * rhs,
92            y: self.y * rhs,
93        }
94    }
95}
96
97impl core::ops::Neg for Vector2 {
98    type Output = Self;
99    fn neg(self) -> Self {
100        self.impl_neg()
101    }
102}
103impl core::ops::Neg for &Vector2 {
104    type Output = Vector2;
105    fn neg(self) -> Vector2 {
106        self.impl_neg()
107    }
108}
109impl core::ops::Add<Self> for Vector2 {
110    type Output = Self;
111    fn add(self, rhs: Self) -> Self {
112        self.impl_add(&rhs)
113    }
114}
115impl core::ops::Add<&Self> for Vector2 {
116    type Output = Self;
117    fn add(self, rhs: &Self) -> Self {
118        self.impl_add(rhs)
119    }
120}
121impl core::ops::Add<Vector2> for &Vector2 {
122    type Output = Vector2;
123    fn add(self, rhs: Vector2) -> Vector2 {
124        self.impl_add(&rhs)
125    }
126}
127impl core::ops::Add<&Vector2> for &Vector2 {
128    type Output = Vector2;
129    fn add(self, rhs: &Vector2) -> Vector2 {
130        self.impl_add(rhs)
131    }
132}
133impl core::ops::Sub<Self> for Vector2 {
134    type Output = Self;
135    fn sub(self, rhs: Self) -> Self {
136        self.impl_sub(&rhs)
137    }
138}
139impl core::ops::Sub<&Self> for Vector2 {
140    type Output = Self;
141    fn sub(self, rhs: &Self) -> Self {
142        self.impl_sub(rhs)
143    }
144}
145impl core::ops::Sub<Vector2> for &Vector2 {
146    type Output = Vector2;
147    fn sub(self, rhs: Vector2) -> Vector2 {
148        self.impl_sub(&rhs)
149    }
150}
151impl core::ops::Sub<&Vector2> for &Vector2 {
152    type Output = Vector2;
153    fn sub(self, rhs: &Vector2) -> Vector2 {
154        self.impl_sub(rhs)
155    }
156}
157impl core::ops::Div<Self> for Vector2 {
158    type Output = Self;
159    fn div(self, rhs: Self) -> Self {
160        self.impl_div(&rhs)
161    }
162}
163impl core::ops::Div<&Self> for Vector2 {
164    type Output = Self;
165    fn div(self, rhs: &Self) -> Self {
166        self.impl_div(rhs)
167    }
168}
169impl core::ops::Div<Vector2> for &Vector2 {
170    type Output = Vector2;
171    fn div(self, rhs: Vector2) -> Vector2 {
172        self.impl_div(&rhs)
173    }
174}
175impl core::ops::Div<&Vector2> for &Vector2 {
176    type Output = Vector2;
177    fn div(self, rhs: &Vector2) -> Vector2 {
178        self.impl_div(rhs)
179    }
180}
181impl core::ops::Div<f32> for Vector2 {
182    type Output = Self;
183    fn div(self, rhs: f32) -> Self {
184        self.impl_div_f32(rhs)
185    }
186}
187impl core::ops::Div<f32> for &Vector2 {
188    type Output = Vector2;
189    fn div(self, rhs: f32) -> Vector2 {
190        self.impl_div_f32(rhs)
191    }
192}
193impl core::ops::Mul<Self> for Vector2 {
194    type Output = Self;
195    fn mul(self, rhs: Self) -> Self {
196        self.impl_mul(&rhs)
197    }
198}
199impl core::ops::Mul<&Self> for Vector2 {
200    type Output = Self;
201    fn mul(self, rhs: &Self) -> Self {
202        self.impl_mul(rhs)
203    }
204}
205impl core::ops::Mul<Vector2> for &Vector2 {
206    type Output = Vector2;
207    fn mul(self, rhs: Vector2) -> Vector2 {
208        self.impl_mul(&rhs)
209    }
210}
211impl core::ops::Mul<&Vector2> for &Vector2 {
212    type Output = Vector2;
213    fn mul(self, rhs: &Vector2) -> Vector2 {
214        self.impl_mul(rhs)
215    }
216}
217impl core::ops::Mul<f32> for Vector2 {
218    type Output = Self;
219    fn mul(self, rhs: f32) -> Self {
220        self.impl_mul_f32(rhs)
221    }
222}
223impl core::ops::Mul<f32> for &Vector2 {
224    type Output = Vector2;
225    fn mul(self, rhs: f32) -> Vector2 {
226        self.impl_mul_f32(rhs)
227    }
228}