Skip to main content

windows_numerics/
vector4.rs

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